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