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