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