xref: /original-bsd/sbin/mount/mount.c (revision 7bd6ee9e)
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.16 (Berkeley) 03/27/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 		/* Go find an executable. */
295 		edir = edirs;
296 		do {
297 			(void)snprintf(execname,
298 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
299 			execv(execname, (char * const *)argv);
300 			if (errno != ENOENT)
301 				warn("exec %s for %s", execname, name);
302 		} while (*++edir != NULL);
303 
304 		if (errno == ENOENT)
305 			warn("exec %s for %s", execname, name);
306 		exit(1);
307 		/* NOTREACHED */
308 	default:				/* Parent. */
309 		free(optbuf);
310 
311 		if (waitpid(pid, &status, 0) < 0) {
312 			warn("waitpid");
313 			return (1);
314 		}
315 
316 		if (WIFEXITED(status)) {
317 			if (WEXITSTATUS(status) != 0)
318 				return (WEXITSTATUS(status));
319 		} else if (WIFSIGNALED(status)) {
320 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
321 			return (1);
322 		}
323 
324 		if (verbose) {
325 			if (statfs(name, &sf) < 0) {
326 				warn("%s", name);
327 				return (1);
328 			}
329 			prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags);
330 		}
331 		break;
332 	}
333 
334 	return (0);
335 }
336 
337 void
338 prmount(spec, name, flags)
339 	const char *spec, *name;
340 	int flags;
341 {
342 	struct opt *o;
343 	int f;
344 
345 	(void)printf("%s on %s", spec, name);
346 
347 	flags &= MNT_VISFLAGMASK;
348 	for (f = 0, o = optnames; flags && o->o_opt; o++)
349 		if (flags & o->o_opt) {
350 			(void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
351 			flags &= ~o->o_opt;
352 		}
353 	(void)printf(f ? ")\n" : "\n");
354 }
355 
356 struct statfs *
357 getmntpt(name)
358 	const char *name;
359 {
360 	struct statfs *mntbuf;
361 	int i, mntsize;
362 
363 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
364 	for (i = 0; i < mntsize; i++)
365 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
366 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
367 			return (&mntbuf[i]);
368 	return (NULL);
369 }
370 
371 int
372 badvfsname(vfsname, vfslist)
373 	const char *vfsname;
374 	const char **vfslist;
375 {
376 
377 	if (vfslist == NULL)
378 		return (0);
379 	while (*vfslist != NULL) {
380 		if (strcmp(vfsname, *vfslist) == 0)
381 			return (skipvfs);
382 		++vfslist;
383 	}
384 	return (!skipvfs);
385 }
386 
387 int
388 badvfstype(vfstype, vfslist)
389 	int vfstype;
390 	const char **vfslist;
391 {
392 static const char *vfsnames[] = INITMOUNTNAMES;
393 
394 	if ((vfstype < 0) || (vfstype > MOUNT_MAXTYPE))
395 		return (0);
396 
397 	return (badvfsname(vfsnames[vfstype], vfslist));
398 }
399 
400 const char **
401 makevfslist(fslist)
402 	char *fslist;
403 {
404 	const char **av;
405 	int i;
406 	char *nextcp;
407 
408 	if (fslist == NULL)
409 		return (NULL);
410 	if (fslist[0] == 'n' && fslist[1] == 'o') {
411 		fslist += 2;
412 		skipvfs = 1;
413 	}
414 	for (i = 0, nextcp = fslist; *nextcp; nextcp++)
415 		if (*nextcp == ',')
416 			i++;
417 	if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) {
418 		warn(NULL);
419 		return (NULL);
420 	}
421 	nextcp = fslist;
422 	i = 0;
423 	av[i++] = nextcp;
424 	while ((nextcp = strchr(nextcp, ',')) != NULL) {
425 		*nextcp++ = '\0';
426 		av[i++] = nextcp;
427 	}
428 	av[i++] = NULL;
429 	return (av);
430 }
431 
432 char *
433 catopt(s0, s1)
434 	char *s0;
435 	const char *s1;
436 {
437 	size_t i;
438 	char *cp;
439 
440 	if (s0 && *s0) {
441 		i = strlen(s0) + strlen(s1) + 1 + 1;
442 		if ((cp = malloc(i)) == NULL)
443 			err(1, NULL);
444 		(void)snprintf(cp, i, "%s,%s", s0, s1);
445 	} else
446 		cp = strdup(s1);
447 
448 	if (s0)
449 		free(s0);
450 	return (cp);
451 }
452 
453 void
454 mangle(options, argcp, argv)
455 	char *options;
456 	int *argcp;
457 	const char **argv;
458 {
459 	char *p, *s;
460 	int argc;
461 
462 	argc = *argcp;
463 	for (s = options; (p = strsep(&s, ",")) != NULL;)
464 		if (*p != '\0')
465 			if (*p == '-') {
466 				argv[argc++] = p;
467 				p = strchr(p, '=');
468 				if (p) {
469 					*p = '\0';
470 					argv[argc++] = p+1;
471 				}
472 			} else if (strcmp(p, "rw") != 0) {
473 				argv[argc++] = "-o";
474 				argv[argc++] = p;
475 			}
476 
477 	*argcp = argc;
478 }
479 
480 void
481 usage()
482 {
483 
484 	(void)fprintf(stderr,
485 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
486 		"[-dfruvw] [-o options] [-t ufs | external_type]",
487 			"special node",
488 		"[-adfruvw] [-t ufs | external_type]",
489 		"[-dfruvw] special | node");
490 	exit(1);
491 }
492