Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.12
      1 /*
      2  * Copyright (c) 1980, 1989, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char copyright[] =
     36 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)mount.c	8.19 (Berkeley) 4/19/94";*/
     42 static char *rcsid = "$Id: mount.c,v 1.12 1994/06/08 19:02:43 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/mount.h>
     47 #include <sys/wait.h>
     48 
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <fstab.h>
     52 #include <signal.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 #include "pathnames.h"
     59 
     60 int debug, verbose, skipvfs;
     61 
     62 int	badvfsname __P((const char *, const char **));
     63 char   *catopt __P((char *, const char *));
     64 struct statfs
     65        *getmntpt __P((const char *));
     66 int	hasopt __P((const char *, const char *));
     67 const char
     68       **makevfslist __P((char *));
     69 void	mangle __P((char *, int *, const char **));
     70 int	mountfs __P((const char *, const char *, const char *,
     71 			int, const char *, const char *));
     72 void	prmount __P((const char *, const char *, int));
     73 void	usage __P((void));
     74 
     75 /* From mount_ufs.c. */
     76 int	mount_ufs __P((int, char * const *));
     77 
     78 /* Map from mount otions to printable formats. */
     79 static struct opt {
     80 	int o_opt;
     81 	const char *o_name;
     82 } optnames[] = {
     83 	{ MNT_ASYNC,		"asynchronous" },
     84 	{ MNT_EXPORTED,		"NFS exported" },
     85 	{ MNT_LOCAL,		"local" },
     86 	{ MNT_NODEV,		"nodev" },
     87 	{ MNT_NOEXEC,		"noexec" },
     88 	{ MNT_NOSUID,		"nosuid" },
     89 	{ MNT_QUOTA,		"with quotas" },
     90 	{ MNT_RDONLY,		"read-only" },
     91 	{ MNT_SYNCHRONOUS,	"synchronous" },
     92 	{ MNT_UNION,		"union" },
     93 	{ MNT_USER,		"user mount" },
     94 	{ NULL }
     95 };
     96 
     97 int
     98 main(argc, argv)
     99 	int argc;
    100 	char * const argv[];
    101 {
    102 	const char *mntonname, **vfslist, *vfstype;
    103 	struct fstab *fs;
    104 	struct statfs *mntbuf;
    105 	FILE *mountdfp;
    106 	pid_t pid;
    107 	int all, ch, i, init_flags, mntsize, rval;
    108 	char *options;
    109 
    110 	all = init_flags = 0;
    111 	options = NULL;
    112 	vfslist = NULL;
    113 	vfstype = "ufs";
    114 	while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF)
    115 		switch (ch) {
    116 		case 'a':
    117 			all = 1;
    118 			break;
    119 		case 'd':
    120 			debug = 1;
    121 			break;
    122 		case 'f':
    123 			init_flags |= MNT_FORCE;
    124 			break;
    125 		case 'o':
    126 			if (*optarg)
    127 				options = catopt(options, optarg);
    128 			break;
    129 		case 'r':
    130 			init_flags |= MNT_RDONLY;
    131 			break;
    132 		case 't':
    133 			if (vfslist != NULL)
    134 				errx(1, "only one -t option may be specified.");
    135 			vfslist = makevfslist(optarg);
    136 			vfstype = optarg;
    137 			break;
    138 		case 'u':
    139 			init_flags |= MNT_UPDATE;
    140 			break;
    141 		case 'v':
    142 			verbose = 1;
    143 			break;
    144 		case 'w':
    145 			init_flags &= ~MNT_RDONLY;
    146 			break;
    147 		case '?':
    148 		default:
    149 			usage();
    150 			/* NOTREACHED */
    151 		}
    152 	argc -= optind;
    153 	argv += optind;
    154 
    155 #define	BADTYPE(type)							\
    156 	(strcmp(type, FSTAB_RO) &&					\
    157 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    158 
    159 	rval = 0;
    160 	switch (argc) {
    161 	case 0:
    162 		if (all)
    163 			while ((fs = getfsent()) != NULL) {
    164 				if (BADTYPE(fs->fs_type))
    165 					continue;
    166 				if (badvfsname(fs->fs_vfstype, vfslist))
    167 					continue;
    168 				if (hasopt(fs->fs_mntops, "noauto"))
    169 					continue;
    170 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
    171 				    fs->fs_file, init_flags, options,
    172 				    fs->fs_mntops))
    173 					rval = 1;
    174 			}
    175 		else {
    176 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    177 				err(1, "getmntinfo");
    178 			for (i = 0; i < mntsize; i++) {
    179 				if (badvfsname(mntbuf[i].f_fstypename, vfslist))
    180 					continue;
    181 				prmount(mntbuf[i].f_mntfromname,
    182 				    mntbuf[i].f_mntonname, mntbuf[i].f_flags);
    183 			}
    184 		}
    185 		exit(rval);
    186 	case 1:
    187 		if (vfslist != NULL)
    188 			usage();
    189 
    190 		if (init_flags & MNT_UPDATE) {
    191 			if ((mntbuf = getmntpt(*argv)) == NULL)
    192 				errx(1,
    193 				    "unknown special file or file system %s.",
    194 				    *argv);
    195 			if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
    196 				errx(1, "can't find fstab entry for %s.",
    197 				    *argv);
    198 			/* If it's an update, ignore the fstab file options. */
    199 			fs->fs_mntops = NULL;
    200 			mntonname = mntbuf->f_mntonname;
    201 		} else {
    202 			if ((fs = getfsfile(*argv)) == NULL &&
    203 			    (fs = getfsspec(*argv)) == NULL)
    204 				errx(1,
    205 				    "%s: unknown special file or file system.",
    206 				    *argv);
    207 			if (BADTYPE(fs->fs_type))
    208 				errx(1, "%s has unknown file system type.",
    209 				    *argv);
    210 			mntonname = fs->fs_file;
    211 		}
    212 		rval = mountfs(fs->fs_vfstype, fs->fs_spec,
    213 		    mntonname, init_flags, options, fs->fs_mntops);
    214 		break;
    215 	case 2:
    216 		/*
    217 		 * If -t flag has not been specified, and spec contains either
    218 		 * a ':' or a '@' then assume that an NFS filesystem is being
    219 		 * specified ala Sun.
    220 		 */
    221 		if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
    222 			vfstype = "nfs";
    223 		rval = mountfs(vfstype,
    224 		    argv[0], argv[1], init_flags, options, NULL);
    225 		break;
    226 	default:
    227 		usage();
    228 		/* NOTREACHED */
    229 	}
    230 
    231 	/*
    232 	 * If the mount was successfully, and done by root, tell mountd the
    233 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    234 	 */
    235 	if (rval == 0 && getuid() == 0 &&
    236 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    237 		if (fscanf(mountdfp, "%ld", &pid) == 1 &&
    238 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    239 			err(1, "signal mountd");
    240 		(void)fclose(mountdfp);
    241 	}
    242 
    243 	exit(rval);
    244 }
    245 
    246 int
    247 hasopt(mntopts, option)
    248 	const char *mntopts, *option;
    249 {
    250 	int negative, found;
    251 	char *opt, *optbuf;
    252 
    253 	if (option[0] == 'n' && option[1] == 'o') {
    254 		negative = 1;
    255 		option += 2;
    256 	} else
    257 		negative = 0;
    258 	optbuf = strdup(mntopts);
    259 	found = 0;
    260 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    261 		if (opt[0] == 'n' && opt[1] == 'o') {
    262 			if (!strcasecmp(opt + 2, option))
    263 				found = negative;
    264 		} else if (!strcasecmp(opt, option))
    265 			found = !negative;
    266 	}
    267 	free(optbuf);
    268 	return (found);
    269 }
    270 
    271 int
    272 mountfs(vfstype, spec, name, flags, options, mntopts)
    273 	const char *vfstype, *spec, *name, *options, *mntopts;
    274 	int flags;
    275 {
    276 	/* List of directories containing mount_xxx subcommands. */
    277 	static const char *edirs[] = {
    278 		_PATH_SBIN,
    279 		_PATH_USRSBIN,
    280 		NULL
    281 	};
    282 	const char *argv[100], **edir;
    283 	struct statfs sf;
    284 	pid_t pid;
    285 	int argc, i, status;
    286 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
    287 
    288 	if (realpath(name, mntpath) == NULL) {
    289 		warn("realpath %s", mntpath);
    290 		return (1);
    291 	}
    292 
    293 	name = mntpath;
    294 
    295 	if (mntopts == NULL)
    296 		mntopts = "";
    297 	if (options == NULL) {
    298 		if (*mntopts == '\0')
    299 			options = "rw";
    300 		else {
    301 			options = mntopts;
    302 			mntopts = "";
    303 		}
    304 	}
    305 	optbuf = catopt(strdup(mntopts), options);
    306 
    307 	if (strcmp(name, "/") == 0)
    308 		flags |= MNT_UPDATE;
    309 	if (flags & MNT_FORCE)
    310 		optbuf = catopt(optbuf, "force");
    311 	if (flags & MNT_RDONLY)
    312 		optbuf = catopt(optbuf, "ro");
    313 	/*
    314 	 * XXX
    315 	 * The mount_mfs (newfs) command uses -o to select the
    316 	 * optimisation mode.  We don't pass the default "-o rw"
    317 	 * for that reason.
    318 	 */
    319 	if (flags & MNT_UPDATE)
    320 		optbuf = catopt(optbuf, "update");
    321 
    322 	argc = 0;
    323 	argv[argc++] = vfstype;
    324 	mangle(optbuf, &argc, argv);
    325 	argv[argc++] = spec;
    326 	argv[argc++] = name;
    327 	argv[argc] = NULL;
    328 
    329 	if (debug) {
    330 		(void)printf("exec: mount_%s", vfstype);
    331 		for (i = 1; i < argc; i++)
    332 			(void)printf(" %s", argv[i]);
    333 		(void)printf("\n");
    334 		return (0);
    335 	}
    336 
    337 	switch (pid = vfork()) {
    338 	case -1:				/* Error. */
    339 		warn("vfork");
    340 		free(optbuf);
    341 		return (1);
    342 	case 0:					/* Child. */
    343 		if (strcmp(vfstype, "ufs") == 0)
    344 			exit(mount_ufs(argc, (char * const *) argv));
    345 
    346 		/* Go find an executable. */
    347 		edir = edirs;
    348 		do {
    349 			(void)snprintf(execname,
    350 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
    351 			execv(execname, (char * const *)argv);
    352 			if (errno != ENOENT)
    353 				warn("exec %s for %s", execname, name);
    354 		} while (*++edir != NULL);
    355 
    356 		if (errno == ENOENT)
    357 			warn("exec %s for %s", execname, name);
    358 		exit(1);
    359 		/* NOTREACHED */
    360 	default:				/* Parent. */
    361 		free(optbuf);
    362 
    363 		if (waitpid(pid, &status, 0) < 0) {
    364 			warn("waitpid");
    365 			return (1);
    366 		}
    367 
    368 		if (WIFEXITED(status)) {
    369 			if (WEXITSTATUS(status) != 0)
    370 				return (WEXITSTATUS(status));
    371 		} else if (WIFSIGNALED(status)) {
    372 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
    373 			return (1);
    374 		}
    375 
    376 		if (verbose) {
    377 			if (statfs(name, &sf) < 0) {
    378 				warn("statfs %s", name);
    379 				return (1);
    380 			}
    381 			prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags);
    382 		}
    383 		break;
    384 	}
    385 
    386 	return (0);
    387 }
    388 
    389 void
    390 prmount(spec, name, flags)
    391 	const char *spec, *name;
    392 	int flags;
    393 {
    394 	struct opt *o;
    395 	int f;
    396 
    397 	(void)printf("%s on %s", spec, name);
    398 
    399 	flags &= MNT_VISFLAGMASK;
    400 	for (f = 0, o = optnames; flags && o->o_opt; o++)
    401 		if (flags & o->o_opt) {
    402 			(void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
    403 			flags &= ~o->o_opt;
    404 		}
    405 	(void)printf(f ? ")\n" : "\n");
    406 }
    407 
    408 struct statfs *
    409 getmntpt(name)
    410 	const char *name;
    411 {
    412 	struct statfs *mntbuf;
    413 	int i, mntsize;
    414 
    415 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    416 	for (i = 0; i < mntsize; i++)
    417 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    418 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    419 			return (&mntbuf[i]);
    420 	return (NULL);
    421 }
    422 
    423 int
    424 badvfsname(vfsname, vfslist)
    425 	const char *vfsname;
    426 	const char **vfslist;
    427 {
    428 
    429 	if (vfslist == NULL)
    430 		return (0);
    431 	while (*vfslist != NULL) {
    432 		if (strcmp(vfsname, *vfslist) == 0)
    433 			return (skipvfs);
    434 		++vfslist;
    435 	}
    436 	return (!skipvfs);
    437 }
    438 
    439 const char **
    440 makevfslist(fslist)
    441 	char *fslist;
    442 {
    443 	const char **av;
    444 	int i;
    445 	char *nextcp;
    446 
    447 	if (fslist == NULL)
    448 		return (NULL);
    449 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    450 		fslist += 2;
    451 		skipvfs = 1;
    452 	}
    453 	for (i = 0, nextcp = fslist; *nextcp; nextcp++)
    454 		if (*nextcp == ',')
    455 			i++;
    456 	if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) {
    457 		warn(NULL);
    458 		return (NULL);
    459 	}
    460 	nextcp = fslist;
    461 	i = 0;
    462 	av[i++] = nextcp;
    463 	while ((nextcp = strchr(nextcp, ',')) != NULL) {
    464 		*nextcp++ = '\0';
    465 		av[i++] = nextcp;
    466 	}
    467 	av[i++] = NULL;
    468 	return (av);
    469 }
    470 
    471 char *
    472 catopt(s0, s1)
    473 	char *s0;
    474 	const char *s1;
    475 {
    476 	size_t i;
    477 	char *cp;
    478 
    479 	if (s0 && *s0) {
    480 		i = strlen(s0) + strlen(s1) + 1 + 1;
    481 		if ((cp = malloc(i)) == NULL)
    482 			err(1, NULL);
    483 		(void)snprintf(cp, i, "%s,%s", s0, s1);
    484 	} else
    485 		cp = strdup(s1);
    486 
    487 	if (s0)
    488 		free(s0);
    489 	return (cp);
    490 }
    491 
    492 void
    493 mangle(options, argcp, argv)
    494 	char *options;
    495 	int *argcp;
    496 	const char **argv;
    497 {
    498 	char *p, *s;
    499 	int argc;
    500 
    501 	argc = *argcp;
    502 	for (s = options; (p = strsep(&s, ",")) != NULL;)
    503 		if (*p != '\0')
    504 			if (*p == '-') {
    505 				argv[argc++] = p;
    506 				p = strchr(p, '=');
    507 				if (p) {
    508 					*p = '\0';
    509 					argv[argc++] = p+1;
    510 				}
    511 			} else if (strcmp(p, "rw") != 0) {
    512 				argv[argc++] = "-o";
    513 				argv[argc++] = p;
    514 			}
    515 
    516 	*argcp = argc;
    517 }
    518 
    519 void
    520 usage()
    521 {
    522 
    523 	(void)fprintf(stderr,
    524 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
    525 		"[-dfruvw] [-o options] [-t ufs | external_type]",
    526 			"special node",
    527 		"[-adfruvw] [-t ufs | external_type]",
    528 		"[-dfruvw] special | node");
    529 	exit(1);
    530 }
    531