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