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