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