initial-freebsd-port-work-20010414
[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 <sys/param.h>
11 #include <afs/param.h>
12 #include <rx/xdr.h>
13 #include <afs/afsint.h>
14 #include <ctype.h>
15 #if !defined(AFS_SGI_ENV)
16 #ifdef  AFS_OSF_ENV
17 #include <ufs/fs.h>
18 #else   /* AFS_OSF_ENV */
19 #ifdef AFS_VFSINCL_ENV
20 #define VFS
21 #ifdef  AFS_SUN5_ENV
22 #include <sys/fs/ufs_fs.h>
23 #else
24 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
25 #include <ufs/ufs/dinode.h>
26 #include <ufs/ffs/fs.h>
27 #else
28 #include <ufs/fs.h>
29 #endif
30 #endif
31 #else /* AFS_VFSINCL_ENV */
32 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_FBSD_ENV)
33 #include <sys/fs.h>
34 #endif
35 #endif /* AFS_VFSINCL_ENV */
36 #endif  /* AFS_OSF_ENV */
37 #endif /* AFS_SGI_ENV */
38 #include <sys/errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <stdio.h>
42 #include <sys/file.h>
43 #ifdef  AFS_AIX_ENV
44 #include <sys/vfs.h>
45 #include <fcntl.h>
46 #else
47 #ifdef  AFS_HPUX_ENV
48 #include <fcntl.h>
49 #include <mntent.h>
50 #else
51 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
52 #ifdef  AFS_SUN5_ENV
53 #include <sys/mnttab.h>
54 #include <sys/mntent.h>
55 #else
56 #include <mntent.h>
57 #endif
58 #else
59 #if defined(AFS_SGI_ENV)
60 #include <fcntl.h>
61 #include <mntent.h>
62 #ifdef AFS_SGI_EFS_IOPS_ENV
63 #define ROOTINO EFS_ROOTINO
64 #include "../sgiefs/efs.h"
65 #endif
66 #else
67 #ifdef AFS_LINUX22_ENV
68 #include <mntent.h>
69 #else
70 #include <fstab.h>
71 #endif
72 #endif
73 #endif /* AFS_SGI_ENV */
74 #endif /* AFS_HPUX_ENV */
75 #endif
76 #include <netdb.h>
77 #include <netinet/in.h>
78 #include <sys/wait.h>
79 #include <setjmp.h>
80 #ifndef ITIMER_REAL
81 #include <sys/time.h>
82 #endif /* ITIMER_REAL */
83 #include "partition.h"
84 #ifdef AFS_LINUX22_ENV
85 #include <asm/types.h>
86 #include <linux/ext2_fs.h>
87 #define ROOTINO EXT2_ROOT_INO /* Assuming we do this on ext2, of course. */
88 #endif
89
90 /* ensure that we don't have a "/" instead of a "/dev/rxd0a" type of device.
91  * returns pointer to static storage; copy it out quickly!
92  */
93 char *vol_DevName(adev, wpath)
94 char *wpath;
95 dev_t adev; {
96     static char pbuffer[128];
97     char pbuf[128], *ptr;
98     int code, i;
99 #ifdef  AFS_SUN5_ENV
100     struct mnttab mnt;
101     FILE *mntfile;
102 #else
103 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
104     struct mntent *mntent;
105     FILE *mfd;
106 #else
107     struct fstab *fsent;
108 #endif
109 #endif
110 #ifdef  AFS_AIX_ENV
111     int nmounts;
112     struct vmount *vmountp;
113 #endif
114
115 #ifdef  AFS_AIX_ENV
116     if ((nmounts = getmount(&vmountp)) <= 0)    {   
117         return (char *)0;
118     }
119     for (; nmounts; nmounts--, vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
120         char *part = vmt2dataptr(vmountp, VMT_STUB);
121 #else
122 #ifdef  AFS_SUN5_ENV
123     if (!(mntfile = fopen(MNTTAB, "r"))) {
124         return (char *)0;
125     }
126     while (!getmntent(mntfile, &mnt)) {
127         char *part = mnt.mnt_mountp;
128 #else
129 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
130 #ifdef AFS_LINUX22_ENV
131     if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
132         if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
133             return (char *)0;
134         }
135     }
136 #else
137     if ((mfd = setmntent(MOUNTED/*MNTTAB*/, "r")) == NULL) {
138         return (char *)0;
139     }
140 #endif
141     while (mntent = getmntent(mfd)) {
142         char *part = mntent->mnt_dir;
143 #else
144     setfsent();
145     while (fsent = getfsent()) {
146         char *part = fsent->fs_file;
147 #endif
148 #endif /* AFS_SGI_ENV */
149 #endif
150         struct stat status;
151 #ifdef  AFS_AIX_ENV
152         if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE)) continue; /* Ignore any "special" partitions */
153 #else
154 #ifdef  AFS_SUN5_ENV
155         /* Ignore non ufs or non read/write partitions */
156         if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
157             (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0)) 
158             continue; 
159 #else
160 #if defined(AFS_LINUX22_ENV)
161         if (strcmp(mntent->mnt_type, "ext2"))
162             continue;
163 #else
164 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
165         if (!hasmntopt(mntent, MNTOPT_RW)) continue;
166 #else
167         if (strcmp(fsent->fs_type, "rw") != 0) continue; /* Ignore non read/write partitions */
168 #endif /* AFS_LINUX22_ENV */
169 #endif /* AFS_SGI_ENV */
170 #endif
171 #endif
172         /* Only keep track of "/vicepx" partitions since it can get hairy when NFS mounts are involved.. */
173         if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
174             continue;           /* Non /vicepx; ignore */
175         }
176         if (stat(part, &status) == -1) {
177             continue;
178         }
179 #ifndef AFS_SGI_XFS_IOPS_ENV
180         if ((status.st_ino != ROOTINO) /*|| ((status.st_mode & S_IFMT) != S_IFBLK)*/) {
181             continue;
182         }
183 #endif
184         if (status.st_dev == adev) {
185 #ifdef  AFS_AIX_ENV
186             strcpy(pbuffer, vmt2dataptr(vmountp, VMT_OBJECT));
187 #else
188 #ifdef  AFS_SUN5_ENV
189             strcpy(pbuffer, mnt.mnt_special);
190 #else
191 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
192             strcpy(pbuffer, mntent->mnt_fsname);
193 #else
194             strcpy(pbuffer, fsent->fs_spec);
195 #endif
196 #endif  /* AFS_SGI_ENV */
197 #endif
198             if (wpath) {
199                 strcpy(pbuf, pbuffer);
200                 ptr = (char *)rindex(pbuf, '/');
201                 if (ptr) {
202                     *ptr = '\0';
203                     strcpy(wpath, pbuf);
204                 } else
205                     return (char *)0;
206             }
207             ptr = (char *)rindex(pbuffer, '/');     
208             if (ptr) {
209                 strcpy(pbuffer, ptr+1);
210                 return pbuffer;
211             } else
212                 return (char *)0;
213         }
214     }
215 #ifndef AFS_AIX_ENV
216 #ifdef  AFS_SUN5_ENV
217    (void) fclose(mntfile);
218 #else
219 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
220     endmntent(mfd);
221 #else
222     endfsent();
223 #endif
224 #endif /* AFS_SGI_ENV */
225 #endif
226     return (char *)0;
227 }
228
229
230 /* Search for the raw device name. Will put an "r" in front of each
231  * directory and file entry of the pathname until we find a character
232  * device.
233  */
234 char *afs_rawname(devfile)
235   char *devfile;
236 {
237   static char rawname[100];
238   struct stat statbuf;
239   char *p;
240   int code, i;
241
242   i = strlen(devfile);
243   while (i >= 0) {
244      strcpy(rawname, devfile);
245      if (devfile[i] == '/') {
246         rawname[i+1] = 'r';
247         rawname[i+2] = 0;
248         strcat(rawname, &devfile[i+1]);
249      }
250
251      code = stat(rawname, &statbuf);
252      if (!code && S_ISCHR(statbuf.st_mode))
253         return rawname;
254   
255      while((--i>=0) && (devfile[i] != '/'));
256   }
257
258   return (char *)0;
259 }
260