Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.13
      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.13 1994/08/29 02:38:00 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((struct statfs *));
     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]);
    182 			}
    183 		}
    184 		exit(rval);
    185 	case 1:
    186 		if (vfslist != NULL)
    187 			usage();
    188 
    189 		if (init_flags & MNT_UPDATE) {
    190 			if ((mntbuf = getmntpt(*argv)) == NULL)
    191 				errx(1,
    192 				    "unknown special file or file system %s.",
    193 				    *argv);
    194 			if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
    195 				errx(1, "can't find fstab entry for %s.",
    196 				    *argv);
    197 			/* If it's an update, ignore the fstab file options. */
    198 			fs->fs_mntops = NULL;
    199 			mntonname = mntbuf->f_mntonname;
    200 		} else {
    201 			if ((fs = getfsfile(*argv)) == NULL &&
    202 			    (fs = getfsspec(*argv)) == NULL)
    203 				errx(1,
    204 				    "%s: unknown special file or file system.",
    205 				    *argv);
    206 			if (BADTYPE(fs->fs_type))
    207 				errx(1, "%s has unknown file system type.",
    208 				    *argv);
    209 			mntonname = fs->fs_file;
    210 		}
    211 		rval = mountfs(fs->fs_vfstype, fs->fs_spec,
    212 		    mntonname, init_flags, options, fs->fs_mntops);
    213 		break;
    214 	case 2:
    215 		/*
    216 		 * If -t flag has not been specified, and spec contains either
    217 		 * a ':' or a '@' then assume that an NFS filesystem is being
    218 		 * specified ala Sun.
    219 		 */
    220 		if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
    221 			vfstype = "nfs";
    222 		rval = mountfs(vfstype,
    223 		    argv[0], argv[1], init_flags, options, NULL);
    224 		break;
    225 	default:
    226 		usage();
    227 		/* NOTREACHED */
    228 	}
    229 
    230 	/*
    231 	 * If the mount was successfully, and done by root, tell mountd the
    232 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    233 	 */
    234 	if (rval == 0 && getuid() == 0 &&
    235 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    236 		if (fscanf(mountdfp, "%ld", &pid) == 1 &&
    237 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    238 			err(1, "signal mountd");
    239 		(void)fclose(mountdfp);
    240 	}
    241 
    242 	exit(rval);
    243 }
    244 
    245 int
    246 hasopt(mntopts, option)
    247 	const char *mntopts, *option;
    248 {
    249 	int negative, found;
    250 	char *opt, *optbuf;
    251 
    252 	if (option[0] == 'n' && option[1] == 'o') {
    253 		negative = 1;
    254 		option += 2;
    255 	} else
    256 		negative = 0;
    257 	optbuf = strdup(mntopts);
    258 	found = 0;
    259 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    260 		if (opt[0] == 'n' && opt[1] == 'o') {
    261 			if (!strcasecmp(opt + 2, option))
    262 				found = negative;
    263 		} else if (!strcasecmp(opt, option))
    264 			found = !negative;
    265 	}
    266 	free(optbuf);
    267 	return (found);
    268 }
    269 
    270 int
    271 mountfs(vfstype, spec, name, flags, options, mntopts)
    272 	const char *vfstype, *spec, *name, *options, *mntopts;
    273 	int flags;
    274 {
    275 	/* List of directories containing mount_xxx subcommands. */
    276 	static const char *edirs[] = {
    277 		_PATH_SBIN,
    278 		_PATH_USRSBIN,
    279 		NULL
    280 	};
    281 	const char *argv[100], **edir;
    282 	struct statfs sf;
    283 	pid_t pid;
    284 	int argc, i, status;
    285 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
    286 
    287 	if (realpath(name, mntpath) == NULL) {
    288 		warn("realpath %s", mntpath);
    289 		return (1);
    290 	}
    291 
    292 	name = mntpath;
    293 
    294 	if (mntopts == NULL)
    295 		mntopts = "";
    296 	if (options == NULL) {
    297 		if (*mntopts == '\0')
    298 			options = "rw";
    299 		else {
    300 			options = mntopts;
    301 			mntopts = "";
    302 		}
    303 	}
    304 	optbuf = catopt(strdup(mntopts), options);
    305 
    306 	if (strcmp(name, "/") == 0)
    307 		flags |= MNT_UPDATE;
    308 	if (flags & MNT_FORCE)
    309 		optbuf = catopt(optbuf, "force");
    310 	if (flags & MNT_RDONLY)
    311 		optbuf = catopt(optbuf, "ro");
    312 	/*
    313 	 * XXX
    314 	 * The mount_mfs (newfs) command uses -o to select the
    315 	 * optimisation mode.  We don't pass the default "-o rw"
    316 	 * for that reason.
    317 	 */
    318 	if (flags & MNT_UPDATE)
    319 		optbuf = catopt(optbuf, "update");
    320 
    321 	argc = 0;
    322 	argv[argc++] = vfstype;
    323 	mangle(optbuf, &argc, argv);
    324 	argv[argc++] = spec;
    325 	argv[argc++] = name;
    326 	argv[argc] = NULL;
    327 
    328 	if (debug) {
    329 		(void)printf("exec: mount_%s", vfstype);
    330 		for (i = 1; i < argc; i++)
    331 			(void)printf(" %s", argv[i]);
    332 		(void)printf("\n");
    333 		return (0);
    334 	}
    335 
    336 	switch (pid = vfork()) {
    337 	case -1:				/* Error. */
    338 		warn("vfork");
    339 		free(optbuf);
    340 		return (1);
    341 	case 0:					/* Child. */
    342 		if (strcmp(vfstype, "ufs") == 0)
    343 			exit(mount_ufs(argc, (char * const *) argv));
    344 
    345 		/* Go find an executable. */
    346 		edir = edirs;
    347 		do {
    348 			(void)snprintf(execname,
    349 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
    350 			execv(execname, (char * const *)argv);
    351 			if (errno != ENOENT)
    352 				warn("exec %s for %s", execname, name);
    353 		} while (*++edir != NULL);
    354 
    355 		if (errno == ENOENT)
    356 			warn("exec %s for %s", execname, name);
    357 		exit(1);
    358 		/* NOTREACHED */
    359 	default:				/* Parent. */
    360 		free(optbuf);
    361 
    362 		if (waitpid(pid, &status, 0) < 0) {
    363 			warn("waitpid");
    364 			return (1);
    365 		}
    366 
    367 		if (WIFEXITED(status)) {
    368 			if (WEXITSTATUS(status) != 0)
    369 				return (WEXITSTATUS(status));
    370 		} else if (WIFSIGNALED(status)) {
    371 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
    372 			return (1);
    373 		}
    374 
    375 		if (verbose) {
    376 			if (statfs(name, &sf) < 0) {
    377 				warn("statfs %s", name);
    378 				return (1);
    379 			}
    380 			prmount(&sf);
    381 		}
    382 		break;
    383 	}
    384 
    385 	return (0);
    386 }
    387 
    388 void
    389 prmount(sf)
    390 	struct statfs *sf;
    391 {
    392 	int flags;
    393 	struct opt *o;
    394 	int f;
    395 
    396 	(void)printf("%s on %s type %s", sf->f_mntfromname, sf->f_mntonname,
    397 	    sf->f_fstypename);
    398 
    399 	flags = sf->f_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