mount.c revision 14350
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 (strstr(fs->fs_mntops, "noauto"))
170					continue;
171				if (mountfs(fs->fs_vfstype, fs->fs_spec,
172		    			fs->fs_file, init_flags, options,
173			    		fs->fs_mntops))
174						rval = 1;
175			}
176		else {
177			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
178				err(1, "getmntinfo");
179			for (i = 0; i < mntsize; i++) {
180				if (badvfstype(mntbuf[i].f_type, vfslist))
181					continue;
182				prmount(mntbuf[i].f_mntfromname,
183				    mntbuf[i].f_mntonname, mntbuf[i].f_flags);
184			}
185		}
186		exit(rval);
187	case 1:
188		if (vfslist != NULL)
189			usage();
190
191		if (init_flags & MNT_UPDATE) {
192			if ((mntbuf = getmntpt(*argv)) == NULL)
193				errx(1,
194				    "unknown special file or file system %s.",
195				    *argv);
196			if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
197				errx(1, "can't find fstab entry for %s.",
198				    *argv);
199			/* If it's an update, ignore the fstab file options. */
200			fs->fs_mntops = NULL;
201			mntonname = mntbuf->f_mntonname;
202		} else {
203			if ((fs = getfsfile(*argv)) == NULL &&
204			    (fs = getfsspec(*argv)) == NULL)
205				errx(1,
206				    "%s: unknown special file or file system.",
207				    *argv);
208			if (BADTYPE(fs->fs_type))
209				errx(1, "%s has unknown file system type.",
210				    *argv);
211			mntonname = fs->fs_file;
212		}
213		rval = mountfs(fs->fs_vfstype, fs->fs_spec,
214		    mntonname, init_flags, options, fs->fs_mntops);
215		break;
216	case 2:
217		/*
218		 * If -t flag has not been specified, and spec contains either
219		 * a ':' or a '@' then assume that an NFS filesystem is being
220		 * specified ala Sun.
221		 */
222		if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
223			vfstype = "nfs";
224		rval = mountfs(vfstype,
225		    argv[0], argv[1], init_flags, options, NULL);
226		break;
227	default:
228		usage();
229		/* NOTREACHED */
230	}
231
232	/*
233	 * If the mount was successfully, and done by root, tell mountd the
234	 * good news.  Pid checks are probably unnecessary, but don't hurt.
235	 */
236	if (rval == 0 && getuid() == 0 &&
237	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
238		if (fscanf(mountdfp, "%ld", &pid) == 1 &&
239		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
240			err(1, "signal mountd");
241		(void)fclose(mountdfp);
242	}
243
244	exit(rval);
245}
246
247int
248mountfs(vfstype, spec, name, flags, options, mntopts)
249	const char *vfstype, *spec, *name, *options, *mntopts;
250	int flags;
251{
252	struct stat sb;
253
254	/* List of directories containing mount_xxx subcommands. */
255	static const char *edirs[] = {
256		_PATH_SBIN,
257		_PATH_USRSBIN,
258		NULL
259	};
260	const char *argv[100], **edir;
261	struct statfs sf;
262	pid_t pid;
263	int argc, i, status;
264	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
265
266	if (realpath(name, mntpath) != NULL && stat(mntpath, &sb) == 0) {
267		if (!S_ISDIR(sb.st_mode)) {
268			warnx("%s: Not a directory", mntpath);
269			return (1);
270		}
271	} else {
272		warn("%s", mntpath);
273		return (1);
274	}
275
276	if (mntopts == NULL)
277		mntopts = "";
278
279	name = mntpath;
280
281	if (options == NULL) {
282		if (*mntopts == '\0')
283			options = "rw";
284		else
285			options = mntopts;
286		mntopts = "";
287	}
288	optbuf = catopt(strdup(mntopts), options);
289
290	if (strcmp(name, "/") == 0)
291		flags |= MNT_UPDATE;
292	if (flags & MNT_FORCE)
293		optbuf = catopt(optbuf, "force");
294	if (flags & MNT_RDONLY)
295		optbuf = catopt(optbuf, "ro");
296	/*
297	 * XXX
298	 * The mount_mfs (newfs) command uses -o to select the
299	 * optimisation mode.  We don't pass the default "-o rw"
300	 * for that reason.
301	 */
302	if (flags & MNT_UPDATE)
303		optbuf = catopt(optbuf, "update");
304
305	argc = 0;
306	argv[argc++] = vfstype;
307	mangle(optbuf, &argc, argv);
308	argv[argc++] = spec;
309	argv[argc++] = name;
310	argv[argc] = NULL;
311
312	if (debug) {
313		(void)printf("exec: mount_%s", vfstype);
314		for (i = 1; i < argc; i++)
315			(void)printf(" %s", argv[i]);
316		(void)printf("\n");
317		return (0);
318	}
319
320	switch (pid = vfork()) {
321	case -1:				/* Error. */
322		warn("vfork");
323		free(optbuf);
324		return (1);
325	case 0:					/* Child. */
326		if (strcmp(vfstype, "ufs") == 0)
327			exit(mount_ufs(argc, (char * const *) argv));
328
329		/* Go find an executable. */
330		edir = edirs;
331		do {
332			(void)snprintf(execname,
333			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
334			execv(execname, (char * const *)argv);
335			warn("exec %s for %s", execname, name);
336		} while (*++edir != NULL);
337		exit(1);
338		/* NOTREACHED */
339	default:				/* Parent. */
340		free(optbuf);
341
342		if (waitpid(pid, &status, 0) < 0) {
343			warn("waitpid");
344			return (1);
345		}
346
347		if (WIFEXITED(status)) {
348			if (WEXITSTATUS(status) != 0)
349				return (WEXITSTATUS(status));
350		} else if (WIFSIGNALED(status)) {
351			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
352			return (1);
353		}
354
355		if (verbose) {
356			if (statfs(name, &sf) < 0) {
357				warn("%s", name);
358				return (1);
359			}
360			prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags);
361		}
362		break;
363	}
364
365	return (0);
366}
367
368void
369prmount(spec, name, flags)
370	const char *spec, *name;
371	int flags;
372{
373	struct opt *o;
374	int f;
375
376	(void)printf("%s on %s", spec, name);
377
378	flags &= MNT_VISFLAGMASK;
379	for (f = 0, o = optnames; flags && o->o_opt; o++)
380		if (flags & o->o_opt) {
381			(void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
382			flags &= ~o->o_opt;
383		}
384	(void)printf(f ? ")\n" : "\n");
385}
386
387struct statfs *
388getmntpt(name)
389	const char *name;
390{
391	struct statfs *mntbuf;
392	int i, mntsize;
393
394	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
395	for (i = 0; i < mntsize; i++)
396		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
397		    strcmp(mntbuf[i].f_mntonname, name) == 0)
398			return (&mntbuf[i]);
399	return (NULL);
400}
401
402int
403badvfsname(vfsname, vfslist)
404	const char *vfsname;
405	const char **vfslist;
406{
407
408	if (vfslist == NULL)
409		return (0);
410	while (*vfslist != NULL) {
411		if (strcmp(vfsname, *vfslist) == 0)
412			return (skipvfs);
413		++vfslist;
414	}
415	return (!skipvfs);
416}
417
418int
419badvfstype(vfstype, vfslist)
420	int vfstype;
421	const char **vfslist;
422{
423	struct vfsconf *vfc;
424	vfc = getvfsbytype(vfstype);
425
426	if ( ! vfc )
427		return (0);
428
429	return (badvfsname(vfc->vfc_name, vfslist));
430}
431
432const char **
433makevfslist(fslist)
434	char *fslist;
435{
436	const char **av;
437	int i;
438	char *nextcp;
439
440	if (fslist == NULL)
441		return (NULL);
442	if (fslist[0] == 'n' && fslist[1] == 'o') {
443		fslist += 2;
444		skipvfs = 1;
445	}
446	for (i = 0, nextcp = fslist; *nextcp; nextcp++)
447		if (*nextcp == ',')
448			i++;
449	if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) {
450		warn(NULL);
451		return (NULL);
452	}
453	nextcp = fslist;
454	i = 0;
455	av[i++] = nextcp;
456	while ((nextcp = strchr(nextcp, ',')) != NULL) {
457		*nextcp++ = '\0';
458		av[i++] = nextcp;
459	}
460	av[i++] = NULL;
461	return (av);
462}
463
464char *
465catopt(s0, s1)
466	char *s0;
467	const char *s1;
468{
469	size_t i;
470	char *cp;
471
472	if (s0 && *s0) {
473		i = strlen(s0) + strlen(s1) + 1 + 1;
474		if ((cp = malloc(i)) == NULL)
475			err(1, NULL);
476		(void)snprintf(cp, i, "%s,%s", s0, s1);
477	} else
478		cp = strdup(s1);
479
480	if (s0)
481		free(s0);
482	return (cp);
483}
484
485void
486mangle(options, argcp, argv)
487	char *options;
488	int *argcp;
489	const char **argv;
490{
491	char *p, *s;
492	int argc;
493
494	argc = *argcp;
495	for (s = options; (p = strsep(&s, ",")) != NULL;)
496		if (*p != '\0')
497			if (*p == '-') {
498				argv[argc++] = p;
499				p = strchr(p, '=');
500				if (p) {
501					*p = '\0';
502					argv[argc++] = p+1;
503				}
504			} else if (strcmp(p, "rw") != 0) {
505				argv[argc++] = "-o";
506				argv[argc++] = p;
507			}
508
509	*argcp = argc;
510}
511
512void
513usage()
514{
515
516	(void)fprintf(stderr,
517		"usage: mount %s %s\n       mount %s\n       mount %s\n",
518		"[-dfruvw] [-o options] [-t ufs | external_type]",
519			"special node",
520		"[-adfruvw] [-t ufs | external_type]",
521		"[-dfruvw] special | node");
522	exit(1);
523}
524