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