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