xref: /dragonfly/sbin/mount/mount.c (revision 61c0377f)
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. 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  * @(#) Copyright (c) 1980, 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
30  * @(#)mount.c	8.25 (Berkeley) 5/8/95
31  * $FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/mountctl.h>
37 #define DKTYPENAMES
38 #include <sys/dtype.h>
39 #include <sys/diskslice.h>
40 #include <sys/stat.h>
41 #include <sys/wait.h>
42 
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <fstab.h>
47 #include <mntopts.h>
48 #include <pwd.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include "extern.h"
56 #include "pathnames.h"
57 
58 /* `meta' options */
59 #define MOUNT_META_OPTION_FSTAB		"fstab"
60 #define MOUNT_META_OPTION_CURRENT	"current"
61 
62 int debug, fstab_style, verbose;
63 
64 static char  *catopt(char *, const char *);
65 static struct statfs
66              *getmntpt(const char *);
67 static int    hasopt(const char *, const char *);
68 static int    ismounted(struct fstab *, struct statfs *, int);
69 static int    isremountable(const char *);
70 static void   mangle(char *, int *, const char **);
71 static char  *update_options(char *, char *, int);
72 static int    mountfs(const char *, const char *, const char *,
73 		      int, const char *, const char *);
74 static void   remopt(char *, const char *);
75 static void   printdefvals(const struct statfs *);
76 static void   prmount(struct statfs *);
77 static void   putfsent(const struct statfs *);
78 static void   usage(void);
79 static char  *flags2opts(int);
80 static char  *xstrdup(const char *str);
81 static void   checkdisklabel(const char *devpath, const char **vfstypep);
82 
83 /*
84  * List of VFS types that can be remounted without becoming mounted on top
85  * of each other.
86  * XXX Is this list correct?
87  */
88 static const char *remountable_fs_names[] = {
89 	"ufs", "ffs", "ext2fs", "hammer", "hammer2",
90 	NULL
91 };
92 
93 int
94 main(int argc, char **argv)
95 {
96 	const char *mntfromname, **vfslist, *vfstype;
97 	struct fstab *fs;
98 	struct statfs *mntbuf;
99 	FILE *mountdfp;
100 	pid_t pid;
101 	int all, ch, i, init_flags, mntsize, rval, have_fstab;
102 	char *options;
103 
104 	all = init_flags = 0;
105 	options = NULL;
106 	vfslist = NULL;
107 	vfstype = "ufs";
108 	while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1) {
109 		switch (ch) {
110 		case 'a':
111 			all = 1;
112 			break;
113 		case 'd':
114 			debug = 1;
115 			break;
116 		case 'F':
117 			setfstab(optarg);
118 			break;
119 		case 'f':
120 			init_flags |= MNT_FORCE;
121 			break;
122 		case 'o':
123 			if (*optarg)
124 				options = catopt(options, optarg);
125 			break;
126 		case 'p':
127 			fstab_style = 1;
128 			verbose = 1;
129 			break;
130 		case 'r':
131 			options = catopt(options, "ro");
132 			break;
133 		case 't':
134 			if (vfslist != NULL)
135 				errx(1, "only one -t option may be specified");
136 			vfslist = makevfslist(optarg);
137 			vfstype = optarg;
138 			break;
139 		case 'u':
140 			init_flags |= MNT_UPDATE;
141 			break;
142 		case 'v':
143 			verbose = 1;
144 			break;
145 		case 'w':
146 			options = catopt(options, "noro");
147 			break;
148 		case '?':
149 		default:
150 			usage();
151 			/* NOTREACHED */
152 		}
153 	}
154 	argc -= optind;
155 	argv += optind;
156 
157 #define	BADTYPE(type)							\
158 	(strcmp(type, FSTAB_RO) &&					\
159 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
160 
161 	rval = 0;
162 	switch (argc) {
163 	case 0:
164 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
165 			err(1, "getmntinfo");
166 		if (all) {
167 			while ((fs = getfsent()) != NULL) {
168 				if (BADTYPE(fs->fs_type))
169 					continue;
170 				if (checkvfsname(fs->fs_vfstype, vfslist))
171 					continue;
172 				if (hasopt(fs->fs_mntops, "noauto"))
173 					continue;
174 				if (!(init_flags & MNT_UPDATE) &&
175 				    ismounted(fs, mntbuf, mntsize))
176 					continue;
177 				options = update_options(options,
178 				    fs->fs_mntops, mntbuf->f_flags);
179 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
180 				    fs->fs_file, init_flags, options,
181 				    fs->fs_mntops))
182 					rval = 1;
183 			}
184 		} else if (fstab_style) {
185 			for (i = 0; i < mntsize; i++) {
186 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
187 					continue;
188 				putfsent(&mntbuf[i]);
189 			}
190 		} else {
191 			for (i = 0; i < mntsize; i++) {
192 				if (checkvfsname(mntbuf[i].f_fstypename,
193 				    vfslist))
194 					continue;
195 				prmount(&mntbuf[i]);
196 			}
197 		}
198 		exit(rval);
199 	case 1:
200 		if (vfslist != NULL)
201 			usage();
202 
203 		rmslashes(*argv, *argv);
204 
205 		if (init_flags & MNT_UPDATE) {
206 			mntfromname = NULL;
207 			have_fstab = 0;
208 			if ((mntbuf = getmntpt(*argv)) == NULL)
209 				errx(1, "not currently mounted %s", *argv);
210 			/*
211 			 * Only get the mntflags from fstab if both mntpoint
212 			 * and mntspec are identical. Also handle the special
213 			 * case where just '/' is mounted and 'spec' is not
214 			 * identical with the one from fstab ('/dev' is missing
215 			 * in the spec-string at boot-time).
216 			 */
217 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
218 				if (strcmp(fs->fs_spec,
219 				    mntbuf->f_mntfromname) == 0 &&
220 				    strcmp(fs->fs_file,
221 				    mntbuf->f_mntonname) == 0) {
222 					have_fstab = 1;
223 					mntfromname = mntbuf->f_mntfromname;
224 				} else if (argv[0][0] == '/' &&
225 				    argv[0][1] == '\0') {
226 					fs = getfsfile("/");
227 					have_fstab = 1;
228 					mntfromname = fs->fs_spec;
229 				}
230 			}
231 			if (have_fstab) {
232 				options = update_options(options, fs->fs_mntops,
233 				    mntbuf->f_flags);
234 			} else {
235 				mntfromname = mntbuf->f_mntfromname;
236 				options = update_options(options, NULL,
237 				    mntbuf->f_flags);
238 			}
239 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
240 			    mntbuf->f_mntonname, init_flags, options, 0);
241 			break;
242 		}
243 		if ((fs = getfsfile(*argv)) == NULL &&
244 		    (fs = getfsspec(*argv)) == NULL)
245 			errx(1, "%s: unknown special file or file system",
246 			    *argv);
247 		if (BADTYPE(fs->fs_type))
248 			errx(1, "%s has unknown file system type",
249 			    *argv);
250 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
251 		    init_flags, options, fs->fs_mntops);
252 		break;
253 	case 2:
254 		/*
255 		 * If -t flag has not been specified, the path cannot be
256 		 * found.
257 		 *
258 		 * If the spec is not a file and contains a ':' then assume
259 		 * NFS.
260 		 *
261 		 * If the spec is a cdev attempt to extract the fstype from
262 		 * the label.
263 		 *
264 		 * When all else fails ufs is assumed.
265 		 */
266 		if (vfslist == NULL) {
267 			if (strpbrk(argv[0], ":") != NULL &&
268 			    access(argv[0], 0) == -1) {
269 				vfstype = "nfs";
270 			} else {
271 				checkdisklabel(argv[0], &vfstype);
272 			}
273 		}
274 
275 		rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
276 			       init_flags, options, NULL);
277 		break;
278 	default:
279 		usage();
280 		/* NOTREACHED */
281 	}
282 
283 	/*
284 	 * If the mount was successfully, and done by root, tell mountd the
285 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
286 	 */
287 	if (rval == 0 && getuid() == 0 &&
288 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
289 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
290 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
291 			err(1, "signal mountd");
292 		fclose(mountdfp);
293 	}
294 
295 	exit(rval);
296 }
297 
298 static int
299 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
300 {
301 	int i;
302 
303 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
304 		/* the root file system can always be remounted */
305 		return (0);
306 
307 	for (i = mntsize - 1; i >= 0; --i)
308 		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
309 		    (!isremountable(fs->fs_vfstype) ||
310 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
311 			return (1);
312 	return (0);
313 }
314 
315 static int
316 isremountable(const char *vfsname)
317 {
318 	const char **cp;
319 
320 	for (cp = remountable_fs_names; *cp; cp++)
321 		if (strcmp(*cp, vfsname) == 0)
322 			return (1);
323 	return (0);
324 }
325 
326 static int
327 hasopt(const char *mntopts, const char *option)
328 {
329 	int negative, found;
330 	char *opt, *optbuf;
331 
332 	if (option[0] == 'n' && option[1] == 'o') {
333 		negative = 1;
334 		option += 2;
335 	} else
336 		negative = 0;
337 	optbuf = xstrdup(mntopts);
338 	found = 0;
339 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
340 		if (opt[0] == 'n' && opt[1] == 'o') {
341 			if (!strcasecmp(opt + 2, option))
342 				found = negative;
343 		} else if (!strcasecmp(opt, option))
344 			found = !negative;
345 	}
346 	free(optbuf);
347 	return (found);
348 }
349 
350 static int
351 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
352         const char *options, const char *mntopts)
353 {
354 	/* List of directories containing mount_xxx subcommands. */
355 	static const char *edirs[] = {
356 		_PATH_SBIN,
357 		_PATH_USRSBIN,
358 		NULL
359 	};
360 	const char *argv[100], **edir;
361 	struct statfs sf;
362 	pid_t pid;
363 	int argc, i, status;
364 	char *optbuf;
365 	char *argv0;
366 	char execname[MAXPATHLEN + 1];
367 	char mntpath[MAXPATHLEN];
368 
369 #if __GNUC__
370 	(void)&optbuf;
371 	(void)&name;
372 #endif
373 
374 	/* resolve the mountpoint with realpath(3) */
375 	checkpath(name, mntpath);
376 	name = mntpath;
377 
378 	if (mntopts == NULL)
379 		mntopts = "";
380 	if (options == NULL) {
381 		if (*mntopts == '\0') {
382 			options = "rw";
383 		} else {
384 			options = mntopts;
385 			mntopts = "";
386 		}
387 	}
388 	optbuf = catopt(xstrdup(mntopts), options);
389 
390 	if (strcmp(name, "/") == 0)
391 		flags |= MNT_UPDATE;
392 	if (flags & MNT_FORCE)
393 		optbuf = catopt(optbuf, "force");
394 	if (flags & MNT_RDONLY)
395 		optbuf = catopt(optbuf, "ro");
396 	/*
397 	 * XXX
398 	 * The mount_mfs (newfs) command uses -o to select the
399 	 * optimization mode.  We don't pass the default "-o rw"
400 	 * for that reason.
401 	 */
402 	if (flags & MNT_UPDATE)
403 		optbuf = catopt(optbuf, "update");
404 
405 	asprintf(&argv0, "mount_%s", vfstype);
406 
407 	argc = 0;
408 	argv[argc++] = argv0;
409 	mangle(optbuf, &argc, argv);
410 	argv[argc++] = spec;
411 	argv[argc++] = name;
412 	argv[argc] = NULL;
413 
414 	if (debug) {
415 		printf("exec: mount_%s", vfstype);
416 		for (i = 1; i < argc; i++)
417 			printf(" %s", argv[i]);
418 		printf("\n");
419 		return (0);
420 	}
421 
422 	switch (pid = fork()) {
423 	case -1:				/* Error. */
424 		warn("fork");
425 		free(optbuf);
426 		return (1);
427 	case 0:					/* Child. */
428 		/* Go find an executable. */
429 		for (edir = edirs; *edir; edir++) {
430 			snprintf(execname, sizeof(execname),
431 				 "%s/mount_%s", *edir, vfstype);
432 			execv(execname, __DECONST(char * const *, argv));
433 		}
434 		if (errno == ENOENT) {
435 			int len = 0;
436 			char *cp;
437 			for (edir = edirs; *edir; edir++)
438 				len += strlen(*edir) + 2;	/* ", " */
439 			if ((cp = malloc(len)) == NULL)
440 				errx(1, "malloc failed");
441 			cp[0] = '\0';
442 			for (edir = edirs; *edir; edir++) {
443 				strcat(cp, *edir);
444 				if (edir[1] != NULL)
445 					strcat(cp, ", ");
446 			}
447 			warn("exec mount_%s not found in %s", vfstype, cp);
448 		}
449 		exit(1);
450 		/* NOTREACHED */
451 	default:				/* Parent. */
452 		free(optbuf);
453 
454 		if (waitpid(pid, &status, 0) < 0) {
455 			warn("waitpid");
456 			return (1);
457 		}
458 
459 		if (WIFEXITED(status)) {
460 			if (WEXITSTATUS(status) != 0)
461 				return (WEXITSTATUS(status));
462 		} else if (WIFSIGNALED(status)) {
463 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
464 			return (1);
465 		}
466 
467 		if (verbose) {
468 			if (statfs(name, &sf) < 0) {
469 				warn("statfs %s", name);
470 				return (1);
471 			}
472 			if (fstab_style)
473 				putfsent(&sf);
474 			else
475 				prmount(&sf);
476 		}
477 		break;
478 	}
479 
480 	return (0);
481 }
482 
483 static void
484 prmount(struct statfs *sfp)
485 {
486 	struct passwd *pw;
487 	char *buf;
488 	int error;
489 	int len;
490 
491 	error = 0;
492 	len = 256;
493 
494 	if ((buf = malloc(len)) == NULL)
495 		errx(1, "malloc failed");
496 
497 	printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
498 	    sfp->f_fstypename);
499 
500 	/* Get a string buffer with all the used flags names */
501 	error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
502 			 -1, NULL, 0, buf, len);
503 
504 	if (sfp->f_owner) {
505 		printf(", mounted by ");
506 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
507 			printf("%s", pw->pw_name);
508 		else
509 			printf("%d", sfp->f_owner);
510 	}
511 
512 	if (error != -1 && strlen(buf))
513 		printf(", %s", buf);
514 
515 	if (verbose) {
516 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
517 			printf(", writes: sync %ld async %ld",
518 			    sfp->f_syncwrites, sfp->f_asyncwrites);
519 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
520 			printf(", reads: sync %ld async %ld",
521 			    sfp->f_syncreads, sfp->f_asyncreads);
522 	}
523 	printf(")\n");
524 }
525 
526 static struct statfs *
527 getmntpt(const char *name)
528 {
529 	struct statfs *mntbuf;
530 	int i, mntsize;
531 
532 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
533 	for (i = mntsize - 1; i >= 0; i--) {
534 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
535 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
536 			return (&mntbuf[i]);
537 	}
538 	return (NULL);
539 }
540 
541 static char *
542 catopt(char *s0, const char *s1)
543 {
544 	size_t i;
545 	char *cp;
546 
547 	if (s1 == NULL || *s1 == '\0')
548 		return s0;
549 
550 	if (s0 && *s0) {
551 		i = strlen(s0) + strlen(s1) + 1 + 1;
552 		if ((cp = malloc(i)) == NULL)
553 			errx(1, "malloc failed");
554 		snprintf(cp, i, "%s,%s", s0, s1);
555 	} else
556 		cp = xstrdup(s1);
557 
558 	if (s0)
559 		free(s0);
560 	return (cp);
561 }
562 
563 static void
564 mangle(char *options, int *argcp, const char **argv)
565 {
566 	char *p, *s;
567 	int argc;
568 
569 	argc = *argcp;
570 	for (s = options; (p = strsep(&s, ",")) != NULL;)
571 		if (*p != '\0') {
572 			if (*p == '-') {
573 				argv[argc++] = p;
574 				p = strchr(p, '=');
575 				if (p) {
576 					*p = '\0';
577 					argv[argc++] = p+1;
578 				}
579 			} else if (strcmp(p, "rw") != 0) {
580 				argv[argc++] = "-o";
581 				argv[argc++] = p;
582 			}
583 		}
584 
585 	*argcp = argc;
586 }
587 
588 
589 static char *
590 update_options(char *opts, char *fstab, int curflags)
591 {
592 	char *o, *p;
593 	char *cur;
594 	char *expopt, *newopt, *tmpopt;
595 
596 	if (opts == NULL)
597 		return xstrdup("");
598 
599 	/* remove meta options from list */
600 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
601 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
602 	cur = flags2opts(curflags);
603 
604 	/*
605 	 * Expand all meta-options passed to us first.
606 	 */
607 	expopt = NULL;
608 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
609 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
610 			expopt = catopt(expopt, fstab);
611 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
612 			expopt = catopt(expopt, cur);
613 		else
614 			expopt = catopt(expopt, o);
615 	}
616 	free(cur);
617 	free(opts);
618 
619 	/*
620 	 * Remove previous contradictory arguments. Given option "foo" we
621 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
622 	 * and "foo" - so we can deal with possible options like "notice".
623 	 */
624 	newopt = NULL;
625 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
626 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
627 			errx(1, "malloc failed");
628 
629 		strcpy(tmpopt, "no");
630 		strcat(tmpopt, o);
631 		remopt(newopt, tmpopt);
632 		free(tmpopt);
633 
634 		if (strncmp("no", o, 2) == 0)
635 			remopt(newopt, o+2);
636 
637 		newopt = catopt(newopt, o);
638 	}
639 	free(expopt);
640 
641 	return newopt;
642 }
643 
644 static void
645 remopt(char *string, const char *opt)
646 {
647 	char *o, *p, *r;
648 
649 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
650 		return;
651 
652 	r = string;
653 
654 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
655 		if (strcmp(opt, o) != 0) {
656 			if (*r == ',' && *o != '\0')
657 				r++;
658 			while ((*r++ = *o++) != '\0')
659 			    ;
660 			*--r = ',';
661 		}
662 	}
663 	*r = '\0';
664 }
665 
666 static void
667 usage(void)
668 {
669 
670 	fprintf(stderr, "%s\n%s\n%s\n",
671 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
672 "       mount [-dfpruvw] {special | node}",
673 "       mount [-dfpruvw] [-o options] [-t type] special node");
674 	exit(1);
675 }
676 
677 /*
678  * Prints the default values for dump frequency and pass number of fsck.
679  * This happens when mount(8) is called with -p and there is no fstab file
680  * or there is but the values aren't specified.
681  */
682 static void
683 printdefvals(const struct statfs *ent)
684 {
685         if (strcmp(ent->f_fstypename, "ufs") == 0) {
686                 if (strcmp(ent->f_mntonname, "/") == 0)
687                         printf("\t1 1\n");
688                 else
689                         printf("\t2 2\n");
690         } else {
691                 printf("\t0 0\n");
692 	}
693 }
694 
695 static void
696 putfsent(const struct statfs *ent)
697 {
698 	struct stat sb;
699 	struct fstab *fst;
700 	char *opts;
701 
702 	opts = flags2opts(ent->f_flags);
703 	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
704 	    ent->f_fstypename, opts);
705 	free(opts);
706 
707 	/*
708 	 * If fstab file is missing don't call getfsspec() or getfsfile()
709 	 * at all, because the same warning will be printed twice for every
710 	 * mounted filesystem.
711 	 */
712 	if (stat(_PATH_FSTAB, &sb) != -1) {
713 		if ((fst = getfsspec(ent->f_mntfromname)))
714 			printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
715 		else if ((fst = getfsfile(ent->f_mntonname)))
716 			printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
717 		else
718 			printdefvals(ent);
719 	} else {
720 		printdefvals(ent);
721 	}
722 }
723 
724 
725 static char *
726 flags2opts(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 	if (flags & MNT_IGNORE)		res = catopt(res, "ignore");
746 
747 	return res;
748 }
749 
750 static char*
751 xstrdup(const char *str)
752 {
753 	char* ret = strdup(str);
754 	if(ret == NULL) {
755 		errx(1, "strdup failed (could not allocate memory)");
756 	}
757 	return ret;
758 }
759 
760 /*
761  * Hack 'cd9660' for the default fstype on cd media if no disklabel
762  * found.
763  */
764 static int
765 iscdmedia(const char *path, int fd __unused)
766 {
767 	int n;
768 
769 	if (strrchr(path, '/'))
770 		path = strrchr(path, '/') + 1;
771 	if (sscanf(path, "acd%d", &n) == 1)
772 		return 1;
773 	if (sscanf(path, "cd%d", &n) == 1)
774 		return 1;
775 	return 0;
776 }
777 
778 /*
779  * If the device path is a cdev attempt to access the disklabel to determine
780  * the filesystem type.  Adjust *vfstypep if we can figure it out
781  * definitively.
782  *
783  * Ignore any portion of the device path after an '@' or ':'.
784  */
785 static void
786 checkdisklabel(const char *devpath, const char **vfstypep)
787 {
788 	struct stat st;
789 	struct partinfo info;
790 	char *path = strdup(devpath);
791 	int fd;
792 
793 	if (strchr(path, '@'))
794 		*strchr(path, '@') = 0;
795 	if (strchr(path, ':'))
796 		*strchr(path, ':') = 0;
797 
798 	if (stat(path, &st) < 0)
799 		goto done;
800 	if (!S_ISCHR(st.st_mode))
801 		goto done;
802 	fd = open(path, O_RDONLY);
803 	if (fd < 0)
804 		goto done;
805 	if (ioctl(fd, DIOCGPART, &info) == 0) {
806 		if (info.fstype >= 0 && info.fstype < (int)FSMAXTYPES) {
807 			if (fstype_to_vfsname[info.fstype]) {
808 				*vfstypep = fstype_to_vfsname[info.fstype];
809 			} else if (iscdmedia(path, fd)) {
810 				*vfstypep = "cd9660";
811 			} else {
812 				fprintf(stderr,
813 					"mount: warning: fstype in disklabel "
814 					"not set to anything I understand\n");
815 				fprintf(stderr,
816 					"attempting to mount with -t ufs\n");
817 			}
818 		}
819 	}
820 	close(fd);
821 done:
822 	free(path);
823 }
824