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