Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.60
      1  1.60     lukem /*	$NetBSD: mount.c,v 1.60 2002/08/23 03:17:19 lukem Exp $	*/
      2  1.16       cgd 
      3   1.1       cgd /*
      4  1.12   mycroft  * Copyright (c) 1980, 1989, 1993, 1994
      5  1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.29  christos #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.29  christos __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
     39  1.29  christos 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43  1.16       cgd #if 0
     44  1.32     lukem static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
     45  1.16       cgd #else
     46  1.60     lukem __RCSID("$NetBSD: mount.c,v 1.60 2002/08/23 03:17:19 lukem Exp $");
     47  1.16       cgd #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #include <sys/param.h>
     51  1.12   mycroft #include <sys/mount.h>
     52   1.1       cgd #include <sys/wait.h>
     53  1.12   mycroft 
     54  1.12   mycroft #include <err.h>
     55  1.12   mycroft #include <errno.h>
     56   1.1       cgd #include <fstab.h>
     57  1.32     lukem #include <pwd.h>
     58  1.12   mycroft #include <signal.h>
     59   1.1       cgd #include <stdio.h>
     60   1.1       cgd #include <stdlib.h>
     61  1.12   mycroft #include <string.h>
     62  1.12   mycroft #include <unistd.h>
     63  1.12   mycroft 
     64  1.48       abs #define MOUNTNAMES
     65  1.48       abs #include <fcntl.h>
     66  1.48       abs #include <sys/disklabel.h>
     67  1.48       abs #include <sys/ioctl.h>
     68  1.48       abs 
     69   1.1       cgd #include "pathnames.h"
     70  1.55     enami #include "vfslist.h"
     71   1.1       cgd 
     72  1.52  jdolecek static int	debug, verbose;
     73   1.1       cgd 
     74  1.52  jdolecek static void	catopt __P((char **, const char *));
     75  1.53     enami static const char *
     76  1.53     enami 		getfslab __P((const char *str));
     77  1.53     enami static struct statfs *
     78  1.53     enami 		getmntpt __P((const char *));
     79  1.52  jdolecek static int	hasopt __P((const char *, const char *));
     80  1.53     enami static void	mangle __P((char *, int *, const char ***, int *));
     81  1.52  jdolecek static int	mountfs __P((const char *, const char *, const char *,
     82  1.53     enami 		    int, const char *, const char *, int));
     83  1.52  jdolecek static void	prmount __P((struct statfs *));
     84  1.52  jdolecek static void	usage __P((void));
     85  1.53     enami 
     86  1.56   tsutsui #ifndef NO_MOUNT_PROGS
     87  1.53     enami void	checkname __P((int, char *[]));
     88  1.56   tsutsui #endif
     89  1.29  christos int	main __P((int, char *[]));
     90  1.12   mycroft 
     91  1.12   mycroft /* Map from mount otions to printable formats. */
     92  1.52  jdolecek static const struct opt {
     93  1.12   mycroft 	int o_opt;
     94  1.24       cgd 	int o_silent;
     95  1.12   mycroft 	const char *o_name;
     96  1.12   mycroft } optnames[] = {
     97  1.58  christos 	__MNT_FLAGS
     98   1.1       cgd };
     99   1.1       cgd 
    100  1.40       cgd static char ffs_fstype[] = "ffs";
    101  1.30  christos 
    102  1.12   mycroft int
    103  1.12   mycroft main(argc, argv)
    104   1.1       cgd 	int argc;
    105  1.29  christos 	char *argv[];
    106   1.1       cgd {
    107  1.32     lukem 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
    108  1.12   mycroft 	struct fstab *fs;
    109   1.9       cgd 	struct statfs *mntbuf;
    110  1.12   mycroft 	FILE *mountdfp;
    111  1.22       cgd 	int all, ch, forceall, i, init_flags, mntsize, rval;
    112  1.12   mycroft 	char *options;
    113  1.43   mycroft 	const char *mountopts, *fstypename;
    114   1.1       cgd 
    115  1.56   tsutsui #ifndef NO_MOUNT_PROGS
    116  1.52  jdolecek 	/* if called as specific mount, call it's main mount routine */
    117  1.52  jdolecek 	checkname(argc, argv);
    118  1.56   tsutsui #endif
    119  1.52  jdolecek 
    120  1.52  jdolecek 	/* started as "mount" */
    121  1.22       cgd 	all = forceall = init_flags = 0;
    122  1.12   mycroft 	options = NULL;
    123  1.32     lukem 	vfslist = NULL;
    124  1.40       cgd 	vfstype = ffs_fstype;
    125  1.31     lukem 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
    126  1.12   mycroft 		switch (ch) {
    127  1.22       cgd 		case 'A':
    128  1.22       cgd 			all = forceall = 1;
    129  1.22       cgd 			break;
    130   1.1       cgd 		case 'a':
    131   1.1       cgd 			all = 1;
    132   1.1       cgd 			break;
    133  1.12   mycroft 		case 'd':
    134  1.12   mycroft 			debug = 1;
    135  1.12   mycroft 			break;
    136   1.1       cgd 		case 'f':
    137  1.12   mycroft 			init_flags |= MNT_FORCE;
    138  1.12   mycroft 			break;
    139  1.12   mycroft 		case 'o':
    140  1.12   mycroft 			if (*optarg)
    141  1.35   mycroft 				catopt(&options, optarg);
    142   1.1       cgd 			break;
    143   1.1       cgd 		case 'r':
    144  1.12   mycroft 			init_flags |= MNT_RDONLY;
    145  1.12   mycroft 			break;
    146  1.12   mycroft 		case 't':
    147  1.32     lukem 			if (vfslist != NULL)
    148  1.53     enami 				errx(1,
    149  1.53     enami 				    "only one -t option may be specified.");
    150  1.32     lukem 			vfslist = makevfslist(optarg);
    151  1.12   mycroft 			vfstype = optarg;
    152   1.1       cgd 			break;
    153   1.1       cgd 		case 'u':
    154  1.12   mycroft 			init_flags |= MNT_UPDATE;
    155   1.1       cgd 			break;
    156   1.1       cgd 		case 'v':
    157   1.1       cgd 			verbose = 1;
    158   1.1       cgd 			break;
    159   1.1       cgd 		case 'w':
    160  1.12   mycroft 			init_flags &= ~MNT_RDONLY;
    161   1.1       cgd 			break;
    162   1.1       cgd 		case '?':
    163   1.1       cgd 		default:
    164   1.1       cgd 			usage();
    165   1.1       cgd 			/* NOTREACHED */
    166   1.1       cgd 		}
    167   1.1       cgd 	argc -= optind;
    168   1.1       cgd 	argv += optind;
    169   1.1       cgd 
    170  1.12   mycroft #define	BADTYPE(type)							\
    171  1.12   mycroft 	(strcmp(type, FSTAB_RO) &&					\
    172  1.12   mycroft 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    173  1.12   mycroft 
    174  1.12   mycroft 	rval = 0;
    175  1.12   mycroft 	switch (argc) {
    176  1.12   mycroft 	case 0:
    177  1.12   mycroft 		if (all)
    178  1.12   mycroft 			while ((fs = getfsent()) != NULL) {
    179  1.12   mycroft 				if (BADTYPE(fs->fs_type))
    180  1.12   mycroft 					continue;
    181  1.32     lukem 				if (checkvfsname(fs->fs_vfstype, vfslist))
    182  1.12   mycroft 					continue;
    183  1.12   mycroft 				if (hasopt(fs->fs_mntops, "noauto"))
    184  1.12   mycroft 					continue;
    185  1.12   mycroft 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
    186  1.12   mycroft 				    fs->fs_file, init_flags, options,
    187  1.22       cgd 				    fs->fs_mntops, !forceall))
    188  1.12   mycroft 					rval = 1;
    189  1.12   mycroft 			}
    190  1.12   mycroft 		else {
    191  1.12   mycroft 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    192  1.12   mycroft 				err(1, "getmntinfo");
    193  1.12   mycroft 			for (i = 0; i < mntsize; i++) {
    194  1.32     lukem 				if (checkvfsname(mntbuf[i].f_fstypename,
    195  1.32     lukem 				    vfslist))
    196  1.12   mycroft 					continue;
    197  1.13   mycroft 				prmount(&mntbuf[i]);
    198  1.12   mycroft 			}
    199   1.1       cgd 		}
    200   1.1       cgd 		exit(rval);
    201  1.40       cgd 		/* NOTREACHED */
    202  1.12   mycroft 	case 1:
    203  1.40       cgd 		if (vfslist != NULL) {
    204   1.1       cgd 			usage();
    205  1.40       cgd 			/* NOTREACHED */
    206  1.40       cgd 		}
    207   1.1       cgd 
    208  1.12   mycroft 		if (init_flags & MNT_UPDATE) {
    209  1.12   mycroft 			if ((mntbuf = getmntpt(*argv)) == NULL)
    210  1.12   mycroft 				errx(1,
    211  1.12   mycroft 				    "unknown special file or file system %s.",
    212  1.12   mycroft 				    *argv);
    213  1.42      ross 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    214  1.32     lukem 				mntfromname = fs->fs_spec;
    215  1.42      ross 				/* ignore the fstab file options.  */
    216  1.42      ross 				fs->fs_mntops = NULL;
    217  1.42      ross 			} else
    218  1.32     lukem 				mntfromname = mntbuf->f_mntfromname;
    219  1.42      ross 			mntonname  = mntbuf->f_mntonname;
    220  1.42      ross 			fstypename = mntbuf->f_fstypename;
    221  1.42      ross 			mountopts  = NULL;
    222  1.12   mycroft 		} else {
    223  1.12   mycroft 			if ((fs = getfsfile(*argv)) == NULL &&
    224  1.12   mycroft 			    (fs = getfsspec(*argv)) == NULL)
    225  1.12   mycroft 				errx(1,
    226  1.12   mycroft 				    "%s: unknown special file or file system.",
    227  1.12   mycroft 				    *argv);
    228  1.12   mycroft 			if (BADTYPE(fs->fs_type))
    229  1.12   mycroft 				errx(1, "%s has unknown file system type.",
    230  1.12   mycroft 				    *argv);
    231  1.32     lukem 			mntfromname = fs->fs_spec;
    232  1.42      ross 			mntonname   = fs->fs_file;
    233  1.42      ross 			fstypename  = fs->fs_vfstype;
    234  1.42      ross 			mountopts   = fs->fs_mntops;
    235  1.12   mycroft 		}
    236  1.42      ross 		rval = mountfs(fstypename, mntfromname,
    237  1.42      ross 		    mntonname, init_flags, options, mountopts, 0);
    238  1.12   mycroft 		break;
    239  1.12   mycroft 	case 2:
    240   1.1       cgd 		/*
    241  1.12   mycroft 		 * If -t flag has not been specified, and spec contains either
    242  1.12   mycroft 		 * a ':' or a '@' then assume that an NFS filesystem is being
    243  1.12   mycroft 		 * specified ala Sun.
    244   1.1       cgd 		 */
    245  1.48       abs 		if (vfslist == NULL) {
    246  1.48       abs 			if (strpbrk(argv[0], ":@") != NULL)
    247  1.48       abs 				vfstype = "nfs";
    248  1.48       abs 			else {
    249  1.48       abs 				vfstype = getfslab(argv[0]);
    250  1.48       abs 				if (vfstype == NULL)
    251  1.48       abs 					vfstype = ffs_fstype;
    252  1.48       abs 			}
    253  1.48       abs 		}
    254  1.12   mycroft 		rval = mountfs(vfstype,
    255  1.22       cgd 		    argv[0], argv[1], init_flags, options, NULL, 0);
    256  1.12   mycroft 		break;
    257  1.12   mycroft 	default:
    258  1.12   mycroft 		usage();
    259  1.12   mycroft 		/* NOTREACHED */
    260   1.1       cgd 	}
    261   1.1       cgd 
    262  1.12   mycroft 	/*
    263  1.12   mycroft 	 * If the mount was successfully, and done by root, tell mountd the
    264  1.12   mycroft 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    265  1.12   mycroft 	 */
    266  1.12   mycroft 	if (rval == 0 && getuid() == 0 &&
    267  1.12   mycroft 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    268  1.29  christos 		int pid;
    269  1.29  christos 
    270  1.29  christos 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    271  1.53     enami 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    272  1.12   mycroft 			err(1, "signal mountd");
    273  1.12   mycroft 		(void)fclose(mountdfp);
    274   1.1       cgd 	}
    275   1.1       cgd 
    276  1.12   mycroft 	exit(rval);
    277  1.40       cgd 	/* NOTREACHED */
    278   1.1       cgd }
    279   1.1       cgd 
    280  1.12   mycroft int
    281  1.12   mycroft hasopt(mntopts, option)
    282  1.12   mycroft 	const char *mntopts, *option;
    283   1.1       cgd {
    284  1.12   mycroft 	int negative, found;
    285  1.12   mycroft 	char *opt, *optbuf;
    286   1.1       cgd 
    287  1.12   mycroft 	if (option[0] == 'n' && option[1] == 'o') {
    288  1.12   mycroft 		negative = 1;
    289  1.12   mycroft 		option += 2;
    290  1.12   mycroft 	} else
    291  1.12   mycroft 		negative = 0;
    292  1.12   mycroft 	optbuf = strdup(mntopts);
    293  1.12   mycroft 	found = 0;
    294  1.12   mycroft 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    295  1.12   mycroft 		if (opt[0] == 'n' && opt[1] == 'o') {
    296  1.12   mycroft 			if (!strcasecmp(opt + 2, option))
    297  1.12   mycroft 				found = negative;
    298  1.12   mycroft 		} else if (!strcasecmp(opt, option))
    299  1.12   mycroft 			found = !negative;
    300  1.12   mycroft 	}
    301  1.12   mycroft 	free(optbuf);
    302  1.12   mycroft 	return (found);
    303   1.1       cgd }
    304   1.1       cgd 
    305  1.52  jdolecek static int
    306  1.22       cgd mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
    307  1.12   mycroft 	const char *vfstype, *spec, *name, *options, *mntopts;
    308  1.22       cgd 	int flags, skipmounted;
    309   1.1       cgd {
    310  1.12   mycroft 	/* List of directories containing mount_xxx subcommands. */
    311  1.12   mycroft 	static const char *edirs[] = {
    312  1.60     lukem #ifdef _PATH_RESCUE
    313  1.60     lukem 		_PATH_RESCUE,
    314  1.60     lukem #endif
    315  1.12   mycroft 		_PATH_SBIN,
    316  1.12   mycroft 		_PATH_USRSBIN,
    317  1.12   mycroft 		NULL
    318  1.12   mycroft 	};
    319  1.35   mycroft 	const char **argv, **edir;
    320  1.36  drochner 	struct statfs *sfp, sf;
    321  1.12   mycroft 	pid_t pid;
    322  1.36  drochner 	int argc, numfs, i, status, maxargc;
    323  1.33  christos 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
    324  1.33  christos 	    mntpath[MAXPATHLEN];
    325  1.35   mycroft 
    326  1.29  christos #ifdef __GNUC__
    327  1.29  christos 	(void) &name;
    328  1.29  christos 	(void) &optbuf;
    329  1.30  christos 	(void) &vfstype;
    330  1.29  christos #endif
    331   1.1       cgd 
    332  1.12   mycroft 	if (realpath(name, mntpath) == NULL) {
    333  1.20   ghudson 		warn("realpath %s", name);
    334  1.12   mycroft 		return (1);
    335  1.12   mycroft 	}
    336   1.1       cgd 
    337  1.12   mycroft 	name = mntpath;
    338   1.1       cgd 
    339  1.35   mycroft 	optbuf = NULL;
    340  1.35   mycroft 	if (mntopts)
    341  1.35   mycroft 		catopt(&optbuf, mntopts);
    342  1.35   mycroft 	if (options)
    343  1.35   mycroft 		catopt(&optbuf, options);
    344  1.35   mycroft 	if (!mntopts && !options)
    345  1.35   mycroft 		catopt(&optbuf, "rw");
    346  1.12   mycroft 
    347  1.35   mycroft 	if (!strcmp(name, "/"))
    348  1.12   mycroft 		flags |= MNT_UPDATE;
    349  1.22       cgd 	else if (skipmounted) {
    350  1.36  drochner 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
    351  1.36  drochner 			warn("getmntinfo");
    352  1.21       cgd 			return (1);
    353  1.21       cgd 		}
    354  1.36  drochner 		for(i = 0; i < numfs; i++) {
    355  1.53     enami 			/*
    356  1.53     enami 			 * XXX can't check f_mntfromname,
    357  1.53     enami 			 * thanks to mfs, union, etc.
    358  1.53     enami 			 */
    359  1.36  drochner 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
    360  1.36  drochner 			    strncmp(vfstype, sfp[i].f_fstypename,
    361  1.53     enami 				MFSNAMELEN) == 0) {
    362  1.36  drochner 				if (verbose)
    363  1.53     enami 					(void)printf("%s on %s type %.*s: "
    364  1.53     enami 					    "%s\n",
    365  1.53     enami 					    sfp[i].f_mntfromname,
    366  1.53     enami 					    sfp[i].f_mntonname,
    367  1.53     enami 					    MFSNAMELEN,
    368  1.53     enami 					    sfp[i].f_fstypename,
    369  1.53     enami 					    "already mounted");
    370  1.36  drochner 				return (0);
    371  1.36  drochner 			}
    372  1.21       cgd 		}
    373  1.21       cgd 	}
    374  1.12   mycroft 	if (flags & MNT_FORCE)
    375  1.35   mycroft 		catopt(&optbuf, "force");
    376  1.12   mycroft 	if (flags & MNT_RDONLY)
    377  1.35   mycroft 		catopt(&optbuf, "ro");
    378  1.33  christos 
    379  1.30  christos 	if (flags & MNT_UPDATE) {
    380  1.35   mycroft 		catopt(&optbuf, "update");
    381  1.30  christos 		/* Figure out the fstype only if we defaulted to ffs */
    382  1.40       cgd 		if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
    383  1.30  christos 			vfstype = sf.f_fstypename;
    384  1.30  christos 	}
    385  1.12   mycroft 
    386  1.35   mycroft 	maxargc = 64;
    387  1.35   mycroft 	argv = malloc(sizeof(char *) * maxargc);
    388  1.35   mycroft 
    389  1.33  christos 	(void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    390  1.12   mycroft 	argc = 0;
    391  1.33  christos 	argv[argc++] = execbase;
    392  1.35   mycroft 	if (optbuf)
    393  1.35   mycroft 		mangle(optbuf, &argc, &argv, &maxargc);
    394  1.12   mycroft 	argv[argc++] = spec;
    395  1.12   mycroft 	argv[argc++] = name;
    396  1.12   mycroft 	argv[argc] = NULL;
    397  1.12   mycroft 
    398  1.34  christos 	if (verbose) {
    399  1.35   mycroft 		(void)printf("exec:");
    400  1.35   mycroft 		for (i = 0; i < argc; i++)
    401  1.12   mycroft 			(void)printf(" %s", argv[i]);
    402  1.12   mycroft 		(void)printf("\n");
    403  1.12   mycroft 	}
    404   1.5   mycroft 
    405  1.12   mycroft 	switch (pid = vfork()) {
    406  1.12   mycroft 	case -1:				/* Error. */
    407  1.12   mycroft 		warn("vfork");
    408  1.35   mycroft 		if (optbuf)
    409  1.35   mycroft 			free(optbuf);
    410  1.12   mycroft 		return (1);
    411  1.35   mycroft 
    412  1.12   mycroft 	case 0:					/* Child. */
    413  1.34  christos 		if (debug)
    414  1.34  christos 			_exit(0);
    415  1.34  christos 
    416  1.12   mycroft 		/* Go find an executable. */
    417  1.12   mycroft 		edir = edirs;
    418  1.12   mycroft 		do {
    419  1.12   mycroft 			(void)snprintf(execname,
    420  1.33  christos 			    sizeof(execname), "%s/%s", *edir, execbase);
    421  1.40       cgd 			(void)execv(execname, (char * const *)argv);
    422  1.12   mycroft 			if (errno != ENOENT)
    423  1.12   mycroft 				warn("exec %s for %s", execname, name);
    424  1.12   mycroft 		} while (*++edir != NULL);
    425   1.1       cgd 
    426  1.12   mycroft 		if (errno == ENOENT)
    427  1.33  christos 			warnx("%s not found for %s", execbase, name);
    428  1.34  christos 		_exit(1);
    429  1.12   mycroft 		/* NOTREACHED */
    430  1.35   mycroft 
    431  1.12   mycroft 	default:				/* Parent. */
    432  1.35   mycroft 		if (optbuf)
    433  1.35   mycroft 			free(optbuf);
    434   1.1       cgd 
    435  1.12   mycroft 		if (waitpid(pid, &status, 0) < 0) {
    436  1.12   mycroft 			warn("waitpid");
    437  1.12   mycroft 			return (1);
    438   1.1       cgd 		}
    439  1.12   mycroft 
    440  1.12   mycroft 		if (WIFEXITED(status)) {
    441  1.12   mycroft 			if (WEXITSTATUS(status) != 0)
    442  1.12   mycroft 				return (WEXITSTATUS(status));
    443  1.12   mycroft 		} else if (WIFSIGNALED(status)) {
    444  1.17       jtc 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    445  1.12   mycroft 			return (1);
    446   1.1       cgd 		}
    447  1.12   mycroft 
    448  1.12   mycroft 		if (verbose) {
    449  1.12   mycroft 			if (statfs(name, &sf) < 0) {
    450  1.12   mycroft 				warn("statfs %s", name);
    451  1.12   mycroft 				return (1);
    452  1.12   mycroft 			}
    453  1.13   mycroft 			prmount(&sf);
    454   1.1       cgd 		}
    455  1.12   mycroft 		break;
    456   1.1       cgd 	}
    457  1.12   mycroft 
    458  1.12   mycroft 	return (0);
    459   1.1       cgd }
    460   1.1       cgd 
    461  1.52  jdolecek static void
    462  1.32     lukem prmount(sfp)
    463  1.32     lukem 	struct statfs *sfp;
    464  1.13   mycroft {
    465  1.12   mycroft 	int flags;
    466  1.52  jdolecek 	const struct opt *o;
    467  1.32     lukem 	struct passwd *pw;
    468  1.12   mycroft 	int f;
    469   1.1       cgd 
    470  1.53     enami 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    471  1.53     enami 	    sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
    472   1.1       cgd 
    473  1.32     lukem 	flags = sfp->f_flags & MNT_VISFLAGMASK;
    474  1.58  christos 	for (f = 0, o = optnames; flags && o <
    475  1.58  christos 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
    476  1.12   mycroft 		if (flags & o->o_opt) {
    477  1.58  christos 			if (!o->o_silent || verbose)
    478  1.24       cgd 				(void)printf("%s%s", !f++ ? " (" : ", ",
    479  1.24       cgd 				    o->o_name);
    480  1.12   mycroft 			flags &= ~o->o_opt;
    481  1.12   mycroft 		}
    482  1.24       cgd 	if (flags)
    483  1.27        pk 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    484  1.24       cgd 		    flags & (flags - 1) ? "s" : "", flags);
    485  1.32     lukem 	if (sfp->f_owner) {
    486  1.32     lukem 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    487  1.32     lukem 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    488  1.32     lukem 			(void)printf("%s", pw->pw_name);
    489  1.32     lukem 		else
    490  1.32     lukem 			(void)printf("%d", sfp->f_owner);
    491  1.32     lukem 	}
    492  1.46      fvdl 	if (verbose)
    493  1.46      fvdl 		(void)printf("%swrites: sync %ld async %ld)\n",
    494  1.46      fvdl 		    !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
    495  1.46      fvdl 	else
    496  1.50        is 		(void)printf("%s", f ? ")\n" : "\n");
    497   1.1       cgd }
    498   1.1       cgd 
    499  1.52  jdolecek static struct statfs *
    500   1.1       cgd getmntpt(name)
    501  1.12   mycroft 	const char *name;
    502   1.1       cgd {
    503   1.1       cgd 	struct statfs *mntbuf;
    504  1.12   mycroft 	int i, mntsize;
    505   1.1       cgd 
    506   1.1       cgd 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    507  1.12   mycroft 	for (i = 0; i < mntsize; i++)
    508  1.12   mycroft 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    509  1.12   mycroft 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    510   1.1       cgd 			return (&mntbuf[i]);
    511  1.12   mycroft 	return (NULL);
    512   1.1       cgd }
    513   1.1       cgd 
    514  1.52  jdolecek static void
    515  1.35   mycroft catopt(sp, o)
    516  1.35   mycroft 	char **sp;
    517  1.35   mycroft 	const char *o;
    518  1.12   mycroft {
    519  1.35   mycroft 	char *s;
    520  1.35   mycroft 	size_t i, j;
    521  1.12   mycroft 
    522  1.35   mycroft 	s = *sp;
    523  1.35   mycroft 	if (s) {
    524  1.35   mycroft 		i = strlen(s);
    525  1.35   mycroft 		j = i + 1 + strlen(o) + 1;
    526  1.35   mycroft 		s = realloc(s, j);
    527  1.35   mycroft 		(void)snprintf(s + i, j, ",%s", o);
    528  1.12   mycroft 	} else
    529  1.35   mycroft 		s = strdup(o);
    530  1.35   mycroft 	*sp = s;
    531   1.1       cgd }
    532   1.1       cgd 
    533  1.35   mycroft static void
    534  1.35   mycroft mangle(options, argcp, argvp, maxargcp)
    535  1.12   mycroft 	char *options;
    536  1.35   mycroft 	int *argcp, *maxargcp;
    537  1.35   mycroft 	const char ***argvp;
    538   1.1       cgd {
    539  1.12   mycroft 	char *p, *s;
    540  1.35   mycroft 	int argc, maxargc;
    541  1.35   mycroft 	const char **argv;
    542   1.1       cgd 
    543  1.12   mycroft 	argc = *argcp;
    544  1.35   mycroft 	argv = *argvp;
    545  1.35   mycroft 	maxargc = *maxargcp;
    546  1.35   mycroft 
    547  1.35   mycroft 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    548  1.35   mycroft 		/* Always leave space for one more argument and the NULL. */
    549  1.35   mycroft 		if (argc >= maxargc - 4) {
    550  1.35   mycroft 			maxargc <<= 1;
    551  1.35   mycroft 			argv = realloc(argv, maxargc * sizeof(char *));
    552  1.35   mycroft 		}
    553  1.44      ross 		if (*p != '\0') {
    554  1.12   mycroft 			if (*p == '-') {
    555  1.12   mycroft 				argv[argc++] = p;
    556  1.12   mycroft 				p = strchr(p, '=');
    557  1.12   mycroft 				if (p) {
    558  1.12   mycroft 					*p = '\0';
    559  1.12   mycroft 					argv[argc++] = p+1;
    560   1.1       cgd 				}
    561  1.12   mycroft 			} else if (strcmp(p, "rw") != 0) {
    562  1.12   mycroft 				argv[argc++] = "-o";
    563  1.12   mycroft 				argv[argc++] = p;
    564   1.1       cgd 			}
    565  1.44      ross 		}
    566  1.35   mycroft 	}
    567  1.12   mycroft 
    568  1.12   mycroft 	*argcp = argc;
    569  1.35   mycroft 	*argvp = argv;
    570  1.35   mycroft 	*maxargcp = maxargc;
    571  1.48       abs }
    572  1.48       abs 
    573  1.53     enami /* Deduce the filesystem type from the disk label. */
    574  1.53     enami static const char *
    575  1.48       abs getfslab(str)
    576  1.48       abs 	const char *str;
    577  1.48       abs {
    578  1.48       abs 	struct disklabel dl;
    579  1.48       abs 	int fd;
    580  1.48       abs 	int part;
    581  1.48       abs 	const char *vfstype;
    582  1.48       abs 	u_char fstype;
    583  1.53     enami 	char buf[MAXPATHLEN + 1];
    584  1.51       abs 	char *sp, *ep;
    585  1.48       abs 
    586  1.51       abs 	if ((fd = open(str, O_RDONLY)) == -1) {
    587  1.51       abs 		/*
    588  1.51       abs 		 * Iff we get EBUSY try the raw device. Since mount always uses
    589  1.51       abs 		 * the block device we know we are never passed a raw device.
    590  1.51       abs 		 */
    591  1.51       abs 		if (errno != EBUSY)
    592  1.51       abs 			err(1, "cannot open `%s'", str);
    593  1.51       abs 		strlcpy(buf, str, MAXPATHLEN);
    594  1.51       abs 		if ((sp = strrchr(buf, '/')) != NULL)
    595  1.51       abs 			++sp;
    596  1.51       abs 		else
    597  1.51       abs 			sp = buf;
    598  1.53     enami 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    599  1.53     enami 			*ep = *(ep - 1);
    600  1.51       abs 		*sp = 'r';
    601  1.51       abs 
    602  1.51       abs 		/* Silently fail here - mount call can display error */
    603  1.51       abs 		if ((fd = open(buf, O_RDONLY)) == -1)
    604  1.53     enami 			return (NULL);
    605  1.51       abs 	}
    606  1.49       abs 
    607  1.51       abs 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    608  1.54     enami 		(void) close(fd);
    609  1.53     enami 		return (NULL);
    610  1.49       abs 	}
    611  1.48       abs 
    612  1.48       abs 	(void) close(fd);
    613  1.48       abs 
    614  1.48       abs 	part = str[strlen(str) - 1] - 'a';
    615  1.48       abs 
    616  1.48       abs 	if (part < 0 || part >= dl.d_npartitions)
    617  1.59   nathanw 		return (NULL);
    618  1.48       abs 
    619  1.48       abs 	/* Return NULL for unknown types - caller can fall back to ffs */
    620  1.48       abs 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    621  1.48       abs 		vfstype = NULL;
    622  1.48       abs 	else
    623  1.48       abs 		vfstype = mountnames[fstype];
    624  1.48       abs 
    625  1.53     enami 	return (vfstype);
    626   1.1       cgd }
    627   1.1       cgd 
    628  1.52  jdolecek static void
    629  1.12   mycroft usage()
    630   1.1       cgd {
    631   1.1       cgd 
    632  1.12   mycroft 	(void)fprintf(stderr,
    633  1.53     enami 	    "usage: mount %s\n       mount %s\n       mount %s\n",
    634  1.57     soren 	    "[-Aadfruvw] [-t type]",
    635  1.57     soren 	    "[-dfruvw] special | node",
    636  1.57     soren 	    "[-dfruvw] [-o options] [-t type] special node");
    637  1.12   mycroft 	exit(1);
    638  1.40       cgd 	/* NOTREACHED */
    639   1.1       cgd }
    640