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