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