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