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