Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.54
      1  1.54     enami /*	$NetBSD: mount.c,v 1.54 2000/11/01 04:06:49 enami 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.54     enami __RCSID("$NetBSD: mount.c,v 1.54 2000/11/01 04:06:49 enami 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.1       cgd 
     71  1.52  jdolecek static int	debug, verbose;
     72   1.1       cgd 
     73  1.52  jdolecek static void	catopt __P((char **, const char *));
     74  1.53     enami static const char *
     75  1.53     enami 		getfslab __P((const char *str));
     76  1.53     enami static struct statfs *
     77  1.53     enami 		getmntpt __P((const char *));
     78  1.52  jdolecek static int	hasopt __P((const char *, const char *));
     79  1.53     enami static void	mangle __P((char *, int *, const char ***, int *));
     80  1.52  jdolecek static int	mountfs __P((const char *, const char *, const char *,
     81  1.53     enami 		    int, const char *, const char *, int));
     82  1.52  jdolecek static void	prmount __P((struct statfs *));
     83  1.52  jdolecek static void	usage __P((void));
     84  1.53     enami 
     85  1.53     enami void	checkname __P((int, char *[]));
     86  1.53     enami int	checkvfsname __P((const char *, const char **));
     87  1.29  christos int	main __P((int, char *[]));
     88  1.53     enami const char **
     89  1.53     enami 	makevfslist __P((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.24       cgd 	{ MNT_ASYNC,		0,	"asynchronous" },
     98  1.24       cgd 	{ MNT_DEFEXPORTED,	1,	"exported to the world" },
     99  1.24       cgd 	{ MNT_EXKERB,		1,	"kerberos uid mapping" },
    100  1.24       cgd 	{ MNT_EXPORTED,		0,	"NFS exported" },
    101  1.24       cgd 	{ MNT_EXPORTANON,	1,	"anon uid mapping" },
    102  1.24       cgd 	{ MNT_EXRDONLY,		1,	"exported read-only" },
    103  1.24       cgd 	{ MNT_LOCAL,		0,	"local" },
    104  1.26     mikel 	{ MNT_NOATIME,		0,	"noatime" },
    105  1.25       cgd 	{ MNT_NOCOREDUMP,	0,	"nocoredump" },
    106  1.24       cgd 	{ MNT_NODEV,		0,	"nodev" },
    107  1.45      kenh 	{ MNT_NODEVMTIME,	0,	"nodevmtime" },
    108  1.24       cgd 	{ MNT_NOEXEC,		0,	"noexec" },
    109  1.24       cgd 	{ MNT_NOSUID,		0,	"nosuid" },
    110  1.24       cgd 	{ MNT_QUOTA,		0,	"with quotas" },
    111  1.24       cgd 	{ MNT_RDONLY,		0,	"read-only" },
    112  1.24       cgd 	{ MNT_ROOTFS,		1,	"root file system" },
    113  1.41     enami 	{ MNT_SYMPERM,		0,	"symperm" },
    114  1.24       cgd 	{ MNT_SYNCHRONOUS,	0,	"synchronous" },
    115  1.24       cgd 	{ MNT_UNION,		0,	"union" },
    116  1.46      fvdl 	{ MNT_SOFTDEP,		0,	"soft dependencies" },
    117  1.27        pk 	{ 0 }
    118   1.1       cgd };
    119   1.1       cgd 
    120  1.40       cgd static char ffs_fstype[] = "ffs";
    121  1.30  christos 
    122  1.12   mycroft int
    123  1.12   mycroft main(argc, argv)
    124   1.1       cgd 	int argc;
    125  1.29  christos 	char *argv[];
    126   1.1       cgd {
    127  1.32     lukem 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
    128  1.12   mycroft 	struct fstab *fs;
    129   1.9       cgd 	struct statfs *mntbuf;
    130  1.12   mycroft 	FILE *mountdfp;
    131  1.22       cgd 	int all, ch, forceall, i, init_flags, mntsize, rval;
    132  1.12   mycroft 	char *options;
    133  1.43   mycroft 	const char *mountopts, *fstypename;
    134   1.1       cgd 
    135  1.52  jdolecek 	/* if called as specific mount, call it's main mount routine */
    136  1.52  jdolecek 	checkname(argc, argv);
    137  1.52  jdolecek 
    138  1.52  jdolecek 	/* started as "mount" */
    139  1.22       cgd 	all = forceall = init_flags = 0;
    140  1.12   mycroft 	options = NULL;
    141  1.32     lukem 	vfslist = NULL;
    142  1.40       cgd 	vfstype = ffs_fstype;
    143  1.31     lukem 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
    144  1.12   mycroft 		switch (ch) {
    145  1.22       cgd 		case 'A':
    146  1.22       cgd 			all = forceall = 1;
    147  1.22       cgd 			break;
    148   1.1       cgd 		case 'a':
    149   1.1       cgd 			all = 1;
    150   1.1       cgd 			break;
    151  1.12   mycroft 		case 'd':
    152  1.12   mycroft 			debug = 1;
    153  1.12   mycroft 			break;
    154   1.1       cgd 		case 'f':
    155  1.12   mycroft 			init_flags |= MNT_FORCE;
    156  1.12   mycroft 			break;
    157  1.12   mycroft 		case 'o':
    158  1.12   mycroft 			if (*optarg)
    159  1.35   mycroft 				catopt(&options, optarg);
    160   1.1       cgd 			break;
    161   1.1       cgd 		case 'r':
    162  1.12   mycroft 			init_flags |= MNT_RDONLY;
    163  1.12   mycroft 			break;
    164  1.12   mycroft 		case 't':
    165  1.32     lukem 			if (vfslist != NULL)
    166  1.53     enami 				errx(1,
    167  1.53     enami 				    "only one -t option may be specified.");
    168  1.32     lukem 			vfslist = makevfslist(optarg);
    169  1.12   mycroft 			vfstype = optarg;
    170   1.1       cgd 			break;
    171   1.1       cgd 		case 'u':
    172  1.12   mycroft 			init_flags |= MNT_UPDATE;
    173   1.1       cgd 			break;
    174   1.1       cgd 		case 'v':
    175   1.1       cgd 			verbose = 1;
    176   1.1       cgd 			break;
    177   1.1       cgd 		case 'w':
    178  1.12   mycroft 			init_flags &= ~MNT_RDONLY;
    179   1.1       cgd 			break;
    180   1.1       cgd 		case '?':
    181   1.1       cgd 		default:
    182   1.1       cgd 			usage();
    183   1.1       cgd 			/* NOTREACHED */
    184   1.1       cgd 		}
    185   1.1       cgd 	argc -= optind;
    186   1.1       cgd 	argv += optind;
    187   1.1       cgd 
    188  1.12   mycroft #define	BADTYPE(type)							\
    189  1.12   mycroft 	(strcmp(type, FSTAB_RO) &&					\
    190  1.12   mycroft 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    191  1.12   mycroft 
    192  1.12   mycroft 	rval = 0;
    193  1.12   mycroft 	switch (argc) {
    194  1.12   mycroft 	case 0:
    195  1.12   mycroft 		if (all)
    196  1.12   mycroft 			while ((fs = getfsent()) != NULL) {
    197  1.12   mycroft 				if (BADTYPE(fs->fs_type))
    198  1.12   mycroft 					continue;
    199  1.32     lukem 				if (checkvfsname(fs->fs_vfstype, vfslist))
    200  1.12   mycroft 					continue;
    201  1.12   mycroft 				if (hasopt(fs->fs_mntops, "noauto"))
    202  1.12   mycroft 					continue;
    203  1.12   mycroft 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
    204  1.12   mycroft 				    fs->fs_file, init_flags, options,
    205  1.22       cgd 				    fs->fs_mntops, !forceall))
    206  1.12   mycroft 					rval = 1;
    207  1.12   mycroft 			}
    208  1.12   mycroft 		else {
    209  1.12   mycroft 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    210  1.12   mycroft 				err(1, "getmntinfo");
    211  1.12   mycroft 			for (i = 0; i < mntsize; i++) {
    212  1.32     lukem 				if (checkvfsname(mntbuf[i].f_fstypename,
    213  1.32     lukem 				    vfslist))
    214  1.12   mycroft 					continue;
    215  1.13   mycroft 				prmount(&mntbuf[i]);
    216  1.12   mycroft 			}
    217   1.1       cgd 		}
    218   1.1       cgd 		exit(rval);
    219  1.40       cgd 		/* NOTREACHED */
    220  1.12   mycroft 	case 1:
    221  1.40       cgd 		if (vfslist != NULL) {
    222   1.1       cgd 			usage();
    223  1.40       cgd 			/* NOTREACHED */
    224  1.40       cgd 		}
    225   1.1       cgd 
    226  1.12   mycroft 		if (init_flags & MNT_UPDATE) {
    227  1.12   mycroft 			if ((mntbuf = getmntpt(*argv)) == NULL)
    228  1.12   mycroft 				errx(1,
    229  1.12   mycroft 				    "unknown special file or file system %s.",
    230  1.12   mycroft 				    *argv);
    231  1.42      ross 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    232  1.32     lukem 				mntfromname = fs->fs_spec;
    233  1.42      ross 				/* ignore the fstab file options.  */
    234  1.42      ross 				fs->fs_mntops = NULL;
    235  1.42      ross 			} else
    236  1.32     lukem 				mntfromname = mntbuf->f_mntfromname;
    237  1.42      ross 			mntonname  = mntbuf->f_mntonname;
    238  1.42      ross 			fstypename = mntbuf->f_fstypename;
    239  1.42      ross 			mountopts  = NULL;
    240  1.12   mycroft 		} else {
    241  1.12   mycroft 			if ((fs = getfsfile(*argv)) == NULL &&
    242  1.12   mycroft 			    (fs = getfsspec(*argv)) == NULL)
    243  1.12   mycroft 				errx(1,
    244  1.12   mycroft 				    "%s: unknown special file or file system.",
    245  1.12   mycroft 				    *argv);
    246  1.12   mycroft 			if (BADTYPE(fs->fs_type))
    247  1.12   mycroft 				errx(1, "%s has unknown file system type.",
    248  1.12   mycroft 				    *argv);
    249  1.32     lukem 			mntfromname = fs->fs_spec;
    250  1.42      ross 			mntonname   = fs->fs_file;
    251  1.42      ross 			fstypename  = fs->fs_vfstype;
    252  1.42      ross 			mountopts   = fs->fs_mntops;
    253  1.12   mycroft 		}
    254  1.42      ross 		rval = mountfs(fstypename, mntfromname,
    255  1.42      ross 		    mntonname, init_flags, options, mountopts, 0);
    256  1.12   mycroft 		break;
    257  1.12   mycroft 	case 2:
    258   1.1       cgd 		/*
    259  1.12   mycroft 		 * If -t flag has not been specified, and spec contains either
    260  1.12   mycroft 		 * a ':' or a '@' then assume that an NFS filesystem is being
    261  1.12   mycroft 		 * specified ala Sun.
    262   1.1       cgd 		 */
    263  1.48       abs 		if (vfslist == NULL) {
    264  1.48       abs 			if (strpbrk(argv[0], ":@") != NULL)
    265  1.48       abs 				vfstype = "nfs";
    266  1.48       abs 			else {
    267  1.48       abs 				vfstype = getfslab(argv[0]);
    268  1.48       abs 				if (vfstype == NULL)
    269  1.48       abs 					vfstype = ffs_fstype;
    270  1.48       abs 			}
    271  1.48       abs 		}
    272  1.12   mycroft 		rval = mountfs(vfstype,
    273  1.22       cgd 		    argv[0], argv[1], init_flags, options, NULL, 0);
    274  1.12   mycroft 		break;
    275  1.12   mycroft 	default:
    276  1.12   mycroft 		usage();
    277  1.12   mycroft 		/* NOTREACHED */
    278   1.1       cgd 	}
    279   1.1       cgd 
    280  1.12   mycroft 	/*
    281  1.12   mycroft 	 * If the mount was successfully, and done by root, tell mountd the
    282  1.12   mycroft 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    283  1.12   mycroft 	 */
    284  1.12   mycroft 	if (rval == 0 && getuid() == 0 &&
    285  1.12   mycroft 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    286  1.29  christos 		int pid;
    287  1.29  christos 
    288  1.29  christos 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    289  1.53     enami 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    290  1.12   mycroft 			err(1, "signal mountd");
    291  1.12   mycroft 		(void)fclose(mountdfp);
    292   1.1       cgd 	}
    293   1.1       cgd 
    294  1.12   mycroft 	exit(rval);
    295  1.40       cgd 	/* NOTREACHED */
    296   1.1       cgd }
    297   1.1       cgd 
    298  1.12   mycroft int
    299  1.12   mycroft hasopt(mntopts, option)
    300  1.12   mycroft 	const char *mntopts, *option;
    301   1.1       cgd {
    302  1.12   mycroft 	int negative, found;
    303  1.12   mycroft 	char *opt, *optbuf;
    304   1.1       cgd 
    305  1.12   mycroft 	if (option[0] == 'n' && option[1] == 'o') {
    306  1.12   mycroft 		negative = 1;
    307  1.12   mycroft 		option += 2;
    308  1.12   mycroft 	} else
    309  1.12   mycroft 		negative = 0;
    310  1.12   mycroft 	optbuf = strdup(mntopts);
    311  1.12   mycroft 	found = 0;
    312  1.12   mycroft 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    313  1.12   mycroft 		if (opt[0] == 'n' && opt[1] == 'o') {
    314  1.12   mycroft 			if (!strcasecmp(opt + 2, option))
    315  1.12   mycroft 				found = negative;
    316  1.12   mycroft 		} else if (!strcasecmp(opt, option))
    317  1.12   mycroft 			found = !negative;
    318  1.12   mycroft 	}
    319  1.12   mycroft 	free(optbuf);
    320  1.12   mycroft 	return (found);
    321   1.1       cgd }
    322   1.1       cgd 
    323  1.52  jdolecek static int
    324  1.22       cgd mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
    325  1.12   mycroft 	const char *vfstype, *spec, *name, *options, *mntopts;
    326  1.22       cgd 	int flags, skipmounted;
    327   1.1       cgd {
    328  1.12   mycroft 	/* List of directories containing mount_xxx subcommands. */
    329  1.12   mycroft 	static const char *edirs[] = {
    330  1.12   mycroft 		_PATH_SBIN,
    331  1.12   mycroft 		_PATH_USRSBIN,
    332  1.12   mycroft 		NULL
    333  1.12   mycroft 	};
    334  1.35   mycroft 	const char **argv, **edir;
    335  1.36  drochner 	struct statfs *sfp, sf;
    336  1.12   mycroft 	pid_t pid;
    337  1.36  drochner 	int argc, numfs, i, status, maxargc;
    338  1.33  christos 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
    339  1.33  christos 	    mntpath[MAXPATHLEN];
    340  1.35   mycroft 
    341  1.29  christos #ifdef __GNUC__
    342  1.29  christos 	(void) &name;
    343  1.29  christos 	(void) &optbuf;
    344  1.30  christos 	(void) &vfstype;
    345  1.29  christos #endif
    346   1.1       cgd 
    347  1.12   mycroft 	if (realpath(name, mntpath) == NULL) {
    348  1.20   ghudson 		warn("realpath %s", name);
    349  1.12   mycroft 		return (1);
    350  1.12   mycroft 	}
    351   1.1       cgd 
    352  1.12   mycroft 	name = mntpath;
    353   1.1       cgd 
    354  1.35   mycroft 	optbuf = NULL;
    355  1.35   mycroft 	if (mntopts)
    356  1.35   mycroft 		catopt(&optbuf, mntopts);
    357  1.35   mycroft 	if (options)
    358  1.35   mycroft 		catopt(&optbuf, options);
    359  1.35   mycroft 	if (!mntopts && !options)
    360  1.35   mycroft 		catopt(&optbuf, "rw");
    361  1.12   mycroft 
    362  1.35   mycroft 	if (!strcmp(name, "/"))
    363  1.12   mycroft 		flags |= MNT_UPDATE;
    364  1.22       cgd 	else if (skipmounted) {
    365  1.36  drochner 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
    366  1.36  drochner 			warn("getmntinfo");
    367  1.21       cgd 			return (1);
    368  1.21       cgd 		}
    369  1.36  drochner 		for(i = 0; i < numfs; i++) {
    370  1.53     enami 			/*
    371  1.53     enami 			 * XXX can't check f_mntfromname,
    372  1.53     enami 			 * thanks to mfs, union, etc.
    373  1.53     enami 			 */
    374  1.36  drochner 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
    375  1.36  drochner 			    strncmp(vfstype, sfp[i].f_fstypename,
    376  1.53     enami 				MFSNAMELEN) == 0) {
    377  1.36  drochner 				if (verbose)
    378  1.53     enami 					(void)printf("%s on %s type %.*s: "
    379  1.53     enami 					    "%s\n",
    380  1.53     enami 					    sfp[i].f_mntfromname,
    381  1.53     enami 					    sfp[i].f_mntonname,
    382  1.53     enami 					    MFSNAMELEN,
    383  1.53     enami 					    sfp[i].f_fstypename,
    384  1.53     enami 					    "already mounted");
    385  1.36  drochner 				return (0);
    386  1.36  drochner 			}
    387  1.21       cgd 		}
    388  1.21       cgd 	}
    389  1.12   mycroft 	if (flags & MNT_FORCE)
    390  1.35   mycroft 		catopt(&optbuf, "force");
    391  1.12   mycroft 	if (flags & MNT_RDONLY)
    392  1.35   mycroft 		catopt(&optbuf, "ro");
    393  1.33  christos 
    394  1.30  christos 	if (flags & MNT_UPDATE) {
    395  1.35   mycroft 		catopt(&optbuf, "update");
    396  1.30  christos 		/* Figure out the fstype only if we defaulted to ffs */
    397  1.40       cgd 		if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
    398  1.30  christos 			vfstype = sf.f_fstypename;
    399  1.30  christos 	}
    400  1.12   mycroft 
    401  1.35   mycroft 	maxargc = 64;
    402  1.35   mycroft 	argv = malloc(sizeof(char *) * maxargc);
    403  1.35   mycroft 
    404  1.33  christos 	(void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    405  1.12   mycroft 	argc = 0;
    406  1.33  christos 	argv[argc++] = execbase;
    407  1.35   mycroft 	if (optbuf)
    408  1.35   mycroft 		mangle(optbuf, &argc, &argv, &maxargc);
    409  1.12   mycroft 	argv[argc++] = spec;
    410  1.12   mycroft 	argv[argc++] = name;
    411  1.12   mycroft 	argv[argc] = NULL;
    412  1.12   mycroft 
    413  1.34  christos 	if (verbose) {
    414  1.35   mycroft 		(void)printf("exec:");
    415  1.35   mycroft 		for (i = 0; i < argc; i++)
    416  1.12   mycroft 			(void)printf(" %s", argv[i]);
    417  1.12   mycroft 		(void)printf("\n");
    418  1.12   mycroft 	}
    419   1.5   mycroft 
    420  1.12   mycroft 	switch (pid = vfork()) {
    421  1.12   mycroft 	case -1:				/* Error. */
    422  1.12   mycroft 		warn("vfork");
    423  1.35   mycroft 		if (optbuf)
    424  1.35   mycroft 			free(optbuf);
    425  1.12   mycroft 		return (1);
    426  1.35   mycroft 
    427  1.12   mycroft 	case 0:					/* Child. */
    428  1.34  christos 		if (debug)
    429  1.34  christos 			_exit(0);
    430  1.34  christos 
    431  1.12   mycroft 		/* Go find an executable. */
    432  1.12   mycroft 		edir = edirs;
    433  1.12   mycroft 		do {
    434  1.12   mycroft 			(void)snprintf(execname,
    435  1.33  christos 			    sizeof(execname), "%s/%s", *edir, execbase);
    436  1.40       cgd 			(void)execv(execname, (char * const *)argv);
    437  1.12   mycroft 			if (errno != ENOENT)
    438  1.12   mycroft 				warn("exec %s for %s", execname, name);
    439  1.12   mycroft 		} while (*++edir != NULL);
    440   1.1       cgd 
    441  1.12   mycroft 		if (errno == ENOENT)
    442  1.33  christos 			warnx("%s not found for %s", execbase, name);
    443  1.34  christos 		_exit(1);
    444  1.12   mycroft 		/* NOTREACHED */
    445  1.35   mycroft 
    446  1.12   mycroft 	default:				/* Parent. */
    447  1.35   mycroft 		if (optbuf)
    448  1.35   mycroft 			free(optbuf);
    449   1.1       cgd 
    450  1.12   mycroft 		if (waitpid(pid, &status, 0) < 0) {
    451  1.12   mycroft 			warn("waitpid");
    452  1.12   mycroft 			return (1);
    453   1.1       cgd 		}
    454  1.12   mycroft 
    455  1.12   mycroft 		if (WIFEXITED(status)) {
    456  1.12   mycroft 			if (WEXITSTATUS(status) != 0)
    457  1.12   mycroft 				return (WEXITSTATUS(status));
    458  1.12   mycroft 		} else if (WIFSIGNALED(status)) {
    459  1.17       jtc 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    460  1.12   mycroft 			return (1);
    461   1.1       cgd 		}
    462  1.12   mycroft 
    463  1.12   mycroft 		if (verbose) {
    464  1.12   mycroft 			if (statfs(name, &sf) < 0) {
    465  1.12   mycroft 				warn("statfs %s", name);
    466  1.12   mycroft 				return (1);
    467  1.12   mycroft 			}
    468  1.13   mycroft 			prmount(&sf);
    469   1.1       cgd 		}
    470  1.12   mycroft 		break;
    471   1.1       cgd 	}
    472  1.12   mycroft 
    473  1.12   mycroft 	return (0);
    474   1.1       cgd }
    475   1.1       cgd 
    476  1.52  jdolecek static void
    477  1.32     lukem prmount(sfp)
    478  1.32     lukem 	struct statfs *sfp;
    479  1.13   mycroft {
    480  1.12   mycroft 	int flags;
    481  1.52  jdolecek 	const struct opt *o;
    482  1.32     lukem 	struct passwd *pw;
    483  1.12   mycroft 	int f;
    484   1.1       cgd 
    485  1.53     enami 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    486  1.53     enami 	    sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
    487   1.1       cgd 
    488  1.32     lukem 	flags = sfp->f_flags & MNT_VISFLAGMASK;
    489  1.12   mycroft 	for (f = 0, o = optnames; flags && o->o_opt; o++)
    490  1.12   mycroft 		if (flags & o->o_opt) {
    491  1.24       cgd 			if (!o->o_silent)
    492  1.24       cgd 				(void)printf("%s%s", !f++ ? " (" : ", ",
    493  1.24       cgd 				    o->o_name);
    494  1.12   mycroft 			flags &= ~o->o_opt;
    495  1.12   mycroft 		}
    496  1.24       cgd 	if (flags)
    497  1.27        pk 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    498  1.24       cgd 		    flags & (flags - 1) ? "s" : "", flags);
    499  1.32     lukem 	if (sfp->f_owner) {
    500  1.32     lukem 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    501  1.32     lukem 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    502  1.32     lukem 			(void)printf("%s", pw->pw_name);
    503  1.32     lukem 		else
    504  1.32     lukem 			(void)printf("%d", sfp->f_owner);
    505  1.32     lukem 	}
    506  1.46      fvdl 	if (verbose)
    507  1.46      fvdl 		(void)printf("%swrites: sync %ld async %ld)\n",
    508  1.46      fvdl 		    !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
    509  1.46      fvdl 	else
    510  1.50        is 		(void)printf("%s", f ? ")\n" : "\n");
    511   1.1       cgd }
    512   1.1       cgd 
    513  1.52  jdolecek static struct statfs *
    514   1.1       cgd getmntpt(name)
    515  1.12   mycroft 	const char *name;
    516   1.1       cgd {
    517   1.1       cgd 	struct statfs *mntbuf;
    518  1.12   mycroft 	int i, mntsize;
    519   1.1       cgd 
    520   1.1       cgd 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    521  1.12   mycroft 	for (i = 0; i < mntsize; i++)
    522  1.12   mycroft 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    523  1.12   mycroft 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    524   1.1       cgd 			return (&mntbuf[i]);
    525  1.12   mycroft 	return (NULL);
    526   1.1       cgd }
    527   1.1       cgd 
    528  1.52  jdolecek static void
    529  1.35   mycroft catopt(sp, o)
    530  1.35   mycroft 	char **sp;
    531  1.35   mycroft 	const char *o;
    532  1.12   mycroft {
    533  1.35   mycroft 	char *s;
    534  1.35   mycroft 	size_t i, j;
    535  1.12   mycroft 
    536  1.35   mycroft 	s = *sp;
    537  1.35   mycroft 	if (s) {
    538  1.35   mycroft 		i = strlen(s);
    539  1.35   mycroft 		j = i + 1 + strlen(o) + 1;
    540  1.35   mycroft 		s = realloc(s, j);
    541  1.35   mycroft 		(void)snprintf(s + i, j, ",%s", o);
    542  1.12   mycroft 	} else
    543  1.35   mycroft 		s = strdup(o);
    544  1.35   mycroft 	*sp = s;
    545   1.1       cgd }
    546   1.1       cgd 
    547  1.35   mycroft static void
    548  1.35   mycroft mangle(options, argcp, argvp, maxargcp)
    549  1.12   mycroft 	char *options;
    550  1.35   mycroft 	int *argcp, *maxargcp;
    551  1.35   mycroft 	const char ***argvp;
    552   1.1       cgd {
    553  1.12   mycroft 	char *p, *s;
    554  1.35   mycroft 	int argc, maxargc;
    555  1.35   mycroft 	const char **argv;
    556   1.1       cgd 
    557  1.12   mycroft 	argc = *argcp;
    558  1.35   mycroft 	argv = *argvp;
    559  1.35   mycroft 	maxargc = *maxargcp;
    560  1.35   mycroft 
    561  1.35   mycroft 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    562  1.35   mycroft 		/* Always leave space for one more argument and the NULL. */
    563  1.35   mycroft 		if (argc >= maxargc - 4) {
    564  1.35   mycroft 			maxargc <<= 1;
    565  1.35   mycroft 			argv = realloc(argv, maxargc * sizeof(char *));
    566  1.35   mycroft 		}
    567  1.44      ross 		if (*p != '\0') {
    568  1.12   mycroft 			if (*p == '-') {
    569  1.12   mycroft 				argv[argc++] = p;
    570  1.12   mycroft 				p = strchr(p, '=');
    571  1.12   mycroft 				if (p) {
    572  1.12   mycroft 					*p = '\0';
    573  1.12   mycroft 					argv[argc++] = p+1;
    574   1.1       cgd 				}
    575  1.12   mycroft 			} else if (strcmp(p, "rw") != 0) {
    576  1.12   mycroft 				argv[argc++] = "-o";
    577  1.12   mycroft 				argv[argc++] = p;
    578   1.1       cgd 			}
    579  1.44      ross 		}
    580  1.35   mycroft 	}
    581  1.12   mycroft 
    582  1.12   mycroft 	*argcp = argc;
    583  1.35   mycroft 	*argvp = argv;
    584  1.35   mycroft 	*maxargcp = maxargc;
    585  1.48       abs }
    586  1.48       abs 
    587  1.53     enami /* Deduce the filesystem type from the disk label. */
    588  1.53     enami static const char *
    589  1.48       abs getfslab(str)
    590  1.48       abs 	const char *str;
    591  1.48       abs {
    592  1.48       abs 	struct disklabel dl;
    593  1.48       abs 	int fd;
    594  1.48       abs 	int part;
    595  1.48       abs 	const char *vfstype;
    596  1.48       abs 	u_char fstype;
    597  1.53     enami 	char buf[MAXPATHLEN + 1];
    598  1.51       abs 	char *sp, *ep;
    599  1.48       abs 
    600  1.51       abs 	if ((fd = open(str, O_RDONLY)) == -1) {
    601  1.51       abs 		/*
    602  1.51       abs 		 * Iff we get EBUSY try the raw device. Since mount always uses
    603  1.51       abs 		 * the block device we know we are never passed a raw device.
    604  1.51       abs 		 */
    605  1.51       abs 		if (errno != EBUSY)
    606  1.51       abs 			err(1, "cannot open `%s'", str);
    607  1.51       abs 		strlcpy(buf, str, MAXPATHLEN);
    608  1.51       abs 		if ((sp = strrchr(buf, '/')) != NULL)
    609  1.51       abs 			++sp;
    610  1.51       abs 		else
    611  1.51       abs 			sp = buf;
    612  1.53     enami 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    613  1.53     enami 			*ep = *(ep - 1);
    614  1.51       abs 		*sp = 'r';
    615  1.51       abs 
    616  1.51       abs 		/* Silently fail here - mount call can display error */
    617  1.51       abs 		if ((fd = open(buf, O_RDONLY)) == -1)
    618  1.53     enami 			return (NULL);
    619  1.51       abs 	}
    620  1.49       abs 
    621  1.51       abs 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    622  1.54     enami 		(void) close(fd);
    623  1.53     enami 		return (NULL);
    624  1.49       abs 	}
    625  1.48       abs 
    626  1.48       abs 	(void) close(fd);
    627  1.48       abs 
    628  1.48       abs 	part = str[strlen(str) - 1] - 'a';
    629  1.48       abs 
    630  1.48       abs 	if (part < 0 || part >= dl.d_npartitions)
    631  1.48       abs 		errx(1, "partition `%s' is not defined on disk", str);
    632  1.48       abs 
    633  1.48       abs 	/* Return NULL for unknown types - caller can fall back to ffs */
    634  1.48       abs 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    635  1.48       abs 		vfstype = NULL;
    636  1.48       abs 	else
    637  1.48       abs 		vfstype = mountnames[fstype];
    638  1.48       abs 
    639  1.53     enami 	return (vfstype);
    640   1.1       cgd }
    641   1.1       cgd 
    642  1.52  jdolecek static void
    643  1.12   mycroft usage()
    644   1.1       cgd {
    645   1.1       cgd 
    646  1.12   mycroft 	(void)fprintf(stderr,
    647  1.53     enami 	    "usage: mount %s\n       mount %s\n       mount %s\n",
    648  1.53     enami 	    "[-dfruvw] [-o options] [-t ffs | external_type] special node",
    649  1.53     enami 	    "[-adfruvw] [-t ffs | external_type]",
    650  1.53     enami 	    "[-dfruvw] special | node");
    651  1.12   mycroft 	exit(1);
    652  1.40       cgd 	/* NOTREACHED */
    653   1.1       cgd }
    654