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