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