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