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