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