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