xref: /freebsd/sbin/mount/mount.c (revision 183e8aa749c1fe7de44c842542d9e9eadf11b8cb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #define _WANT_MNTOPTNAMES
34 #include <sys/mount.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 
38 #include <ctype.h>
39 
40 #include <errno.h>
41 #include <fstab.h>
42 #include <paths.h>
43 #include <pwd.h>
44 #include <signal.h>
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <libutil.h>
51 #include <libxo/xo.h>
52 
53 #include "extern.h"
54 #include "mntopts.h"
55 #include "pathnames.h"
56 
57 #define EXIT(a) {			\
58 	xo_close_container("mount");	\
59 	if (xo_finish() < 0)			\
60 		xo_err(EXIT_FAILURE, "stdout");	\
61 	exit(a);			\
62 	}
63 
64 /* `meta' options */
65 #define MOUNT_META_OPTION_FSTAB		"fstab"
66 #define MOUNT_META_OPTION_CURRENT	"current"
67 
68 static int debug, fstab_style, verbose;
69 
70 struct cpa {
71 	char	**a;
72 	ssize_t	sz;
73 	int	c;
74 };
75 
76 char   *catopt(char *, const char *);
77 int	hasopt(const char *, const char *);
78 int	ismounted(struct fstab *, struct statfs *, int);
79 int	isremountable(const char *);
80 int	allow_file_mount(const char *);
81 void	mangle(char *, struct cpa *);
82 char   *update_options(char *, char *, int);
83 int	mountfs(const char *, const char *, const char *,
84 			int, const char *, const char *);
85 void	remopt(char *, const char *);
86 void	prmount(struct statfs *);
87 void	putfsent(struct statfs *);
88 void	usage(void);
89 char   *flags2opts(int);
90 
91 /* Map from mount options to printable formats. */
92 static struct mntoptnames optnames[] = {
93 	MNTOPT_NAMES
94 };
95 
96 /*
97  * List of VFS types that can be remounted without becoming mounted on top
98  * of each other.
99  * XXX Is this list correct?
100  */
101 static const char *
102 remountable_fs_names[] = {
103 	"ufs", "ffs", "ext2fs",
104 	0
105 };
106 
107 static const char userquotaeq[] = "userquota=";
108 static const char groupquotaeq[] = "groupquota=";
109 
110 static char *mountprog = NULL;
111 
112 static int
use_mountprog(const char * vfstype)113 use_mountprog(const char *vfstype)
114 {
115 	/* XXX: We need to get away from implementing external mount
116 	 *      programs for every filesystem, and move towards having
117 	 *	each filesystem properly implement the nmount() system call.
118 	 */
119 	unsigned int i;
120 	const char *fs[] = {
121 	"cd9660", "mfs", "msdosfs", "nfs",
122 	"nullfs", "smbfs", "udf", "unionfs",
123 	NULL
124 	};
125 
126 	if (mountprog != NULL)
127 		return (1);
128 
129 	for (i = 0; fs[i] != NULL; ++i) {
130 		if (strcmp(vfstype, fs[i]) == 0)
131 			return (1);
132 	}
133 
134 	return (0);
135 }
136 
137 static int
exec_mountprog(const char * name,const char * execname,char * const argv[])138 exec_mountprog(const char *name, const char *execname, char *const argv[])
139 {
140 	pid_t pid;
141 	int status;
142 
143 	switch (pid = fork()) {
144 	case -1:				/* Error. */
145 		xo_warn("fork");
146 		EXIT(EXIT_FAILURE);
147 	case 0:					/* Child. */
148 		/* Go find an executable. */
149 		execvP(execname, _PATH_SYSPATH, argv);
150 		if (errno == ENOENT) {
151 			xo_warn("exec %s not found", execname);
152 			if (execname[0] != '/') {
153 				xo_warnx("in path: %s", _PATH_SYSPATH);
154 			}
155 		}
156 		EXIT(EXIT_FAILURE);
157 	default:				/* Parent. */
158 		if (waitpid(pid, &status, 0) < 0) {
159 			xo_warn("waitpid");
160 			return (1);
161 		}
162 
163 		if (WIFEXITED(status)) {
164 			if (WEXITSTATUS(status) != 0)
165 				return (WEXITSTATUS(status));
166 		} else if (WIFSIGNALED(status)) {
167 			xo_warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
168 			return (1);
169 		}
170 		break;
171 	}
172 
173 	return (0);
174 }
175 
176 static int
specified_ro(const char * arg)177 specified_ro(const char *arg)
178 {
179 	char *optbuf, *opt;
180 	int ret = 0;
181 
182 	optbuf = strdup(arg);
183 	if (optbuf == NULL)
184 		 xo_err(1, "strdup failed");
185 
186 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
187 		if (strcmp(opt, "ro") == 0) {
188 			ret = 1;
189 			break;
190 		}
191 	}
192 	free(optbuf);
193 	return (ret);
194 }
195 
196 static void
restart_mountd(void)197 restart_mountd(void)
198 {
199 
200 	pidfile_signal(_PATH_MOUNTDPID, SIGHUP, NULL);
201 }
202 
203 int
main(int argc,char * argv[])204 main(int argc, char *argv[])
205 {
206 	const char *mntfromname, **vfslist, *vfstype;
207 	struct fstab *fs;
208 	struct statfs *mntbuf;
209 	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
210 	int onlylate;
211 	char *cp, *ep, *options;
212 
213 	all = init_flags = late = onlylate = 0;
214 	ro = 0;
215 	options = NULL;
216 	vfslist = NULL;
217 	vfstype = "ufs";
218 
219 	argc = xo_parse_args(argc, argv);
220 	if (argc < 0)
221 		exit(EXIT_FAILURE);
222 	xo_open_container("mount");
223 
224 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
225 		switch (ch) {
226 		case 'a':
227 			all = 1;
228 			break;
229 		case 'd':
230 			debug = 1;
231 			break;
232 		case 'F':
233 			setfstab(optarg);
234 			break;
235 		case 'f':
236 			init_flags |= MNT_FORCE;
237 			break;
238 		case 'L':
239 			onlylate = 1;
240 			late = 1;
241 			break;
242 		case 'l':
243 			late = 1;
244 			break;
245 		case 'n':
246 			/* For compatibility with the Linux version of mount. */
247 			break;
248 		case 'o':
249 			if (*optarg) {
250 				options = catopt(options, optarg);
251 				if (specified_ro(optarg))
252 					ro = 1;
253 			}
254 			break;
255 		case 'p':
256 			fstab_style = 1;
257 			verbose = 1;
258 			break;
259 		case 'r':
260 			options = catopt(options, "ro");
261 			ro = 1;
262 			break;
263 		case 't':
264 			if (vfslist != NULL)
265 				xo_errx(1, "only one -t option may be specified");
266 			vfslist = makevfslist(optarg);
267 			vfstype = optarg;
268 			break;
269 		case 'u':
270 			init_flags |= MNT_UPDATE;
271 			break;
272 		case 'v':
273 			verbose = 1;
274 			break;
275 		case 'w':
276 			options = catopt(options, "noro");
277 			break;
278 		case '?':
279 		default:
280 			usage();
281 			/* NOTREACHED */
282 		}
283 	argc -= optind;
284 	argv += optind;
285 
286 #define	BADTYPE(type)							\
287 	(strcmp(type, FSTAB_RO) &&					\
288 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
289 
290 	if ((init_flags & MNT_UPDATE) && (ro == 0))
291 		options = catopt(options, "noro");
292 
293 	rval = EXIT_SUCCESS;
294 	switch (argc) {
295 	case 0:
296 		if ((mntsize = getmntinfo(&mntbuf,
297 		     verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
298 			xo_err(1, "getmntinfo");
299 		if (all) {
300 			while ((fs = getfsent()) != NULL) {
301 				if (BADTYPE(fs->fs_type))
302 					continue;
303 				if (checkvfsname(fs->fs_vfstype, vfslist))
304 					continue;
305 				if (hasopt(fs->fs_mntops, "noauto"))
306 					continue;
307 				if (!hasopt(fs->fs_mntops, "late") && onlylate)
308 					continue;
309 				if (hasopt(fs->fs_mntops, "late") && !late)
310 					continue;
311 				if (hasopt(fs->fs_mntops, "failok"))
312 					failok = 1;
313 				else
314 					failok = 0;
315 				if (!(init_flags & MNT_UPDATE) &&
316 				    !hasopt(fs->fs_mntops, "update") &&
317 				    ismounted(fs, mntbuf, mntsize))
318 					continue;
319 				options = update_options(options, fs->fs_mntops,
320 				    mntbuf->f_flags);
321 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
322 				    fs->fs_file, init_flags, options,
323 				    fs->fs_mntops) && !failok)
324 					rval = EXIT_FAILURE;
325 			}
326 		} else if (fstab_style) {
327 			xo_open_list("fstab");
328 			for (i = 0; i < mntsize; i++) {
329 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
330 					continue;
331 				xo_open_instance("fstab");
332 				putfsent(&mntbuf[i]);
333 				xo_close_instance("fstab");
334 			}
335 			xo_close_list("fstab");
336 		} else {
337 			xo_open_list("mounted");
338 			for (i = 0; i < mntsize; i++) {
339 				if (checkvfsname(mntbuf[i].f_fstypename,
340 				    vfslist))
341 					continue;
342 				if (!verbose &&
343 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
344 					continue;
345 				xo_open_instance("mounted");
346 				prmount(&mntbuf[i]);
347 				xo_close_instance("mounted");
348 			}
349 			xo_close_list("mounted");
350 		}
351 		EXIT(rval);
352 	case 1:
353 		if (vfslist != NULL)
354 			usage();
355 
356 		rmslashes(*argv, *argv);
357 		if (init_flags & MNT_UPDATE) {
358 			mntfromname = NULL;
359 			have_fstab = 0;
360 			if ((mntbuf = getmntpoint(*argv)) == NULL)
361 				xo_errx(1, "not currently mounted %s", *argv);
362 			/*
363 			 * Only get the mntflags from fstab if both mntpoint
364 			 * and mntspec are identical. Also handle the special
365 			 * case where just '/' is mounted and 'spec' is not
366 			 * identical with the one from fstab ('/dev' is missing
367 			 * in the spec-string at boot-time).
368 			 */
369 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
370 				if (strcmp(fs->fs_spec,
371 				    mntbuf->f_mntfromname) == 0 &&
372 				    strcmp(fs->fs_file,
373 				    mntbuf->f_mntonname) == 0) {
374 					have_fstab = 1;
375 					mntfromname = mntbuf->f_mntfromname;
376 				} else if (argv[0][0] == '/' &&
377 				    argv[0][1] == '\0' &&
378 				    strcmp(fs->fs_vfstype,
379 				    mntbuf->f_fstypename) == 0) {
380 					fs = getfsfile("/");
381 					have_fstab = 1;
382 					mntfromname = fs->fs_spec;
383 				}
384 			}
385 			if (have_fstab) {
386 				options = update_options(options, fs->fs_mntops,
387 				    mntbuf->f_flags);
388 			} else {
389 				mntfromname = mntbuf->f_mntfromname;
390 				options = update_options(options, NULL,
391 				    mntbuf->f_flags);
392 			}
393 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
394 			    mntbuf->f_mntonname, init_flags, options, 0);
395 			break;
396 		}
397 		if ((fs = getfsfile(*argv)) == NULL &&
398 		    (fs = getfsspec(*argv)) == NULL)
399 			xo_errx(1, "%s: unknown special file or file system",
400 			    *argv);
401 		if (BADTYPE(fs->fs_type))
402 			xo_errx(1, "%s has unknown file system type",
403 			    *argv);
404 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
405 		    init_flags, options, fs->fs_mntops);
406 		break;
407 	case 2:
408 		/*
409 		 * If -t flag has not been specified, the path cannot be
410 		 * found, spec contains either a ':' or a '@', then assume
411 		 * that an NFS file system is being specified ala Sun.
412 		 * Check if the hostname contains only allowed characters
413 		 * to reduce false positives.  IPv6 addresses containing
414 		 * ':' will be correctly parsed only if the separator is '@'.
415 		 * The definition of a valid hostname is taken from RFC 1034.
416 		 */
417 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
418 		    (ep = strchr(argv[0], ':')) != NULL)) {
419 			if (*ep == '@') {
420 				cp = ep + 1;
421 				ep = cp + strlen(cp);
422 			} else
423 				cp = argv[0];
424 			while (cp != ep) {
425 				if (!isdigit(*cp) && !isalpha(*cp) &&
426 				    *cp != '.' && *cp != '-' && *cp != ':')
427 					break;
428 				cp++;
429 			}
430 			if (cp == ep)
431 				vfstype = "nfs";
432 		}
433 		rval = mountfs(vfstype,
434 		    argv[0], argv[1], init_flags, options, NULL);
435 		break;
436 	default:
437 		usage();
438 		/* NOTREACHED */
439 	}
440 
441 	/*
442 	 * If the mount was successfully, and done by root, tell mountd the
443 	 * good news.
444 	 */
445 	if (rval == EXIT_SUCCESS && getuid() == 0)
446 		restart_mountd();
447 
448 	EXIT(rval);
449 }
450 
451 int
ismounted(struct fstab * fs,struct statfs * mntbuf,int mntsize)452 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
453 {
454 	char realfsfile[PATH_MAX];
455 	int i;
456 
457 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
458 		/* the root file system can always be remounted */
459 		return (0);
460 
461 	/* The user may have specified a symlink in fstab, resolve the path */
462 	if (realpath(fs->fs_file, realfsfile) == NULL) {
463 		/* Cannot resolve the path, use original one */
464 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
465 	}
466 
467 	/*
468 	 * Consider the filesystem to be mounted if:
469 	 * It has the same mountpoint as a mounted filesystem, and
470 	 * It has the same type as that same mounted filesystem, and
471 	 * It has the same device name as that same mounted filesystem, OR
472 	 *     It is a nonremountable filesystem
473 	 */
474 	for (i = mntsize - 1; i >= 0; --i)
475 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
476 		    strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
477 		    (!isremountable(fs->fs_vfstype) ||
478 		     (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
479 			return (1);
480 	return (0);
481 }
482 
483 int
isremountable(const char * vfsname)484 isremountable(const char *vfsname)
485 {
486 	const char **cp;
487 
488 	for (cp = remountable_fs_names; *cp; cp++)
489 		if (strcmp(*cp, vfsname) == 0)
490 			return (1);
491 	return (0);
492 }
493 
494 int
allow_file_mount(const char * vfsname)495 allow_file_mount(const char *vfsname)
496 {
497 
498 	if (strcmp(vfsname, "nullfs") == 0)
499 		return (1);
500 	return (0);
501 }
502 
503 int
hasopt(const char * mntopts,const char * option)504 hasopt(const char *mntopts, const char *option)
505 {
506 	int negative, found;
507 	char *opt, *optbuf;
508 
509 	if (option[0] == 'n' && option[1] == 'o') {
510 		negative = 1;
511 		option += 2;
512 	} else
513 		negative = 0;
514 	optbuf = strdup(mntopts);
515 	found = 0;
516 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
517 		if (opt[0] == 'n' && opt[1] == 'o') {
518 			if (!strcasecmp(opt + 2, option))
519 				found = negative;
520 		} else if (!strcasecmp(opt, option))
521 			found = !negative;
522 	}
523 	free(optbuf);
524 	return (found);
525 }
526 
527 static void
append_arg(struct cpa * sa,char * arg)528 append_arg(struct cpa *sa, char *arg)
529 {
530 	if (sa->c + 1 == sa->sz) {
531 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
532 		sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
533 		if (sa->a == NULL)
534 			xo_errx(1, "realloc failed");
535 	}
536 	sa->a[++sa->c] = arg;
537 }
538 
539 int
mountfs(const char * vfstype,const char * spec,const char * name,int flags,const char * options,const char * mntopts)540 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
541 	const char *options, const char *mntopts)
542 {
543 	struct statfs sf;
544 	int i, ret;
545 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
546 	static struct cpa mnt_argv;
547 
548 	/* resolve the mountpoint with realpath(3) */
549 	if (allow_file_mount(vfstype)) {
550 		if (checkpath_allow_file(name, mntpath) != 0) {
551 			xo_warn("%s", mntpath);
552 			return (1);
553 		}
554 	} else {
555 		if (checkpath(name, mntpath) != 0) {
556 			xo_warn("%s", mntpath);
557 			return (1);
558 		}
559 	}
560 	name = mntpath;
561 
562 	if (mntopts == NULL)
563 		mntopts = "";
564 	optbuf = catopt(strdup(mntopts), options);
565 
566 	if (strcmp(name, "/") == 0)
567 		flags |= MNT_UPDATE;
568 	if (flags & MNT_FORCE)
569 		optbuf = catopt(optbuf, "force");
570 	if (flags & MNT_RDONLY)
571 		optbuf = catopt(optbuf, "ro");
572 	/*
573 	 * XXX
574 	 * The mount_mfs (newfs) command uses -o to select the
575 	 * optimization mode.  We don't pass the default "-o rw"
576 	 * for that reason.
577 	 */
578 	if (flags & MNT_UPDATE)
579 		optbuf = catopt(optbuf, "update");
580 
581 	/* Compatibility glue. */
582 	if (strcmp(vfstype, "msdos") == 0)
583 		vfstype = "msdosfs";
584 
585 	/* Construct the name of the appropriate mount command */
586 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
587 
588 	mnt_argv.c = -1;
589 	append_arg(&mnt_argv, execname);
590 	mangle(optbuf, &mnt_argv);
591 	if (mountprog != NULL)
592 		strlcpy(execname, mountprog, sizeof(execname));
593 
594 	append_arg(&mnt_argv, strdup(spec));
595 	append_arg(&mnt_argv, strdup(name));
596 	append_arg(&mnt_argv, NULL);
597 
598 	if (debug) {
599 		if (use_mountprog(vfstype))
600 			xo_emit("{Lwc:exec}{:execname/%s}", execname);
601 		else
602 			xo_emit("{:execname/mount}{P: }{l:opts/-t}{P: }{l:opts/%s}", vfstype);
603 		for (i = 1; i < mnt_argv.c; i++)
604 			xo_emit("{P: }{l:opts}", mnt_argv.a[i]);
605 		xo_emit("\n");
606 		free(optbuf);
607 		free(mountprog);
608 		mountprog = NULL;
609 		return (0);
610 	}
611 
612 	if (use_mountprog(vfstype)) {
613 		ret = exec_mountprog(name, execname, mnt_argv.a);
614 	} else {
615 		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
616 	}
617 
618 	free(optbuf);
619 	free(mountprog);
620 	mountprog = NULL;
621 
622 	if (verbose) {
623 		if (statfs(name, &sf) < 0) {
624 			xo_warn("statfs %s", name);
625 			return (1);
626 		}
627 		if (fstab_style) {
628 			xo_open_list("fstab");
629 			xo_open_instance("fstab");
630 			putfsent(&sf);
631 			xo_close_instance("fstab");
632 			xo_close_list("fstab");
633 		} else {
634 			xo_open_list("mounted");
635 			xo_open_instance("mounted");
636 			prmount(&sf);
637 			xo_close_instance("mounted");
638 			xo_close_list("mounted");
639 		}
640 	}
641 
642 	return (ret);
643 }
644 
645 void
prmount(struct statfs * sfp)646 prmount(struct statfs *sfp)
647 {
648 	uint64_t flags;
649 	unsigned int i;
650 	struct mntoptnames *o;
651 	struct passwd *pw;
652 	char *fsidbuf;
653 
654 	xo_emit("{:special/%hs}{L: on }{:node/%hs}{L: (}{:fstype}", sfp->f_mntfromname,
655 	    sfp->f_mntonname, sfp->f_fstypename);
656 
657 	flags = sfp->f_flags & MNT_VISFLAGMASK;
658 	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
659 		if (flags & o->o_opt) {
660 			xo_emit("{D:, }{l:opts}", o->o_name);
661 			flags &= ~o->o_opt;
662 		}
663 	/*
664 	 * Inform when file system is mounted by an unprivileged user
665 	 * or privileged non-root user.
666 	 */
667 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
668 		xo_emit("{D:, }{L:mounted by }");
669 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
670 			xo_emit("{:mounter/%hs}", pw->pw_name);
671 		else
672 			xo_emit("{:mounter/%hs}", sfp->f_owner);
673 	}
674 	if (verbose) {
675 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) {
676 			xo_open_container("writes");
677 			xo_emit("{D:, }{Lwc:writes}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
678 			    (uintmax_t)sfp->f_syncwrites,
679 			    (uintmax_t)sfp->f_asyncwrites);
680 			xo_close_container("writes");
681 		}
682 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) {
683 			xo_open_container("reads");
684 			xo_emit("{D:, }{Lwc:reads}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
685 			    (uintmax_t)sfp->f_syncreads,
686 			    (uintmax_t)sfp->f_asyncreads);
687 			xo_close_container("reads");
688 		}
689 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
690 			fsidbuf = malloc(sizeof(sfp->f_fsid) * 2 + 1);
691 			if (fsidbuf == NULL)
692 				xo_errx(1, "malloc failed");
693 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
694 				sprintf(&fsidbuf[i * 2], "%02x",
695 				    ((u_char *)&sfp->f_fsid)[i]);
696 			fsidbuf[i * 2] = '\0';
697 			xo_emit("{D:, }{Lw:fsid}{:fsid}", fsidbuf);
698 			free(fsidbuf);
699 		}
700 		if (sfp->f_nvnodelistsize != 0) {
701 			xo_open_container("vnodes");
702 			xo_emit("{D:, }{Lwc:vnodes}{Lw:count}{w:count/%ju}",
703 			    (uintmax_t)sfp->f_nvnodelistsize);
704 			xo_close_container("vnodes");
705 		}
706 	}
707 	xo_emit("{D:)}\n");
708 }
709 
710 char *
catopt(char * s0,const char * s1)711 catopt(char *s0, const char *s1)
712 {
713 	char *cp;
714 
715 	if (s1 == NULL || *s1 == '\0')
716 		return (s0);
717 
718 	if (s0 && *s0) {
719 		if (asprintf(&cp, "%s,%s", s0, s1) == -1)
720 			xo_errx(1, "asprintf failed");
721 	} else
722 		cp = strdup(s1);
723 
724 	if (s0)
725 		free(s0);
726 	return (cp);
727 }
728 
729 void
mangle(char * options,struct cpa * a)730 mangle(char *options, struct cpa *a)
731 {
732 	char *p, *s, *val;
733 
734 	for (s = options; (p = strsep(&s, ",")) != NULL;)
735 		if (*p != '\0') {
736 			if (strcmp(p, "noauto") == 0) {
737 				/*
738 				 * Do not pass noauto option to nmount().
739 				 * or external mount program.  noauto is
740 				 * only used to prevent mounting a filesystem
741 				 * when 'mount -a' is specified, and is
742 				 * not a real mount option.
743 				 */
744 				continue;
745 			} else if (strcmp(p, "late") == 0) {
746 				/*
747 				 * "late" is used to prevent certain file
748 				 * systems from being mounted before late
749 				 * in the boot cycle; for instance,
750 				 * loopback NFS mounts can't be mounted
751 				 * before mountd starts.
752 				 */
753 				continue;
754 			} else if (strcmp(p, "failok") == 0) {
755 				/*
756 				 * "failok" is used to prevent certain file
757 				 * systems from being causing the system to
758 				 * drop into single user mode in the boot
759 				 * cycle, and is not a real mount option.
760 				 */
761 				continue;
762 			} else if (strncmp(p, "mountprog", 9) == 0) {
763 				/*
764 				 * "mountprog" is used to force the use of
765 				 * userland mount programs.
766 				 */
767 				val = strchr(p, '=');
768                         	if (val != NULL) {
769                                 	++val;
770 					if (*val != '\0')
771 						mountprog = strdup(val);
772 				}
773 
774 				if (mountprog == NULL) {
775 					xo_errx(1, "Need value for -o mountprog");
776 				}
777 				continue;
778 			} else if (strcmp(p, "userquota") == 0) {
779 				continue;
780 			} else if (strncmp(p, userquotaeq,
781 			    sizeof(userquotaeq) - 1) == 0) {
782 				continue;
783 			} else if (strcmp(p, "groupquota") == 0) {
784 				continue;
785 			} else if (strncmp(p, groupquotaeq,
786 			    sizeof(groupquotaeq) - 1) == 0) {
787 				continue;
788 			} else if (*p == '-') {
789 				append_arg(a, p);
790 				p = strchr(p, '=');
791 				if (p != NULL) {
792 					*p = '\0';
793 					append_arg(a, p + 1);
794 				}
795 			} else {
796 				append_arg(a, strdup("-o"));
797 				append_arg(a, p);
798 			}
799 		}
800 }
801 
802 
803 char *
update_options(char * opts,char * fstab,int curflags)804 update_options(char *opts, char *fstab, int curflags)
805 {
806 	char *o, *p;
807 	char *cur;
808 	char *expopt, *newopt, *tmpopt;
809 
810 	if (opts == NULL)
811 		return (strdup(""));
812 
813 	/* remove meta options from list */
814 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
815 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
816 	cur = flags2opts(curflags);
817 
818 	/*
819 	 * Expand all meta-options passed to us first.
820 	 */
821 	expopt = NULL;
822 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
823 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
824 			expopt = catopt(expopt, fstab);
825 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
826 			expopt = catopt(expopt, cur);
827 		else
828 			expopt = catopt(expopt, o);
829 	}
830 	free(cur);
831 	free(opts);
832 
833 	/*
834 	 * Remove previous contradictory arguments. Given option "foo" we
835 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
836 	 * and "foo" - so we can deal with possible options like "notice".
837 	 */
838 	newopt = NULL;
839 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
840 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
841 			xo_errx(1, "malloc failed");
842 
843 		strcpy(tmpopt, "no");
844 		strcat(tmpopt, o);
845 		remopt(newopt, tmpopt);
846 		free(tmpopt);
847 
848 		if (strncmp("no", o, 2) == 0)
849 			remopt(newopt, o+2);
850 
851 		newopt = catopt(newopt, o);
852 	}
853 	free(expopt);
854 
855 	return (newopt);
856 }
857 
858 void
remopt(char * string,const char * opt)859 remopt(char *string, const char *opt)
860 {
861 	char *o, *p, *r;
862 
863 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
864 		return;
865 
866 	r = string;
867 
868 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
869 		if (strcmp(opt, o) != 0) {
870 			if (*r == ',' && *o != '\0')
871 				r++;
872 			while ((*r++ = *o++) != '\0')
873 			    ;
874 			*--r = ',';
875 		}
876 	}
877 	*r = '\0';
878 }
879 
880 void
usage(void)881 usage(void)
882 {
883 
884 	xo_error("%s\n%s\n%s\n",
885 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
886 "       mount [-dfpruvw] special | node",
887 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
888 	EXIT(EXIT_FAILURE);
889 }
890 
891 void
putfsent(struct statfs * ent)892 putfsent(struct statfs *ent)
893 {
894 	struct fstab *fst;
895 	char *opts, *rw;
896 	int l;
897 
898 	opts = NULL;
899 	/* flags2opts() doesn't return the "rw" option. */
900 	if ((ent->f_flags & MNT_RDONLY) != 0)
901 		rw = NULL;
902 	else
903 		rw = catopt(NULL, "rw");
904 
905 	opts = flags2opts(ent->f_flags);
906 	opts = catopt(rw, opts);
907 
908 	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
909 	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
910 		strlcpy(ent->f_mntfromname,
911 		    (strnstr(ent->f_mntfromname, ":", 8) +1),
912 		    sizeof(ent->f_mntfromname));
913 	}
914 
915 	l = strlen(ent->f_mntfromname);
916 	xo_emit("{:device}{P:/%s}{P:/%s}{P:/%s}",
917 	    ent->f_mntfromname,
918 	    l < 8 ? "\t" : "",
919 	    l < 16 ? "\t" : "",
920 	    l < 24 ? "\t" : " ");
921 	l = strlen(ent->f_mntonname);
922 	xo_emit("{:mntpoint}{P:/%s}{P:/%s}{P:/%s}",
923 	    ent->f_mntonname,
924 	    l < 8 ? "\t" : "",
925 	    l < 16 ? "\t" : "",
926 	    l < 24 ? "\t" : " ");
927 	xo_emit("{:fstype}{P:\t}", ent->f_fstypename);
928 	l = strlen(opts);
929 	xo_emit("{:opts}{P:/%s}", opts,
930 	    l < 8 ? "\t" : " ");
931 	free(opts);
932 
933 	if ((fst = getfsspec(ent->f_mntfromname)))
934 		xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
935 		    fst->fs_freq, fst->fs_passno);
936 	else if ((fst = getfsfile(ent->f_mntonname)))
937 		xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
938 		    fst->fs_freq, fst->fs_passno);
939 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
940 		if (strcmp(ent->f_mntonname, "/") == 0)
941 			xo_emit("{P:\t}{n:dump/1}{P: }{n:pass/1}\n");
942 		else
943 			xo_emit("{P:\t}{n:dump/2}{P: }{n:pass/2}\n");
944 	} else
945 		xo_emit("{P:\t}{n:dump/0}{P: }{n:pass/0}\n");
946 }
947 
948 
949 char *
flags2opts(int flags)950 flags2opts(int flags)
951 {
952 	char *res;
953 
954 	res = NULL;
955 
956 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
957 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
958 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
959 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
960 	if (flags & MNT_UNION)		res = catopt(res, "union");
961 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
962 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
963 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
964 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
965 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
966 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
967 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
968 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
969 	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
970 	if (flags & MNT_UNTRUSTED)	res = catopt(res, "untrusted");
971 	if (flags & MNT_NOCOVER)	res = catopt(res, "nocover");
972 	if (flags & MNT_EMPTYDIR)	res = catopt(res, "emptydir");
973 
974 	return (res);
975 }
976