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