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