Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.60
      1 /*	$NetBSD: mount.c,v 1.60 2002/08/23 03:17:19 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.60 2002/08/23 03:17:19 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 #define MOUNTNAMES
     65 #include <fcntl.h>
     66 #include <sys/disklabel.h>
     67 #include <sys/ioctl.h>
     68 
     69 #include "pathnames.h"
     70 #include "vfslist.h"
     71 
     72 static int	debug, verbose;
     73 
     74 static void	catopt __P((char **, const char *));
     75 static const char *
     76 		getfslab __P((const char *str));
     77 static struct statfs *
     78 		getmntpt __P((const char *));
     79 static int	hasopt __P((const char *, const char *));
     80 static void	mangle __P((char *, int *, const char ***, int *));
     81 static int	mountfs __P((const char *, const char *, const char *,
     82 		    int, const char *, const char *, int));
     83 static void	prmount __P((struct statfs *));
     84 static void	usage __P((void));
     85 
     86 #ifndef NO_MOUNT_PROGS
     87 void	checkname __P((int, char *[]));
     88 #endif
     89 int	main __P((int, char *[]));
     90 
     91 /* Map from mount otions to printable formats. */
     92 static const struct opt {
     93 	int o_opt;
     94 	int o_silent;
     95 	const char *o_name;
     96 } optnames[] = {
     97 	__MNT_FLAGS
     98 };
     99 
    100 static char ffs_fstype[] = "ffs";
    101 
    102 int
    103 main(argc, argv)
    104 	int argc;
    105 	char *argv[];
    106 {
    107 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
    108 	struct fstab *fs;
    109 	struct statfs *mntbuf;
    110 	FILE *mountdfp;
    111 	int all, ch, forceall, i, init_flags, mntsize, rval;
    112 	char *options;
    113 	const char *mountopts, *fstypename;
    114 
    115 #ifndef NO_MOUNT_PROGS
    116 	/* if called as specific mount, call it's main mount routine */
    117 	checkname(argc, argv);
    118 #endif
    119 
    120 	/* started as "mount" */
    121 	all = forceall = init_flags = 0;
    122 	options = NULL;
    123 	vfslist = NULL;
    124 	vfstype = ffs_fstype;
    125 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
    126 		switch (ch) {
    127 		case 'A':
    128 			all = forceall = 1;
    129 			break;
    130 		case 'a':
    131 			all = 1;
    132 			break;
    133 		case 'd':
    134 			debug = 1;
    135 			break;
    136 		case 'f':
    137 			init_flags |= MNT_FORCE;
    138 			break;
    139 		case 'o':
    140 			if (*optarg)
    141 				catopt(&options, optarg);
    142 			break;
    143 		case 'r':
    144 			init_flags |= MNT_RDONLY;
    145 			break;
    146 		case 't':
    147 			if (vfslist != NULL)
    148 				errx(1,
    149 				    "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 		/* NOTREACHED */
    202 	case 1:
    203 		if (vfslist != NULL) {
    204 			usage();
    205 			/* NOTREACHED */
    206 		}
    207 
    208 		if (init_flags & MNT_UPDATE) {
    209 			if ((mntbuf = getmntpt(*argv)) == NULL)
    210 				errx(1,
    211 				    "unknown special file or file system %s.",
    212 				    *argv);
    213 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    214 				mntfromname = fs->fs_spec;
    215 				/* ignore the fstab file options.  */
    216 				fs->fs_mntops = NULL;
    217 			} else
    218 				mntfromname = mntbuf->f_mntfromname;
    219 			mntonname  = mntbuf->f_mntonname;
    220 			fstypename = mntbuf->f_fstypename;
    221 			mountopts  = NULL;
    222 		} else {
    223 			if ((fs = getfsfile(*argv)) == NULL &&
    224 			    (fs = getfsspec(*argv)) == NULL)
    225 				errx(1,
    226 				    "%s: unknown special file or file system.",
    227 				    *argv);
    228 			if (BADTYPE(fs->fs_type))
    229 				errx(1, "%s has unknown file system type.",
    230 				    *argv);
    231 			mntfromname = fs->fs_spec;
    232 			mntonname   = fs->fs_file;
    233 			fstypename  = fs->fs_vfstype;
    234 			mountopts   = fs->fs_mntops;
    235 		}
    236 		rval = mountfs(fstypename, mntfromname,
    237 		    mntonname, init_flags, options, mountopts, 0);
    238 		break;
    239 	case 2:
    240 		/*
    241 		 * If -t flag has not been specified, and spec contains either
    242 		 * a ':' or a '@' then assume that an NFS filesystem is being
    243 		 * specified ala Sun.
    244 		 */
    245 		if (vfslist == NULL) {
    246 			if (strpbrk(argv[0], ":@") != NULL)
    247 				vfstype = "nfs";
    248 			else {
    249 				vfstype = getfslab(argv[0]);
    250 				if (vfstype == NULL)
    251 					vfstype = ffs_fstype;
    252 			}
    253 		}
    254 		rval = mountfs(vfstype,
    255 		    argv[0], argv[1], init_flags, options, NULL, 0);
    256 		break;
    257 	default:
    258 		usage();
    259 		/* NOTREACHED */
    260 	}
    261 
    262 	/*
    263 	 * If the mount was successfully, and done by root, tell mountd the
    264 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    265 	 */
    266 	if (rval == 0 && getuid() == 0 &&
    267 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    268 		int pid;
    269 
    270 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    271 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    272 			err(1, "signal mountd");
    273 		(void)fclose(mountdfp);
    274 	}
    275 
    276 	exit(rval);
    277 	/* NOTREACHED */
    278 }
    279 
    280 int
    281 hasopt(mntopts, option)
    282 	const char *mntopts, *option;
    283 {
    284 	int negative, found;
    285 	char *opt, *optbuf;
    286 
    287 	if (option[0] == 'n' && option[1] == 'o') {
    288 		negative = 1;
    289 		option += 2;
    290 	} else
    291 		negative = 0;
    292 	optbuf = strdup(mntopts);
    293 	found = 0;
    294 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    295 		if (opt[0] == 'n' && opt[1] == 'o') {
    296 			if (!strcasecmp(opt + 2, option))
    297 				found = negative;
    298 		} else if (!strcasecmp(opt, option))
    299 			found = !negative;
    300 	}
    301 	free(optbuf);
    302 	return (found);
    303 }
    304 
    305 static int
    306 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
    307 	const char *vfstype, *spec, *name, *options, *mntopts;
    308 	int flags, skipmounted;
    309 {
    310 	/* List of directories containing mount_xxx subcommands. */
    311 	static const char *edirs[] = {
    312 #ifdef _PATH_RESCUE
    313 		_PATH_RESCUE,
    314 #endif
    315 		_PATH_SBIN,
    316 		_PATH_USRSBIN,
    317 		NULL
    318 	};
    319 	const char **argv, **edir;
    320 	struct statfs *sfp, sf;
    321 	pid_t pid;
    322 	int argc, numfs, i, status, maxargc;
    323 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
    324 	    mntpath[MAXPATHLEN];
    325 
    326 #ifdef __GNUC__
    327 	(void) &name;
    328 	(void) &optbuf;
    329 	(void) &vfstype;
    330 #endif
    331 
    332 	if (realpath(name, mntpath) == NULL) {
    333 		warn("realpath %s", name);
    334 		return (1);
    335 	}
    336 
    337 	name = mntpath;
    338 
    339 	optbuf = NULL;
    340 	if (mntopts)
    341 		catopt(&optbuf, mntopts);
    342 	if (options)
    343 		catopt(&optbuf, options);
    344 	if (!mntopts && !options)
    345 		catopt(&optbuf, "rw");
    346 
    347 	if (!strcmp(name, "/"))
    348 		flags |= MNT_UPDATE;
    349 	else if (skipmounted) {
    350 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
    351 			warn("getmntinfo");
    352 			return (1);
    353 		}
    354 		for(i = 0; i < numfs; i++) {
    355 			/*
    356 			 * XXX can't check f_mntfromname,
    357 			 * thanks to mfs, union, etc.
    358 			 */
    359 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
    360 			    strncmp(vfstype, sfp[i].f_fstypename,
    361 				MFSNAMELEN) == 0) {
    362 				if (verbose)
    363 					(void)printf("%s on %s type %.*s: "
    364 					    "%s\n",
    365 					    sfp[i].f_mntfromname,
    366 					    sfp[i].f_mntonname,
    367 					    MFSNAMELEN,
    368 					    sfp[i].f_fstypename,
    369 					    "already mounted");
    370 				return (0);
    371 			}
    372 		}
    373 	}
    374 	if (flags & MNT_FORCE)
    375 		catopt(&optbuf, "force");
    376 	if (flags & MNT_RDONLY)
    377 		catopt(&optbuf, "ro");
    378 
    379 	if (flags & MNT_UPDATE) {
    380 		catopt(&optbuf, "update");
    381 		/* Figure out the fstype only if we defaulted to ffs */
    382 		if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
    383 			vfstype = sf.f_fstypename;
    384 	}
    385 
    386 	maxargc = 64;
    387 	argv = malloc(sizeof(char *) * maxargc);
    388 
    389 	(void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    390 	argc = 0;
    391 	argv[argc++] = execbase;
    392 	if (optbuf)
    393 		mangle(optbuf, &argc, &argv, &maxargc);
    394 	argv[argc++] = spec;
    395 	argv[argc++] = name;
    396 	argv[argc] = NULL;
    397 
    398 	if (verbose) {
    399 		(void)printf("exec:");
    400 		for (i = 0; i < argc; i++)
    401 			(void)printf(" %s", argv[i]);
    402 		(void)printf("\n");
    403 	}
    404 
    405 	switch (pid = vfork()) {
    406 	case -1:				/* Error. */
    407 		warn("vfork");
    408 		if (optbuf)
    409 			free(optbuf);
    410 		return (1);
    411 
    412 	case 0:					/* Child. */
    413 		if (debug)
    414 			_exit(0);
    415 
    416 		/* Go find an executable. */
    417 		edir = edirs;
    418 		do {
    419 			(void)snprintf(execname,
    420 			    sizeof(execname), "%s/%s", *edir, execbase);
    421 			(void)execv(execname, (char * const *)argv);
    422 			if (errno != ENOENT)
    423 				warn("exec %s for %s", execname, name);
    424 		} while (*++edir != NULL);
    425 
    426 		if (errno == ENOENT)
    427 			warnx("%s not found for %s", execbase, name);
    428 		_exit(1);
    429 		/* NOTREACHED */
    430 
    431 	default:				/* Parent. */
    432 		if (optbuf)
    433 			free(optbuf);
    434 
    435 		if (waitpid(pid, &status, 0) < 0) {
    436 			warn("waitpid");
    437 			return (1);
    438 		}
    439 
    440 		if (WIFEXITED(status)) {
    441 			if (WEXITSTATUS(status) != 0)
    442 				return (WEXITSTATUS(status));
    443 		} else if (WIFSIGNALED(status)) {
    444 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    445 			return (1);
    446 		}
    447 
    448 		if (verbose) {
    449 			if (statfs(name, &sf) < 0) {
    450 				warn("statfs %s", name);
    451 				return (1);
    452 			}
    453 			prmount(&sf);
    454 		}
    455 		break;
    456 	}
    457 
    458 	return (0);
    459 }
    460 
    461 static void
    462 prmount(sfp)
    463 	struct statfs *sfp;
    464 {
    465 	int flags;
    466 	const struct opt *o;
    467 	struct passwd *pw;
    468 	int f;
    469 
    470 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    471 	    sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
    472 
    473 	flags = sfp->f_flags & MNT_VISFLAGMASK;
    474 	for (f = 0, o = optnames; flags && o <
    475 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
    476 		if (flags & o->o_opt) {
    477 			if (!o->o_silent || verbose)
    478 				(void)printf("%s%s", !f++ ? " (" : ", ",
    479 				    o->o_name);
    480 			flags &= ~o->o_opt;
    481 		}
    482 	if (flags)
    483 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    484 		    flags & (flags - 1) ? "s" : "", flags);
    485 	if (sfp->f_owner) {
    486 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    487 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    488 			(void)printf("%s", pw->pw_name);
    489 		else
    490 			(void)printf("%d", sfp->f_owner);
    491 	}
    492 	if (verbose)
    493 		(void)printf("%swrites: sync %ld async %ld)\n",
    494 		    !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
    495 	else
    496 		(void)printf("%s", f ? ")\n" : "\n");
    497 }
    498 
    499 static struct statfs *
    500 getmntpt(name)
    501 	const char *name;
    502 {
    503 	struct statfs *mntbuf;
    504 	int i, mntsize;
    505 
    506 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    507 	for (i = 0; i < mntsize; i++)
    508 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    509 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    510 			return (&mntbuf[i]);
    511 	return (NULL);
    512 }
    513 
    514 static void
    515 catopt(sp, o)
    516 	char **sp;
    517 	const char *o;
    518 {
    519 	char *s;
    520 	size_t i, j;
    521 
    522 	s = *sp;
    523 	if (s) {
    524 		i = strlen(s);
    525 		j = i + 1 + strlen(o) + 1;
    526 		s = realloc(s, j);
    527 		(void)snprintf(s + i, j, ",%s", o);
    528 	} else
    529 		s = strdup(o);
    530 	*sp = s;
    531 }
    532 
    533 static void
    534 mangle(options, argcp, argvp, maxargcp)
    535 	char *options;
    536 	int *argcp, *maxargcp;
    537 	const char ***argvp;
    538 {
    539 	char *p, *s;
    540 	int argc, maxargc;
    541 	const char **argv;
    542 
    543 	argc = *argcp;
    544 	argv = *argvp;
    545 	maxargc = *maxargcp;
    546 
    547 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    548 		/* Always leave space for one more argument and the NULL. */
    549 		if (argc >= maxargc - 4) {
    550 			maxargc <<= 1;
    551 			argv = realloc(argv, maxargc * sizeof(char *));
    552 		}
    553 		if (*p != '\0') {
    554 			if (*p == '-') {
    555 				argv[argc++] = p;
    556 				p = strchr(p, '=');
    557 				if (p) {
    558 					*p = '\0';
    559 					argv[argc++] = p+1;
    560 				}
    561 			} else if (strcmp(p, "rw") != 0) {
    562 				argv[argc++] = "-o";
    563 				argv[argc++] = p;
    564 			}
    565 		}
    566 	}
    567 
    568 	*argcp = argc;
    569 	*argvp = argv;
    570 	*maxargcp = maxargc;
    571 }
    572 
    573 /* Deduce the filesystem type from the disk label. */
    574 static const char *
    575 getfslab(str)
    576 	const char *str;
    577 {
    578 	struct disklabel dl;
    579 	int fd;
    580 	int part;
    581 	const char *vfstype;
    582 	u_char fstype;
    583 	char buf[MAXPATHLEN + 1];
    584 	char *sp, *ep;
    585 
    586 	if ((fd = open(str, O_RDONLY)) == -1) {
    587 		/*
    588 		 * Iff we get EBUSY try the raw device. Since mount always uses
    589 		 * the block device we know we are never passed a raw device.
    590 		 */
    591 		if (errno != EBUSY)
    592 			err(1, "cannot open `%s'", str);
    593 		strlcpy(buf, str, MAXPATHLEN);
    594 		if ((sp = strrchr(buf, '/')) != NULL)
    595 			++sp;
    596 		else
    597 			sp = buf;
    598 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    599 			*ep = *(ep - 1);
    600 		*sp = 'r';
    601 
    602 		/* Silently fail here - mount call can display error */
    603 		if ((fd = open(buf, O_RDONLY)) == -1)
    604 			return (NULL);
    605 	}
    606 
    607 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    608 		(void) close(fd);
    609 		return (NULL);
    610 	}
    611 
    612 	(void) close(fd);
    613 
    614 	part = str[strlen(str) - 1] - 'a';
    615 
    616 	if (part < 0 || part >= dl.d_npartitions)
    617 		return (NULL);
    618 
    619 	/* Return NULL for unknown types - caller can fall back to ffs */
    620 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    621 		vfstype = NULL;
    622 	else
    623 		vfstype = mountnames[fstype];
    624 
    625 	return (vfstype);
    626 }
    627 
    628 static void
    629 usage()
    630 {
    631 
    632 	(void)fprintf(stderr,
    633 	    "usage: mount %s\n       mount %s\n       mount %s\n",
    634 	    "[-Aadfruvw] [-t type]",
    635 	    "[-dfruvw] special | node",
    636 	    "[-dfruvw] [-o options] [-t type] special node");
    637 	exit(1);
    638 	/* NOTREACHED */
    639 }
    640