xref: /dragonfly/sbin/mount/mount.c (revision 984263bc)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/stat.h>
51 #include <sys/wait.h>
52 
53 #include <err.h>
54 #include <errno.h>
55 #include <fstab.h>
56 #include <pwd.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 #include "extern.h"
64 #include "mntopts.h"
65 #include "pathnames.h"
66 
67 /* `meta' options */
68 #define MOUNT_META_OPTION_FSTAB		"fstab"
69 #define MOUNT_META_OPTION_CURRENT	"current"
70 
71 int debug, fstab_style, verbose;
72 
73 char   *catopt __P((char *, const char *));
74 struct statfs
75        *getmntpt __P((const char *));
76 int	hasopt __P((const char *, const char *));
77 int	ismounted __P((struct fstab *, struct statfs *, int));
78 int	isremountable __P((const char *));
79 void	mangle __P((char *, int *, const char **));
80 char   *update_options __P((char *, char *, int));
81 int	mountfs __P((const char *, const char *, const char *,
82 			int, const char *, const char *));
83 void	remopt __P((char *, const char *));
84 void	prmount __P((struct statfs *));
85 void	putfsent __P((const struct statfs *));
86 void	usage __P((void));
87 char   *flags2opts __P((int));
88 
89 /* Map from mount options to printable formats. */
90 static struct opt {
91 	int o_opt;
92 	const char *o_name;
93 } optnames[] = {
94 	{ MNT_ASYNC,		"asynchronous" },
95 	{ MNT_EXPORTED,		"NFS exported" },
96 	{ MNT_LOCAL,		"local" },
97 	{ MNT_NOATIME,		"noatime" },
98 	{ MNT_NODEV,		"nodev" },
99 	{ MNT_NOEXEC,		"noexec" },
100 	{ MNT_NOSUID,		"nosuid" },
101 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
102 	{ MNT_QUOTA,		"with quotas" },
103 	{ MNT_RDONLY,		"read-only" },
104 	{ MNT_SYNCHRONOUS,	"synchronous" },
105 	{ MNT_UNION,		"union" },
106 	{ MNT_NOCLUSTERR,	"noclusterr" },
107 	{ MNT_NOCLUSTERW,	"noclusterw" },
108 	{ MNT_SUIDDIR,		"suiddir" },
109 	{ MNT_SOFTDEP,		"soft-updates" },
110 	{ 0, NULL }
111 };
112 
113 /*
114  * List of VFS types that can be remounted without becoming mounted on top
115  * of each other.
116  * XXX Is this list correct?
117  */
118 static const char *
119 remountable_fs_names[] = {
120 	"ufs", "ffs", "ext2fs",
121 	0
122 };
123 
124 int
125 main(argc, argv)
126 	int argc;
127 	char * const argv[];
128 {
129 	const char *mntfromname, **vfslist, *vfstype;
130 	struct fstab *fs;
131 	struct statfs *mntbuf;
132 	FILE *mountdfp;
133 	pid_t pid;
134 	int all, ch, i, init_flags, mntsize, rval, have_fstab;
135 	char *options;
136 
137 	all = init_flags = 0;
138 	options = NULL;
139 	vfslist = NULL;
140 	vfstype = "ufs";
141 	while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
142 		switch (ch) {
143 		case 'a':
144 			all = 1;
145 			break;
146 		case 'd':
147 			debug = 1;
148 			break;
149 		case 'f':
150 			init_flags |= MNT_FORCE;
151 			break;
152 		case 'o':
153 			if (*optarg)
154 				options = catopt(options, optarg);
155 			break;
156 		case 'p':
157 			fstab_style = 1;
158 			verbose = 1;
159 			break;
160 		case 'r':
161 			options = catopt(options, "ro");
162 			break;
163 		case 't':
164 			if (vfslist != NULL)
165 				errx(1, "only one -t option may be specified");
166 			vfslist = makevfslist(optarg);
167 			vfstype = optarg;
168 			break;
169 		case 'u':
170 			init_flags |= MNT_UPDATE;
171 			break;
172 		case 'v':
173 			verbose = 1;
174 			break;
175 		case 'w':
176 			options = catopt(options, "noro");
177 			break;
178 		case '?':
179 		default:
180 			usage();
181 			/* NOTREACHED */
182 		}
183 	argc -= optind;
184 	argv += optind;
185 
186 #define	BADTYPE(type)							\
187 	(strcmp(type, FSTAB_RO) &&					\
188 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
189 
190 	rval = 0;
191 	switch (argc) {
192 	case 0:
193 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
194 			err(1, "getmntinfo");
195 		if (all) {
196 			while ((fs = getfsent()) != NULL) {
197 				if (BADTYPE(fs->fs_type))
198 					continue;
199 				if (checkvfsname(fs->fs_vfstype, vfslist))
200 					continue;
201 				if (hasopt(fs->fs_mntops, "noauto"))
202 					continue;
203 				if (!(init_flags & MNT_UPDATE) &&
204 				    ismounted(fs, mntbuf, mntsize))
205 					continue;
206 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
207 				    fs->fs_file, init_flags, options,
208 				    fs->fs_mntops))
209 					rval = 1;
210 			}
211 		} else if (fstab_style) {
212 			for (i = 0; i < mntsize; i++) {
213 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
214 					continue;
215 				putfsent(&mntbuf[i]);
216 			}
217 		} else {
218 			for (i = 0; i < mntsize; i++) {
219 				if (checkvfsname(mntbuf[i].f_fstypename,
220 				    vfslist))
221 					continue;
222 				prmount(&mntbuf[i]);
223 			}
224 		}
225 		exit(rval);
226 	case 1:
227 		if (vfslist != NULL)
228 			usage();
229 
230 		if (init_flags & MNT_UPDATE) {
231 			mntfromname = NULL;
232 			have_fstab = 0;
233 			if ((mntbuf = getmntpt(*argv)) == NULL)
234 				errx(1, "not currently mounted %s", *argv);
235 			/*
236 			 * Only get the mntflags from fstab if both mntpoint
237 			 * and mntspec are identical. Also handle the special
238 			 * case where just '/' is mounted and 'spec' is not
239 			 * identical with the one from fstab ('/dev' is missing
240 			 * in the spec-string at boot-time).
241 			 */
242 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
243 				if (strcmp(fs->fs_spec,
244 				    mntbuf->f_mntfromname) == 0 &&
245 				    strcmp(fs->fs_file,
246 				    mntbuf->f_mntonname) == 0) {
247 					have_fstab = 1;
248 					mntfromname = mntbuf->f_mntfromname;
249 				} else if (argv[0][0] == '/' &&
250 				    argv[0][1] == '\0') {
251 					fs = getfsfile("/");
252 					have_fstab = 1;
253 					mntfromname = fs->fs_spec;
254 				}
255 			}
256 			if (have_fstab) {
257 				options = update_options(options, fs->fs_mntops,
258 				    mntbuf->f_flags);
259 			} else {
260 				mntfromname = mntbuf->f_mntfromname;
261 				options = update_options(options, NULL,
262 				    mntbuf->f_flags);
263 			}
264 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
265 			    mntbuf->f_mntonname, init_flags, options, 0);
266 			break;
267 		}
268 		rmslashes(*argv, *argv);
269 		if ((fs = getfsfile(*argv)) == NULL &&
270 		    (fs = getfsspec(*argv)) == NULL)
271 			errx(1, "%s: unknown special file or file system",
272 			    *argv);
273 		if (BADTYPE(fs->fs_type))
274 			errx(1, "%s has unknown file system type",
275 			    *argv);
276 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
277 		    init_flags, options, fs->fs_mntops);
278 		break;
279 	case 2:
280 		/*
281 		 * If -t flag has not been specified, the path cannot be
282 		 * found, spec contains either a ':' or a '@', and the
283 		 * spec is not a file with those characters, then assume
284 		 * that an NFS filesystem is being specified ala Sun.
285 		 */
286 		if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
287 		    access(argv[0], 0) == -1)
288 			vfstype = "nfs";
289 		rval = mountfs(vfstype,
290 		    argv[0], argv[1], init_flags, options, NULL);
291 		break;
292 	default:
293 		usage();
294 		/* NOTREACHED */
295 	}
296 
297 	/*
298 	 * If the mount was successfully, and done by root, tell mountd the
299 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
300 	 */
301 	if (rval == 0 && getuid() == 0 &&
302 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
303 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
304 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
305 			err(1, "signal mountd");
306 		(void)fclose(mountdfp);
307 	}
308 
309 	exit(rval);
310 }
311 
312 int
313 ismounted(fs, mntbuf, mntsize)
314 	struct fstab *fs;
315 	struct statfs *mntbuf;
316 	int mntsize;
317 {
318 	int i;
319 
320 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
321 		/* the root file system can always be remounted */
322 		return (0);
323 
324 	for (i = mntsize - 1; i >= 0; --i)
325 		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
326 		    (!isremountable(fs->fs_vfstype) ||
327 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
328 			return (1);
329 	return (0);
330 }
331 
332 int
333 isremountable(vfsname)
334 	const char *vfsname;
335 {
336 	const char **cp;
337 
338 	for (cp = remountable_fs_names; *cp; cp++)
339 		if (strcmp(*cp, vfsname) == 0)
340 			return (1);
341 	return (0);
342 }
343 
344 int
345 hasopt(mntopts, option)
346 	const char *mntopts, *option;
347 {
348 	int negative, found;
349 	char *opt, *optbuf;
350 
351 	if (option[0] == 'n' && option[1] == 'o') {
352 		negative = 1;
353 		option += 2;
354 	} else
355 		negative = 0;
356 	optbuf = strdup(mntopts);
357 	found = 0;
358 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
359 		if (opt[0] == 'n' && opt[1] == 'o') {
360 			if (!strcasecmp(opt + 2, option))
361 				found = negative;
362 		} else if (!strcasecmp(opt, option))
363 			found = !negative;
364 	}
365 	free(optbuf);
366 	return (found);
367 }
368 
369 int
370 mountfs(vfstype, spec, name, flags, options, mntopts)
371 	const char *vfstype, *spec, *name, *options, *mntopts;
372 	int flags;
373 {
374 	/* List of directories containing mount_xxx subcommands. */
375 	static const char *edirs[] = {
376 		_PATH_SBIN,
377 		_PATH_USRSBIN,
378 		NULL
379 	};
380 	const char *argv[100], **edir;
381 	struct statfs sf;
382 	pid_t pid;
383 	int argc, i, status;
384 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
385 
386 #if __GNUC__
387 	(void)&optbuf;
388 	(void)&name;
389 #endif
390 
391 	/* resolve the mountpoint with realpath(3) */
392 	(void)checkpath(name, mntpath);
393 	name = mntpath;
394 
395 	if (mntopts == NULL)
396 		mntopts = "";
397 	if (options == NULL) {
398 		if (*mntopts == '\0') {
399 			options = "rw";
400 		} else {
401 			options = mntopts;
402 			mntopts = "";
403 		}
404 	}
405 	optbuf = catopt(strdup(mntopts), options);
406 
407 	if (strcmp(name, "/") == 0)
408 		flags |= MNT_UPDATE;
409 	if (flags & MNT_FORCE)
410 		optbuf = catopt(optbuf, "force");
411 	if (flags & MNT_RDONLY)
412 		optbuf = catopt(optbuf, "ro");
413 	/*
414 	 * XXX
415 	 * The mount_mfs (newfs) command uses -o to select the
416 	 * optimization mode.  We don't pass the default "-o rw"
417 	 * for that reason.
418 	 */
419 	if (flags & MNT_UPDATE)
420 		optbuf = catopt(optbuf, "update");
421 
422 	argc = 0;
423 	argv[argc++] = vfstype;
424 	mangle(optbuf, &argc, argv);
425 	argv[argc++] = spec;
426 	argv[argc++] = name;
427 	argv[argc] = NULL;
428 
429 	if (debug) {
430 		(void)printf("exec: mount_%s", vfstype);
431 		for (i = 1; i < argc; i++)
432 			(void)printf(" %s", argv[i]);
433 		(void)printf("\n");
434 		return (0);
435 	}
436 
437 	switch (pid = fork()) {
438 	case -1:				/* Error. */
439 		warn("fork");
440 		free(optbuf);
441 		return (1);
442 	case 0:					/* Child. */
443 		if (strcmp(vfstype, "ufs") == 0)
444 			exit(mount_ufs(argc, (char * const *) argv));
445 
446 		/* Go find an executable. */
447 		for (edir = edirs; *edir; edir++) {
448 			(void)snprintf(execname,
449 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
450 			execv(execname, (char * const *)argv);
451 		}
452 		if (errno == ENOENT) {
453 			int len = 0;
454 			char *cp;
455 			for (edir = edirs; *edir; edir++)
456 				len += strlen(*edir) + 2;	/* ", " */
457 			if ((cp = malloc(len)) == NULL)
458 				errx(1, "malloc failed");
459 			cp[0] = '\0';
460 			for (edir = edirs; *edir; edir++) {
461 				strcat(cp, *edir);
462 				if (edir[1] != NULL)
463 					strcat(cp, ", ");
464 			}
465 			warn("exec mount_%s not found in %s", vfstype, cp);
466 		}
467 		exit(1);
468 		/* NOTREACHED */
469 	default:				/* Parent. */
470 		free(optbuf);
471 
472 		if (waitpid(pid, &status, 0) < 0) {
473 			warn("waitpid");
474 			return (1);
475 		}
476 
477 		if (WIFEXITED(status)) {
478 			if (WEXITSTATUS(status) != 0)
479 				return (WEXITSTATUS(status));
480 		} else if (WIFSIGNALED(status)) {
481 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
482 			return (1);
483 		}
484 
485 		if (verbose) {
486 			if (statfs(name, &sf) < 0) {
487 				warn("statfs %s", name);
488 				return (1);
489 			}
490 			if (fstab_style)
491 				putfsent(&sf);
492 			else
493 				prmount(&sf);
494 		}
495 		break;
496 	}
497 
498 	return (0);
499 }
500 
501 void
502 prmount(sfp)
503 	struct statfs *sfp;
504 {
505 	int flags;
506 	struct opt *o;
507 	struct passwd *pw;
508 
509 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
510 	    sfp->f_fstypename);
511 
512 	flags = sfp->f_flags & MNT_VISFLAGMASK;
513 	for (o = optnames; flags && o->o_opt; o++)
514 		if (flags & o->o_opt) {
515 			(void)printf(", %s", o->o_name);
516 			flags &= ~o->o_opt;
517 		}
518 	if (sfp->f_owner) {
519 		(void)printf(", mounted by ");
520 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
521 			(void)printf("%s", pw->pw_name);
522 		else
523 			(void)printf("%d", sfp->f_owner);
524 	}
525 	if (verbose) {
526 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
527 			(void)printf(", writes: sync %ld async %ld",
528 			    sfp->f_syncwrites, sfp->f_asyncwrites);
529 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
530 			(void)printf(", reads: sync %ld async %ld",
531 			    sfp->f_syncreads, sfp->f_asyncreads);
532 	}
533 	(void)printf(")\n");
534 }
535 
536 struct statfs *
537 getmntpt(name)
538 	const char *name;
539 {
540 	struct statfs *mntbuf;
541 	int i, mntsize;
542 
543 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
544 	for (i = mntsize - 1; i >= 0; i--) {
545 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
546 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
547 			return (&mntbuf[i]);
548 	}
549 	return (NULL);
550 }
551 
552 char *
553 catopt(s0, s1)
554 	char *s0;
555 	const char *s1;
556 {
557 	size_t i;
558 	char *cp;
559 
560 	if (s1 == NULL || *s1 == '\0')
561 		return s0;
562 
563 	if (s0 && *s0) {
564 		i = strlen(s0) + strlen(s1) + 1 + 1;
565 		if ((cp = malloc(i)) == NULL)
566 			errx(1, "malloc failed");
567 		(void)snprintf(cp, i, "%s,%s", s0, s1);
568 	} else
569 		cp = strdup(s1);
570 
571 	if (s0)
572 		free(s0);
573 	return (cp);
574 }
575 
576 void
577 mangle(options, argcp, argv)
578 	char *options;
579 	int *argcp;
580 	const char **argv;
581 {
582 	char *p, *s;
583 	int argc;
584 
585 	argc = *argcp;
586 	for (s = options; (p = strsep(&s, ",")) != NULL;)
587 		if (*p != '\0') {
588 			if (*p == '-') {
589 				argv[argc++] = p;
590 				p = strchr(p, '=');
591 				if (p) {
592 					*p = '\0';
593 					argv[argc++] = p+1;
594 				}
595 			} else if (strcmp(p, "rw") != 0) {
596 				argv[argc++] = "-o";
597 				argv[argc++] = p;
598 			}
599 		}
600 
601 	*argcp = argc;
602 }
603 
604 
605 char *
606 update_options(opts, fstab, curflags)
607 	char *opts;
608 	char *fstab;
609 	int curflags;
610 {
611 	char *o, *p;
612 	char *cur;
613 	char *expopt, *newopt, *tmpopt;
614 
615 	if (opts == NULL)
616 		return strdup("");
617 
618 	/* remove meta options from list */
619 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
620 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
621 	cur = flags2opts(curflags);
622 
623 	/*
624 	 * Expand all meta-options passed to us first.
625 	 */
626 	expopt = NULL;
627 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
628 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
629 			expopt = catopt(expopt, fstab);
630 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
631 			expopt = catopt(expopt, cur);
632 		else
633 			expopt = catopt(expopt, o);
634 	}
635 	free(cur);
636 	free(opts);
637 
638 	/*
639 	 * Remove previous contradictory arguments. Given option "foo" we
640 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
641 	 * and "foo" - so we can deal with possible options like "notice".
642 	 */
643 	newopt = NULL;
644 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
645 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
646 			errx(1, "malloc failed");
647 
648 		strcpy(tmpopt, "no");
649 		strcat(tmpopt, o);
650 		remopt(newopt, tmpopt);
651 		free(tmpopt);
652 
653 		if (strncmp("no", o, 2) == 0)
654 			remopt(newopt, o+2);
655 
656 		newopt = catopt(newopt, o);
657 	}
658 	free(expopt);
659 
660 	return newopt;
661 }
662 
663 void
664 remopt(string, opt)
665 	char *string;
666  	const char *opt;
667 {
668 	char *o, *p, *r;
669 
670 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
671 		return;
672 
673 	r = string;
674 
675 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
676 		if (strcmp(opt, o) != 0) {
677 			if (*r == ',' && *o != '\0')
678 				r++;
679 			while ((*r++ = *o++) != '\0')
680 			    ;
681 			*--r = ',';
682 		}
683 	}
684 	*r = '\0';
685 }
686 
687 void
688 usage()
689 {
690 
691 	(void)fprintf(stderr, "%s\n%s\n%s\n",
692 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
693 "       mount [-adfpruvw] [-t ufs | external_type]",
694 "       mount [-dfpruvw] special | node");
695 	exit(1);
696 }
697 
698 void
699 putfsent(ent)
700 	const struct statfs *ent;
701 {
702 	struct fstab *fst;
703 	char *opts;
704 
705 	opts = flags2opts(ent->f_flags);
706 	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
707 	    ent->f_fstypename, opts);
708 	free(opts);
709 
710 	if ((fst = getfsspec(ent->f_mntfromname)))
711 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
712 	else if ((fst = getfsfile(ent->f_mntonname)))
713 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
714 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
715 		if (strcmp(ent->f_mntonname, "/") == 0)
716 			printf("\t1 1\n");
717 		else
718 			printf("\t2 2\n");
719 	} else
720 		printf("\t0 0\n");
721 }
722 
723 
724 char *
725 flags2opts(flags)
726 	int flags;
727 {
728 	char *res;
729 
730 	res = NULL;
731 
732 	res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
733 
734 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
735 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
736 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
737 	if (flags & MNT_NODEV)		res = catopt(res, "nodev");
738 	if (flags & MNT_UNION)		res = catopt(res, "union");
739 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
740 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
741 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
742 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
743 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
744 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
745 
746 	return res;
747 }
748