2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2001 University of Maryland at College Park
4 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of U.M. not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. U.M. makes no representations about the
14 * suitability of this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
17 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Authors: the Amanda Development Team. Its members are listed in a
25 * file named AUTHORS, in the root directory of this distribution.
28 * $Id: getfsent.c,v 1.38 2006/07/19 17:41:14 martinea Exp $
30 * generic version of code to read fstab
38 # include <sys/types.h>
43 static char *dev2rdev(char *);
46 * You are in a twisty maze of passages, all alike.
50 #if defined(HAVE_FSTAB_H) && !defined(HAVE_MNTENT_H) /* { */
54 #define GETFSENT_TYPE "BSD (Ultrix, AIX)"
73 generic_fsent_t * fsent)
75 struct fstab *sys_fsent = getfsent();
76 static char *xfsname = NULL, *xmntdir = NULL;
77 static char *xfstype = NULL, *xmntopts = NULL;
81 fsent->fsname = xfsname = newstralloc(xfsname, sys_fsent->fs_spec);
82 fsent->mntdir = xmntdir = newstralloc(xmntdir, sys_fsent->fs_file);
83 fsent->freq = sys_fsent->fs_freq;
84 fsent->passno = sys_fsent->fs_passno;
86 fsent->fstype = xfstype = newstralloc(xfstype, sys_fsent->fs_name);
87 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_opts);
90 fsent->fstype = xfstype = newstralloc(xfstype, _("unknown"));
91 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_type);
93 fsent->fstype = xfstype = newstralloc(xfstype, sys_fsent->fs_vfstype);
94 fsent->mntopts = xmntopts = newstralloc(xmntopts, sys_fsent->fs_mntops);
101 #if defined(HAVE_SYS_VFSTAB_H) /* } { */
103 ** SVR4 (GETFSENT_SOLARIS)
105 #define GETFSENT_TYPE "SVR4 (Solaris)"
107 #include <sys/vfstab.h>
109 static FILE *fstabf = NULL;
115 return (fstabf = fopen(VFSTAB, "r")) != NULL;
128 generic_fsent_t * fsent)
130 struct vfstab sys_fsent;
132 memset(&sys_fsent, 0, SIZEOF(sys_fsent));
133 if(getvfsent(fstabf, &sys_fsent) != 0)
136 fsent->fsname = sys_fsent.vfs_special;
137 fsent->fstype = sys_fsent.vfs_fstype;
138 fsent->mntdir = sys_fsent.vfs_mountp;
139 fsent->mntopts = sys_fsent.vfs_mntopts;
140 fsent->freq = 1; /* N/A */
141 fsent->passno = sys_fsent.vfs_fsckpass? atoi(sys_fsent.vfs_fsckpass) : 0;
146 # if defined(HAVE_MNTENT_H) /* } { */
149 ** System V.3 (GETFSENT_SVR3, GETFSENT_LINUX)
151 #define GETFSENT_TYPE "SVR3 (NeXTstep, Irix, Linux, HP-UX)"
155 #if defined(HAVE_ENDMNTENT)
156 #define AMCLOSE_MNTENT(x) endmntent(x)
158 #define AMCLOSE_MNTENT(x) fclose(x)
161 static FILE *fstabf1 = NULL; /* /proc/mounts */
162 static FILE *fstabf2 = NULL; /* MOUNTED */
163 static FILE *fstabf3 = NULL; /* MNTTAB */
169 #if defined(HAVE_SETMNTENT)
170 fstabf1 = setmntent("/proc/mounts", "r");
171 # if defined(MOUNTED)
172 fstabf2 = setmntent(MOUNTED, "r");
175 fstabf3 = setmntent(MNTTAB, "r");
179 fstabf3 = fopen(MNTTAB, "r");
182 return (fstabf1 != NULL || fstabf2 != NULL || fstabf3 != NULL);
189 AMCLOSE_MNTENT(fstabf1);
193 AMCLOSE_MNTENT(fstabf2);
197 AMCLOSE_MNTENT(fstabf3);
204 generic_fsent_t * fsent)
206 struct mntent *sys_fsent = NULL;
209 sys_fsent = getmntent(fstabf1);
211 AMCLOSE_MNTENT(fstabf1);
215 if(!sys_fsent && fstabf2) {
216 sys_fsent = getmntent(fstabf2);
218 AMCLOSE_MNTENT(fstabf2);
222 if(!sys_fsent && fstabf3) {
223 sys_fsent = getmntent(fstabf3);
225 AMCLOSE_MNTENT(fstabf3);
233 fsent->fsname = sys_fsent->mnt_fsname;
234 fsent->fstype = sys_fsent->mnt_type;
235 fsent->mntdir = sys_fsent->mnt_dir;
236 fsent->mntopts = sys_fsent->mnt_opts;
237 fsent->freq = sys_fsent->mnt_freq;
238 fsent->passno = sys_fsent->mnt_passno;
243 # if defined(HAVE_SYS_MNTTAB_H) || defined(STATFS_SCO_OS5) /* } { */
245 /* we won't actually include mnttab.h, since it contains nothing useful.. */
247 #define GETFSENT_TYPE "SVR3 (Interactive UNIX)"
253 #define FSTAB "/etc/fstab"
255 static FILE *fstabf = NULL;
261 return (fstabf = fopen(FSTAB, "r")) != NULL;
272 static generic_fsent_t _fsent;
276 generic_fsent_t * fsent)
278 static char *lfsnam = NULL;
279 static char *opts = NULL;
280 static char *cp = NULL;
285 for (; (cp = agets(fstabf)) != NULL; free(cp)) {
288 fsent->fsname = strtok(cp, " \t");
289 if ( fsent->fsname && *fsent->fsname != '#' )
292 if (cp == NULL) return 0;
294 fsent->mntdir = strtok((char *)NULL, " \t");
295 fsent->mntopts = strtok((char *)NULL, " \t");
296 if ( *fsent->mntopts != '-' ) {
297 fsent->fstype = fsent->mntopts;
298 fsent->mntopts = "rw";
301 if (strcmp(fsent->mntopts, "-r") == 0) {
302 fsent->mntopts = "ro";
305 if ((s = strchr(fsent->fstype, ',')) != NULL) {
307 strappend(fsent->mntopts, ",");
308 strappend(fsent->mntopts, s);
311 lfsnam = newstralloc(lfsnam, fsent->fstype);
313 while((ch = *s++) != '\0') {
314 if(isupper(ch)) ch = tolower(ch);
317 fsent->fstype = lfsnam;
319 if (strncmp_const(fsent->fstype, "hs") == 0)
320 fsent->fstype = "iso9660";
329 # if defined(HAVE_MNTTAB_H) /* } { */
331 #define GETFSENT_TYPE "SVR3 (SCO UNIX)"
334 #include <sys/fstyp.h>
335 #include <sys/statfs.h>
337 #define MNTTAB "/etc/mnttab"
340 * If these are defined somewhere please let me know.
343 #define MNT_READONLY 0101
344 #define MNT_READWRITE 0100
346 static FILE *fstabf = NULL;
352 return (fstabf = fopen(MNTTAB, "r")) != NULL;
363 static generic_fsent_t _fsent;
367 generic_fsent_t *fsent)
370 char typebuf[FSTYPSZ];
371 static struct mnttab mnt;
374 if(!fread (&mnt, SIZEOF(mnt), 1, fstabf))
377 fsent->fsname = mnt.mt_dev;
378 fsent->mntdir = mnt.mt_filsys;
381 if (statfs (fsent->mntdir, &fsd, SIZEOF(fsd), 0) != -1
382 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1) {
384 ep = fsent->fstype = malloc(strlen(typebuf)+2);
386 *ep++ = tolower(*dp++);
390 if ( mnt.mt_ro_flg == MNT_READONLY ) {
391 fsent->mntopts = "ro";
393 fsent->mntopts = "rw";
403 #define GETFSENT_TYPE "undefined"
412 *=====================================================================
413 * Convert either a block or character device name to a character (raw)
416 * static char *dev2rdev(const char *name);
418 * entry: name - device name to convert
419 * exit: matching character device name if found,
420 * otherwise returns the input
422 * The input must be an absolute path.
424 * The exit string area is always an alloc-d area that the caller is
425 * responsible for releasing.
426 *=====================================================================
438 if(stat(name, &st) == 0 && !S_ISBLK(st.st_mode)) {
440 * If the input is already a character device, just return it.
442 return stralloc(name);
448 if(ch == '\0' || ch != '/') return stralloc(name);
450 ch = *s++; /* start after first '/' */
452 * Break the input path at each '/' and create a new name with an
453 * 'r' before the right part. For instance:
455 * /dev/sd0a -> /dev/rsd0a
456 * /dev/dsk/c0t0d0s0 -> /dev/rdsk/c0t0d0s0 -> /dev/dsk/rc0t0d0s0
461 fname = newvstralloc(fname, name, "/r", s, NULL);
463 if(stat(fname, &st) == 0 && S_ISCHR(st.st_mode)) return fname;
468 return stralloc(name); /* no match */
472 static int samefile(struct stat[3], struct stat *);
476 struct stat stats[3],
480 for(i = 0; i < 3; ++i) {
481 if (stats[i].st_dev == estat->st_dev &&
482 stats[i].st_ino == estat->st_ino)
487 #endif /* !IGNORE_FSTAB */
492 generic_fsent_t * fsent,
496 /* There is no real mount table so this will always fail and
497 * we are using GNU tar so we can just return here.
499 (void)name; /* Quiet unused parameter warning */
500 (void)fsent; /* Quiet unused parameter warning */
501 (void)check_dev; /* Quiet unused parameter warning */
504 struct stat stats[3];
505 char *fullname = NULL;
512 memset(stats, 0, SIZEOF(stats));
513 stats[0].st_dev = stats[1].st_dev = stats[2].st_dev = (dev_t)-1;
515 if (stat(name, &stats[0]) == -1)
516 stats[0].st_dev = (dev_t)-1;
517 if (name[0] != '/') {
518 fullname = stralloc2(DEV_PREFIX, name);
519 if (stat(fullname, &stats[1]) == -1)
520 stats[1].st_dev = (dev_t)-1;
521 fullname = newstralloc2(fullname, RDEV_PREFIX, name);
522 if (stat(fullname, &stats[2]) == -1)
523 stats[2].st_dev = (dev_t)-1;
526 else if (stat((rdev = dev2rdev(name)), &stats[1]) == -1)
527 stats[1].st_dev = (dev_t)-1;
535 while(get_fstab_nextentry(fsent)) {
539 int smnt = -1, sfs = -1, sfsr = -1;
543 if(fsent->mntdir != NULL)
544 smnt = stat(fsent->mntdir, &mntstat);
546 if(fsent->fsname != NULL) {
547 sfs = stat(fsent->fsname, &fsstat);
548 sfsr = stat((rdev = dev2rdev(fsent->fsname)), &fsrstat);
549 if(check_dev == 1 && sfs == -1 && sfsr == -1)
553 if((fsent->mntdir != NULL &&
555 samefile(stats, &mntstat)) ||
556 (fsent->fsname != NULL &&
558 samefile(stats, &fsstat)) ||
559 (fsent->fsname != NULL &&
561 samefile(stats, &fsrstat))) {
569 #endif /* !IGNORE_FSTAB */
574 generic_fsent_t * fsent)
576 if(fsent->fstype == NULL) /* unknown, assume local */
579 /* just eliminate fstypes known to be remote or unsavable */
581 return strcmp(fsent->fstype, "nfs") != 0 && /* NFS */
582 strcmp(fsent->fstype, "afs") != 0 && /* Andrew Filesystem */
583 strcmp(fsent->fstype, "swap") != 0 && /* Swap */
584 strcmp(fsent->fstype, "iso9660") != 0 && /* CDROM */
585 strcmp(fsent->fstype, "hs") != 0 && /* CDROM */
586 strcmp(fsent->fstype, "piofs") != 0; /* an AIX printer thing? */
594 generic_fsent_t fsent;
596 if(search_fstab(str, &fsent, 1) && fsent.fsname != NULL)
598 else if(search_fstab(str, &fsent, 0) && fsent.fsname != NULL)
601 return dev2rdev(str);
608 generic_fsent_t fsent;
610 if(search_fstab(str, &fsent, 1) && fsent.mntdir != NULL)
612 else if(search_fstab(str, &fsent, 0) && fsent.mntdir != NULL)
615 return stralloc(str);
618 char *amname_to_fstype(
621 generic_fsent_t fsent;
623 if (!search_fstab(str, &fsent, 1) && !search_fstab(str, &fsent, 0))
626 return stralloc(fsent.fstype);
631 void print_entry(generic_fsent_t *fsent);
635 generic_fsent_t * fsent)
637 #define nchk(s) ((s)? (s) : "<NULL>")
638 g_printf("%-20.20s %-14.14s %-7.7s %4d %5d %s\n",
639 nchk(fsent->fsname), nchk(fsent->mntdir), nchk(fsent->fstype),
640 fsent->freq, fsent->passno, nchk(fsent->mntopts));
648 generic_fsent_t fsent;
653 * Configure program for internationalization:
654 * 1) Only set the message locale for now.
655 * 2) Set textdomain for all amanda related programs to "amanda"
656 * We don't want to be forced to support dozens of message catalogs.
658 setlocale(LC_MESSAGES, "C");
659 textdomain("amanda");
663 set_pname("getfsent");
667 /* Don't die when child closes pipe */
668 signal(SIGPIPE, SIG_IGN);
671 g_fprintf(stderr, _("getfsent_test: could not open fstab\n"));
675 g_printf("getfsent (%s)\n",GETFSENT_TYPE);
676 g_printf("l/r fsname mntdir fstype freq pass# mntopts\n");
677 while(get_fstab_nextentry(&fsent)) {
678 g_printf("%c ",is_local_fstype(&fsent)? 'l' : 'r');
681 g_printf("--------\n");
685 name = newstralloc(name, "/usr");
686 if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
687 g_printf(_("Found %s mount for %s:\n"),
688 is_local_fstype(&fsent)? _("local") : _("remote"), name);
692 g_printf(_("Mount for %s not found\n"), name);
694 name = newstralloc(name, "/");
695 if(search_fstab(name, &fsent, 1) || search_fstab(name, &fsent, 0)) {
696 g_printf(_("Found %s mount for %s:\n"),
697 is_local_fstype(&fsent)? _("local") : _("remote"), name);
701 g_printf(_("Mount for %s not found\n"), name);
703 name = newstralloc(name, "/");
704 s = amname_to_fstype(name);
705 g_printf(_("fstype of `%s': %s\n"), name, s);
707 name = newstralloc(name, "/dev/root");
708 s = amname_to_fstype(name);
709 g_printf(_("fstype of `%s': %s\n"), name, s);
711 name = newstralloc(name, "/usr");
712 s = amname_to_fstype(name);
713 g_printf(_("fstype of `%s': %s\n"), name, s);
715 name = newstralloc(name, "c0t3d0s0");
716 s = amname_to_fstype(name);
717 g_printf(_("fstype of `%s': %s\n"), name, s);
720 name = newstralloc(name, "/tmp/foo");
721 s = amname_to_devname(name);
722 g_printf(_("device of `%s': %s\n"), name, s);
724 s = amname_to_dirname(name);
725 g_printf(_("dirname of `%s': %s\n"), name, s);
727 s = amname_to_fstype(name);
728 g_printf(_("fstype of `%s': %s\n"), name, s);
731 name = newstralloc(name, "./foo");
732 s = amname_to_devname(name);
733 g_printf(_("device of `%s': %s\n"), name, s);
735 s = amname_to_dirname(name);
736 g_printf(_("dirname of `%s': %s\n"), name, s);
738 s = amname_to_fstype(name);
739 g_printf(_("fstype of `%s': %s\n"), name, s);
743 name = newstralloc(name, *++argv);
744 s = amname_to_devname(name);
745 g_printf(_("device of `%s': %s\n"), name, s);
747 s = amname_to_dirname(name);
748 g_printf(_("dirname of `%s': %s\n"), name, s);
750 s = amname_to_fstype(name);
751 g_printf(_("fstype of `%s': %s\n"), name, s);