venus: Remove dedebug
[openafs.git] / src / vol / devname.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #ifndef AFS_NAMEI_ENV
14
15 #include <roken.h>
16
17 #include <ctype.h>
18
19 #include <rx/xdr.h>
20 #include <rx/rx_queue.h>
21 #include <afs/afsint.h>
22
23 #if !defined(AFS_SGI_ENV)
24 # ifdef AFS_VFSINCL_ENV
25 #  define VFS
26 #  ifdef        AFS_SUN5_ENV
27 #   include <sys/fs/ufs_fs.h>
28 #  else
29 #   include <ufs/fs.h>
30 #  endif
31 # else /* AFS_VFSINCL_ENV */
32 #  if !defined(AFS_AIX_ENV)
33 #   include <sys/fs.h>
34 #  endif
35 # endif /* AFS_VFSINCL_ENV */
36 #endif /* AFS_SGI_ENV */
37
38 #include <sys/file.h>
39
40 #ifdef  AFS_AIX_ENV
41 # include <sys/vfs.h>
42 #else
43 # ifdef AFS_HPUX_ENV
44 #  include <mntent.h>
45 # else
46 #  if   defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
47 #   ifdef       AFS_SUN5_ENV
48 #    include <sys/mnttab.h>
49 #    include <sys/mntent.h>
50 #   else
51 #    include <mntent.h>
52 #   endif
53 #  else
54 #   if defined(AFS_SGI_ENV)
55 #    include <mntent.h>
56 #   else
57 #    include <fstab.h>
58 #   endif
59 #  endif /* AFS_SGI_ENV */
60 # endif /* AFS_HPUX_ENV */
61 #endif
62
63 #include <sys/wait.h>
64 #include <setjmp.h>
65
66 #include "ihandle.h"
67 #include "partition.h"
68
69 /* ensure that we don't have a "/" instead of a "/dev/rxd0a" type of device.
70  * returns pointer to static storage; copy it out quickly!
71  */
72 char *
73 vol_DevName(dev_t adev, char *wpath)
74 {
75     static char pbuffer[128];
76     char pbuf[128], *ptr;
77 #ifdef  AFS_SUN5_ENV
78     struct mnttab mnt;
79     FILE *mntfile;
80 #else
81 # if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
82     struct mntent *mntent;
83     FILE *mfd;
84 # else
85     struct fstab *fsent;
86 # endif
87 #endif /* AFS_SUN5_ENV */
88
89 #ifdef  AFS_AIX_ENV
90     int nmounts;
91     struct vmount *vmountp;
92 #endif
93
94 #ifdef  AFS_AIX_ENV
95     if ((nmounts = getmount(&vmountp)) <= 0) {
96         return NULL;
97     }
98     for (; nmounts;
99          nmounts--, vmountp =
100          (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
101         char *part = vmt2dataptr(vmountp, VMT_STUB);
102 #else
103 # ifdef AFS_SUN5_ENV
104     if (!(mntfile = fopen(MNTTAB, "r"))) {
105         return NULL;
106     }
107     while (!getmntent(mntfile, &mnt)) {
108         char *part = mnt.mnt_mountp;
109 # else
110 #  if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
111     if ((mfd = setmntent(MOUNTED /*MNTTAB*/, "r")) == NULL) {
112         return NULL;
113     }
114     while ((mntent = getmntent(mfd))) {
115         char *part = mntent->mnt_dir;
116 #  else
117     setfsent();
118     while ((fsent = getfsent())) {
119         char *part = fsent->fs_file;
120 #  endif
121 # endif /* AFS_SGI_ENV */
122 #endif
123         struct stat status;
124 #ifdef  AFS_AIX_ENV
125         if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
126             continue;           /* Ignore any "special" partitions */
127 #else
128 # ifdef AFS_SUN5_ENV
129         /* Ignore non ufs or non read/write partitions */
130         if ((strcmp(mnt.mnt_fstype, "ufs") != 0)
131             || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
132             continue;
133 # else
134 #  if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
135         if (!hasmntopt(mntent, MNTOPT_RW))
136             continue;
137 #  else
138         if (strcmp(fsent->fs_type, "rw") != 0)
139             continue;           /* Ignore non read/write partitions */
140 #  endif /* AFS_SGI_ENV */
141 # endif
142 #endif
143         /* Only keep track of "/vicepx" partitions since it can get hairy when NFS mounts are involved.. */
144         if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
145             continue;           /* Non /vicepx; ignore */
146         }
147         if (stat(part, &status) == -1) {
148             continue;
149         }
150 #if !defined(AFS_SGI_XFS_IOPS_ENV)
151         if ((status.st_ino !=
152              ROOTINO) /*|| ((status.st_mode & S_IFMT) != S_IFBLK) */ ) {
153             continue;
154         }
155 #endif
156         if (status.st_dev == adev) {
157 #ifdef  AFS_AIX_ENV
158             strcpy(pbuffer, vmt2dataptr(vmountp, VMT_OBJECT));
159 #else
160 # ifdef AFS_SUN5_ENV
161             strcpy(pbuffer, mnt.mnt_special);
162 # else
163 #  if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
164             strcpy(pbuffer, mntent->mnt_fsname);
165 #  else
166             strcpy(pbuffer, fsent->fs_spec);
167 #  endif
168 # endif /* AFS_SGI_ENV */
169 #endif
170             if (wpath) {
171                 strcpy(pbuf, pbuffer);
172                 ptr = (char *)strrchr(pbuf, OS_DIRSEPC);
173                 if (ptr) {
174                     *ptr = '\0';
175                     strcpy(wpath, pbuf);
176                 } else
177                     return NULL;
178             }
179             ptr = (char *)strrchr(pbuffer, OS_DIRSEPC);
180             if (ptr) {
181                 strcpy(pbuffer, ptr + 1);
182                 return pbuffer;
183             } else
184                 return NULL;
185         }
186     }
187 #ifdef  AFS_SUN5_ENV
188     (void)fclose(mntfile);
189 #else
190 # if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
191     endmntent(mfd);
192 # else
193 #  ifndef       AFS_AIX_ENV
194     endfsent();
195 #  endif
196 # endif
197 #endif /* AFS_SGI_ENV */
198     return NULL;
199 }
200
201 /* Search for the raw device name. Will put an "r" in front of each
202  * directory and file entry of the pathname until we find a character
203  * device.
204  */
205 char *
206 afs_rawname(char *devfile)
207 {
208     static char rawname[100];
209     struct stat statbuf;
210     int code, i;
211
212     i = strlen(devfile);
213     while (i >= 0) {
214         strcpy(rawname, devfile);
215         if (devfile[i] == OS_DIRSEPC) {
216             rawname[i + 1] = 'r';
217             rawname[i + 2] = 0;
218             strcat(rawname, &devfile[i + 1]);
219         }
220
221         code = stat(rawname, &statbuf);
222         if (!code && S_ISCHR(statbuf.st_mode))
223             return rawname;
224
225         while ((--i >= 0) && (devfile[i] != OS_DIRSEPC));
226     }
227
228     return NULL;
229 }
230
231 #endif /* AFS_NAMEI_ENV */