mount.c revision 36134
1185377Ssam/*-
2185377Ssam * Copyright (c) 1980, 1989, 1993, 1994
3185377Ssam *	The Regents of the University of California.  All rights reserved.
4185377Ssam *
5185377Ssam * Redistribution and use in source and binary forms, with or without
6185377Ssam * modification, are permitted provided that the following conditions
7185377Ssam * are met:
8185377Ssam * 1. Redistributions of source code must retain the above copyright
9185377Ssam *    notice, this list of conditions and the following disclaimer.
10185377Ssam * 2. Redistributions in binary form must reproduce the above copyright
11185377Ssam *    notice, this list of conditions and the following disclaimer in the
12185377Ssam *    documentation and/or other materials provided with the distribution.
13185377Ssam * 3. All advertising materials mentioning features or use of this software
14185377Ssam *    must display the following acknowledgement:
15185377Ssam *	This product includes software developed by the University of
16185377Ssam *	California, Berkeley and its contributors.
17204644Srpaulo * 4. Neither the name of the University nor the names of its contributors
18185377Ssam *    may be used to endorse or promote products derived from this software
19185377Ssam *    without specific prior written permission.
20185377Ssam *
21185377Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22185377Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23185377Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24185377Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25185377Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26185377Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27185377Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28185377Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29185377Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30185377Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31185377Ssam * SUCH DAMAGE.
32185377Ssam */
33225444Sadrian
34225444Sadrian#ifndef lint
35225444Sadrianstatic const char copyright[] =
36225444Sadrian"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37225444Sadrian	The Regents of the University of California.  All rights reserved.\n";
38225444Sadrian#endif /* not lint */
39225444Sadrian
40225444Sadrian#ifndef lint
41225444Sadrian#if 0
42225444Sadrianstatic char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
43225444Sadrian#else
44185377Ssamstatic const char rcsid[] =
45185377Ssam	"$Id: mount.c,v 1.25 1998/04/08 18:31:18 wosch Exp $";
46185377Ssam#endif
47185377Ssam#endif /* not lint */
48185377Ssam
49185377Ssam#include <sys/param.h>
50185377Ssam#include <sys/mount.h>
51185377Ssam#include <sys/stat.h>
52185377Ssam#include <sys/wait.h>
53185377Ssam
54185377Ssam#include <err.h>
55185377Ssam#include <errno.h>
56185377Ssam#include <fstab.h>
57185377Ssam#include <pwd.h>
58185377Ssam#include <signal.h>
59185377Ssam#include <stdio.h>
60185377Ssam#include <stdlib.h>
61185377Ssam#include <string.h>
62185377Ssam#include <unistd.h>
63185377Ssam
64185377Ssam#include "extern.h"
65185377Ssam#include "pathnames.h"
66185377Ssam
67185377Ssamint debug, fstab_style, verbose;
68185377Ssam
69185377Ssamchar   *catopt __P((char *, const char *));
70185377Ssamstruct statfs
71185377Ssam       *getmntpt __P((const char *));
72185377Ssamint	hasopt __P((const char *, const char *));
73185377Ssamint	ismounted __P((struct fstab *, struct statfs *, int));
74185377Ssamint	isremountable __P((const char *));
75185377Ssamvoid	mangle __P((char *, int *, const char **));
76185377Ssamint	mountfs __P((const char *, const char *, const char *,
77185377Ssam			int, const char *, const char *));
78185377Ssamvoid	prmount __P((struct statfs *));
79185377Ssamvoid	putfsent __P((const struct statfs *));
80185377Ssamvoid	usage __P((void));
81185377Ssam
82185377Ssam/* Map from mount otions to printable formats. */
83185377Ssamstatic struct opt {
84185377Ssam	int o_opt;
85223459Sadrian	const char *o_name;
86185377Ssam} optnames[] = {
87223459Sadrian	{ MNT_ASYNC,		"asynchronous" },
88185377Ssam	{ MNT_EXPORTED,		"NFS exported" },
89185377Ssam	{ MNT_LOCAL,		"local" },
90185377Ssam	{ MNT_NOATIME,		"noatime" },
91185377Ssam	{ MNT_NODEV,		"nodev" },
92185377Ssam	{ MNT_NOEXEC,		"noexec" },
93185377Ssam	{ MNT_NOSUID,		"nosuid" },
94185377Ssam	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
95185377Ssam	{ MNT_QUOTA,		"with quotas" },
96185377Ssam	{ MNT_RDONLY,		"read-only" },
97185377Ssam	{ MNT_SYNCHRONOUS,	"synchronous" },
98185377Ssam	{ MNT_UNION,		"union" },
99185377Ssam	{ MNT_NOCLUSTERR,	"noclusterr" },
100185377Ssam	{ MNT_NOCLUSTERW,	"noclusterw" },
101185377Ssam	{ MNT_SUIDDIR,		"suiddir" },
102185377Ssam	{ MNT_SOFTDEP,		"soft-updates" },
103185377Ssam	{ NULL }
104185377Ssam};
105185377Ssam
106185377Ssam/*
107185377Ssam * List of VFS types that can be remounted without becoming mounted on top
108185377Ssam * of each other.
109185377Ssam * XXX Is this list correct?
110185377Ssam */
111185377Ssamstatic const char *
112185377Ssamremountable_fs_names[] = {
113185377Ssam	"ufs", "ffs", "lfs", "ext2fs",
114185377Ssam	0
115185377Ssam};
116185377Ssam
117185377Ssamint
118185377Ssammain(argc, argv)
119185377Ssam	int argc;
120185377Ssam	char * const argv[];
121185377Ssam{
122185377Ssam	const char *mntfromname, **vfslist, *vfstype;
123185377Ssam	struct fstab *fs;
124185377Ssam	struct statfs *mntbuf;
125185377Ssam	FILE *mountdfp;
126185377Ssam	pid_t pid;
127185377Ssam	int all, ch, i, init_flags, mntsize, rval;
128185377Ssam	char *options;
129185377Ssam
130185377Ssam	all = init_flags = 0;
131185377Ssam	options = NULL;
132185377Ssam	vfslist = NULL;
133185377Ssam	vfstype = "ufs";
134185377Ssam	while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
135185377Ssam		switch (ch) {
136185377Ssam		case 'a':
137185377Ssam			all = 1;
138185377Ssam			break;
139185377Ssam		case 'd':
140185377Ssam			debug = 1;
141185377Ssam			break;
142185377Ssam		case 'f':
143185377Ssam			init_flags |= MNT_FORCE;
144185377Ssam			break;
145185377Ssam		case 'o':
146185377Ssam			if (*optarg)
147185377Ssam				options = catopt(options, optarg);
148185377Ssam			break;
149185377Ssam		case 'p':
150185377Ssam			fstab_style = 1;
151185377Ssam			verbose = 1;
152185377Ssam			break;
153185377Ssam		case 'r':
154185377Ssam			init_flags |= MNT_RDONLY;
155185377Ssam			break;
156185377Ssam		case 't':
157185377Ssam			if (vfslist != NULL)
158185377Ssam				errx(1, "only one -t option may be specified.");
159185377Ssam			vfslist = makevfslist(optarg);
160185377Ssam			vfstype = optarg;
161185377Ssam			break;
162185377Ssam		case 'u':
163185377Ssam			init_flags |= MNT_UPDATE;
164185377Ssam			break;
165185377Ssam		case 'v':
166185377Ssam			verbose = 1;
167185377Ssam			break;
168185377Ssam		case 'w':
169185377Ssam			init_flags &= ~MNT_RDONLY;
170185377Ssam			break;
171185377Ssam		case '?':
172185377Ssam		default:
173185377Ssam			usage();
174185377Ssam			/* NOTREACHED */
175185377Ssam		}
176185377Ssam	argc -= optind;
177185377Ssam	argv += optind;
178185377Ssam
179185377Ssam#define	BADTYPE(type)							\
180185377Ssam	(strcmp(type, FSTAB_RO) &&					\
181185377Ssam	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
182185377Ssam
183185377Ssam	rval = 0;
184185377Ssam	switch (argc) {
185	case 0:
186		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
187			err(1, "getmntinfo");
188		if (all) {
189			while ((fs = getfsent()) != NULL) {
190				if (BADTYPE(fs->fs_type))
191					continue;
192				if (checkvfsname(fs->fs_vfstype, vfslist))
193					continue;
194				if (hasopt(fs->fs_mntops, "noauto"))
195					continue;
196				if (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	 * optimisation 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				warn(NULL);
432				exit(1);
433			}
434			cp[0] = '\0';
435			for (edir = edirs; *edir; edir++) {
436				strcat(cp, *edir);
437				if (edir[1] != NULL)
438					strcat(cp, ", ");
439			}
440			warn("exec mount_%s not found in %s", vfstype, cp);
441		}
442		exit(1);
443		/* NOTREACHED */
444	default:				/* Parent. */
445		free(optbuf);
446
447		if (waitpid(pid, &status, 0) < 0) {
448			warn("waitpid");
449			return (1);
450		}
451
452		if (WIFEXITED(status)) {
453			if (WEXITSTATUS(status) != 0)
454				return (WEXITSTATUS(status));
455		} else if (WIFSIGNALED(status)) {
456			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
457			return (1);
458		}
459
460		if (verbose) {
461			if (statfs(name, &sf) < 0) {
462				warn("statfs %s", name);
463				return (1);
464			}
465			if (fstab_style)
466				putfsent(&sf);
467			else
468				prmount(&sf);
469		}
470		break;
471	}
472
473	return (0);
474}
475
476void
477prmount(sfp)
478	struct statfs *sfp;
479{
480	int flags;
481	struct opt *o;
482	struct passwd *pw;
483	int f;
484
485	(void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname);
486
487	flags = sfp->f_flags & MNT_VISFLAGMASK;
488	for (f = 0, o = optnames; flags && o->o_opt; o++)
489		if (flags & o->o_opt) {
490			(void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
491			flags &= ~o->o_opt;
492		}
493	if (sfp->f_owner) {
494		(void)printf("%smounted by ", !f++ ? " (" : ", ");
495		if ((pw = getpwuid(sfp->f_owner)) != NULL)
496			(void)printf("%s", pw->pw_name);
497		else
498			(void)printf("%d", sfp->f_owner);
499	}
500	if ((sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) &&
501	    (sfp->f_flags & MNT_RDONLY) == 0)
502		(void)printf("%swrites: sync %d async %d",
503		    !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
504	(void)printf("%s\n", f ? ")" : "");
505}
506
507struct statfs *
508getmntpt(name)
509	const char *name;
510{
511	struct statfs *mntbuf;
512	int i, mntsize;
513
514	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
515	for (i = 0; i < mntsize; i++)
516		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
517		    strcmp(mntbuf[i].f_mntonname, name) == 0)
518			return (&mntbuf[i]);
519	return (NULL);
520}
521
522char *
523catopt(s0, s1)
524	char *s0;
525	const char *s1;
526{
527	size_t i;
528	char *cp;
529
530	if (s0 && *s0) {
531		i = strlen(s0) + strlen(s1) + 1 + 1;
532		if ((cp = malloc(i)) == NULL)
533			err(1, NULL);
534		(void)snprintf(cp, i, "%s,%s", s0, s1);
535	} else
536		cp = strdup(s1);
537
538	if (s0)
539		free(s0);
540	return (cp);
541}
542
543void
544mangle(options, argcp, argv)
545	char *options;
546	int *argcp;
547	const char **argv;
548{
549	char *p, *s;
550	int argc;
551
552	argc = *argcp;
553	for (s = options; (p = strsep(&s, ",")) != NULL;)
554		if (*p != '\0')
555			if (*p == '-') {
556				argv[argc++] = p;
557				p = strchr(p, '=');
558				if (p) {
559					*p = '\0';
560					argv[argc++] = p+1;
561				}
562			} else if (strcmp(p, "rw") != 0) {
563				argv[argc++] = "-o";
564				argv[argc++] = p;
565			}
566
567	*argcp = argc;
568}
569
570void
571usage()
572{
573
574	(void)fprintf(stderr,
575		"usage: mount %s %s\n       mount %s\n       mount %s\n",
576		"[-dfpruvw] [-o options] [-t ufs | external_type]",
577			"special node",
578		"[-adfpruvw] [-t ufs | external_type]",
579		"[-dfpruvw] special | node");
580	exit(1);
581}
582
583void
584putfsent(ent)
585	const struct statfs *ent;
586{
587	struct fstab *fst;
588
589	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
590	    ent->f_fstypename, (ent->f_flags & MNT_RDONLY) ? "ro" : "rw");
591
592	/* XXX should use optnames[] - put shorter names in it. */
593	if (ent->f_flags & MNT_SYNCHRONOUS)
594		printf(",sync");
595	if (ent->f_flags & MNT_NOEXEC)
596		printf(",noexec");
597	if (ent->f_flags & MNT_NOSUID)
598		printf(",nosuid");
599	if (ent->f_flags & MNT_NODEV)
600		printf(",nodev");
601	if (ent->f_flags & MNT_UNION)
602		printf(",union");
603	if (ent->f_flags & MNT_ASYNC)
604		printf(",async");
605	if (ent->f_flags & MNT_NOATIME)
606		printf(",noatime");
607	if (ent->f_flags & MNT_NOCLUSTERR)
608		printf(",noclusterr");
609	if (ent->f_flags & MNT_NOCLUSTERW)
610		printf(",noclusterw");
611	if (ent->f_flags & MNT_NOSYMFOLLOW)
612		printf (",nosymfollow");
613	if (ent->f_flags & MNT_SUIDDIR)
614		printf(",suiddir");
615
616	if ((fst = getfsspec(ent->f_mntfromname)))
617		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
618	else if ((fst = getfsfile(ent->f_mntonname)))
619		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
620	else if (strcmp(ent->f_fstypename, "ufs") == 0)
621		printf("\t1 1\n");
622	else
623		printf("\t0 0\n");
624}
625