2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2001 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: getfsent.c,v 1.20.4.1.2.2.2.6.2.2 2005/09/30 19:13:36 martinea Exp $
29 * generic version of code to read fstab
36 # include <sys/types.h>
44 * You are in a twisty maze of passages, all alike.
48 #if defined(HAVE_FSTAB_H) && !defined(HAVE_MNTENT_H) /* { */
52 #define GETFSENT_TYPE "BSD (Ultrix, AIX)"
67 int get_fstab_nextentry(fsent)
68 generic_fsent_t *fsent;
70 struct fstab *sys_fsent = getfsent();
71 static char *xfsname = NULL, *xmntdir = NULL;
72 static char *xfstype = NULL, *xmntopts = NULL;
76 fsent->fsname = xfsname = newstralloc(xfsname, sys_fsent->fs_spec);
77 fsent->mntdir = xmntdir = newstralloc(xmntdir, sys_fsent->fs_file);
78 fsent->freq = sys_fsent->fs_freq;
79 fsent->passno = sys_fsent->fs_passno;
81 fsent->fstype = xfstype = newstralloc(xfstype, sys_fsent->fs_name);
82 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_opts);
85 fsent->fstype = xfstype = newstralloc(xfstype, "unknown");
86 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_type);
88 fsent->fstype = xfstype = newstralloc(xfstype, sys_fsent->fs_vfstype);
89 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_mntops);
96 #if defined(HAVE_SYS_VFSTAB_H) /* } { */
98 ** SVR4 (GETFSENT_SOLARIS)
100 #define GETFSENT_TYPE "SVR4 (Solaris)"
102 #include <sys/vfstab.h>
104 static FILE *fstabf = NULL;
109 return (fstabf = fopen(VFSTAB, "r")) != NULL;
119 int get_fstab_nextentry(fsent)
120 generic_fsent_t *fsent;
122 struct vfstab sys_fsent;
124 if(getvfsent(fstabf, &sys_fsent) != 0)
127 fsent->fsname = sys_fsent.vfs_special;
128 fsent->fstype = sys_fsent.vfs_fstype;
129 fsent->mntdir = sys_fsent.vfs_mountp;
130 fsent->mntopts = sys_fsent.vfs_mntopts;
131 fsent->freq = 1; /* N/A */
132 fsent->passno = sys_fsent.vfs_fsckpass? atoi(sys_fsent.vfs_fsckpass) : 0;
137 # if defined(HAVE_MNTENT_H) /* } { */
140 ** System V.3 (GETFSENT_SVR3, GETFSENT_LINUX)
142 #define GETFSENT_TYPE "SVR3 (NeXTstep, Irix, Linux, HP-UX)"
146 #if defined(HAVE_ENDMNTENT)
147 #define AMCLOSE_MNTENT(x) endmntent(x)
149 #define AMCLOSE_MNTENT(x) fclose(x)
152 static FILE *fstabf1 = NULL; /* /proc/mounts */
153 static FILE *fstabf2 = NULL; /* MOUNTED */
154 static FILE *fstabf3 = NULL; /* MNTTAB */
159 #if defined(HAVE_SETMNTENT)
160 fstabf1 = setmntent("/proc/mounts", "r");
161 # if defined(MOUNTED)
162 fstabf2 = setmntent(MOUNTED, "r");
165 fstabf3 = setmntent(MNTTAB, "r");
169 fstabf3 = fopen(MNTTAB, "r");
172 return (fstabf1 != NULL || fstabf2 != NULL || fstabf3 != NULL);
178 AMCLOSE_MNTENT(fstabf1);
182 AMCLOSE_MNTENT(fstabf2);
186 AMCLOSE_MNTENT(fstabf3);
191 int get_fstab_nextentry(fsent)
192 generic_fsent_t *fsent;
194 struct mntent *sys_fsent = NULL;
197 sys_fsent = getmntent(fstabf1);
199 AMCLOSE_MNTENT(fstabf1);
203 if(!sys_fsent && fstabf2) {
204 sys_fsent = getmntent(fstabf2);
206 AMCLOSE_MNTENT(fstabf2);
210 if(!sys_fsent && fstabf3) {
211 sys_fsent = getmntent(fstabf3);
213 AMCLOSE_MNTENT(fstabf3);
221 fsent->fsname = sys_fsent->mnt_fsname;
222 fsent->fstype = sys_fsent->mnt_type;
223 fsent->mntdir = sys_fsent->mnt_dir;
224 fsent->mntopts = sys_fsent->mnt_opts;
225 fsent->freq = sys_fsent->mnt_freq;
226 fsent->passno = sys_fsent->mnt_passno;
231 # if defined(HAVE_SYS_MNTTAB_H) || defined(STATFS_SCO_OS5) /* } { */
233 /* we won't actually include mnttab.h, since it contains nothing useful.. */
235 #define GETFSENT_TYPE "SVR3 (Interactive UNIX)"
241 #define FSTAB "/etc/fstab"
243 static FILE *fstabf = NULL;
248 return (fstabf = fopen(FSTAB, "r")) != NULL;
258 static generic_fsent_t _fsent;
260 int get_fstab_nextentry(fsent)
261 generic_fsent_t *fsent;
263 static char *lfsnam = NULL;
264 static char *opts = NULL;
265 static char *cp = NULL;
270 for (; (cp = agets(fstabf)) != NULL; free(cp)) {
271 fsent->fsname = strtok(cp, " \t");
272 if ( fsent->fsname && *fsent->fsname != '#' )
275 if (cp == NULL) return 0;
277 fsent->mntdir = strtok((char *)NULL, " \t");
278 fsent->mntopts = strtok((char *)NULL, " \t");
279 if ( *fsent->mntopts != '-' ) {
280 fsent->fstype = fsent->mntopts;
281 fsent->mntopts = "rw";
284 if (strcmp(fsent->mntopts, "-r") == 0) {
285 fsent->mntopts = "ro";
288 if ((s = strchr(fsent->fstype, ',')) != NULL) {
290 strappend(fsent->mntopts, ",");
291 strappend(fsent->mntopts, s);
294 lfsnam = newstralloc(lfsnam, fsent->fstype);
296 while((ch = *s++) != '\0') {
297 if(isupper(ch)) ch = tolower(ch);
300 fsent->fstype = lfsnam;
303 if (strncmp(fsent->fstype, sc, sizeof(sc)-1) == 0)
304 fsent->fstype = "iso9660";
314 # if defined(HAVE_MNTTAB_H) /* } { */
316 #define GETFSENT_TYPE "SVR3 (SCO UNIX)"
319 #include <sys/fstyp.h>
320 #include <sys/statfs.h>
322 #define MNTTAB "/etc/mnttab"
325 * If these are defined somewhere please let me know.
328 #define MNT_READONLY 0101
329 #define MNT_READWRITE 0100
331 static FILE *fstabf = NULL;
336 return (fstabf = fopen(MNTTAB, "r")) != NULL;
346 static generic_fsent_t _fsent;
348 int get_fstab_nextentry(fsent)
349 generic_fsent_t *fsent;
352 char typebuf[FSTYPSZ];
353 static struct mnttab mnt;
356 if(!fread (&mnt, sizeof mnt, 1, fstabf))
359 fsent->fsname = mnt.mt_dev;
360 fsent->mntdir = mnt.mt_filsys;
363 if (statfs (fsent->mntdir, &fsd, sizeof fsd, 0) != -1
364 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1) {
366 ep = fsent->fstype = malloc(strlen(typebuf)+2);
368 *ep++ = tolower(*dp++);
372 if ( mnt.mt_ro_flg == MNT_READONLY ) {
373 fsent->mntopts = "ro";
375 fsent->mntopts = "rw";
385 #define GETFSENT_TYPE "undefined"
394 *=====================================================================
395 * Convert either a block or character device name to a character (raw)
398 * char *dev2rdev(char *name);
400 * entry: name - device name to convert
401 * exit: matching character device name if found,
402 * otherwise returns the input
404 * The input must be an absolute path.
406 * The exit string area is always an alloc-d area that the caller is
407 * responsible for releasing.
408 *=====================================================================
411 static char *dev2rdev(name)
419 if(stat(name, &st) == 0 && S_ISCHR(st.st_mode)) {
421 * If the input is already a character device, just return it.
423 return stralloc(name);
429 if(ch == '\0' || ch != '/') return stralloc(name);
431 ch = *s++; /* start after first '/' */
433 * Break the input path at each '/' and create a new name with an
434 * 'r' before the right part. For instance:
436 * /dev/sd0a -> /dev/rsd0a
437 * /dev/dsk/c0t0d0s0 -> /dev/rdsk/c0t0d0s0 -> /dev/dsk/rc0t0d0s0
442 fname = newvstralloc(fname, name, "/r", s, NULL);
444 if(stat(fname, &st) == 0 && S_ISCHR(st.st_mode)) return fname;
449 return stralloc(name); /* no match */
452 static int samefile(stats, estat)
453 struct stat stats[3], *estat;
456 for(i = 0; i < 3; ++i) {
457 if (stats[i].st_dev == estat->st_dev &&
458 stats[i].st_ino == estat->st_ino)
464 int search_fstab(name, fsent, check_dev)
466 generic_fsent_t *fsent;
470 /* There is no real mount table so this will always fail and
471 * we are using GNU tar so we can just return here.
475 struct stat stats[3];
476 char *fullname = NULL;
483 stats[0].st_dev = stats[1].st_dev = stats[2].st_dev = -1;
485 if (stat(name, &stats[0]) == -1)
486 stats[0].st_dev = -1;
487 if (name[0] != '/') {
488 fullname = stralloc2(DEV_PREFIX, name);
489 if (stat(fullname, &stats[1]) == -1)
490 stats[1].st_dev = -1;
491 fullname = newstralloc2(fullname, RDEV_PREFIX, name);
492 if (stat(fullname, &stats[2]) == -1)
493 stats[2].st_dev = -1;
496 else if (stat((rdev = dev2rdev(name)), &stats[1]) == -1)
497 stats[1].st_dev = -1;
505 while(get_fstab_nextentry(fsent)) {
509 int smnt = -1, sfs = -1, sfsr = -1;
513 if(fsent->mntdir != NULL &&
514 (smnt = stat(fsent->mntdir, &mntstat)) == -1)
517 if(fsent->fsname != NULL) {
518 sfs = stat(fsent->fsname, &fsstat);
519 sfsr = stat((rdev = dev2rdev(fsent->fsname)), &fsrstat);
520 if(check_dev == 1 && sfs == -1 && sfsr == -1)
524 if((fsent->mntdir != NULL &&
526 samefile(stats, &mntstat)) ||
527 (fsent->fsname != NULL &&
529 samefile(stats, &fsstat)) ||
530 (fsent->fsname != NULL &&
532 samefile(stats, &fsrstat))) {
540 #endif /* !IGNORE_FSTAB */
543 int is_local_fstype(fsent)
544 generic_fsent_t *fsent;
546 if(fsent->fstype == NULL) /* unknown, assume local */
549 /* just eliminate fstypes known to be remote or unsavable */
551 return strcmp(fsent->fstype, "nfs") != 0 && /* NFS */
552 strcmp(fsent->fstype, "afs") != 0 && /* Andrew Filesystem */
553 strcmp(fsent->fstype, "swap") != 0 && /* Swap */
554 strcmp(fsent->fstype, "iso9660") != 0 && /* CDROM */
555 strcmp(fsent->fstype, "hs") != 0 && /* CDROM */
556 strcmp(fsent->fstype, "piofs") != 0; /* an AIX printer thing? */
560 char *amname_to_devname(str)
563 generic_fsent_t fsent;
565 if(search_fstab(str, &fsent, 1) && fsent.fsname != NULL)
567 else if(search_fstab(str, &fsent, 0) && fsent.fsname != NULL)
570 return dev2rdev(str);
573 char *amname_to_dirname(str)
576 generic_fsent_t fsent;
578 if(search_fstab(str, &fsent, 1) && fsent.mntdir != NULL)
580 else if(search_fstab(str, &fsent, 0) && fsent.mntdir != NULL)
583 return stralloc(str);
586 char *amname_to_fstype(str)
589 generic_fsent_t fsent;
591 if (!search_fstab(str, &fsent, 1) && !search_fstab(str, &fsent, 0))
594 return stralloc(fsent.fstype);
601 generic_fsent_t *fsent;
603 #define nchk(s) ((s)? (s) : "<NULL>")
604 printf("%-20.20s %-14.14s %-7.7s %4d %5d %s\n",
605 nchk(fsent->fsname), nchk(fsent->mntdir), nchk(fsent->fstype),
606 fsent->freq, fsent->passno, nchk(fsent->mntopts));
613 generic_fsent_t fsent;
616 unsigned long malloc_hist_1, malloc_size_1;
617 unsigned long malloc_hist_2, malloc_size_2;
621 set_pname("getfsent");
623 malloc_size_1 = malloc_inuse(&malloc_hist_1);
626 fprintf(stderr, "getfsent_test: could not open fstab\n");
630 printf("getfsent (%s)\n",GETFSENT_TYPE);
631 printf("l/r fsname mntdir fstype freq pass# mntopts\n");
632 while(get_fstab_nextentry(&fsent)) {
633 printf("%c ",is_local_fstype(&fsent)? 'l' : 'r');
636 printf("--------\n");
640 name = newstralloc(name, "/usr");
641 if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
642 printf("Found %s mount for %s:\n",
643 is_local_fstype(&fsent)? "local" : "remote", name);
647 printf("Mount for %s not found\n", name);
649 name = newstralloc(name, "/");
650 if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
651 printf("Found %s mount for %s:\n",
652 is_local_fstype(&fsent)? "local" : "remote", name);
656 printf("Mount for %s not found\n", name);
658 name = newstralloc(name, "/");
659 s = amname_to_fstype(name);
660 printf("fstype of `%s': %s\n", name, s);
662 name = newstralloc(name, "/dev/root");
663 s = amname_to_fstype(name);
664 printf("fstype of `%s': %s\n", name, s);
666 name = newstralloc(name, "/usr");
667 s = amname_to_fstype(name);
668 printf("fstype of `%s': %s\n", name, s);
670 name = newstralloc(name, "c0t3d0s0");
671 s = amname_to_fstype(name);
672 printf("fstype of `%s': %s\n", name, s);
675 name = newstralloc(name, "/tmp/foo");
676 s = amname_to_devname(name);
677 printf("device of `%s': %s\n", name, s);
679 s = amname_to_dirname(name);
680 printf("dirname of `%s': %s\n", name, s);
682 s = amname_to_fstype(name);
683 printf("fstype of `%s': %s\n", name, s);
686 name = newstralloc(name, "./foo");
687 s = amname_to_devname(name);
688 printf("device of `%s': %s\n", name, s);
690 s = amname_to_dirname(name);
691 printf("dirname of `%s': %s\n", name, s);
693 s = amname_to_fstype(name);
694 printf("fstype of `%s': %s\n", name, s);
698 name = newstralloc(name, *++argv);
699 s = amname_to_devname(name);
700 printf("device of `%s': %s\n", name, s);
702 s = amname_to_dirname(name);
703 printf("dirname of `%s': %s\n", name, s);
705 s = amname_to_fstype(name);
706 printf("fstype of `%s': %s\n", name, s);
712 malloc_size_2 = malloc_inuse(&malloc_hist_2);
714 if(malloc_size_1 != malloc_size_2) {
715 malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);