viced-vol-ansification-20030303
[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_XBSD_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_XBSD_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
95 #ifdef HAVE_STRING_H
96 #include <string.h>
97 #else
98 #ifdef HAVE_STRINGS_H
99 #include <strings.h>
100 #endif
101 #endif
102
103
104 /* ensure that we don't have a "/" instead of a "/dev/rxd0a" type of device.
105  * returns pointer to static storage; copy it out quickly!
106  */
107 char *vol_DevName(dev_t adev, char *wpath)
108 {
109     static char pbuffer[128];
110     char pbuf[128], *ptr;
111     int code, i;
112 #ifdef  AFS_SUN5_ENV
113     struct mnttab mnt;
114     FILE *mntfile;
115 #else
116 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
117     struct mntent *mntent;
118     FILE *mfd;
119 #else
120     struct fstab *fsent;
121 #endif
122 #endif
123 #ifdef  AFS_AIX_ENV
124     int nmounts;
125     struct vmount *vmountp;
126 #endif
127
128 #ifdef  AFS_AIX_ENV
129     if ((nmounts = getmount(&vmountp)) <= 0)    {   
130         return NULL;
131     }
132     for (; nmounts; nmounts--, vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
133         char *part = vmt2dataptr(vmountp, VMT_STUB);
134 #else
135 #ifdef  AFS_SUN5_ENV
136     if (!(mntfile = fopen(MNTTAB, "r"))) {
137         return NULL;
138     }
139     while (!getmntent(mntfile, &mnt)) {
140         char *part = mnt.mnt_mountp;
141 #else
142 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
143 #ifdef AFS_LINUX22_ENV
144     if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
145         if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
146             return NULL;
147         }
148     }
149 #else
150     if ((mfd = setmntent(MOUNTED/*MNTTAB*/, "r")) == NULL) {
151         return NULL;
152     }
153 #endif
154     while ((mntent = getmntent(mfd))) {
155         char *part = mntent->mnt_dir;
156 #else
157     setfsent();
158     while ((fsent = getfsent())) {
159         char *part = fsent->fs_file;
160 #endif
161 #endif /* AFS_SGI_ENV */
162 #endif
163         struct stat status;
164 #ifdef  AFS_AIX_ENV
165         if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE)) continue; /* Ignore any "special" partitions */
166 #else
167 #ifdef  AFS_SUN5_ENV
168         /* Ignore non ufs or non read/write partitions */
169         if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
170             (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0)) 
171             continue; 
172 #else
173 #if defined(AFS_LINUX22_ENV)
174         if (strcmp(mntent->mnt_type, "ext2"))
175             continue;
176 #else
177 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
178         if (!hasmntopt(mntent, MNTOPT_RW)) continue;
179 #else
180         if (strcmp(fsent->fs_type, "rw") != 0) continue; /* Ignore non read/write partitions */
181 #endif /* AFS_LINUX22_ENV */
182 #endif /* AFS_SGI_ENV */
183 #endif
184 #endif
185         /* Only keep track of "/vicepx" partitions since it can get hairy when NFS mounts are involved.. */
186         if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
187             continue;           /* Non /vicepx; ignore */
188         }
189         if (stat(part, &status) == -1) {
190             continue;
191         }
192 #ifndef AFS_SGI_XFS_IOPS_ENV
193         if ((status.st_ino != ROOTINO) /*|| ((status.st_mode & S_IFMT) != S_IFBLK)*/) {
194             continue;
195         }
196 #endif
197         if (status.st_dev == adev) {
198 #ifdef  AFS_AIX_ENV
199             strcpy(pbuffer, vmt2dataptr(vmountp, VMT_OBJECT));
200 #else
201 #ifdef  AFS_SUN5_ENV
202             strcpy(pbuffer, mnt.mnt_special);
203 #else
204 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
205             strcpy(pbuffer, mntent->mnt_fsname);
206 #else
207             strcpy(pbuffer, fsent->fs_spec);
208 #endif
209 #endif  /* AFS_SGI_ENV */
210 #endif
211             if (wpath) {
212                 strcpy(pbuf, pbuffer);
213                 ptr = (char *)strrchr(pbuf, '/');
214                 if (ptr) {
215                     *ptr = '\0';
216                     strcpy(wpath, pbuf);
217                 } else
218                     return NULL;
219             }
220             ptr = (char *)strrchr(pbuffer, '/');            
221             if (ptr) {
222                 strcpy(pbuffer, ptr+1);
223                 return pbuffer;
224             } else
225                 return NULL;
226         }
227     }
228 #ifdef  AFS_SUN5_ENV
229    (void) fclose(mntfile);
230 #else
231 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
232     endmntent(mfd);
233 #else
234 #ifndef AFS_AIX_ENV
235     endfsent();
236 #endif
237 #endif
238 #endif /* AFS_SGI_ENV */
239     return NULL;
240 }
241
242 /* Search for the raw device name. Will put an "r" in front of each
243  * directory and file entry of the pathname until we find a character
244  * device.
245  */
246 char *afs_rawname(char *devfile)
247 {
248   static char rawname[100];
249   struct stat statbuf;
250   char *p;
251   int code, i;
252
253   i = strlen(devfile);
254   while (i >= 0) {
255      strcpy(rawname, devfile);
256      if (devfile[i] == '/') {
257         rawname[i+1] = 'r';
258         rawname[i+2] = 0;
259         strcat(rawname, &devfile[i+1]);
260      }
261
262      code = stat(rawname, &statbuf);
263      if (!code && S_ISCHR(statbuf.st_mode))
264         return rawname;
265   
266      while((--i>=0) && (devfile[i] != '/'));
267   }
268
269   return NULL;
270 }
271