Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.26
      1 /*	$NetBSD: mount.c,v 1.26 1997/03/10 03:59:54 mikel 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.26 1997/03/10 03:59:54 mikel 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_NOATIME,		0,	"noatime" },
     94 	{ MNT_NOCOREDUMP,	0,	"nocoredump" },
     95 	{ MNT_NODEV,		0,	"nodev" },
     96 	{ MNT_NOEXEC,		0,	"noexec" },
     97 	{ MNT_NOSUID,		0,	"nosuid" },
     98 	{ MNT_QUOTA,		0,	"with quotas" },
     99 	{ MNT_RDONLY,		0,	"read-only" },
    100 	{ MNT_ROOTFS,		1,	"root file system" },
    101 	{ MNT_SYNCHRONOUS,	0,	"synchronous" },
    102 	{ MNT_UNION,		0,	"union" },
    103 	{ NULL }
    104 };
    105 
    106 int
    107 main(argc, argv)
    108 	int argc;
    109 	char * const argv[];
    110 {
    111 	const char *mntonname, *vfstype;
    112 	struct fstab *fs;
    113 	struct statfs *mntbuf;
    114 	FILE *mountdfp;
    115 	pid_t pid;
    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 		if (fscanf(mountdfp, "%ld", &pid) == 1 &&
    248 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    249 			err(1, "signal mountd");
    250 		(void)fclose(mountdfp);
    251 	}
    252 
    253 	exit(rval);
    254 }
    255 
    256 int
    257 hasopt(mntopts, option)
    258 	const char *mntopts, *option;
    259 {
    260 	int negative, found;
    261 	char *opt, *optbuf;
    262 
    263 	if (option[0] == 'n' && option[1] == 'o') {
    264 		negative = 1;
    265 		option += 2;
    266 	} else
    267 		negative = 0;
    268 	optbuf = strdup(mntopts);
    269 	found = 0;
    270 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    271 		if (opt[0] == 'n' && opt[1] == 'o') {
    272 			if (!strcasecmp(opt + 2, option))
    273 				found = negative;
    274 		} else if (!strcasecmp(opt, option))
    275 			found = !negative;
    276 	}
    277 	free(optbuf);
    278 	return (found);
    279 }
    280 
    281 int
    282 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
    283 	const char *vfstype, *spec, *name, *options, *mntopts;
    284 	int flags, skipmounted;
    285 {
    286 	/* List of directories containing mount_xxx subcommands. */
    287 	static const char *edirs[] = {
    288 		_PATH_SBIN,
    289 		_PATH_USRSBIN,
    290 		NULL
    291 	};
    292 	const char *argv[100], **edir;
    293 	struct statfs sf;
    294 	pid_t pid;
    295 	int argc, i, status;
    296 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
    297 
    298 	if (realpath(name, mntpath) == NULL) {
    299 		warn("realpath %s", name);
    300 		return (1);
    301 	}
    302 
    303 	name = mntpath;
    304 
    305 	if (mntopts == NULL)
    306 		mntopts = "";
    307 	if (options == NULL) {
    308 		if (*mntopts == '\0')
    309 			options = "rw";
    310 		else {
    311 			options = mntopts;
    312 			mntopts = "";
    313 		}
    314 	}
    315 	optbuf = catopt(strdup(mntopts), options);
    316 
    317 	if (strcmp(name, "/") == 0)
    318 		flags |= MNT_UPDATE;
    319 	else if (skipmounted) {
    320 		if (statfs(name, &sf) < 0) {
    321 			warn("statfs %s", name);
    322 			return (1);
    323 		}
    324 		/* XXX can't check f_mntfromname, thanks to mfs, union, etc. */
    325 		if (strncmp(name, sf.f_mntonname, MNAMELEN) == 0 &&
    326 		    strncmp(vfstype, sf.f_fstypename, MFSNAMELEN) == 0) {
    327 			if (verbose)
    328 				(void)printf("%s on %s type %.*s: %s\n",
    329 				    sf.f_mntfromname, sf.f_mntonname,
    330 			            MFSNAMELEN, sf.f_fstypename,
    331 				    "already mounted");
    332 			return (0);
    333 		}
    334 	}
    335 	if (flags & MNT_FORCE)
    336 		optbuf = catopt(optbuf, "force");
    337 	if (flags & MNT_RDONLY)
    338 		optbuf = catopt(optbuf, "ro");
    339 	/*
    340 	 * XXX
    341 	 * The mount_mfs (newfs) command uses -o to select the
    342 	 * optimisation mode.  We don't pass the default "-o rw"
    343 	 * for that reason.
    344 	 */
    345 	if (flags & MNT_UPDATE)
    346 		optbuf = catopt(optbuf, "update");
    347 
    348 	argc = 0;
    349 	argv[argc++] = vfstype;
    350 	mangle(optbuf, &argc, argv);
    351 	argv[argc++] = spec;
    352 	argv[argc++] = name;
    353 	argv[argc] = NULL;
    354 
    355 	if (debug) {
    356 		(void)printf("exec: mount_%s", vfstype);
    357 		for (i = 1; i < argc; i++)
    358 			(void)printf(" %s", argv[i]);
    359 		(void)printf("\n");
    360 		return (0);
    361 	}
    362 
    363 	switch (pid = vfork()) {
    364 	case -1:				/* Error. */
    365 		warn("vfork");
    366 		free(optbuf);
    367 		return (1);
    368 	case 0:					/* Child. */
    369 		/* Go find an executable. */
    370 		edir = edirs;
    371 		do {
    372 			(void)snprintf(execname,
    373 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
    374 			execv(execname, (char * const *)argv);
    375 			if (errno != ENOENT)
    376 				warn("exec %s for %s", execname, name);
    377 		} while (*++edir != NULL);
    378 
    379 		if (errno == ENOENT)
    380 			warn("exec %s for %s", execname, name);
    381 		exit(1);
    382 		/* NOTREACHED */
    383 	default:				/* Parent. */
    384 		free(optbuf);
    385 
    386 		if (waitpid(pid, &status, 0) < 0) {
    387 			warn("waitpid");
    388 			return (1);
    389 		}
    390 
    391 		if (WIFEXITED(status)) {
    392 			if (WEXITSTATUS(status) != 0)
    393 				return (WEXITSTATUS(status));
    394 		} else if (WIFSIGNALED(status)) {
    395 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    396 			return (1);
    397 		}
    398 
    399 		if (verbose) {
    400 			if (statfs(name, &sf) < 0) {
    401 				warn("statfs %s", name);
    402 				return (1);
    403 			}
    404 			prmount(&sf);
    405 		}
    406 		break;
    407 	}
    408 
    409 	return (0);
    410 }
    411 
    412 void
    413 prmount(sf)
    414 	struct statfs *sf;
    415 {
    416 	int flags;
    417 	struct opt *o;
    418 	int f;
    419 
    420 	(void)printf("%s on %s type %.*s", sf->f_mntfromname, sf->f_mntonname,
    421 	    MFSNAMELEN, sf->f_fstypename);
    422 
    423 	flags = sf->f_flags & MNT_VISFLAGMASK;
    424 	for (f = 0, o = optnames; flags && o->o_opt; o++)
    425 		if (flags & o->o_opt) {
    426 			if (!o->o_silent)
    427 				(void)printf("%s%s", !f++ ? " (" : ", ",
    428 				    o->o_name);
    429 			flags &= ~o->o_opt;
    430 		}
    431 	if (flags)
    432 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    433 		    flags & (flags - 1) ? "s" : "", flags);
    434 	(void)printf(f ? ")\n" : "\n");
    435 }
    436 
    437 struct statfs *
    438 getmntpt(name)
    439 	const char *name;
    440 {
    441 	struct statfs *mntbuf;
    442 	int i, mntsize;
    443 
    444 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    445 	for (i = 0; i < mntsize; i++)
    446 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    447 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    448 			return (&mntbuf[i]);
    449 	return (NULL);
    450 }
    451 
    452 static enum { IN_LIST, NOT_IN_LIST } which;
    453 
    454 int
    455 selected(type)
    456 	const char *type;
    457 {
    458 	char **av;
    459 
    460 	/* If no type specified, it's always selected. */
    461 	if (typelist == NULL)
    462 		return (1);
    463 	for (av = typelist; *av != NULL; ++av)
    464 		if (!strncmp(type, *av, MFSNAMELEN))
    465 			return (which == IN_LIST ? 1 : 0);
    466 	return (which == IN_LIST ? 0 : 1);
    467 }
    468 
    469 void
    470 maketypelist(fslist)
    471 	char *fslist;
    472 {
    473 	int i;
    474 	char *nextcp, **av;
    475 
    476 	if ((fslist == NULL) || (fslist[0] == '\0'))
    477 		errx(1, "empty type list");
    478 
    479 	/*
    480 	 * XXX
    481 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    482 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    483 	 */
    484 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    485 		fslist += 2;
    486 		which = NOT_IN_LIST;
    487 	} else
    488 		which = IN_LIST;
    489 
    490 	/* Count the number of types. */
    491 	for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
    492 		++nextcp;
    493 
    494 	/* Build an array of that many types. */
    495 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    496 		err(1, NULL);
    497 	av[0] = fslist;
    498 	for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
    499 		*nextcp = '\0';
    500 		av[i] = ++nextcp;
    501 	}
    502 	/* Terminate the array. */
    503 	av[i] = NULL;
    504 }
    505 
    506 char *
    507 catopt(s0, s1)
    508 	char *s0;
    509 	const char *s1;
    510 {
    511 	size_t i;
    512 	char *cp;
    513 
    514 	if (s0 && *s0) {
    515 		i = strlen(s0) + strlen(s1) + 1 + 1;
    516 		if ((cp = malloc(i)) == NULL)
    517 			err(1, NULL);
    518 		(void)snprintf(cp, i, "%s,%s", s0, s1);
    519 	} else
    520 		cp = strdup(s1);
    521 
    522 	if (s0)
    523 		free(s0);
    524 	return (cp);
    525 }
    526 
    527 void
    528 mangle(options, argcp, argv)
    529 	char *options;
    530 	int *argcp;
    531 	const char **argv;
    532 {
    533 	char *p, *s;
    534 	int argc;
    535 
    536 	argc = *argcp;
    537 	for (s = options; (p = strsep(&s, ",")) != NULL;)
    538 		if (*p != '\0')
    539 			if (*p == '-') {
    540 				argv[argc++] = p;
    541 				p = strchr(p, '=');
    542 				if (p) {
    543 					*p = '\0';
    544 					argv[argc++] = p+1;
    545 				}
    546 			} else if (strcmp(p, "rw") != 0) {
    547 				argv[argc++] = "-o";
    548 				argv[argc++] = p;
    549 			}
    550 
    551 	*argcp = argc;
    552 }
    553 
    554 void
    555 usage()
    556 {
    557 
    558 	(void)fprintf(stderr,
    559 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
    560 		"[-dfruvw] [-o options] [-t ffs | external_type]",
    561 			"special node",
    562 		"[-adfruvw] [-t ffs | external_type]",
    563 		"[-dfruvw] special | node");
    564 	exit(1);
    565 }
    566