Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.31
      1 /*	$NetBSD: mount.c,v 1.31 1997/09/15 04:09:07 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1989, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)mount.c	8.19 (Berkeley) 4/19/94";
     45 #else
     46 __RCSID("$NetBSD: mount.c,v 1.31 1997/09/15 04:09:07 lukem Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/mount.h>
     52 #include <sys/wait.h>
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fstab.h>
     57 #include <signal.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 
     63 #include "pathnames.h"
     64 
     65 int	debug, verbose;
     66 char	**typelist = NULL;
     67 
     68 int	selected __P((const char *));
     69 char   *catopt __P((char *, const char *));
     70 struct statfs
     71        *getmntpt __P((const char *));
     72 int	hasopt __P((const char *, const char *));
     73 void	maketypelist __P((char *));
     74 void	mangle __P((char *, int *, const char **));
     75 int	mountfs __P((const char *, const char *, const char *,
     76 			int, const char *, const char *, int));
     77 void	prmount __P((struct statfs *));
     78 void	usage __P((void));
     79 int	main __P((int, char *[]));
     80 
     81 /* Map from mount otions to printable formats. */
     82 static struct opt {
     83 	int o_opt;
     84 	int o_silent;
     85 	const char *o_name;
     86 } optnames[] = {
     87 	{ MNT_ASYNC,		0,	"asynchronous" },
     88 	{ MNT_DEFEXPORTED,	1,	"exported to the world" },
     89 	{ MNT_EXKERB,		1,	"kerberos uid mapping" },
     90 	{ MNT_EXPORTED,		0,	"NFS exported" },
     91 	{ MNT_EXPORTANON,	1,	"anon uid mapping" },
     92 	{ MNT_EXRDONLY,		1,	"exported read-only" },
     93 	{ MNT_LOCAL,		0,	"local" },
     94 	{ MNT_NOATIME,		0,	"noatime" },
     95 	{ MNT_NOCOREDUMP,	0,	"nocoredump" },
     96 	{ MNT_NODEV,		0,	"nodev" },
     97 	{ MNT_NOEXEC,		0,	"noexec" },
     98 	{ MNT_NOSUID,		0,	"nosuid" },
     99 	{ MNT_QUOTA,		0,	"with quotas" },
    100 	{ MNT_RDONLY,		0,	"read-only" },
    101 	{ MNT_ROOTFS,		1,	"root file system" },
    102 	{ MNT_SYNCHRONOUS,	0,	"synchronous" },
    103 	{ MNT_UNION,		0,	"union" },
    104 	{ 0 }
    105 };
    106 
    107 static char ffs[] = "ffs";
    108 
    109 int
    110 main(argc, argv)
    111 	int argc;
    112 	char *argv[];
    113 {
    114 	const char *mntonname, *vfstype;
    115 	struct fstab *fs;
    116 	struct statfs *mntbuf;
    117 	FILE *mountdfp;
    118 	int all, ch, forceall, i, init_flags, mntsize, rval;
    119 	char *options;
    120 
    121 	all = forceall = init_flags = 0;
    122 	options = NULL;
    123 	vfstype = ffs;
    124 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
    125 		switch (ch) {
    126 		case 'A':
    127 			all = forceall = 1;
    128 			break;
    129 		case 'a':
    130 			all = 1;
    131 			break;
    132 		case 'd':
    133 			debug = 1;
    134 			break;
    135 		case 'f':
    136 			init_flags |= MNT_FORCE;
    137 			break;
    138 		case 'o':
    139 			if (*optarg)
    140 				options = catopt(options, optarg);
    141 			break;
    142 		case 'r':
    143 			init_flags |= MNT_RDONLY;
    144 			break;
    145 		case 't':
    146 			if (typelist != NULL)
    147 				errx(1, "only one -t option may be specified.");
    148 			maketypelist(optarg);
    149 			vfstype = optarg;
    150 			break;
    151 		case 'u':
    152 			init_flags |= MNT_UPDATE;
    153 			break;
    154 		case 'v':
    155 			verbose = 1;
    156 			break;
    157 		case 'w':
    158 			init_flags &= ~MNT_RDONLY;
    159 			break;
    160 		case '?':
    161 		default:
    162 			usage();
    163 			/* NOTREACHED */
    164 		}
    165 	argc -= optind;
    166 	argv += optind;
    167 
    168 #define	BADTYPE(type)							\
    169 	(strcmp(type, FSTAB_RO) &&					\
    170 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    171 
    172 	rval = 0;
    173 	switch (argc) {
    174 	case 0:
    175 		if (all)
    176 			while ((fs = getfsent()) != NULL) {
    177 				if (BADTYPE(fs->fs_type))
    178 					continue;
    179 				if (!selected(fs->fs_vfstype))
    180 					continue;
    181 				if (hasopt(fs->fs_mntops, "noauto"))
    182 					continue;
    183 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
    184 				    fs->fs_file, init_flags, options,
    185 				    fs->fs_mntops, !forceall))
    186 					rval = 1;
    187 			}
    188 		else {
    189 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    190 				err(1, "getmntinfo");
    191 			for (i = 0; i < mntsize; i++) {
    192 				if (!selected(mntbuf[i].f_fstypename))
    193 					continue;
    194 				prmount(&mntbuf[i]);
    195 			}
    196 		}
    197 		exit(rval);
    198 	case 1:
    199 		if (typelist != NULL)
    200 			usage();
    201 
    202 		if (init_flags & MNT_UPDATE) {
    203 			if ((mntbuf = getmntpt(*argv)) == NULL)
    204 				errx(1,
    205 				    "unknown special file or file system %s.",
    206 				    *argv);
    207 			if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
    208 				errx(1, "can't find fstab entry for %s.",
    209 				    *argv);
    210 			/* If it's an update, ignore the fstab file options. */
    211 			fs->fs_mntops = NULL;
    212 			mntonname = mntbuf->f_mntonname;
    213 		} else {
    214 			if ((fs = getfsfile(*argv)) == NULL &&
    215 			    (fs = getfsspec(*argv)) == NULL)
    216 				errx(1,
    217 				    "%s: unknown special file or file system.",
    218 				    *argv);
    219 			if (BADTYPE(fs->fs_type))
    220 				errx(1, "%s has unknown file system type.",
    221 				    *argv);
    222 			mntonname = fs->fs_file;
    223 		}
    224 		rval = mountfs(fs->fs_vfstype, fs->fs_spec,
    225 		    mntonname, init_flags, options, fs->fs_mntops, 0);
    226 		break;
    227 	case 2:
    228 		/*
    229 		 * If -t flag has not been specified, and spec contains either
    230 		 * a ':' or a '@' then assume that an NFS filesystem is being
    231 		 * specified ala Sun.
    232 		 */
    233 		if (typelist == NULL && strpbrk(argv[0], ":@") != NULL)
    234 			vfstype = "nfs";
    235 		rval = mountfs(vfstype,
    236 		    argv[0], argv[1], init_flags, options, NULL, 0);
    237 		break;
    238 	default:
    239 		usage();
    240 		/* NOTREACHED */
    241 	}
    242 
    243 	/*
    244 	 * If the mount was successfully, and done by root, tell mountd the
    245 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    246 	 */
    247 	if (rval == 0 && getuid() == 0 &&
    248 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    249 		int pid;
    250 
    251 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    252 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    253 			err(1, "signal mountd");
    254 		(void)fclose(mountdfp);
    255 	}
    256 
    257 	exit(rval);
    258 }
    259 
    260 int
    261 hasopt(mntopts, option)
    262 	const char *mntopts, *option;
    263 {
    264 	int negative, found;
    265 	char *opt, *optbuf;
    266 
    267 	if (option[0] == 'n' && option[1] == 'o') {
    268 		negative = 1;
    269 		option += 2;
    270 	} else
    271 		negative = 0;
    272 	optbuf = strdup(mntopts);
    273 	found = 0;
    274 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    275 		if (opt[0] == 'n' && opt[1] == 'o') {
    276 			if (!strcasecmp(opt + 2, option))
    277 				found = negative;
    278 		} else if (!strcasecmp(opt, option))
    279 			found = !negative;
    280 	}
    281 	free(optbuf);
    282 	return (found);
    283 }
    284 
    285 int
    286 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
    287 	const char *vfstype, *spec, *name, *options, *mntopts;
    288 	int flags, skipmounted;
    289 {
    290 	/* List of directories containing mount_xxx subcommands. */
    291 	static const char *edirs[] = {
    292 		_PATH_SBIN,
    293 		_PATH_USRSBIN,
    294 		NULL
    295 	};
    296 	const char *argv[100], **edir;
    297 	struct statfs sf;
    298 	pid_t pid;
    299 	int argc, i, status;
    300 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
    301 #ifdef __GNUC__
    302 	(void) &name;
    303 	(void) &optbuf;
    304 	(void) &vfstype;
    305 #endif
    306 
    307 	if (realpath(name, mntpath) == NULL) {
    308 		warn("realpath %s", name);
    309 		return (1);
    310 	}
    311 
    312 	name = mntpath;
    313 
    314 	if (mntopts == NULL)
    315 		mntopts = "";
    316 	if (options == NULL) {
    317 		if (*mntopts == '\0')
    318 			options = "rw";
    319 		else {
    320 			options = mntopts;
    321 			mntopts = "";
    322 		}
    323 	}
    324 	optbuf = catopt(strdup(mntopts), options);
    325 
    326 	if (strcmp(name, "/") == 0)
    327 		flags |= MNT_UPDATE;
    328 	else if (skipmounted) {
    329 		if (statfs(name, &sf) < 0) {
    330 			warn("statfs %s", name);
    331 			return (1);
    332 		}
    333 		/* XXX can't check f_mntfromname, thanks to mfs, union, etc. */
    334 		if (strncmp(name, sf.f_mntonname, MNAMELEN) == 0 &&
    335 		    strncmp(vfstype, sf.f_fstypename, MFSNAMELEN) == 0) {
    336 			if (verbose)
    337 				(void)printf("%s on %s type %.*s: %s\n",
    338 				    sf.f_mntfromname, sf.f_mntonname,
    339 			            MFSNAMELEN, sf.f_fstypename,
    340 				    "already mounted");
    341 			return (0);
    342 		}
    343 	}
    344 	if (flags & MNT_FORCE)
    345 		optbuf = catopt(optbuf, "force");
    346 	if (flags & MNT_RDONLY)
    347 		optbuf = catopt(optbuf, "ro");
    348 	/*
    349 	 * XXX
    350 	 * The mount_mfs (newfs) command uses -o to select the
    351 	 * optimisation mode.  We don't pass the default "-o rw"
    352 	 * for that reason.
    353 	 */
    354 	if (flags & MNT_UPDATE) {
    355 		optbuf = catopt(optbuf, "update");
    356 		/* Figure out the fstype only if we defaulted to ffs */
    357 		if (vfstype == ffs && statfs(name, &sf) != -1)
    358 			vfstype = sf.f_fstypename;
    359 	}
    360 
    361 	argc = 0;
    362 	argv[argc++] = vfstype;
    363 	mangle(optbuf, &argc, argv);
    364 	argv[argc++] = spec;
    365 	argv[argc++] = name;
    366 	argv[argc] = NULL;
    367 
    368 	if (debug) {
    369 		(void)printf("exec: mount_%s", vfstype);
    370 		for (i = 1; i < argc; i++)
    371 			(void)printf(" %s", argv[i]);
    372 		(void)printf("\n");
    373 		return (0);
    374 	}
    375 
    376 	switch (pid = vfork()) {
    377 	case -1:				/* Error. */
    378 		warn("vfork");
    379 		free(optbuf);
    380 		return (1);
    381 	case 0:					/* Child. */
    382 		/* Go find an executable. */
    383 		edir = edirs;
    384 		do {
    385 			(void)snprintf(execname,
    386 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
    387 			execv(execname, (char * const *)argv);
    388 			if (errno != ENOENT)
    389 				warn("exec %s for %s", execname, name);
    390 		} while (*++edir != NULL);
    391 
    392 		if (errno == ENOENT)
    393 			warnx("mount_%s not found for %s", vfstype, name);
    394 		exit(1);
    395 		/* NOTREACHED */
    396 	default:				/* Parent. */
    397 		free(optbuf);
    398 
    399 		if (waitpid(pid, &status, 0) < 0) {
    400 			warn("waitpid");
    401 			return (1);
    402 		}
    403 
    404 		if (WIFEXITED(status)) {
    405 			if (WEXITSTATUS(status) != 0)
    406 				return (WEXITSTATUS(status));
    407 		} else if (WIFSIGNALED(status)) {
    408 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    409 			return (1);
    410 		}
    411 
    412 		if (verbose) {
    413 			if (statfs(name, &sf) < 0) {
    414 				warn("statfs %s", name);
    415 				return (1);
    416 			}
    417 			prmount(&sf);
    418 		}
    419 		break;
    420 	}
    421 
    422 	return (0);
    423 }
    424 
    425 void
    426 prmount(sf)
    427 	struct statfs *sf;
    428 {
    429 	int flags;
    430 	struct opt *o;
    431 	int f;
    432 
    433 	(void)printf("%s on %s type %.*s", sf->f_mntfromname, sf->f_mntonname,
    434 	    MFSNAMELEN, sf->f_fstypename);
    435 
    436 	flags = sf->f_flags & MNT_VISFLAGMASK;
    437 	for (f = 0, o = optnames; flags && o->o_opt; o++)
    438 		if (flags & o->o_opt) {
    439 			if (!o->o_silent)
    440 				(void)printf("%s%s", !f++ ? " (" : ", ",
    441 				    o->o_name);
    442 			flags &= ~o->o_opt;
    443 		}
    444 	if (flags)
    445 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    446 		    flags & (flags - 1) ? "s" : "", flags);
    447 	(void)printf(f ? ")\n" : "\n");
    448 }
    449 
    450 struct statfs *
    451 getmntpt(name)
    452 	const char *name;
    453 {
    454 	struct statfs *mntbuf;
    455 	int i, mntsize;
    456 
    457 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    458 	for (i = 0; i < mntsize; i++)
    459 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    460 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    461 			return (&mntbuf[i]);
    462 	return (NULL);
    463 }
    464 
    465 static enum { IN_LIST, NOT_IN_LIST } which;
    466 
    467 int
    468 selected(type)
    469 	const char *type;
    470 {
    471 	char **av;
    472 
    473 	/* If no type specified, it's always selected. */
    474 	if (typelist == NULL)
    475 		return (1);
    476 	for (av = typelist; *av != NULL; ++av)
    477 		if (!strncmp(type, *av, MFSNAMELEN))
    478 			return (which == IN_LIST ? 1 : 0);
    479 	return (which == IN_LIST ? 0 : 1);
    480 }
    481 
    482 void
    483 maketypelist(fslist)
    484 	char *fslist;
    485 {
    486 	int i;
    487 	char *nextcp, **av;
    488 
    489 	if ((fslist == NULL) || (fslist[0] == '\0'))
    490 		errx(1, "empty type list");
    491 
    492 	/*
    493 	 * XXX
    494 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    495 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    496 	 */
    497 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    498 		fslist += 2;
    499 		which = NOT_IN_LIST;
    500 	} else
    501 		which = IN_LIST;
    502 
    503 	/* Count the number of types. */
    504 	for (i = 1, nextcp = fslist; (nextcp = strchr(nextcp, ',')) != NULL; i++)
    505 		++nextcp;
    506 
    507 	/* Build an array of that many types. */
    508 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    509 		err(1, "%s", "");
    510 	av[0] = fslist;
    511 	for (i = 1, nextcp = fslist; (nextcp = strchr(nextcp, ',')) != NULL; i++) {
    512 		*nextcp = '\0';
    513 		av[i] = ++nextcp;
    514 	}
    515 	/* Terminate the array. */
    516 	av[i] = NULL;
    517 }
    518 
    519 char *
    520 catopt(s0, s1)
    521 	char *s0;
    522 	const char *s1;
    523 {
    524 	size_t i;
    525 	char *cp;
    526 
    527 	if (s0 && *s0) {
    528 		i = strlen(s0) + strlen(s1) + 1 + 1;
    529 		if ((cp = malloc(i)) == NULL)
    530 			err(1, "%s", "");
    531 		(void)snprintf(cp, i, "%s,%s", s0, s1);
    532 	} else
    533 		cp = strdup(s1);
    534 
    535 	if (s0)
    536 		free(s0);
    537 	return (cp);
    538 }
    539 
    540 void
    541 mangle(options, argcp, argv)
    542 	char *options;
    543 	int *argcp;
    544 	const char **argv;
    545 {
    546 	char *p, *s;
    547 	int argc;
    548 
    549 	argc = *argcp;
    550 	for (s = options; (p = strsep(&s, ",")) != NULL;)
    551 		if (*p != '\0')
    552 			if (*p == '-') {
    553 				argv[argc++] = p;
    554 				p = strchr(p, '=');
    555 				if (p) {
    556 					*p = '\0';
    557 					argv[argc++] = p+1;
    558 				}
    559 			} else if (strcmp(p, "rw") != 0) {
    560 				argv[argc++] = "-o";
    561 				argv[argc++] = p;
    562 			}
    563 
    564 	*argcp = argc;
    565 }
    566 
    567 void
    568 usage()
    569 {
    570 
    571 	(void)fprintf(stderr,
    572 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
    573 		"[-dfruvw] [-o options] [-t ffs | external_type]",
    574 			"special node",
    575 		"[-adfruvw] [-t ffs | external_type]",
    576 		"[-dfruvw] special | node");
    577 	exit(1);
    578 }
    579