xref: /freebsd/sbin/mount/mount.c (revision 953a3198a35204535cc9d450f04da982a4fea59b)
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
35 static 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 static 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 
61 int debug, verbose, skipvfs;
62 
63 int	badvfsname __P((const char *, const char **));
64 int	badvfstype __P((int, const char **));
65 char   *catopt __P((char *, const char *));
66 struct statfs
67        *getmntpt __P((const char *));
68 const char
69       **makevfslist __P((char *));
70 void	mangle __P((char *, int *, const char **));
71 int	mountfs __P((const char *, const char *, const char *,
72 			int, const char *, const char *));
73 void	prmount __P((const char *, const char *, int));
74 void	usage __P((void));
75 
76 /* From mount_ufs.c. */
77 int	mount_ufs __P((int, char * const *));
78 
79 /* Map from mount otions to printable formats. */
80 static 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 
98 int
99 main(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 
247 int
248 mountfs(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 			if (errno != ENOENT)
336 				warn("exec %s for %s", execname, name);
337 		} while (*++edir != NULL);
338 
339 		if (errno == ENOENT)
340 			warn("exec %s for %s", execname, name);
341 		exit(1);
342 		/* NOTREACHED */
343 	default:				/* Parent. */
344 		free(optbuf);
345 
346 		if (waitpid(pid, &status, 0) < 0) {
347 			warn("waitpid");
348 			return (1);
349 		}
350 
351 		if (WIFEXITED(status)) {
352 			if (WEXITSTATUS(status) != 0)
353 				return (WEXITSTATUS(status));
354 		} else if (WIFSIGNALED(status)) {
355 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
356 			return (1);
357 		}
358 
359 		if (verbose) {
360 			if (statfs(name, &sf) < 0) {
361 				warn("%s", name);
362 				return (1);
363 			}
364 			prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags);
365 		}
366 		break;
367 	}
368 
369 	return (0);
370 }
371 
372 void
373 prmount(spec, name, flags)
374 	const char *spec, *name;
375 	int flags;
376 {
377 	struct opt *o;
378 	int f;
379 
380 	(void)printf("%s on %s", spec, name);
381 
382 	flags &= MNT_VISFLAGMASK;
383 	for (f = 0, o = optnames; flags && o->o_opt; o++)
384 		if (flags & o->o_opt) {
385 			(void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
386 			flags &= ~o->o_opt;
387 		}
388 	(void)printf(f ? ")\n" : "\n");
389 }
390 
391 struct statfs *
392 getmntpt(name)
393 	const char *name;
394 {
395 	struct statfs *mntbuf;
396 	int i, mntsize;
397 
398 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
399 	for (i = 0; i < mntsize; i++)
400 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
401 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
402 			return (&mntbuf[i]);
403 	return (NULL);
404 }
405 
406 int
407 badvfsname(vfsname, vfslist)
408 	const char *vfsname;
409 	const char **vfslist;
410 {
411 
412 	if (vfslist == NULL)
413 		return (0);
414 	while (*vfslist != NULL) {
415 		if (strcmp(vfsname, *vfslist) == 0)
416 			return (skipvfs);
417 		++vfslist;
418 	}
419 	return (!skipvfs);
420 }
421 
422 int
423 badvfstype(vfstype, vfslist)
424 	int vfstype;
425 	const char **vfslist;
426 {
427 	struct vfsconf *vfc;
428 	vfc = getvfsbytype(vfstype);
429 
430 	if ( ! vfc )
431 		return (0);
432 
433 	return (badvfsname(vfc->vfc_name, vfslist));
434 }
435 
436 const char **
437 makevfslist(fslist)
438 	char *fslist;
439 {
440 	const char **av;
441 	int i;
442 	char *nextcp;
443 
444 	if (fslist == NULL)
445 		return (NULL);
446 	if (fslist[0] == 'n' && fslist[1] == 'o') {
447 		fslist += 2;
448 		skipvfs = 1;
449 	}
450 	for (i = 0, nextcp = fslist; *nextcp; nextcp++)
451 		if (*nextcp == ',')
452 			i++;
453 	if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) {
454 		warn(NULL);
455 		return (NULL);
456 	}
457 	nextcp = fslist;
458 	i = 0;
459 	av[i++] = nextcp;
460 	while ((nextcp = strchr(nextcp, ',')) != NULL) {
461 		*nextcp++ = '\0';
462 		av[i++] = nextcp;
463 	}
464 	av[i++] = NULL;
465 	return (av);
466 }
467 
468 char *
469 catopt(s0, s1)
470 	char *s0;
471 	const char *s1;
472 {
473 	size_t i;
474 	char *cp;
475 
476 	if (s0 && *s0) {
477 		i = strlen(s0) + strlen(s1) + 1 + 1;
478 		if ((cp = malloc(i)) == NULL)
479 			err(1, NULL);
480 		(void)snprintf(cp, i, "%s,%s", s0, s1);
481 	} else
482 		cp = strdup(s1);
483 
484 	if (s0)
485 		free(s0);
486 	return (cp);
487 }
488 
489 void
490 mangle(options, argcp, argv)
491 	char *options;
492 	int *argcp;
493 	const char **argv;
494 {
495 	char *p, *s;
496 	int argc;
497 
498 	argc = *argcp;
499 	for (s = options; (p = strsep(&s, ",")) != NULL;)
500 		if (*p != '\0')
501 			if (*p == '-') {
502 				argv[argc++] = p;
503 				p = strchr(p, '=');
504 				if (p) {
505 					*p = '\0';
506 					argv[argc++] = p+1;
507 				}
508 			} else if (strcmp(p, "rw") != 0) {
509 				argv[argc++] = "-o";
510 				argv[argc++] = p;
511 			}
512 
513 	*argcp = argc;
514 }
515 
516 void
517 usage()
518 {
519 
520 	(void)fprintf(stderr,
521 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
522 		"[-dfruvw] [-o options] [-t ufs | external_type]",
523 			"special node",
524 		"[-adfruvw] [-t ufs | external_type]",
525 		"[-dfruvw] special | node");
526 	exit(1);
527 }
528