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