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