xref: /freebsd/sbin/mount/mount.c (revision e4e9813eb92cd7c4d4b819a8fbed5cbd3d92f5d8)
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 #endif /* not lint */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
39 #endif
40 static const char rcsid[] =
41   "$FreeBSD$";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48 
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fstab.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include "extern.h"
63 #include "mntopts.h"
64 #include "pathnames.h"
65 
66 /* `meta' options */
67 #define MOUNT_META_OPTION_FSTAB		"fstab"
68 #define MOUNT_META_OPTION_CURRENT	"current"
69 
70 int debug, fstab_style, verbose;
71 
72 char   *catopt(char *, const char *);
73 struct statfs *getmntpt(const char *);
74 int	hasopt(const char *, const char *);
75 int	ismounted(struct fstab *, struct statfs *, int);
76 int	isremountable(const char *);
77 void	mangle(char *, int *, char *[]);
78 char   *update_options(char *, char *, int);
79 int	mountfs(const char *, const char *, const char *,
80 			int, const char *, const char *);
81 void	remopt(char *, const char *);
82 void	prmount(struct statfs *);
83 void	putfsent(const struct statfs *);
84 void	usage(void);
85 char   *flags2opts(int);
86 
87 /* Map from mount options to printable formats. */
88 static struct opt {
89 	int o_opt;
90 	const char *o_name;
91 } optnames[] = {
92 	{ MNT_ASYNC,		"asynchronous" },
93 	{ MNT_EXPORTED,		"NFS exported" },
94 	{ MNT_LOCAL,		"local" },
95 	{ MNT_NOATIME,		"noatime" },
96 	{ MNT_NOEXEC,		"noexec" },
97 	{ MNT_NOSUID,		"nosuid" },
98 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
99 	{ MNT_QUOTA,		"with quotas" },
100 	{ MNT_RDONLY,		"read-only" },
101 	{ MNT_SYNCHRONOUS,	"synchronous" },
102 	{ MNT_UNION,		"union" },
103 	{ MNT_NOCLUSTERR,	"noclusterr" },
104 	{ MNT_NOCLUSTERW,	"noclusterw" },
105 	{ MNT_SUIDDIR,		"suiddir" },
106 	{ MNT_SOFTDEP,		"soft-updates" },
107 	{ MNT_MULTILABEL,	"multilabel" },
108 	{ MNT_ACLS,		"acls" },
109 	{ 0, NULL }
110 };
111 
112 /*
113  * List of VFS types that can be remounted without becoming mounted on top
114  * of each other.
115  * XXX Is this list correct?
116  */
117 static const char *
118 remountable_fs_names[] = {
119 	"ufs", "ffs", "ext2fs",
120 	0
121 };
122 
123 static const char userquotaeq[] = "userquota=";
124 static const char groupquotaeq[] = "groupquota=";
125 
126 static int
127 use_mountprog(const char *vfstype)
128 {
129 	/* XXX: We need to get away from implementing external mount
130 	 *      programs for every filesystem, and move towards having
131 	 *	each filesystem properly implement the nmount() system call.
132 	 */
133 	unsigned int i;
134 	const char *fs[] = {
135 	"cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
136 	"nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
137 	"unionfs",
138 	NULL
139 	};
140 
141 	for (i = 0; fs[i] != NULL; ++i) {
142 		if (strcmp(vfstype, fs[i]) == 0)
143 			return (1);
144 	}
145 
146 	return (0);
147 }
148 
149 static int
150 exec_mountprog(const char *name, const char *execname, char *const argv[])
151 {
152 	pid_t pid;
153 	int status;
154 
155 	switch (pid = fork()) {
156 	case -1:				/* Error. */
157 		warn("fork");
158 		exit (1);
159 	case 0:					/* Child. */
160 		/* Go find an executable. */
161 		execvP(execname, _PATH_SYSPATH, argv);
162 		if (errno == ENOENT) {
163 			warn("exec %s not found in %s", execname,
164 			    _PATH_SYSPATH);
165 		}
166 		exit(1);
167 	default:				/* Parent. */
168 		if (waitpid(pid, &status, 0) < 0) {
169 			warn("waitpid");
170 			return (1);
171 		}
172 
173 		if (WIFEXITED(status)) {
174 			if (WEXITSTATUS(status) != 0)
175 				return (WEXITSTATUS(status));
176 		} else if (WIFSIGNALED(status)) {
177 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
178 			return (1);
179 		}
180 		break;
181 	}
182 
183 	return (0);
184 }
185 
186 int
187 main(int argc, char *argv[])
188 {
189 	const char *mntfromname, **vfslist, *vfstype;
190 	struct fstab *fs;
191 	struct statfs *mntbuf;
192 	FILE *mountdfp;
193 	pid_t pid;
194 	int all, ch, i, init_flags, late, mntsize, rval, have_fstab;
195 	char *cp, *ep, *options;
196 
197 	options = strdup("noro");
198 	if (options == NULL)
199 		errx(1, "malloc failed");
200 
201 	all = init_flags = late = 0;
202 	vfslist = NULL;
203 	vfstype = "ufs";
204 	while ((ch = getopt(argc, argv, "adlF:fo:prwt:uv")) != -1)
205 		switch (ch) {
206 		case 'a':
207 			all = 1;
208 			break;
209 		case 'd':
210 			debug = 1;
211 			break;
212 		case 'F':
213 			setfstab(optarg);
214 			break;
215 		case 'f':
216 			init_flags |= MNT_FORCE;
217 			break;
218 		case 'l':
219 			late = 1;
220 			break;
221 		case 'o':
222 			options = catopt(options, optarg);
223 			break;
224 		case 'p':
225 			fstab_style = 1;
226 			verbose = 1;
227 			break;
228 		case 'r':
229 			options = catopt(options, "ro");
230 			break;
231 		case 't':
232 			if (vfslist != NULL)
233 				errx(1, "only one -t option may be specified");
234 			vfslist = makevfslist(optarg);
235 			vfstype = optarg;
236 			break;
237 		case 'u':
238 			init_flags |= MNT_UPDATE;
239 			break;
240 		case 'v':
241 			verbose = 1;
242 			break;
243 		case 'w':
244 			options = catopt(options, "noro");
245 			break;
246 		case '?':
247 		default:
248 			usage();
249 			/* NOTREACHED */
250 		}
251 	argc -= optind;
252 	argv += optind;
253 
254 #define	BADTYPE(type)							\
255 	(strcmp(type, FSTAB_RO) &&					\
256 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
257 
258 	rval = 0;
259 	switch (argc) {
260 	case 0:
261 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
262 			err(1, "getmntinfo");
263 		if (all) {
264 			while ((fs = getfsent()) != NULL) {
265 				if (BADTYPE(fs->fs_type))
266 					continue;
267 				if (checkvfsname(fs->fs_vfstype, vfslist))
268 					continue;
269 				if (hasopt(fs->fs_mntops, "noauto"))
270 					continue;
271 				if (hasopt(fs->fs_mntops, "late") && !late)
272 					continue;
273 				if (!(init_flags & MNT_UPDATE) &&
274 				    ismounted(fs, mntbuf, mntsize))
275 					continue;
276 				options = update_options(options, fs->fs_mntops,
277 				    mntbuf->f_flags);
278 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
279 				    fs->fs_file, init_flags, options,
280 				    fs->fs_mntops))
281 					rval = 1;
282 			}
283 		} else if (fstab_style) {
284 			for (i = 0; i < mntsize; i++) {
285 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
286 					continue;
287 				putfsent(&mntbuf[i]);
288 			}
289 		} else {
290 			for (i = 0; i < mntsize; i++) {
291 				if (checkvfsname(mntbuf[i].f_fstypename,
292 				    vfslist))
293 					continue;
294 				prmount(&mntbuf[i]);
295 			}
296 		}
297 		exit(rval);
298 	case 1:
299 		if (vfslist != NULL)
300 			usage();
301 
302 		rmslashes(*argv, *argv);
303 		if (init_flags & MNT_UPDATE) {
304 			mntfromname = NULL;
305 			have_fstab = 0;
306 			if ((mntbuf = getmntpt(*argv)) == NULL)
307 				errx(1, "not currently mounted %s", *argv);
308 			/*
309 			 * Only get the mntflags from fstab if both mntpoint
310 			 * and mntspec are identical. Also handle the special
311 			 * case where just '/' is mounted and 'spec' is not
312 			 * identical with the one from fstab ('/dev' is missing
313 			 * in the spec-string at boot-time).
314 			 */
315 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
316 				if (strcmp(fs->fs_spec,
317 				    mntbuf->f_mntfromname) == 0 &&
318 				    strcmp(fs->fs_file,
319 				    mntbuf->f_mntonname) == 0) {
320 					have_fstab = 1;
321 					mntfromname = mntbuf->f_mntfromname;
322 				} else if (argv[0][0] == '/' &&
323 				    argv[0][1] == '\0') {
324 					fs = getfsfile("/");
325 					have_fstab = 1;
326 					mntfromname = fs->fs_spec;
327 				}
328 			}
329 			if (have_fstab) {
330 				options = update_options(options, fs->fs_mntops,
331 				    mntbuf->f_flags);
332 			} else {
333 				mntfromname = mntbuf->f_mntfromname;
334 				options = update_options(options, NULL,
335 				    mntbuf->f_flags);
336 			}
337 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
338 			    mntbuf->f_mntonname, init_flags, options, 0);
339 			break;
340 		}
341 		if ((fs = getfsfile(*argv)) == NULL &&
342 		    (fs = getfsspec(*argv)) == NULL)
343 			errx(1, "%s: unknown special file or file system",
344 			    *argv);
345 		if (BADTYPE(fs->fs_type))
346 			errx(1, "%s has unknown file system type",
347 			    *argv);
348 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
349 		    init_flags, options, fs->fs_mntops);
350 		break;
351 	case 2:
352 		/*
353 		 * If -t flag has not been specified, the path cannot be
354 		 * found, spec contains either a ':' or a '@', then assume
355 		 * that an NFS file system is being specified ala Sun.
356 		 * Check if the hostname contains only allowed characters
357 		 * to reduce false positives.  IPv6 addresses containing
358 		 * ':' will be correctly parsed only if the separator is '@'.
359 		 * The definition of a valid hostname is taken from RFC 1034.
360 		 */
361 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
362 		    (ep = strchr(argv[0], ':')) != NULL)) {
363 			if (*ep == '@') {
364 				cp = ep + 1;
365 				ep = cp + strlen(cp);
366 			} else
367 				cp = argv[0];
368 			while (cp != ep) {
369 				if (!isdigit(*cp) && !isalpha(*cp) &&
370 				    *cp != '.' && *cp != '-' && *cp != ':')
371 					break;
372 				cp++;
373 			}
374 			if (cp == ep)
375 				vfstype = "nfs";
376 		}
377 		rval = mountfs(vfstype,
378 		    argv[0], argv[1], init_flags, options, NULL);
379 		break;
380 	default:
381 		usage();
382 		/* NOTREACHED */
383 	}
384 
385 	/*
386 	 * If the mount was successfully, and done by root, tell mountd the
387 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
388 	 */
389 	if (rval == 0 && getuid() == 0 &&
390 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
391 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
392 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
393 			err(1, "signal mountd");
394 		(void)fclose(mountdfp);
395 	}
396 
397 	exit(rval);
398 }
399 
400 int
401 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
402 {
403 	char realfsfile[PATH_MAX];
404 	int i;
405 
406 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
407 		/* the root file system can always be remounted */
408 		return (0);
409 
410 	/* The user may have specified a symlink in fstab, resolve the path */
411 	if (realpath(fs->fs_file, realfsfile) == NULL) {
412 		/* Cannot resolve the path, use original one */
413 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
414 	}
415 
416 	for (i = mntsize - 1; i >= 0; --i)
417 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
418 		    (!isremountable(fs->fs_vfstype) ||
419 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
420 			return (1);
421 	return (0);
422 }
423 
424 int
425 isremountable(const char *vfsname)
426 {
427 	const char **cp;
428 
429 	for (cp = remountable_fs_names; *cp; cp++)
430 		if (strcmp(*cp, vfsname) == 0)
431 			return (1);
432 	return (0);
433 }
434 
435 int
436 hasopt(const char *mntopts, const char *option)
437 {
438 	int negative, found;
439 	char *opt, *optbuf;
440 
441 	if (option[0] == 'n' && option[1] == 'o') {
442 		negative = 1;
443 		option += 2;
444 	} else
445 		negative = 0;
446 	optbuf = strdup(mntopts);
447 	found = 0;
448 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
449 		if (opt[0] == 'n' && opt[1] == 'o') {
450 			if (!strcasecmp(opt + 2, option))
451 				found = negative;
452 		} else if (!strcasecmp(opt, option))
453 			found = !negative;
454 	}
455 	free(optbuf);
456 	return (found);
457 }
458 
459 int
460 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
461 	const char *options, const char *mntopts)
462 {
463 	char *argv[100];
464 	struct statfs sf;
465 	int argc, i, ret;
466 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
467 
468 	/* resolve the mountpoint with realpath(3) */
469 	(void)checkpath(name, mntpath);
470 	name = mntpath;
471 
472 	if (mntopts == NULL)
473 		mntopts = "";
474 	optbuf = catopt(strdup(mntopts), options);
475 
476 	if (strcmp(name, "/") == 0)
477 		flags |= MNT_UPDATE;
478 	if (flags & MNT_FORCE)
479 		optbuf = catopt(optbuf, "force");
480 	if (flags & MNT_RDONLY)
481 		optbuf = catopt(optbuf, "ro");
482 	/*
483 	 * XXX
484 	 * The mount_mfs (newfs) command uses -o to select the
485 	 * optimization mode.  We don't pass the default "-o rw"
486 	 * for that reason.
487 	 */
488 	if (flags & MNT_UPDATE)
489 		optbuf = catopt(optbuf, "update");
490 
491 	/* Compatibility glue. */
492 	if (strcmp(vfstype, "msdos") == 0)
493 		vfstype = "msdosfs";
494 
495 	/* Construct the name of the appropriate mount command */
496 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
497 
498 	argc = 0;
499 	argv[argc++] = execname;
500 	mangle(optbuf, &argc, argv);
501 	argv[argc++] = strdup(spec);
502 	argv[argc++] = strdup(name);
503 	argv[argc] = NULL;
504 
505 	if (debug) {
506 		(void)printf("exec: mount_%s", vfstype);
507 		for (i = 1; i < argc; i++)
508 			(void)printf(" %s", argv[i]);
509 		(void)printf("\n");
510 		return (0);
511 	}
512 
513 	if (use_mountprog(vfstype)) {
514 		ret = exec_mountprog(name, execname, argv);
515 	} else {
516 		ret = mount_fs(vfstype, argc, argv);
517 	}
518 
519 	free(optbuf);
520 
521 	if (verbose) {
522 		if (statfs(name, &sf) < 0) {
523 			warn("statfs %s", name);
524 			return (1);
525 		}
526 		if (fstab_style)
527 			putfsent(&sf);
528 		else
529 			prmount(&sf);
530 	}
531 
532 	return (0);
533 }
534 
535 void
536 prmount(struct statfs *sfp)
537 {
538 	int flags;
539 	unsigned int i;
540 	struct opt *o;
541 	struct passwd *pw;
542 
543 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
544 	    sfp->f_fstypename);
545 
546 	flags = sfp->f_flags & MNT_VISFLAGMASK;
547 	for (o = optnames; flags && o->o_opt; o++)
548 		if (flags & o->o_opt) {
549 			(void)printf(", %s", o->o_name);
550 			flags &= ~o->o_opt;
551 		}
552 	/*
553 	 * Inform when file system is mounted by an unprivileged user
554 	 * or privileged non-root user.
555 	 */
556 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
557 		(void)printf(", mounted by ");
558 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
559 			(void)printf("%s", pw->pw_name);
560 		else
561 			(void)printf("%d", sfp->f_owner);
562 	}
563 	if (verbose) {
564 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
565 			(void)printf(", writes: sync %ju async %ju",
566 			    (uintmax_t)sfp->f_syncwrites,
567 			    (uintmax_t)sfp->f_asyncwrites);
568 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
569 			(void)printf(", reads: sync %ju async %ju",
570 			    (uintmax_t)sfp->f_syncreads,
571 			    (uintmax_t)sfp->f_asyncreads);
572 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
573 			printf(", fsid ");
574 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
575 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
576 		}
577 	}
578 	(void)printf(")\n");
579 }
580 
581 struct statfs *
582 getmntpt(const char *name)
583 {
584 	struct statfs *mntbuf;
585 	int i, mntsize;
586 
587 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
588 	for (i = mntsize - 1; i >= 0; i--) {
589 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
590 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
591 			return (&mntbuf[i]);
592 	}
593 	return (NULL);
594 }
595 
596 char *
597 catopt(char *s0, const char *s1)
598 {
599 	size_t i;
600 	char *cp;
601 
602 	if (s1 == NULL || *s1 == '\0')
603 		return (s0);
604 
605 	if (s0 && *s0) {
606 		i = strlen(s0) + strlen(s1) + 1 + 1;
607 		if ((cp = malloc(i)) == NULL)
608 			errx(1, "malloc failed");
609 		(void)snprintf(cp, i, "%s,%s", s0, s1);
610 	} else
611 		cp = strdup(s1);
612 
613 	if (s0)
614 		free(s0);
615 	return (cp);
616 }
617 
618 void
619 mangle(char *options, int *argcp, char *argv[])
620 {
621 	char *p, *s;
622 	int argc;
623 
624 	argc = *argcp;
625 	for (s = options; (p = strsep(&s, ",")) != NULL;)
626 		if (*p != '\0') {
627 			if (strcmp(p, "noauto") == 0) {
628 				/*
629 				 * Do not pass noauto option to nmount().
630 				 * or external mount program.  noauto is
631 				 * only used to prevent mounting a filesystem
632 				 * when 'mount -a' is specified, and is
633 				 * not a real mount option.
634 				 */
635 				continue;
636 			} else if (strcmp(p, "late") == 0) {
637 				/*
638 				 * "late" is used to prevent certain file
639 				 * systems from being mounted before late
640 				 * in the boot cycle; for instance,
641 				 * loopback NFS mounts can't be mounted
642 				 * before mountd starts.
643 				 */
644 				continue;
645 			} else if (strcmp(p, "userquota") == 0) {
646 				continue;
647 			} else if (strncmp(p, userquotaeq,
648 			    sizeof(userquotaeq) - 1) == 0) {
649 				continue;
650 			} else if (strcmp(p, "groupquota") == 0) {
651 				continue;
652 			} else if (strncmp(p, groupquotaeq,
653 			    sizeof(groupquotaeq) - 1) == 0) {
654 				continue;
655 			} else if (*p == '-') {
656 				argv[argc++] = p;
657 				p = strchr(p, '=');
658 				if (p != NULL) {
659 					*p = '\0';
660 					argv[argc++] = p+1;
661 				}
662 			} else {
663 				argv[argc++] = strdup("-o");
664 				argv[argc++] = p;
665 			}
666 		}
667 
668 	*argcp = argc;
669 }
670 
671 
672 char *
673 update_options(char *opts, char *fstab, int curflags)
674 {
675 	char *o, *p;
676 	char *cur;
677 	char *expopt, *newopt, *tmpopt;
678 
679 	if (opts == NULL)
680 		return (strdup(""));
681 
682 	/* remove meta options from list */
683 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
684 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
685 	cur = flags2opts(curflags);
686 
687 	/*
688 	 * Expand all meta-options passed to us first.
689 	 */
690 	expopt = NULL;
691 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
692 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
693 			expopt = catopt(expopt, fstab);
694 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
695 			expopt = catopt(expopt, cur);
696 		else
697 			expopt = catopt(expopt, o);
698 	}
699 	free(cur);
700 	free(opts);
701 
702 	/*
703 	 * Remove previous contradictory arguments. Given option "foo" we
704 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
705 	 * and "foo" - so we can deal with possible options like "notice".
706 	 */
707 	newopt = NULL;
708 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
709 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
710 			errx(1, "malloc failed");
711 
712 		strcpy(tmpopt, "no");
713 		strcat(tmpopt, o);
714 		remopt(newopt, tmpopt);
715 		free(tmpopt);
716 
717 		if (strncmp("no", o, 2) == 0)
718 			remopt(newopt, o+2);
719 
720 		newopt = catopt(newopt, o);
721 	}
722 	free(expopt);
723 
724 	return (newopt);
725 }
726 
727 void
728 remopt(char *string, const char *opt)
729 {
730 	char *o, *p, *r;
731 
732 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
733 		return;
734 
735 	r = string;
736 
737 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
738 		if (strcmp(opt, o) != 0) {
739 			if (*r == ',' && *o != '\0')
740 				r++;
741 			while ((*r++ = *o++) != '\0')
742 			    ;
743 			*--r = ',';
744 		}
745 	}
746 	*r = '\0';
747 }
748 
749 void
750 usage(void)
751 {
752 
753 	(void)fprintf(stderr, "%s\n%s\n%s\n",
754 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
755 "       mount [-dfpruvw] special | node",
756 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
757 	exit(1);
758 }
759 
760 void
761 putfsent(const struct statfs *ent)
762 {
763 	struct fstab *fst;
764 	char *opts;
765 
766 	opts = flags2opts(ent->f_flags);
767 
768 	/*
769 	 * "rw" is not a real mount option; this is why we print NULL as "rw"
770 	 * if opts is still NULL here.
771 	 */
772 	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
773 	    ent->f_fstypename, opts ? opts : "rw");
774 	free(opts);
775 
776 	if ((fst = getfsspec(ent->f_mntfromname)))
777 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
778 	else if ((fst = getfsfile(ent->f_mntonname)))
779 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
780 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
781 		if (strcmp(ent->f_mntonname, "/") == 0)
782 			printf("\t1 1\n");
783 		else
784 			printf("\t2 2\n");
785 	} else
786 		printf("\t0 0\n");
787 }
788 
789 
790 char *
791 flags2opts(int flags)
792 {
793 	char *res;
794 
795 	res = NULL;
796 
797 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
798 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
799 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
800 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
801 	if (flags & MNT_UNION)		res = catopt(res, "union");
802 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
803 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
804 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
805 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
806 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
807 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
808 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
809 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
810 
811 	return (res);
812 }
813