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