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