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