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