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