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.38 2006/07/19 17:41:14 martinea Exp $
29 * generic version of code to read fstab
36 # include <sys/types.h>
41 static char *dev2rdev(char *);
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)"
71 generic_fsent_t * fsent)
73 struct fstab *sys_fsent = getfsent();
74 static char *xfsname = NULL, *xmntdir = NULL;
75 static char *xfstype = NULL, *xmntopts = NULL;
79 fsent->fsname = xfsname = newstralloc(xfsname, sys_fsent->fs_spec);
80 fsent->mntdir = xmntdir = newstralloc(xmntdir, sys_fsent->fs_file);
81 fsent->freq = sys_fsent->fs_freq;
82 fsent->passno = sys_fsent->fs_passno;
84 fsent->fstype = xfstype = newstralloc(xfstype, sys_fsent->fs_name);
85 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_opts);
88 fsent->fstype = xfstype = newstralloc(xfstype, "unknown");
89 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_type);
91 fsent->fstype = xfstype = newstralloc(xfstype, sys_fsent->fs_vfstype);
92 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_mntops);
99 #if defined(HAVE_SYS_VFSTAB_H) /* } { */
101 ** SVR4 (GETFSENT_SOLARIS)
103 #define GETFSENT_TYPE "SVR4 (Solaris)"
105 #include <sys/vfstab.h>
107 static FILE *fstabf = NULL;
113 return (fstabf = fopen(VFSTAB, "r")) != NULL;
126 generic_fsent_t * fsent)
128 struct vfstab sys_fsent;
130 memset(&sys_fsent, 0, SIZEOF(sys_fsent));
131 if(getvfsent(fstabf, &sys_fsent) != 0)
134 fsent->fsname = sys_fsent.vfs_special;
135 fsent->fstype = sys_fsent.vfs_fstype;
136 fsent->mntdir = sys_fsent.vfs_mountp;
137 fsent->mntopts = sys_fsent.vfs_mntopts;
138 fsent->freq = 1; /* N/A */
139 fsent->passno = sys_fsent.vfs_fsckpass? atoi(sys_fsent.vfs_fsckpass) : 0;
144 # if defined(HAVE_MNTENT_H) /* } { */
147 ** System V.3 (GETFSENT_SVR3, GETFSENT_LINUX)
149 #define GETFSENT_TYPE "SVR3 (NeXTstep, Irix, Linux, HP-UX)"
153 #if defined(HAVE_ENDMNTENT)
154 #define AMCLOSE_MNTENT(x) endmntent(x)
156 #define AMCLOSE_MNTENT(x) fclose(x)
159 static FILE *fstabf1 = NULL; /* /proc/mounts */
160 static FILE *fstabf2 = NULL; /* MOUNTED */
161 static FILE *fstabf3 = NULL; /* MNTTAB */
167 #if defined(HAVE_SETMNTENT)
168 fstabf1 = setmntent("/proc/mounts", "r");
169 # if defined(MOUNTED)
170 fstabf2 = setmntent(MOUNTED, "r");
173 fstabf3 = setmntent(MNTTAB, "r");
177 fstabf3 = fopen(MNTTAB, "r");
180 return (fstabf1 != NULL || fstabf2 != NULL || fstabf3 != NULL);
187 AMCLOSE_MNTENT(fstabf1);
191 AMCLOSE_MNTENT(fstabf2);
195 AMCLOSE_MNTENT(fstabf3);
202 generic_fsent_t * fsent)
204 struct mntent *sys_fsent = NULL;
207 sys_fsent = getmntent(fstabf1);
209 AMCLOSE_MNTENT(fstabf1);
213 if(!sys_fsent && fstabf2) {
214 sys_fsent = getmntent(fstabf2);
216 AMCLOSE_MNTENT(fstabf2);
220 if(!sys_fsent && fstabf3) {
221 sys_fsent = getmntent(fstabf3);
223 AMCLOSE_MNTENT(fstabf3);
231 fsent->fsname = sys_fsent->mnt_fsname;
232 fsent->fstype = sys_fsent->mnt_type;
233 fsent->mntdir = sys_fsent->mnt_dir;
234 fsent->mntopts = sys_fsent->mnt_opts;
235 fsent->freq = sys_fsent->mnt_freq;
236 fsent->passno = sys_fsent->mnt_passno;
241 # if defined(HAVE_SYS_MNTTAB_H) || defined(STATFS_SCO_OS5) /* } { */
243 /* we won't actually include mnttab.h, since it contains nothing useful.. */
245 #define GETFSENT_TYPE "SVR3 (Interactive UNIX)"
251 #define FSTAB "/etc/fstab"
253 static FILE *fstabf = NULL;
259 return (fstabf = fopen(FSTAB, "r")) != NULL;
270 static generic_fsent_t _fsent;
274 generic_fsent_t * fsent)
276 static char *lfsnam = NULL;
277 static char *opts = NULL;
278 static char *cp = NULL;
283 for (; (cp = agets(fstabf)) != NULL; free(cp)) {
286 fsent->fsname = strtok(cp, " \t");
287 if ( fsent->fsname && *fsent->fsname != '#' )
290 if (cp == NULL) return 0;
292 fsent->mntdir = strtok((char *)NULL, " \t");
293 fsent->mntopts = strtok((char *)NULL, " \t");
294 if ( *fsent->mntopts != '-' ) {
295 fsent->fstype = fsent->mntopts;
296 fsent->mntopts = "rw";
299 if (strcmp(fsent->mntopts, "-r") == 0) {
300 fsent->mntopts = "ro";
303 if ((s = strchr(fsent->fstype, ',')) != NULL) {
305 strappend(fsent->mntopts, ",");
306 strappend(fsent->mntopts, s);
309 lfsnam = newstralloc(lfsnam, fsent->fstype);
311 while((ch = *s++) != '\0') {
312 if(isupper(ch)) ch = tolower(ch);
315 fsent->fstype = lfsnam;
317 if (strncmp_const(fsent->fstype, "hs") == 0)
318 fsent->fstype = "iso9660";
327 # if defined(HAVE_MNTTAB_H) /* } { */
329 #define GETFSENT_TYPE "SVR3 (SCO UNIX)"
332 #include <sys/fstyp.h>
333 #include <sys/statfs.h>
335 #define MNTTAB "/etc/mnttab"
338 * If these are defined somewhere please let me know.
341 #define MNT_READONLY 0101
342 #define MNT_READWRITE 0100
344 static FILE *fstabf = NULL;
350 return (fstabf = fopen(MNTTAB, "r")) != NULL;
361 static generic_fsent_t _fsent;
365 generic_fsent_t *fsent)
368 char typebuf[FSTYPSZ];
369 static struct mnttab mnt;
372 if(!fread (&mnt, SIZEOF(mnt), 1, fstabf))
375 fsent->fsname = mnt.mt_dev;
376 fsent->mntdir = mnt.mt_filsys;
379 if (statfs (fsent->mntdir, &fsd, SIZEOF(fsd), 0) != -1
380 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1) {
382 ep = fsent->fstype = malloc(strlen(typebuf)+2);
384 *ep++ = tolower(*dp++);
388 if ( mnt.mt_ro_flg == MNT_READONLY ) {
389 fsent->mntopts = "ro";
391 fsent->mntopts = "rw";
401 #define GETFSENT_TYPE "undefined"
410 *=====================================================================
411 * Convert either a block or character device name to a character (raw)
414 * static char *dev2rdev(const char *name);
416 * entry: name - device name to convert
417 * exit: matching character device name if found,
418 * otherwise returns the input
420 * The input must be an absolute path.
422 * The exit string area is always an alloc-d area that the caller is
423 * responsible for releasing.
424 *=====================================================================
436 if(stat(name, &st) == 0 && S_ISCHR(st.st_mode)) {
438 * If the input is already a character device, just return it.
440 return stralloc(name);
446 if(ch == '\0' || ch != '/') return stralloc(name);
448 ch = *s++; /* start after first '/' */
450 * Break the input path at each '/' and create a new name with an
451 * 'r' before the right part. For instance:
453 * /dev/sd0a -> /dev/rsd0a
454 * /dev/dsk/c0t0d0s0 -> /dev/rdsk/c0t0d0s0 -> /dev/dsk/rc0t0d0s0
459 fname = newvstralloc(fname, name, "/r", s, NULL);
461 if(stat(fname, &st) == 0 && S_ISCHR(st.st_mode)) return fname;
466 return stralloc(name); /* no match */
470 static int samefile(struct stat[3], struct stat *);
474 struct stat stats[3],
478 for(i = 0; i < 3; ++i) {
479 if (stats[i].st_dev == estat->st_dev &&
480 stats[i].st_ino == estat->st_ino)
485 #endif /* !IGNORE_FSTAB */
490 generic_fsent_t * fsent,
494 /* There is no real mount table so this will always fail and
495 * we are using GNU tar so we can just return here.
497 (void)name; /* Quiet unused parameter warning */
498 (void)fsent; /* Quiet unused parameter warning */
499 (void)check_dev; /* Quiet unused parameter warning */
502 struct stat stats[3];
503 char *fullname = NULL;
510 memset(stats, 0, SIZEOF(stats));
511 stats[0].st_dev = stats[1].st_dev = stats[2].st_dev = (dev_t)-1;
513 if (stat(name, &stats[0]) == -1)
514 stats[0].st_dev = (dev_t)-1;
515 if (name[0] != '/') {
516 fullname = stralloc2(DEV_PREFIX, name);
517 if (stat(fullname, &stats[1]) == -1)
518 stats[1].st_dev = (dev_t)-1;
519 fullname = newstralloc2(fullname, RDEV_PREFIX, name);
520 if (stat(fullname, &stats[2]) == -1)
521 stats[2].st_dev = (dev_t)-1;
524 else if (stat((rdev = dev2rdev(name)), &stats[1]) == -1)
525 stats[1].st_dev = (dev_t)-1;
533 while(get_fstab_nextentry(fsent)) {
537 int smnt = -1, sfs = -1, sfsr = -1;
541 if(fsent->mntdir != NULL &&
542 (smnt = stat(fsent->mntdir, &mntstat)) == -1)
545 if(fsent->fsname != NULL) {
546 sfs = stat(fsent->fsname, &fsstat);
547 sfsr = stat((rdev = dev2rdev(fsent->fsname)), &fsrstat);
548 if(check_dev == 1 && sfs == -1 && sfsr == -1)
552 if((fsent->mntdir != NULL &&
554 samefile(stats, &mntstat)) ||
555 (fsent->fsname != NULL &&
557 samefile(stats, &fsstat)) ||
558 (fsent->fsname != NULL &&
560 samefile(stats, &fsrstat))) {
568 #endif /* !IGNORE_FSTAB */
573 generic_fsent_t * fsent)
575 if(fsent->fstype == NULL) /* unknown, assume local */
578 /* just eliminate fstypes known to be remote or unsavable */
580 return strcmp(fsent->fstype, "nfs") != 0 && /* NFS */
581 strcmp(fsent->fstype, "afs") != 0 && /* Andrew Filesystem */
582 strcmp(fsent->fstype, "swap") != 0 && /* Swap */
583 strcmp(fsent->fstype, "iso9660") != 0 && /* CDROM */
584 strcmp(fsent->fstype, "hs") != 0 && /* CDROM */
585 strcmp(fsent->fstype, "piofs") != 0; /* an AIX printer thing? */
593 generic_fsent_t fsent;
595 if(search_fstab(str, &fsent, 1) && fsent.fsname != NULL)
597 else if(search_fstab(str, &fsent, 0) && fsent.fsname != NULL)
600 return dev2rdev(str);
607 generic_fsent_t fsent;
609 if(search_fstab(str, &fsent, 1) && fsent.mntdir != NULL)
611 else if(search_fstab(str, &fsent, 0) && fsent.mntdir != NULL)
614 return stralloc(str);
617 char *amname_to_fstype(
620 generic_fsent_t fsent;
622 if (!search_fstab(str, &fsent, 1) && !search_fstab(str, &fsent, 0))
625 return stralloc(fsent.fstype);
630 void print_entry(generic_fsent_t *fsent);
634 generic_fsent_t * fsent)
636 #define nchk(s) ((s)? (s) : "<NULL>")
637 printf("%-20.20s %-14.14s %-7.7s %4d %5d %s\n",
638 nchk(fsent->fsname), nchk(fsent->mntdir), nchk(fsent->fstype),
639 fsent->freq, fsent->passno, nchk(fsent->mntopts));
647 generic_fsent_t fsent;
650 unsigned long malloc_hist_1, malloc_size_1;
651 unsigned long malloc_hist_2, malloc_size_2;
655 set_pname("getfsent");
659 /* Don't die when child closes pipe */
660 signal(SIGPIPE, SIG_IGN);
662 malloc_size_1 = malloc_inuse(&malloc_hist_1);
665 fprintf(stderr, "getfsent_test: could not open fstab\n");
669 printf("getfsent (%s)\n",GETFSENT_TYPE);
670 printf("l/r fsname mntdir fstype freq pass# mntopts\n");
671 while(get_fstab_nextentry(&fsent)) {
672 printf("%c ",is_local_fstype(&fsent)? 'l' : 'r');
675 printf("--------\n");
679 name = newstralloc(name, "/usr");
680 if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
681 printf("Found %s mount for %s:\n",
682 is_local_fstype(&fsent)? "local" : "remote", name);
686 printf("Mount for %s not found\n", name);
688 name = newstralloc(name, "/");
689 if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
690 printf("Found %s mount for %s:\n",
691 is_local_fstype(&fsent)? "local" : "remote", name);
695 printf("Mount for %s not found\n", name);
697 name = newstralloc(name, "/");
698 s = amname_to_fstype(name);
699 printf("fstype of `%s': %s\n", name, s);
701 name = newstralloc(name, "/dev/root");
702 s = amname_to_fstype(name);
703 printf("fstype of `%s': %s\n", name, s);
705 name = newstralloc(name, "/usr");
706 s = amname_to_fstype(name);
707 printf("fstype of `%s': %s\n", name, s);
709 name = newstralloc(name, "c0t3d0s0");
710 s = amname_to_fstype(name);
711 printf("fstype of `%s': %s\n", name, s);
714 name = newstralloc(name, "/tmp/foo");
715 s = amname_to_devname(name);
716 printf("device of `%s': %s\n", name, s);
718 s = amname_to_dirname(name);
719 printf("dirname of `%s': %s\n", name, s);
721 s = amname_to_fstype(name);
722 printf("fstype of `%s': %s\n", name, s);
725 name = newstralloc(name, "./foo");
726 s = amname_to_devname(name);
727 printf("device of `%s': %s\n", name, s);
729 s = amname_to_dirname(name);
730 printf("dirname of `%s': %s\n", name, s);
732 s = amname_to_fstype(name);
733 printf("fstype of `%s': %s\n", name, s);
737 name = newstralloc(name, *++argv);
738 s = amname_to_devname(name);
739 printf("device of `%s': %s\n", name, s);
741 s = amname_to_dirname(name);
742 printf("dirname of `%s': %s\n", name, s);
744 s = amname_to_fstype(name);
745 printf("fstype of `%s': %s\n", name, s);
751 malloc_size_2 = malloc_inuse(&malloc_hist_2);
753 if(malloc_size_1 != malloc_size_2) {
754 malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);