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