Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.32.2.3
      1  1.32.2.3   mycroft /*	$NetBSD: mount.c,v 1.32.2.3 1998/05/08 05:26:13 mycroft 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.32.2.3   mycroft __RCSID("$NetBSD: mount.c,v 1.32.2.3 1998/05/08 05:26:13 mycroft 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.1       cgd #include "pathnames.h"
     65       1.1       cgd 
     66      1.15   mycroft int	debug, verbose;
     67       1.1       cgd 
     68      1.32     lukem int	checkvfsname __P((const char *, const char **));
     69  1.32.2.1    mellon void	catopt __P((char **, const char *));
     70      1.12   mycroft struct statfs
     71      1.12   mycroft        *getmntpt __P((const char *));
     72      1.12   mycroft int	hasopt __P((const char *, const char *));
     73      1.32     lukem const char
     74      1.32     lukem       **makevfslist __P((char *));
     75  1.32.2.1    mellon static void
     76  1.32.2.1    mellon 	mangle __P((char *, int *, const char ***, int *));
     77      1.12   mycroft int	mountfs __P((const char *, const char *, const char *,
     78      1.22       cgd 			int, const char *, const char *, int));
     79      1.13   mycroft void	prmount __P((struct statfs *));
     80      1.12   mycroft void	usage __P((void));
     81      1.29  christos int	main __P((int, char *[]));
     82      1.12   mycroft 
     83      1.12   mycroft /* Map from mount otions to printable formats. */
     84      1.12   mycroft static struct opt {
     85      1.12   mycroft 	int o_opt;
     86      1.24       cgd 	int o_silent;
     87      1.12   mycroft 	const char *o_name;
     88      1.12   mycroft } optnames[] = {
     89      1.24       cgd 	{ MNT_ASYNC,		0,	"asynchronous" },
     90      1.24       cgd 	{ MNT_DEFEXPORTED,	1,	"exported to the world" },
     91      1.24       cgd 	{ MNT_EXKERB,		1,	"kerberos uid mapping" },
     92      1.24       cgd 	{ MNT_EXPORTED,		0,	"NFS exported" },
     93      1.24       cgd 	{ MNT_EXPORTANON,	1,	"anon uid mapping" },
     94      1.24       cgd 	{ MNT_EXRDONLY,		1,	"exported read-only" },
     95      1.24       cgd 	{ MNT_LOCAL,		0,	"local" },
     96      1.26     mikel 	{ MNT_NOATIME,		0,	"noatime" },
     97      1.25       cgd 	{ MNT_NOCOREDUMP,	0,	"nocoredump" },
     98      1.24       cgd 	{ MNT_NODEV,		0,	"nodev" },
     99      1.24       cgd 	{ MNT_NOEXEC,		0,	"noexec" },
    100      1.24       cgd 	{ MNT_NOSUID,		0,	"nosuid" },
    101      1.24       cgd 	{ MNT_QUOTA,		0,	"with quotas" },
    102      1.24       cgd 	{ MNT_RDONLY,		0,	"read-only" },
    103      1.24       cgd 	{ MNT_ROOTFS,		1,	"root file system" },
    104      1.24       cgd 	{ MNT_SYNCHRONOUS,	0,	"synchronous" },
    105      1.24       cgd 	{ MNT_UNION,		0,	"union" },
    106      1.27        pk 	{ 0 }
    107       1.1       cgd };
    108       1.1       cgd 
    109      1.30  christos static char ffs[] = "ffs";
    110      1.30  christos 
    111      1.12   mycroft int
    112      1.12   mycroft main(argc, argv)
    113       1.1       cgd 	int argc;
    114      1.29  christos 	char *argv[];
    115       1.1       cgd {
    116      1.32     lukem 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
    117      1.12   mycroft 	struct fstab *fs;
    118       1.9       cgd 	struct statfs *mntbuf;
    119      1.12   mycroft 	FILE *mountdfp;
    120      1.22       cgd 	int all, ch, forceall, i, init_flags, mntsize, rval;
    121      1.12   mycroft 	char *options;
    122  1.32.2.3   mycroft 	char *mountopts, *fstypename;
    123       1.1       cgd 
    124      1.22       cgd 	all = forceall = init_flags = 0;
    125      1.12   mycroft 	options = NULL;
    126      1.32     lukem 	vfslist = NULL;
    127      1.30  christos 	vfstype = ffs;
    128      1.31     lukem 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
    129      1.12   mycroft 		switch (ch) {
    130      1.22       cgd 		case 'A':
    131      1.22       cgd 			all = forceall = 1;
    132      1.22       cgd 			break;
    133       1.1       cgd 		case 'a':
    134       1.1       cgd 			all = 1;
    135       1.1       cgd 			break;
    136      1.12   mycroft 		case 'd':
    137      1.12   mycroft 			debug = 1;
    138      1.12   mycroft 			break;
    139       1.1       cgd 		case 'f':
    140      1.12   mycroft 			init_flags |= MNT_FORCE;
    141      1.12   mycroft 			break;
    142      1.12   mycroft 		case 'o':
    143      1.12   mycroft 			if (*optarg)
    144  1.32.2.1    mellon 				catopt(&options, optarg);
    145       1.1       cgd 			break;
    146       1.1       cgd 		case 'r':
    147      1.12   mycroft 			init_flags |= MNT_RDONLY;
    148      1.12   mycroft 			break;
    149      1.12   mycroft 		case 't':
    150      1.32     lukem 			if (vfslist != NULL)
    151      1.12   mycroft 				errx(1, "only one -t option may be specified.");
    152      1.32     lukem 			vfslist = makevfslist(optarg);
    153      1.12   mycroft 			vfstype = optarg;
    154       1.1       cgd 			break;
    155       1.1       cgd 		case 'u':
    156      1.12   mycroft 			init_flags |= MNT_UPDATE;
    157       1.1       cgd 			break;
    158       1.1       cgd 		case 'v':
    159       1.1       cgd 			verbose = 1;
    160       1.1       cgd 			break;
    161       1.1       cgd 		case 'w':
    162      1.12   mycroft 			init_flags &= ~MNT_RDONLY;
    163       1.1       cgd 			break;
    164       1.1       cgd 		case '?':
    165       1.1       cgd 		default:
    166       1.1       cgd 			usage();
    167       1.1       cgd 			/* NOTREACHED */
    168       1.1       cgd 		}
    169       1.1       cgd 	argc -= optind;
    170       1.1       cgd 	argv += optind;
    171       1.1       cgd 
    172      1.12   mycroft #define	BADTYPE(type)							\
    173      1.12   mycroft 	(strcmp(type, FSTAB_RO) &&					\
    174      1.12   mycroft 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    175      1.12   mycroft 
    176      1.12   mycroft 	rval = 0;
    177      1.12   mycroft 	switch (argc) {
    178      1.12   mycroft 	case 0:
    179      1.12   mycroft 		if (all)
    180      1.12   mycroft 			while ((fs = getfsent()) != NULL) {
    181      1.12   mycroft 				if (BADTYPE(fs->fs_type))
    182      1.12   mycroft 					continue;
    183      1.32     lukem 				if (checkvfsname(fs->fs_vfstype, vfslist))
    184      1.12   mycroft 					continue;
    185      1.12   mycroft 				if (hasopt(fs->fs_mntops, "noauto"))
    186      1.12   mycroft 					continue;
    187      1.12   mycroft 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
    188      1.12   mycroft 				    fs->fs_file, init_flags, options,
    189      1.22       cgd 				    fs->fs_mntops, !forceall))
    190      1.12   mycroft 					rval = 1;
    191      1.12   mycroft 			}
    192      1.12   mycroft 		else {
    193      1.12   mycroft 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    194      1.12   mycroft 				err(1, "getmntinfo");
    195      1.12   mycroft 			for (i = 0; i < mntsize; i++) {
    196      1.32     lukem 				if (checkvfsname(mntbuf[i].f_fstypename,
    197      1.32     lukem 				    vfslist))
    198      1.12   mycroft 					continue;
    199      1.13   mycroft 				prmount(&mntbuf[i]);
    200      1.12   mycroft 			}
    201       1.1       cgd 		}
    202       1.1       cgd 		exit(rval);
    203      1.12   mycroft 	case 1:
    204      1.32     lukem 		if (vfslist != NULL)
    205       1.1       cgd 			usage();
    206       1.1       cgd 
    207      1.12   mycroft 		if (init_flags & MNT_UPDATE) {
    208      1.12   mycroft 			if ((mntbuf = getmntpt(*argv)) == NULL)
    209      1.12   mycroft 				errx(1,
    210      1.12   mycroft 				    "unknown special file or file system %s.",
    211      1.12   mycroft 				    *argv);
    212  1.32.2.3   mycroft 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    213      1.32     lukem 				mntfromname = fs->fs_spec;
    214  1.32.2.3   mycroft 				/* ignore the fstab file options.  */
    215  1.32.2.3   mycroft 				fs->fs_mntops = NULL;
    216  1.32.2.3   mycroft 			} else
    217      1.32     lukem 				mntfromname = mntbuf->f_mntfromname;
    218  1.32.2.3   mycroft 			mntonname  = mntbuf->f_mntonname;
    219  1.32.2.3   mycroft 			fstypename = mntbuf->f_fstypename;
    220  1.32.2.3   mycroft 			mountopts  = NULL;
    221      1.12   mycroft 		} else {
    222      1.12   mycroft 			if ((fs = getfsfile(*argv)) == NULL &&
    223      1.12   mycroft 			    (fs = getfsspec(*argv)) == NULL)
    224      1.12   mycroft 				errx(1,
    225      1.12   mycroft 				    "%s: unknown special file or file system.",
    226      1.12   mycroft 				    *argv);
    227      1.12   mycroft 			if (BADTYPE(fs->fs_type))
    228      1.12   mycroft 				errx(1, "%s has unknown file system type.",
    229      1.12   mycroft 				    *argv);
    230      1.32     lukem 			mntfromname = fs->fs_spec;
    231  1.32.2.3   mycroft 			mntonname   = fs->fs_file;
    232  1.32.2.3   mycroft 			fstypename  = fs->fs_vfstype;
    233  1.32.2.3   mycroft 			mountopts   = fs->fs_mntops;
    234      1.12   mycroft 		}
    235  1.32.2.3   mycroft 		rval = mountfs(fstypename, mntfromname,
    236  1.32.2.3   mycroft 		    mntonname, init_flags, options, mountopts, 0);
    237      1.12   mycroft 		break;
    238      1.12   mycroft 	case 2:
    239       1.1       cgd 		/*
    240      1.12   mycroft 		 * If -t flag has not been specified, and spec contains either
    241      1.12   mycroft 		 * a ':' or a '@' then assume that an NFS filesystem is being
    242      1.12   mycroft 		 * specified ala Sun.
    243       1.1       cgd 		 */
    244      1.32     lukem 		if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
    245      1.12   mycroft 			vfstype = "nfs";
    246      1.12   mycroft 		rval = mountfs(vfstype,
    247      1.22       cgd 		    argv[0], argv[1], init_flags, options, NULL, 0);
    248      1.12   mycroft 		break;
    249      1.12   mycroft 	default:
    250      1.12   mycroft 		usage();
    251      1.12   mycroft 		/* NOTREACHED */
    252       1.1       cgd 	}
    253       1.1       cgd 
    254      1.12   mycroft 	/*
    255      1.12   mycroft 	 * If the mount was successfully, and done by root, tell mountd the
    256      1.12   mycroft 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    257      1.12   mycroft 	 */
    258      1.12   mycroft 	if (rval == 0 && getuid() == 0 &&
    259      1.12   mycroft 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    260      1.29  christos 		int pid;
    261      1.29  christos 
    262      1.29  christos 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    263      1.12   mycroft 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    264      1.12   mycroft 			err(1, "signal mountd");
    265      1.12   mycroft 		(void)fclose(mountdfp);
    266       1.1       cgd 	}
    267       1.1       cgd 
    268      1.12   mycroft 	exit(rval);
    269       1.1       cgd }
    270       1.1       cgd 
    271      1.12   mycroft int
    272      1.12   mycroft hasopt(mntopts, option)
    273      1.12   mycroft 	const char *mntopts, *option;
    274       1.1       cgd {
    275      1.12   mycroft 	int negative, found;
    276      1.12   mycroft 	char *opt, *optbuf;
    277       1.1       cgd 
    278      1.12   mycroft 	if (option[0] == 'n' && option[1] == 'o') {
    279      1.12   mycroft 		negative = 1;
    280      1.12   mycroft 		option += 2;
    281      1.12   mycroft 	} else
    282      1.12   mycroft 		negative = 0;
    283      1.12   mycroft 	optbuf = strdup(mntopts);
    284      1.12   mycroft 	found = 0;
    285      1.12   mycroft 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    286      1.12   mycroft 		if (opt[0] == 'n' && opt[1] == 'o') {
    287      1.12   mycroft 			if (!strcasecmp(opt + 2, option))
    288      1.12   mycroft 				found = negative;
    289      1.12   mycroft 		} else if (!strcasecmp(opt, option))
    290      1.12   mycroft 			found = !negative;
    291      1.12   mycroft 	}
    292      1.12   mycroft 	free(optbuf);
    293      1.12   mycroft 	return (found);
    294       1.1       cgd }
    295       1.1       cgd 
    296      1.12   mycroft int
    297      1.22       cgd mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
    298      1.12   mycroft 	const char *vfstype, *spec, *name, *options, *mntopts;
    299      1.22       cgd 	int flags, skipmounted;
    300       1.1       cgd {
    301      1.12   mycroft 	/* List of directories containing mount_xxx subcommands. */
    302      1.12   mycroft 	static const char *edirs[] = {
    303      1.12   mycroft 		_PATH_SBIN,
    304      1.12   mycroft 		_PATH_USRSBIN,
    305      1.12   mycroft 		NULL
    306      1.12   mycroft 	};
    307  1.32.2.1    mellon 	const char **argv, **edir;
    308  1.32.2.2    mellon 	struct statfs *sfp, sf;
    309      1.12   mycroft 	pid_t pid;
    310  1.32.2.2    mellon 	int argc, numfs, i, status, maxargc;
    311  1.32.2.1    mellon 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
    312  1.32.2.1    mellon 	    mntpath[MAXPATHLEN];
    313  1.32.2.1    mellon 
    314      1.29  christos #ifdef __GNUC__
    315      1.29  christos 	(void) &name;
    316      1.29  christos 	(void) &optbuf;
    317      1.30  christos 	(void) &vfstype;
    318      1.29  christos #endif
    319       1.1       cgd 
    320      1.12   mycroft 	if (realpath(name, mntpath) == NULL) {
    321      1.20   ghudson 		warn("realpath %s", name);
    322      1.12   mycroft 		return (1);
    323      1.12   mycroft 	}
    324       1.1       cgd 
    325      1.12   mycroft 	name = mntpath;
    326       1.1       cgd 
    327  1.32.2.1    mellon 	optbuf = NULL;
    328  1.32.2.1    mellon 	if (mntopts)
    329  1.32.2.1    mellon 		catopt(&optbuf, mntopts);
    330  1.32.2.1    mellon 	if (options)
    331  1.32.2.1    mellon 		catopt(&optbuf, options);
    332  1.32.2.1    mellon 	if (!mntopts && !options)
    333  1.32.2.1    mellon 		catopt(&optbuf, "rw");
    334      1.12   mycroft 
    335  1.32.2.1    mellon 	if (!strcmp(name, "/"))
    336      1.12   mycroft 		flags |= MNT_UPDATE;
    337      1.22       cgd 	else if (skipmounted) {
    338  1.32.2.2    mellon 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
    339  1.32.2.2    mellon 			warn("getmntinfo");
    340      1.21       cgd 			return (1);
    341      1.21       cgd 		}
    342  1.32.2.2    mellon 		for(i = 0; i < numfs; i++) {
    343  1.32.2.2    mellon 			/* XXX can't check f_mntfromname,
    344  1.32.2.2    mellon 			 thanks to mfs, union, etc. */
    345  1.32.2.2    mellon 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
    346  1.32.2.2    mellon 			    strncmp(vfstype, sfp[i].f_fstypename,
    347  1.32.2.2    mellon 				    MFSNAMELEN) == 0) {
    348  1.32.2.2    mellon 				if (verbose)
    349  1.32.2.2    mellon 					(void)printf("%s on %s type %.*s: %s\n",
    350  1.32.2.2    mellon 						     sfp[i].f_mntfromname,
    351  1.32.2.2    mellon 						     sfp[i].f_mntonname,
    352  1.32.2.2    mellon 						     MFSNAMELEN,
    353  1.32.2.2    mellon 						     sfp[i].f_fstypename,
    354  1.32.2.2    mellon 						     "already mounted");
    355  1.32.2.2    mellon 				return (0);
    356  1.32.2.2    mellon 			}
    357      1.21       cgd 		}
    358      1.21       cgd 	}
    359      1.12   mycroft 	if (flags & MNT_FORCE)
    360  1.32.2.1    mellon 		catopt(&optbuf, "force");
    361      1.12   mycroft 	if (flags & MNT_RDONLY)
    362  1.32.2.1    mellon 		catopt(&optbuf, "ro");
    363  1.32.2.1    mellon 
    364      1.30  christos 	if (flags & MNT_UPDATE) {
    365  1.32.2.1    mellon 		catopt(&optbuf, "update");
    366      1.30  christos 		/* Figure out the fstype only if we defaulted to ffs */
    367      1.30  christos 		if (vfstype == ffs && statfs(name, &sf) != -1)
    368      1.30  christos 			vfstype = sf.f_fstypename;
    369      1.30  christos 	}
    370      1.12   mycroft 
    371  1.32.2.1    mellon 	maxargc = 64;
    372  1.32.2.1    mellon 	argv = malloc(sizeof(char *) * maxargc);
    373  1.32.2.1    mellon 
    374  1.32.2.1    mellon 	(void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    375      1.12   mycroft 	argc = 0;
    376  1.32.2.1    mellon 	argv[argc++] = execbase;
    377  1.32.2.1    mellon 	if (optbuf)
    378  1.32.2.1    mellon 		mangle(optbuf, &argc, &argv, &maxargc);
    379      1.12   mycroft 	argv[argc++] = spec;
    380      1.12   mycroft 	argv[argc++] = name;
    381      1.12   mycroft 	argv[argc] = NULL;
    382      1.12   mycroft 
    383  1.32.2.1    mellon 	if (verbose) {
    384  1.32.2.1    mellon 		(void)printf("exec:");
    385  1.32.2.1    mellon 		for (i = 0; i < argc; i++)
    386      1.12   mycroft 			(void)printf(" %s", argv[i]);
    387      1.12   mycroft 		(void)printf("\n");
    388      1.12   mycroft 	}
    389       1.5   mycroft 
    390      1.12   mycroft 	switch (pid = vfork()) {
    391      1.12   mycroft 	case -1:				/* Error. */
    392      1.12   mycroft 		warn("vfork");
    393  1.32.2.1    mellon 		if (optbuf)
    394  1.32.2.1    mellon 			free(optbuf);
    395      1.12   mycroft 		return (1);
    396  1.32.2.1    mellon 
    397      1.12   mycroft 	case 0:					/* Child. */
    398  1.32.2.1    mellon 		if (debug)
    399  1.32.2.1    mellon 			_exit(0);
    400  1.32.2.1    mellon 
    401      1.12   mycroft 		/* Go find an executable. */
    402      1.12   mycroft 		edir = edirs;
    403      1.12   mycroft 		do {
    404      1.12   mycroft 			(void)snprintf(execname,
    405  1.32.2.1    mellon 			    sizeof(execname), "%s/%s", *edir, execbase);
    406      1.12   mycroft 			execv(execname, (char * const *)argv);
    407      1.12   mycroft 			if (errno != ENOENT)
    408      1.12   mycroft 				warn("exec %s for %s", execname, name);
    409      1.12   mycroft 		} while (*++edir != NULL);
    410       1.1       cgd 
    411      1.12   mycroft 		if (errno == ENOENT)
    412  1.32.2.1    mellon 			warnx("%s not found for %s", execbase, name);
    413  1.32.2.1    mellon 		_exit(1);
    414      1.12   mycroft 		/* NOTREACHED */
    415  1.32.2.1    mellon 
    416      1.12   mycroft 	default:				/* Parent. */
    417  1.32.2.1    mellon 		if (optbuf)
    418  1.32.2.1    mellon 			free(optbuf);
    419       1.1       cgd 
    420      1.12   mycroft 		if (waitpid(pid, &status, 0) < 0) {
    421      1.12   mycroft 			warn("waitpid");
    422      1.12   mycroft 			return (1);
    423       1.1       cgd 		}
    424      1.12   mycroft 
    425      1.12   mycroft 		if (WIFEXITED(status)) {
    426      1.12   mycroft 			if (WEXITSTATUS(status) != 0)
    427      1.12   mycroft 				return (WEXITSTATUS(status));
    428      1.12   mycroft 		} else if (WIFSIGNALED(status)) {
    429      1.17       jtc 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    430      1.12   mycroft 			return (1);
    431       1.1       cgd 		}
    432      1.12   mycroft 
    433      1.12   mycroft 		if (verbose) {
    434      1.12   mycroft 			if (statfs(name, &sf) < 0) {
    435      1.12   mycroft 				warn("statfs %s", name);
    436      1.12   mycroft 				return (1);
    437      1.12   mycroft 			}
    438      1.13   mycroft 			prmount(&sf);
    439       1.1       cgd 		}
    440      1.12   mycroft 		break;
    441       1.1       cgd 	}
    442      1.12   mycroft 
    443      1.12   mycroft 	return (0);
    444       1.1       cgd }
    445       1.1       cgd 
    446      1.12   mycroft void
    447      1.32     lukem prmount(sfp)
    448      1.32     lukem 	struct statfs *sfp;
    449      1.13   mycroft {
    450      1.12   mycroft 	int flags;
    451      1.12   mycroft 	struct opt *o;
    452      1.32     lukem 	struct passwd *pw;
    453      1.12   mycroft 	int f;
    454       1.1       cgd 
    455      1.32     lukem 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname, sfp->f_mntonname,
    456      1.32     lukem 	    MFSNAMELEN, sfp->f_fstypename);
    457       1.1       cgd 
    458      1.32     lukem 	flags = sfp->f_flags & MNT_VISFLAGMASK;
    459      1.12   mycroft 	for (f = 0, o = optnames; flags && o->o_opt; o++)
    460      1.12   mycroft 		if (flags & o->o_opt) {
    461      1.24       cgd 			if (!o->o_silent)
    462      1.24       cgd 				(void)printf("%s%s", !f++ ? " (" : ", ",
    463      1.24       cgd 				    o->o_name);
    464      1.12   mycroft 			flags &= ~o->o_opt;
    465      1.12   mycroft 		}
    466      1.24       cgd 	if (flags)
    467      1.27        pk 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    468      1.24       cgd 		    flags & (flags - 1) ? "s" : "", flags);
    469      1.32     lukem 	if (sfp->f_owner) {
    470      1.32     lukem 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    471      1.32     lukem 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    472      1.32     lukem 			(void)printf("%s", pw->pw_name);
    473      1.32     lukem 		else
    474      1.32     lukem 			(void)printf("%d", sfp->f_owner);
    475      1.32     lukem 	}
    476      1.12   mycroft 	(void)printf(f ? ")\n" : "\n");
    477       1.1       cgd }
    478       1.1       cgd 
    479       1.1       cgd struct statfs *
    480       1.1       cgd getmntpt(name)
    481      1.12   mycroft 	const char *name;
    482       1.1       cgd {
    483       1.1       cgd 	struct statfs *mntbuf;
    484      1.12   mycroft 	int i, mntsize;
    485       1.1       cgd 
    486       1.1       cgd 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    487      1.12   mycroft 	for (i = 0; i < mntsize; i++)
    488      1.12   mycroft 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    489      1.12   mycroft 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    490       1.1       cgd 			return (&mntbuf[i]);
    491      1.12   mycroft 	return (NULL);
    492       1.1       cgd }
    493       1.1       cgd 
    494  1.32.2.1    mellon void
    495  1.32.2.1    mellon catopt(sp, o)
    496  1.32.2.1    mellon 	char **sp;
    497  1.32.2.1    mellon 	const char *o;
    498      1.12   mycroft {
    499  1.32.2.1    mellon 	char *s;
    500  1.32.2.1    mellon 	size_t i, j;
    501      1.12   mycroft 
    502  1.32.2.1    mellon 	s = *sp;
    503  1.32.2.1    mellon 	if (s) {
    504  1.32.2.1    mellon 		i = strlen(s);
    505  1.32.2.1    mellon 		j = i + 1 + strlen(o) + 1;
    506  1.32.2.1    mellon 		s = realloc(s, j);
    507  1.32.2.1    mellon 		(void)snprintf(s + i, j, ",%s", o);
    508      1.12   mycroft 	} else
    509  1.32.2.1    mellon 		s = strdup(o);
    510  1.32.2.1    mellon 	*sp = s;
    511       1.1       cgd }
    512       1.1       cgd 
    513  1.32.2.1    mellon static void
    514  1.32.2.1    mellon mangle(options, argcp, argvp, maxargcp)
    515      1.12   mycroft 	char *options;
    516  1.32.2.1    mellon 	int *argcp, *maxargcp;
    517  1.32.2.1    mellon 	const char ***argvp;
    518       1.1       cgd {
    519      1.12   mycroft 	char *p, *s;
    520  1.32.2.1    mellon 	int argc, maxargc;
    521  1.32.2.1    mellon 	const char **argv;
    522       1.1       cgd 
    523      1.12   mycroft 	argc = *argcp;
    524  1.32.2.1    mellon 	argv = *argvp;
    525  1.32.2.1    mellon 	maxargc = *maxargcp;
    526  1.32.2.1    mellon 
    527  1.32.2.1    mellon 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    528  1.32.2.1    mellon 		/* Always leave space for one more argument and the NULL. */
    529  1.32.2.1    mellon 		if (argc >= maxargc - 4) {
    530  1.32.2.1    mellon 			maxargc <<= 1;
    531  1.32.2.1    mellon 			argv = realloc(argv, maxargc * sizeof(char *));
    532  1.32.2.1    mellon 		}
    533      1.12   mycroft 		if (*p != '\0')
    534      1.12   mycroft 			if (*p == '-') {
    535      1.12   mycroft 				argv[argc++] = p;
    536      1.12   mycroft 				p = strchr(p, '=');
    537      1.12   mycroft 				if (p) {
    538      1.12   mycroft 					*p = '\0';
    539      1.12   mycroft 					argv[argc++] = p+1;
    540       1.1       cgd 				}
    541      1.12   mycroft 			} else if (strcmp(p, "rw") != 0) {
    542      1.12   mycroft 				argv[argc++] = "-o";
    543      1.12   mycroft 				argv[argc++] = p;
    544       1.1       cgd 			}
    545  1.32.2.1    mellon 	}
    546      1.12   mycroft 
    547      1.12   mycroft 	*argcp = argc;
    548  1.32.2.1    mellon 	*argvp = argv;
    549  1.32.2.1    mellon 	*maxargcp = maxargc;
    550       1.1       cgd }
    551       1.1       cgd 
    552      1.12   mycroft void
    553      1.12   mycroft usage()
    554       1.1       cgd {
    555       1.1       cgd 
    556      1.12   mycroft 	(void)fprintf(stderr,
    557      1.12   mycroft 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
    558      1.23       jtc 		"[-dfruvw] [-o options] [-t ffs | external_type]",
    559      1.12   mycroft 			"special node",
    560      1.23       jtc 		"[-adfruvw] [-t ffs | external_type]",
    561      1.12   mycroft 		"[-dfruvw] special | node");
    562      1.12   mycroft 	exit(1);
    563       1.1       cgd }
    564