xref: /freebsd/sbin/mount/mount.c (revision 183e8aa749c1fe7de44c842542d9e9eadf11b8cb)
1fba1c154SSteve Price /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1980, 1989, 1993, 1994
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes #include <sys/param.h>
3308b242aeSWarner Losh #define _WANT_MNTOPTNAMES
348fae3551SRodney W. Grimes #include <sys/mount.h>
358a978495SDavid Greenman #include <sys/stat.h>
3674cf460bSBruce Evans #include <sys/wait.h>
378fae3551SRodney W. Grimes 
38003dbca6SMaxime Henrion #include <ctype.h>
39*183e8aa7SYan-Hao Wang 
408fae3551SRodney W. Grimes #include <errno.h>
418fae3551SRodney W. Grimes #include <fstab.h>
42a3ba4c65SGordon Tetlow #include <paths.h>
43c06fe0a0SPeter Wemm #include <pwd.h>
448fae3551SRodney W. Grimes #include <signal.h>
4596c65ccbSIan Dowse #include <stdint.h>
468fae3551SRodney W. Grimes #include <stdio.h>
478fae3551SRodney W. Grimes #include <stdlib.h>
488fae3551SRodney W. Grimes #include <string.h>
498fae3551SRodney W. Grimes #include <unistd.h>
50e9988cedSPawel Jakub Dawidek #include <libutil.h>
51e725ee7eSCameron Katri #include <libxo/xo.h>
528fae3551SRodney W. Grimes 
53fba1c154SSteve Price #include "extern.h"
5473dd3167SPoul-Henning Kamp #include "mntopts.h"
558fae3551SRodney W. Grimes #include "pathnames.h"
568fae3551SRodney W. Grimes 
57e725ee7eSCameron Katri #define EXIT(a) {			\
58e725ee7eSCameron Katri 	xo_close_container("mount");	\
59*183e8aa7SYan-Hao Wang 	if (xo_finish() < 0)			\
60*183e8aa7SYan-Hao Wang 		xo_err(EXIT_FAILURE, "stdout");	\
61e725ee7eSCameron Katri 	exit(a);			\
62e725ee7eSCameron Katri 	}
63e725ee7eSCameron Katri 
6418af6044SJoseph Koshy /* `meta' options */
6518af6044SJoseph Koshy #define MOUNT_META_OPTION_FSTAB		"fstab"
6618af6044SJoseph Koshy #define MOUNT_META_OPTION_CURRENT	"current"
6718af6044SJoseph Koshy 
681efe3c6bSEd Schouten static int debug, fstab_style, verbose;
69a257a45eSJordan K. Hubbard 
70a86de995SDavid E. O'Brien struct cpa {
7100356118SDavid E. O'Brien 	char	**a;
720e969ed7SDavid E. O'Brien 	ssize_t	sz;
73a86de995SDavid E. O'Brien 	int	c;
74a86de995SDavid E. O'Brien };
75a86de995SDavid E. O'Brien 
7685429990SWarner Losh char   *catopt(char *, const char *);
7785429990SWarner Losh int	hasopt(const char *, const char *);
7885429990SWarner Losh int	ismounted(struct fstab *, struct statfs *, int);
7985429990SWarner Losh int	isremountable(const char *);
80a3f714c4SDoug Rabson int	allow_file_mount(const char *);
81a86de995SDavid E. O'Brien void	mangle(char *, struct cpa *);
8285429990SWarner Losh char   *update_options(char *, char *, int);
8385429990SWarner Losh int	mountfs(const char *, const char *, const char *,
8485429990SWarner Losh 			int, const char *, const char *);
8585429990SWarner Losh void	remopt(char *, const char *);
8685429990SWarner Losh void	prmount(struct statfs *);
87031ea52fSMatteo Riondato void	putfsent(struct statfs *);
8885429990SWarner Losh void	usage(void);
8985429990SWarner Losh char   *flags2opts(int);
908fae3551SRodney W. Grimes 
91bcb1d846SPhilippe Charnier /* Map from mount options to printable formats. */
9208b242aeSWarner Losh static struct mntoptnames optnames[] = {
9308b242aeSWarner Losh 	MNTOPT_NAMES
948fae3551SRodney W. Grimes };
958fae3551SRodney W. Grimes 
96fba1c154SSteve Price /*
97fba1c154SSteve Price  * List of VFS types that can be remounted without becoming mounted on top
98fba1c154SSteve Price  * of each other.
99fba1c154SSteve Price  * XXX Is this list correct?
100fba1c154SSteve Price  */
101fba1c154SSteve Price static const char *
102fba1c154SSteve Price remountable_fs_names[] = {
1035a4420e3SAlexey Zelkin 	"ufs", "ffs", "ext2fs",
104fba1c154SSteve Price 	0
105fba1c154SSteve Price };
106fba1c154SSteve Price 
1076e74fb9dSMaxim Konovalov static const char userquotaeq[] = "userquota=";
1086e74fb9dSMaxim Konovalov static const char groupquotaeq[] = "groupquota=";
1096e74fb9dSMaxim Konovalov 
110c7835769SCraig Rodrigues static char *mountprog = NULL;
111c7835769SCraig Rodrigues 
1126f5f1a6bSCraig Rodrigues static int
use_mountprog(const char * vfstype)1136f5f1a6bSCraig Rodrigues use_mountprog(const char *vfstype)
1146f5f1a6bSCraig Rodrigues {
1156f5f1a6bSCraig Rodrigues 	/* XXX: We need to get away from implementing external mount
1166f5f1a6bSCraig Rodrigues 	 *      programs for every filesystem, and move towards having
1176f5f1a6bSCraig Rodrigues 	 *	each filesystem properly implement the nmount() system call.
1186f5f1a6bSCraig Rodrigues 	 */
1196f5f1a6bSCraig Rodrigues 	unsigned int i;
1206f5f1a6bSCraig Rodrigues 	const char *fs[] = {
121a42ac676SAttilio Rao 	"cd9660", "mfs", "msdosfs", "nfs",
1224e25c86fSEdward Tomasz Napierala 	"nullfs", "smbfs", "udf", "unionfs",
1236f5f1a6bSCraig Rodrigues 	NULL
1246f5f1a6bSCraig Rodrigues 	};
1256f5f1a6bSCraig Rodrigues 
126c7835769SCraig Rodrigues 	if (mountprog != NULL)
127c7835769SCraig Rodrigues 		return (1);
128c7835769SCraig Rodrigues 
1296f5f1a6bSCraig Rodrigues 	for (i = 0; fs[i] != NULL; ++i) {
1306f5f1a6bSCraig Rodrigues 		if (strcmp(vfstype, fs[i]) == 0)
1314796c6ccSJuli Mallett 			return (1);
1326f5f1a6bSCraig Rodrigues 	}
1336f5f1a6bSCraig Rodrigues 
1344796c6ccSJuli Mallett 	return (0);
1356f5f1a6bSCraig Rodrigues }
1366f5f1a6bSCraig Rodrigues 
1376f5f1a6bSCraig Rodrigues static int
exec_mountprog(const char * name,const char * execname,char * const argv[])1384796c6ccSJuli Mallett exec_mountprog(const char *name, const char *execname, char *const argv[])
1396f5f1a6bSCraig Rodrigues {
1406f5f1a6bSCraig Rodrigues 	pid_t pid;
1416f5f1a6bSCraig Rodrigues 	int status;
1426f5f1a6bSCraig Rodrigues 
1436f5f1a6bSCraig Rodrigues 	switch (pid = fork()) {
1446f5f1a6bSCraig Rodrigues 	case -1:				/* Error. */
145e725ee7eSCameron Katri 		xo_warn("fork");
146*183e8aa7SYan-Hao Wang 		EXIT(EXIT_FAILURE);
1476f5f1a6bSCraig Rodrigues 	case 0:					/* Child. */
1486f5f1a6bSCraig Rodrigues 		/* Go find an executable. */
1496f5f1a6bSCraig Rodrigues 		execvP(execname, _PATH_SYSPATH, argv);
1506f5f1a6bSCraig Rodrigues 		if (errno == ENOENT) {
151e725ee7eSCameron Katri 			xo_warn("exec %s not found", execname);
152c7835769SCraig Rodrigues 			if (execname[0] != '/') {
153e725ee7eSCameron Katri 				xo_warnx("in path: %s", _PATH_SYSPATH);
154c7835769SCraig Rodrigues 			}
1556f5f1a6bSCraig Rodrigues 		}
156*183e8aa7SYan-Hao Wang 		EXIT(EXIT_FAILURE);
1576f5f1a6bSCraig Rodrigues 	default:				/* Parent. */
1586f5f1a6bSCraig Rodrigues 		if (waitpid(pid, &status, 0) < 0) {
159e725ee7eSCameron Katri 			xo_warn("waitpid");
1606f5f1a6bSCraig Rodrigues 			return (1);
1616f5f1a6bSCraig Rodrigues 		}
1626f5f1a6bSCraig Rodrigues 
1636f5f1a6bSCraig Rodrigues 		if (WIFEXITED(status)) {
1646f5f1a6bSCraig Rodrigues 			if (WEXITSTATUS(status) != 0)
1656f5f1a6bSCraig Rodrigues 				return (WEXITSTATUS(status));
1666f5f1a6bSCraig Rodrigues 		} else if (WIFSIGNALED(status)) {
167e725ee7eSCameron Katri 			xo_warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
1686f5f1a6bSCraig Rodrigues 			return (1);
1696f5f1a6bSCraig Rodrigues 		}
1706f5f1a6bSCraig Rodrigues 		break;
1716f5f1a6bSCraig Rodrigues 	}
1726f5f1a6bSCraig Rodrigues 
1736f5f1a6bSCraig Rodrigues 	return (0);
1746f5f1a6bSCraig Rodrigues }
1756f5f1a6bSCraig Rodrigues 
17649a41c4fSRuslan Ermilov static int
specified_ro(const char * arg)17749a41c4fSRuslan Ermilov specified_ro(const char *arg)
1783cbf527eSRuslan Ermilov {
1793cbf527eSRuslan Ermilov 	char *optbuf, *opt;
1803cbf527eSRuslan Ermilov 	int ret = 0;
1813cbf527eSRuslan Ermilov 
1823cbf527eSRuslan Ermilov 	optbuf = strdup(arg);
1833cbf527eSRuslan Ermilov 	if (optbuf == NULL)
1849d324b5fSJohn Baldwin 		 xo_err(1, "strdup failed");
1853cbf527eSRuslan Ermilov 
1863cbf527eSRuslan Ermilov 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
1873cbf527eSRuslan Ermilov 		if (strcmp(opt, "ro") == 0) {
1883cbf527eSRuslan Ermilov 			ret = 1;
1893cbf527eSRuslan Ermilov 			break;
1903cbf527eSRuslan Ermilov 		}
1913cbf527eSRuslan Ermilov 	}
1923cbf527eSRuslan Ermilov 	free(optbuf);
1933cbf527eSRuslan Ermilov 	return (ret);
1943cbf527eSRuslan Ermilov }
1953cbf527eSRuslan Ermilov 
196e9988cedSPawel Jakub Dawidek static void
restart_mountd(void)197e9988cedSPawel Jakub Dawidek restart_mountd(void)
198e9988cedSPawel Jakub Dawidek {
199e9988cedSPawel Jakub Dawidek 
200a3214fbeSMateusz Guzik 	pidfile_signal(_PATH_MOUNTDPID, SIGHUP, NULL);
201e9988cedSPawel Jakub Dawidek }
202e9988cedSPawel Jakub Dawidek 
2038fae3551SRodney W. Grimes int
main(int argc,char * argv[])204e24dc56aSCraig Rodrigues main(int argc, char *argv[])
2058fae3551SRodney W. Grimes {
206c06fe0a0SPeter Wemm 	const char *mntfromname, **vfslist, *vfstype;
2078fae3551SRodney W. Grimes 	struct fstab *fs;
2088fae3551SRodney W. Grimes 	struct statfs *mntbuf;
209c7383075SXin LI 	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
210b48b774fSChris Rees 	int onlylate;
211003dbca6SMaxime Henrion 	char *cp, *ep, *options;
2128fae3551SRodney W. Grimes 
213b48b774fSChris Rees 	all = init_flags = late = onlylate = 0;
2143cbf527eSRuslan Ermilov 	ro = 0;
2153cbf527eSRuslan Ermilov 	options = NULL;
2168fae3551SRodney W. Grimes 	vfslist = NULL;
2178fae3551SRodney W. Grimes 	vfstype = "ufs";
218e725ee7eSCameron Katri 
219e725ee7eSCameron Katri 	argc = xo_parse_args(argc, argv);
220e725ee7eSCameron Katri 	if (argc < 0)
221*183e8aa7SYan-Hao Wang 		exit(EXIT_FAILURE);
222e725ee7eSCameron Katri 	xo_open_container("mount");
223e725ee7eSCameron Katri 
224fc98db27SRobert Millan 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
2258fae3551SRodney W. Grimes 		switch (ch) {
2268fae3551SRodney W. Grimes 		case 'a':
2278fae3551SRodney W. Grimes 			all = 1;
2288fae3551SRodney W. Grimes 			break;
2298fae3551SRodney W. Grimes 		case 'd':
2308fae3551SRodney W. Grimes 			debug = 1;
2318fae3551SRodney W. Grimes 			break;
232ef258dd9SMatthew N. Dodd 		case 'F':
233ef258dd9SMatthew N. Dodd 			setfstab(optarg);
234ef258dd9SMatthew N. Dodd 			break;
2358fae3551SRodney W. Grimes 		case 'f':
2368fae3551SRodney W. Grimes 			init_flags |= MNT_FORCE;
2378fae3551SRodney W. Grimes 			break;
238b48b774fSChris Rees 		case 'L':
239b48b774fSChris Rees 			onlylate = 1;
240b48b774fSChris Rees 			late = 1;
241b48b774fSChris Rees 			break;
2424b4f9170SDag-Erling Smørgrav 		case 'l':
2434b4f9170SDag-Erling Smørgrav 			late = 1;
2444b4f9170SDag-Erling Smørgrav 			break;
245fc98db27SRobert Millan 		case 'n':
246fc98db27SRobert Millan 			/* For compatibility with the Linux version of mount. */
247fc98db27SRobert Millan 			break;
2488fae3551SRodney W. Grimes 		case 'o':
2493cbf527eSRuslan Ermilov 			if (*optarg) {
2508fae3551SRodney W. Grimes 				options = catopt(options, optarg);
2513cbf527eSRuslan Ermilov 				if (specified_ro(optarg))
2523cbf527eSRuslan Ermilov 					ro = 1;
2533cbf527eSRuslan Ermilov 			}
2548fae3551SRodney W. Grimes 			break;
25574cf460bSBruce Evans 		case 'p':
25674cf460bSBruce Evans 			fstab_style = 1;
25774cf460bSBruce Evans 			verbose = 1;
25874cf460bSBruce Evans 			break;
2598fae3551SRodney W. Grimes 		case 'r':
260ad044771SDima Dorfman 			options = catopt(options, "ro");
2613cbf527eSRuslan Ermilov 			ro = 1;
2628fae3551SRodney W. Grimes 			break;
2638fae3551SRodney W. Grimes 		case 't':
2648fae3551SRodney W. Grimes 			if (vfslist != NULL)
265e725ee7eSCameron Katri 				xo_errx(1, "only one -t option may be specified");
2668fae3551SRodney W. Grimes 			vfslist = makevfslist(optarg);
2678fae3551SRodney W. Grimes 			vfstype = optarg;
2688fae3551SRodney W. Grimes 			break;
2698fae3551SRodney W. Grimes 		case 'u':
2708fae3551SRodney W. Grimes 			init_flags |= MNT_UPDATE;
2718fae3551SRodney W. Grimes 			break;
2728fae3551SRodney W. Grimes 		case 'v':
2738fae3551SRodney W. Grimes 			verbose = 1;
2748fae3551SRodney W. Grimes 			break;
2758fae3551SRodney W. Grimes 		case 'w':
276ad044771SDima Dorfman 			options = catopt(options, "noro");
2778fae3551SRodney W. Grimes 			break;
2788fae3551SRodney W. Grimes 		case '?':
2798fae3551SRodney W. Grimes 		default:
2808fae3551SRodney W. Grimes 			usage();
2818fae3551SRodney W. Grimes 			/* NOTREACHED */
2828fae3551SRodney W. Grimes 		}
2838fae3551SRodney W. Grimes 	argc -= optind;
2848fae3551SRodney W. Grimes 	argv += optind;
2858fae3551SRodney W. Grimes 
2868fae3551SRodney W. Grimes #define	BADTYPE(type)							\
2878fae3551SRodney W. Grimes 	(strcmp(type, FSTAB_RO) &&					\
2888fae3551SRodney W. Grimes 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
2898fae3551SRodney W. Grimes 
2903cbf527eSRuslan Ermilov 	if ((init_flags & MNT_UPDATE) && (ro == 0))
2913cbf527eSRuslan Ermilov 		options = catopt(options, "noro");
2923cbf527eSRuslan Ermilov 
293*183e8aa7SYan-Hao Wang 	rval = EXIT_SUCCESS;
2948fae3551SRodney W. Grimes 	switch (argc) {
2958fae3551SRodney W. Grimes 	case 0:
2964818bd98SKirk McKusick 		if ((mntsize = getmntinfo(&mntbuf,
2974818bd98SKirk McKusick 		     verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
298e725ee7eSCameron Katri 			xo_err(1, "getmntinfo");
299fba1c154SSteve Price 		if (all) {
3008fae3551SRodney W. Grimes 			while ((fs = getfsent()) != NULL) {
3018fae3551SRodney W. Grimes 				if (BADTYPE(fs->fs_type))
3028fae3551SRodney W. Grimes 					continue;
303c06fe0a0SPeter Wemm 				if (checkvfsname(fs->fs_vfstype, vfslist))
3048fae3551SRodney W. Grimes 					continue;
305c06fe0a0SPeter Wemm 				if (hasopt(fs->fs_mntops, "noauto"))
30689beb278SDavid Greenman 					continue;
307b48b774fSChris Rees 				if (!hasopt(fs->fs_mntops, "late") && onlylate)
308b48b774fSChris Rees 					continue;
3094b4f9170SDag-Erling Smørgrav 				if (hasopt(fs->fs_mntops, "late") && !late)
3104b4f9170SDag-Erling Smørgrav 					continue;
311c7383075SXin LI 				if (hasopt(fs->fs_mntops, "failok"))
312c7383075SXin LI 					failok = 1;
313c7383075SXin LI 				else
314c7383075SXin LI 					failok = 0;
31598201b0cSBruce Evans 				if (!(init_flags & MNT_UPDATE) &&
316634add3cSMaxim Sobolev 				    !hasopt(fs->fs_mntops, "update") &&
31798201b0cSBruce Evans 				    ismounted(fs, mntbuf, mntsize))
318fba1c154SSteve Price 					continue;
31913cbdf24SGuido van Rooij 				options = update_options(options, fs->fs_mntops,
32013cbdf24SGuido van Rooij 				    mntbuf->f_flags);
3218fae3551SRodney W. Grimes 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
3228fae3551SRodney W. Grimes 				    fs->fs_file, init_flags, options,
323c7383075SXin LI 				    fs->fs_mntops) && !failok)
324*183e8aa7SYan-Hao Wang 					rval = EXIT_FAILURE;
3258fae3551SRodney W. Grimes 			}
326fba1c154SSteve Price 		} else if (fstab_style) {
327e725ee7eSCameron Katri 			xo_open_list("fstab");
328a257a45eSJordan K. Hubbard 			for (i = 0; i < mntsize; i++) {
329c06fe0a0SPeter Wemm 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
330a257a45eSJordan K. Hubbard 					continue;
331e725ee7eSCameron Katri 				xo_open_instance("fstab");
332a257a45eSJordan K. Hubbard 				putfsent(&mntbuf[i]);
333e725ee7eSCameron Katri 				xo_close_instance("fstab");
334a257a45eSJordan K. Hubbard 			}
335e725ee7eSCameron Katri 			xo_close_list("fstab");
33674cf460bSBruce Evans 		} else {
337e725ee7eSCameron Katri 			xo_open_list("mounted");
3388fae3551SRodney W. Grimes 			for (i = 0; i < mntsize; i++) {
33974cf460bSBruce Evans 				if (checkvfsname(mntbuf[i].f_fstypename,
34074cf460bSBruce Evans 				    vfslist))
3418fae3551SRodney W. Grimes 					continue;
3428abb2a6eSPawel Jakub Dawidek 				if (!verbose &&
3438abb2a6eSPawel Jakub Dawidek 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
3448abb2a6eSPawel Jakub Dawidek 					continue;
345e725ee7eSCameron Katri 				xo_open_instance("mounted");
346c06fe0a0SPeter Wemm 				prmount(&mntbuf[i]);
347e725ee7eSCameron Katri 				xo_close_instance("mounted");
3488fae3551SRodney W. Grimes 			}
349e725ee7eSCameron Katri 			xo_close_list("mounted");
3508fae3551SRodney W. Grimes 		}
351e725ee7eSCameron Katri 		EXIT(rval);
3528fae3551SRodney W. Grimes 	case 1:
3538fae3551SRodney W. Grimes 		if (vfslist != NULL)
3548fae3551SRodney W. Grimes 			usage();
3558fae3551SRodney W. Grimes 
35601a9bce5SEric Anholt 		rmslashes(*argv, *argv);
3578fae3551SRodney W. Grimes 		if (init_flags & MNT_UPDATE) {
358cf96af72SBrian Feldman 			mntfromname = NULL;
359cf96af72SBrian Feldman 			have_fstab = 0;
360906c312bSKirk McKusick 			if ((mntbuf = getmntpoint(*argv)) == NULL)
361e725ee7eSCameron Katri 				xo_errx(1, "not currently mounted %s", *argv);
362cf96af72SBrian Feldman 			/*
363cf96af72SBrian Feldman 			 * Only get the mntflags from fstab if both mntpoint
364cf96af72SBrian Feldman 			 * and mntspec are identical. Also handle the special
365cf96af72SBrian Feldman 			 * case where just '/' is mounted and 'spec' is not
366cf96af72SBrian Feldman 			 * identical with the one from fstab ('/dev' is missing
367cf96af72SBrian Feldman 			 * in the spec-string at boot-time).
368cf96af72SBrian Feldman 			 */
36918af6044SJoseph Koshy 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
370cf96af72SBrian Feldman 				if (strcmp(fs->fs_spec,
371cf96af72SBrian Feldman 				    mntbuf->f_mntfromname) == 0 &&
372cf96af72SBrian Feldman 				    strcmp(fs->fs_file,
373cf96af72SBrian Feldman 				    mntbuf->f_mntonname) == 0) {
374cf96af72SBrian Feldman 					have_fstab = 1;
375cf96af72SBrian Feldman 					mntfromname = mntbuf->f_mntfromname;
376cf96af72SBrian Feldman 				} else if (argv[0][0] == '/' &&
3771f3aded0SEdward Tomasz Napierala 				    argv[0][1] == '\0' &&
3781f3aded0SEdward Tomasz Napierala 				    strcmp(fs->fs_vfstype,
3791f3aded0SEdward Tomasz Napierala 				    mntbuf->f_fstypename) == 0) {
380cf96af72SBrian Feldman 					fs = getfsfile("/");
381cf96af72SBrian Feldman 					have_fstab = 1;
382c06fe0a0SPeter Wemm 					mntfromname = fs->fs_spec;
383cf96af72SBrian Feldman 				}
384cf96af72SBrian Feldman 			}
385cf96af72SBrian Feldman 			if (have_fstab) {
38618af6044SJoseph Koshy 				options = update_options(options, fs->fs_mntops,
38718af6044SJoseph Koshy 				    mntbuf->f_flags);
38818af6044SJoseph Koshy 			} else {
389c06fe0a0SPeter Wemm 				mntfromname = mntbuf->f_mntfromname;
39018af6044SJoseph Koshy 				options = update_options(options, NULL,
39118af6044SJoseph Koshy 				    mntbuf->f_flags);
39218af6044SJoseph Koshy 			}
393c06fe0a0SPeter Wemm 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
394c06fe0a0SPeter Wemm 			    mntbuf->f_mntonname, init_flags, options, 0);
395c06fe0a0SPeter Wemm 			break;
396c06fe0a0SPeter Wemm 		}
3978fae3551SRodney W. Grimes 		if ((fs = getfsfile(*argv)) == NULL &&
3988fae3551SRodney W. Grimes 		    (fs = getfsspec(*argv)) == NULL)
399e725ee7eSCameron Katri 			xo_errx(1, "%s: unknown special file or file system",
4008fae3551SRodney W. Grimes 			    *argv);
4018fae3551SRodney W. Grimes 		if (BADTYPE(fs->fs_type))
402e725ee7eSCameron Katri 			xo_errx(1, "%s has unknown file system type",
4038fae3551SRodney W. Grimes 			    *argv);
404c06fe0a0SPeter Wemm 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
405c06fe0a0SPeter Wemm 		    init_flags, options, fs->fs_mntops);
4068fae3551SRodney W. Grimes 		break;
4078fae3551SRodney W. Grimes 	case 2:
4088fae3551SRodney W. Grimes 		/*
409cf96af72SBrian Feldman 		 * If -t flag has not been specified, the path cannot be
410003dbca6SMaxime Henrion 		 * found, spec contains either a ':' or a '@', then assume
411cf96af72SBrian Feldman 		 * that an NFS file system is being specified ala Sun.
412003dbca6SMaxime Henrion 		 * Check if the hostname contains only allowed characters
413003dbca6SMaxime Henrion 		 * to reduce false positives.  IPv6 addresses containing
414003dbca6SMaxime Henrion 		 * ':' will be correctly parsed only if the separator is '@'.
415003dbca6SMaxime Henrion 		 * The definition of a valid hostname is taken from RFC 1034.
4168fae3551SRodney W. Grimes 		 */
41705779418SIan Dowse 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
41805779418SIan Dowse 		    (ep = strchr(argv[0], ':')) != NULL)) {
419da85c82bSMaxime Henrion 			if (*ep == '@') {
420da85c82bSMaxime Henrion 				cp = ep + 1;
421da85c82bSMaxime Henrion 				ep = cp + strlen(cp);
422da85c82bSMaxime Henrion 			} else
423003dbca6SMaxime Henrion 				cp = argv[0];
424003dbca6SMaxime Henrion 			while (cp != ep) {
425003dbca6SMaxime Henrion 				if (!isdigit(*cp) && !isalpha(*cp) &&
426003dbca6SMaxime Henrion 				    *cp != '.' && *cp != '-' && *cp != ':')
427003dbca6SMaxime Henrion 					break;
428003dbca6SMaxime Henrion 				cp++;
429003dbca6SMaxime Henrion 			}
430003dbca6SMaxime Henrion 			if (cp == ep)
4318fae3551SRodney W. Grimes 				vfstype = "nfs";
432003dbca6SMaxime Henrion 		}
4338fae3551SRodney W. Grimes 		rval = mountfs(vfstype,
4348fae3551SRodney W. Grimes 		    argv[0], argv[1], init_flags, options, NULL);
4358fae3551SRodney W. Grimes 		break;
4368fae3551SRodney W. Grimes 	default:
4378fae3551SRodney W. Grimes 		usage();
4388fae3551SRodney W. Grimes 		/* NOTREACHED */
4398fae3551SRodney W. Grimes 	}
4408fae3551SRodney W. Grimes 
4418fae3551SRodney W. Grimes 	/*
4428fae3551SRodney W. Grimes 	 * If the mount was successfully, and done by root, tell mountd the
443e9988cedSPawel Jakub Dawidek 	 * good news.
4448fae3551SRodney W. Grimes 	 */
445*183e8aa7SYan-Hao Wang 	if (rval == EXIT_SUCCESS && getuid() == 0)
446e9988cedSPawel Jakub Dawidek 		restart_mountd();
4478fae3551SRodney W. Grimes 
448e725ee7eSCameron Katri 	EXIT(rval);
4498fae3551SRodney W. Grimes }
4508fae3551SRodney W. Grimes 
4518fae3551SRodney W. Grimes int
ismounted(struct fstab * fs,struct statfs * mntbuf,int mntsize)452e24dc56aSCraig Rodrigues ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
453fba1c154SSteve Price {
45490742659SPawel Jakub Dawidek 	char realfsfile[PATH_MAX];
455fba1c154SSteve Price 	int i;
456fba1c154SSteve Price 
457fba1c154SSteve Price 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
458fba1c154SSteve Price 		/* the root file system can always be remounted */
459fba1c154SSteve Price 		return (0);
460fba1c154SSteve Price 
46190742659SPawel Jakub Dawidek 	/* The user may have specified a symlink in fstab, resolve the path */
46290742659SPawel Jakub Dawidek 	if (realpath(fs->fs_file, realfsfile) == NULL) {
46390742659SPawel Jakub Dawidek 		/* Cannot resolve the path, use original one */
46490742659SPawel Jakub Dawidek 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
46590742659SPawel Jakub Dawidek 	}
46690742659SPawel Jakub Dawidek 
467b5bbeb21SAlan Somers 	/*
468b5bbeb21SAlan Somers 	 * Consider the filesystem to be mounted if:
469c09770b2SGordon Bergling 	 * It has the same mountpoint as a mounted filesystem, and
470b5bbeb21SAlan Somers 	 * It has the same type as that same mounted filesystem, and
471b5bbeb21SAlan Somers 	 * It has the same device name as that same mounted filesystem, OR
472b5bbeb21SAlan Somers 	 *     It is a nonremountable filesystem
473b5bbeb21SAlan Somers 	 */
474fba1c154SSteve Price 	for (i = mntsize - 1; i >= 0; --i)
47590742659SPawel Jakub Dawidek 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
476b5bbeb21SAlan Somers 		    strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
477fba1c154SSteve Price 		    (!isremountable(fs->fs_vfstype) ||
478b5bbeb21SAlan Somers 		     (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
479fba1c154SSteve Price 			return (1);
480fba1c154SSteve Price 	return (0);
481fba1c154SSteve Price }
482fba1c154SSteve Price 
483fba1c154SSteve Price int
isremountable(const char * vfsname)484e24dc56aSCraig Rodrigues isremountable(const char *vfsname)
485fba1c154SSteve Price {
486fba1c154SSteve Price 	const char **cp;
487fba1c154SSteve Price 
488fba1c154SSteve Price 	for (cp = remountable_fs_names; *cp; cp++)
489fba1c154SSteve Price 		if (strcmp(*cp, vfsname) == 0)
490fba1c154SSteve Price 			return (1);
491fba1c154SSteve Price 	return (0);
492fba1c154SSteve Price }
493fba1c154SSteve Price 
494fba1c154SSteve Price int
allow_file_mount(const char * vfsname)495a3f714c4SDoug Rabson allow_file_mount(const char *vfsname)
496a3f714c4SDoug Rabson {
497a3f714c4SDoug Rabson 
498a3f714c4SDoug Rabson 	if (strcmp(vfsname, "nullfs") == 0)
499a3f714c4SDoug Rabson 		return (1);
500a3f714c4SDoug Rabson 	return (0);
501a3f714c4SDoug Rabson }
502a3f714c4SDoug Rabson 
503a3f714c4SDoug Rabson int
hasopt(const char * mntopts,const char * option)504e24dc56aSCraig Rodrigues hasopt(const char *mntopts, const char *option)
505c06fe0a0SPeter Wemm {
506c06fe0a0SPeter Wemm 	int negative, found;
507c06fe0a0SPeter Wemm 	char *opt, *optbuf;
508c06fe0a0SPeter Wemm 
509c06fe0a0SPeter Wemm 	if (option[0] == 'n' && option[1] == 'o') {
510c06fe0a0SPeter Wemm 		negative = 1;
511c06fe0a0SPeter Wemm 		option += 2;
512c06fe0a0SPeter Wemm 	} else
513c06fe0a0SPeter Wemm 		negative = 0;
514c06fe0a0SPeter Wemm 	optbuf = strdup(mntopts);
515c06fe0a0SPeter Wemm 	found = 0;
516c06fe0a0SPeter Wemm 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
517c06fe0a0SPeter Wemm 		if (opt[0] == 'n' && opt[1] == 'o') {
518c06fe0a0SPeter Wemm 			if (!strcasecmp(opt + 2, option))
519c06fe0a0SPeter Wemm 				found = negative;
520c06fe0a0SPeter Wemm 		} else if (!strcasecmp(opt, option))
521c06fe0a0SPeter Wemm 			found = !negative;
522c06fe0a0SPeter Wemm 	}
523c06fe0a0SPeter Wemm 	free(optbuf);
524c06fe0a0SPeter Wemm 	return (found);
525c06fe0a0SPeter Wemm }
526c06fe0a0SPeter Wemm 
527a86de995SDavid E. O'Brien static void
append_arg(struct cpa * sa,char * arg)528a86de995SDavid E. O'Brien append_arg(struct cpa *sa, char *arg)
529a86de995SDavid E. O'Brien {
5300e969ed7SDavid E. O'Brien 	if (sa->c + 1 == sa->sz) {
5310e969ed7SDavid E. O'Brien 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
532e96092e8SUlrich Spörlein 		sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
53300356118SDavid E. O'Brien 		if (sa->a == NULL)
534e725ee7eSCameron Katri 			xo_errx(1, "realloc failed");
53500356118SDavid E. O'Brien 	}
536a86de995SDavid E. O'Brien 	sa->a[++sa->c] = arg;
537a86de995SDavid E. O'Brien }
538a86de995SDavid E. O'Brien 
539c06fe0a0SPeter Wemm int
mountfs(const char * vfstype,const char * spec,const char * name,int flags,const char * options,const char * mntopts)540e24dc56aSCraig Rodrigues mountfs(const char *vfstype, const char *spec, const char *name, int flags,
541e24dc56aSCraig Rodrigues 	const char *options, const char *mntopts)
5428fae3551SRodney W. Grimes {
5438fae3551SRodney W. Grimes 	struct statfs sf;
54476c46216SDavid E. O'Brien 	int i, ret;
54568dd1ff4SWarner Losh 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
5460e969ed7SDavid E. O'Brien 	static struct cpa mnt_argv;
5478fae3551SRodney W. Grimes 
54873dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
549a3f714c4SDoug Rabson 	if (allow_file_mount(vfstype)) {
550a3f714c4SDoug Rabson 		if (checkpath_allow_file(name, mntpath) != 0) {
551a3f714c4SDoug Rabson 			xo_warn("%s", mntpath);
552a3f714c4SDoug Rabson 			return (1);
553a3f714c4SDoug Rabson 		}
554a3f714c4SDoug Rabson 	} else {
555d3250014SJaakko Heinonen 		if (checkpath(name, mntpath) != 0) {
556e725ee7eSCameron Katri 			xo_warn("%s", mntpath);
557d3250014SJaakko Heinonen 			return (1);
558d3250014SJaakko Heinonen 		}
559a3f714c4SDoug Rabson 	}
5608fae3551SRodney W. Grimes 	name = mntpath;
5618fae3551SRodney W. Grimes 
562c06fe0a0SPeter Wemm 	if (mntopts == NULL)
563c06fe0a0SPeter Wemm 		mntopts = "";
5648fae3551SRodney W. Grimes 	optbuf = catopt(strdup(mntopts), options);
5658fae3551SRodney W. Grimes 
5668fae3551SRodney W. Grimes 	if (strcmp(name, "/") == 0)
5678fae3551SRodney W. Grimes 		flags |= MNT_UPDATE;
5688fae3551SRodney W. Grimes 	if (flags & MNT_FORCE)
5698fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "force");
5708fae3551SRodney W. Grimes 	if (flags & MNT_RDONLY)
5718fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "ro");
5728fae3551SRodney W. Grimes 	/*
5738fae3551SRodney W. Grimes 	 * XXX
5748fae3551SRodney W. Grimes 	 * The mount_mfs (newfs) command uses -o to select the
575bcb1d846SPhilippe Charnier 	 * optimization mode.  We don't pass the default "-o rw"
5768fae3551SRodney W. Grimes 	 * for that reason.
5778fae3551SRodney W. Grimes 	 */
5788fae3551SRodney W. Grimes 	if (flags & MNT_UPDATE)
5798fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "update");
5808fae3551SRodney W. Grimes 
5814ccd7546SRuslan Ermilov 	/* Compatibility glue. */
5820b8c3359SWarner Losh 	if (strcmp(vfstype, "msdos") == 0)
5834ccd7546SRuslan Ermilov 		vfstype = "msdosfs";
5844ccd7546SRuslan Ermilov 
5856e34b479SColin Percival 	/* Construct the name of the appropriate mount command */
5866e34b479SColin Percival 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
5876e34b479SColin Percival 
588a86de995SDavid E. O'Brien 	mnt_argv.c = -1;
589a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, execname);
590a86de995SDavid E. O'Brien 	mangle(optbuf, &mnt_argv);
591c7835769SCraig Rodrigues 	if (mountprog != NULL)
5926e24000aSAlan Somers 		strlcpy(execname, mountprog, sizeof(execname));
593c7835769SCraig Rodrigues 
594a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(spec));
595a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(name));
596a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, NULL);
597fce5f960SDavid E. O'Brien 
5988fae3551SRodney W. Grimes 	if (debug) {
599c195c7f6SCraig Rodrigues 		if (use_mountprog(vfstype))
600e725ee7eSCameron Katri 			xo_emit("{Lwc:exec}{:execname/%s}", execname);
601c195c7f6SCraig Rodrigues 		else
602e725ee7eSCameron Katri 			xo_emit("{:execname/mount}{P: }{l:opts/-t}{P: }{l:opts/%s}", vfstype);
603a86de995SDavid E. O'Brien 		for (i = 1; i < mnt_argv.c; i++)
604e725ee7eSCameron Katri 			xo_emit("{P: }{l:opts}", mnt_argv.a[i]);
605e725ee7eSCameron Katri 		xo_emit("\n");
606d2e17ce9SJohn Baldwin 		free(optbuf);
607d2e17ce9SJohn Baldwin 		free(mountprog);
608d2e17ce9SJohn Baldwin 		mountprog = NULL;
6098fae3551SRodney W. Grimes 		return (0);
6108fae3551SRodney W. Grimes 	}
6118fae3551SRodney W. Grimes 
612b1e6b712SCraig Rodrigues 	if (use_mountprog(vfstype)) {
613a86de995SDavid E. O'Brien 		ret = exec_mountprog(name, execname, mnt_argv.a);
6146f5f1a6bSCraig Rodrigues 	} else {
615a86de995SDavid E. O'Brien 		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
6166f5f1a6bSCraig Rodrigues 	}
6176f5f1a6bSCraig Rodrigues 
6188fae3551SRodney W. Grimes 	free(optbuf);
619d2e17ce9SJohn Baldwin 	free(mountprog);
620d2e17ce9SJohn Baldwin 	mountprog = NULL;
6218fae3551SRodney W. Grimes 
6228fae3551SRodney W. Grimes 	if (verbose) {
6238fae3551SRodney W. Grimes 		if (statfs(name, &sf) < 0) {
624e725ee7eSCameron Katri 			xo_warn("statfs %s", name);
6258fae3551SRodney W. Grimes 			return (1);
6268fae3551SRodney W. Grimes 		}
627e725ee7eSCameron Katri 		if (fstab_style) {
628e725ee7eSCameron Katri 			xo_open_list("fstab");
629e725ee7eSCameron Katri 			xo_open_instance("fstab");
630a257a45eSJordan K. Hubbard 			putfsent(&sf);
631e725ee7eSCameron Katri 			xo_close_instance("fstab");
632e725ee7eSCameron Katri 			xo_close_list("fstab");
633e725ee7eSCameron Katri 		} else {
634e725ee7eSCameron Katri 			xo_open_list("mounted");
635e725ee7eSCameron Katri 			xo_open_instance("mounted");
636c06fe0a0SPeter Wemm 			prmount(&sf);
637e725ee7eSCameron Katri 			xo_close_instance("mounted");
638e725ee7eSCameron Katri 			xo_close_list("mounted");
639e725ee7eSCameron Katri 		}
6408fae3551SRodney W. Grimes 	}
6418fae3551SRodney W. Grimes 
642bbe9d7daSMatteo Riondato 	return (ret);
6438fae3551SRodney W. Grimes }
6448fae3551SRodney W. Grimes 
6458fae3551SRodney W. Grimes void
prmount(struct statfs * sfp)646e24dc56aSCraig Rodrigues prmount(struct statfs *sfp)
6478fae3551SRodney W. Grimes {
6489df3a164SBjoern A. Zeeb 	uint64_t flags;
649aedf10acSCraig Rodrigues 	unsigned int i;
65008b242aeSWarner Losh 	struct mntoptnames *o;
651c06fe0a0SPeter Wemm 	struct passwd *pw;
652e725ee7eSCameron Katri 	char *fsidbuf;
6538fae3551SRodney W. Grimes 
654260f26f0SPiotr Pawel Stefaniak 	xo_emit("{:special/%hs}{L: on }{:node/%hs}{L: (}{:fstype}", sfp->f_mntfromname,
655e725ee7eSCameron Katri 	    sfp->f_mntonname, sfp->f_fstypename);
6568fae3551SRodney W. Grimes 
657c06fe0a0SPeter Wemm 	flags = sfp->f_flags & MNT_VISFLAGMASK;
6589df3a164SBjoern A. Zeeb 	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
6598fae3551SRodney W. Grimes 		if (flags & o->o_opt) {
660e725ee7eSCameron Katri 			xo_emit("{D:, }{l:opts}", o->o_name);
6618fae3551SRodney W. Grimes 			flags &= ~o->o_opt;
6628fae3551SRodney W. Grimes 		}
663dc9c6194SPawel Jakub Dawidek 	/*
664dc9c6194SPawel Jakub Dawidek 	 * Inform when file system is mounted by an unprivileged user
665dc9c6194SPawel Jakub Dawidek 	 * or privileged non-root user.
666dc9c6194SPawel Jakub Dawidek 	 */
667ddb842ccSJacques Vidrine 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
668e725ee7eSCameron Katri 		xo_emit("{D:, }{L:mounted by }");
669c06fe0a0SPeter Wemm 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
670260f26f0SPiotr Pawel Stefaniak 			xo_emit("{:mounter/%hs}", pw->pw_name);
671c06fe0a0SPeter Wemm 		else
672260f26f0SPiotr Pawel Stefaniak 			xo_emit("{:mounter/%hs}", sfp->f_owner);
673c06fe0a0SPeter Wemm 	}
674c62ffab6SSheldon Hearn 	if (verbose) {
675e725ee7eSCameron Katri 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) {
676e725ee7eSCameron Katri 			xo_open_container("writes");
677e725ee7eSCameron Katri 			xo_emit("{D:, }{Lwc:writes}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
67896c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncwrites,
67996c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncwrites);
680e725ee7eSCameron Katri 			xo_close_container("writes");
681e725ee7eSCameron Katri 		}
682e725ee7eSCameron Katri 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) {
683e725ee7eSCameron Katri 			xo_open_container("reads");
684e725ee7eSCameron Katri 			xo_emit("{D:, }{Lwc:reads}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
68596c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncreads,
68696c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncreads);
687e725ee7eSCameron Katri 			xo_close_container("reads");
688e725ee7eSCameron Katri 		}
68905779418SIan Dowse 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
690e725ee7eSCameron Katri 			fsidbuf = malloc(sizeof(sfp->f_fsid) * 2 + 1);
691e725ee7eSCameron Katri 			if (fsidbuf == NULL)
692e725ee7eSCameron Katri 				xo_errx(1, "malloc failed");
69338f102c2SIan Dowse 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
694e725ee7eSCameron Katri 				sprintf(&fsidbuf[i * 2], "%02x",
695e725ee7eSCameron Katri 				    ((u_char *)&sfp->f_fsid)[i]);
696e725ee7eSCameron Katri 			fsidbuf[i * 2] = '\0';
697e725ee7eSCameron Katri 			xo_emit("{D:, }{Lw:fsid}{:fsid}", fsidbuf);
698e725ee7eSCameron Katri 			free(fsidbuf);
699c62ffab6SSheldon Hearn 		}
700ce00b119SDoug Ambrisko 		if (sfp->f_nvnodelistsize != 0) {
7016468cd8eSDoug Ambrisko 			xo_open_container("vnodes");
702ce00b119SDoug Ambrisko 			xo_emit("{D:, }{Lwc:vnodes}{Lw:count}{w:count/%ju}",
703ce00b119SDoug Ambrisko 			    (uintmax_t)sfp->f_nvnodelistsize);
7046468cd8eSDoug Ambrisko 			xo_close_container("vnodes");
7056468cd8eSDoug Ambrisko 		}
70605779418SIan Dowse 	}
707e725ee7eSCameron Katri 	xo_emit("{D:)}\n");
7088fae3551SRodney W. Grimes }
7098fae3551SRodney W. Grimes 
7108fae3551SRodney W. Grimes char *
catopt(char * s0,const char * s1)711e24dc56aSCraig Rodrigues catopt(char *s0, const char *s1)
7128fae3551SRodney W. Grimes {
7138fae3551SRodney W. Grimes 	char *cp;
7148fae3551SRodney W. Grimes 
71518af6044SJoseph Koshy 	if (s1 == NULL || *s1 == '\0')
7164796c6ccSJuli Mallett 		return (s0);
71718af6044SJoseph Koshy 
7188fae3551SRodney W. Grimes 	if (s0 && *s0) {
7198967299bSPedro F. Giffuni 		if (asprintf(&cp, "%s,%s", s0, s1) == -1)
720e725ee7eSCameron Katri 			xo_errx(1, "asprintf failed");
7218fae3551SRodney W. Grimes 	} else
7228fae3551SRodney W. Grimes 		cp = strdup(s1);
7238fae3551SRodney W. Grimes 
7248fae3551SRodney W. Grimes 	if (s0)
7258fae3551SRodney W. Grimes 		free(s0);
7268fae3551SRodney W. Grimes 	return (cp);
7278fae3551SRodney W. Grimes }
7288fae3551SRodney W. Grimes 
7298fae3551SRodney W. Grimes void
mangle(char * options,struct cpa * a)730a86de995SDavid E. O'Brien mangle(char *options, struct cpa *a)
7318fae3551SRodney W. Grimes {
732c7835769SCraig Rodrigues 	char *p, *s, *val;
7338fae3551SRodney W. Grimes 
7348fae3551SRodney W. Grimes 	for (s = options; (p = strsep(&s, ",")) != NULL;)
73518af6044SJoseph Koshy 		if (*p != '\0') {
736d9fa6ce7SCraig Rodrigues 			if (strcmp(p, "noauto") == 0) {
737d9fa6ce7SCraig Rodrigues 				/*
738d9fa6ce7SCraig Rodrigues 				 * Do not pass noauto option to nmount().
739d9fa6ce7SCraig Rodrigues 				 * or external mount program.  noauto is
740d9fa6ce7SCraig Rodrigues 				 * only used to prevent mounting a filesystem
741d9fa6ce7SCraig Rodrigues 				 * when 'mount -a' is specified, and is
742d9fa6ce7SCraig Rodrigues 				 * not a real mount option.
743d9fa6ce7SCraig Rodrigues 				 */
744d9fa6ce7SCraig Rodrigues 				continue;
7454b4f9170SDag-Erling Smørgrav 			} else if (strcmp(p, "late") == 0) {
7464b4f9170SDag-Erling Smørgrav 				/*
7474b4f9170SDag-Erling Smørgrav 				 * "late" is used to prevent certain file
7484b4f9170SDag-Erling Smørgrav 				 * systems from being mounted before late
7494b4f9170SDag-Erling Smørgrav 				 * in the boot cycle; for instance,
7504b4f9170SDag-Erling Smørgrav 				 * loopback NFS mounts can't be mounted
7514b4f9170SDag-Erling Smørgrav 				 * before mountd starts.
7524b4f9170SDag-Erling Smørgrav 				 */
7534b4f9170SDag-Erling Smørgrav 				continue;
754c7383075SXin LI 			} else if (strcmp(p, "failok") == 0) {
755c7383075SXin LI 				/*
756c7383075SXin LI 				 * "failok" is used to prevent certain file
757c7383075SXin LI 				 * systems from being causing the system to
758c7383075SXin LI 				 * drop into single user mode in the boot
759c7383075SXin LI 				 * cycle, and is not a real mount option.
760c7383075SXin LI 				 */
761c7383075SXin LI 				continue;
762c7835769SCraig Rodrigues 			} else if (strncmp(p, "mountprog", 9) == 0) {
763c7835769SCraig Rodrigues 				/*
764c7835769SCraig Rodrigues 				 * "mountprog" is used to force the use of
765c7835769SCraig Rodrigues 				 * userland mount programs.
766c7835769SCraig Rodrigues 				 */
767c7835769SCraig Rodrigues 				val = strchr(p, '=');
768c7835769SCraig Rodrigues                         	if (val != NULL) {
769c7835769SCraig Rodrigues                                 	++val;
770c7835769SCraig Rodrigues 					if (*val != '\0')
771c7835769SCraig Rodrigues 						mountprog = strdup(val);
772c7835769SCraig Rodrigues 				}
773c7835769SCraig Rodrigues 
774c7835769SCraig Rodrigues 				if (mountprog == NULL) {
775e725ee7eSCameron Katri 					xo_errx(1, "Need value for -o mountprog");
776c7835769SCraig Rodrigues 				}
777c7835769SCraig Rodrigues 				continue;
77835d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "userquota") == 0) {
77935d6c7f5SCraig Rodrigues 				continue;
7806e74fb9dSMaxim Konovalov 			} else if (strncmp(p, userquotaeq,
7816e74fb9dSMaxim Konovalov 			    sizeof(userquotaeq) - 1) == 0) {
7826e74fb9dSMaxim Konovalov 				continue;
78335d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "groupquota") == 0) {
78435d6c7f5SCraig Rodrigues 				continue;
7856e74fb9dSMaxim Konovalov 			} else if (strncmp(p, groupquotaeq,
7866e74fb9dSMaxim Konovalov 			    sizeof(groupquotaeq) - 1) == 0) {
7876e74fb9dSMaxim Konovalov 				continue;
788d9fa6ce7SCraig Rodrigues 			} else if (*p == '-') {
789a86de995SDavid E. O'Brien 				append_arg(a, p);
7908fae3551SRodney W. Grimes 				p = strchr(p, '=');
791adfdbe22STom Rhodes 				if (p != NULL) {
7928fae3551SRodney W. Grimes 					*p = '\0';
793a86de995SDavid E. O'Brien 					append_arg(a, p + 1);
7948fae3551SRodney W. Grimes 				}
795748e259bSCraig Rodrigues 			} else {
796a86de995SDavid E. O'Brien 				append_arg(a, strdup("-o"));
797a86de995SDavid E. O'Brien 				append_arg(a, p);
7988fae3551SRodney W. Grimes 			}
79918af6044SJoseph Koshy 		}
8008fae3551SRodney W. Grimes }
8018fae3551SRodney W. Grimes 
80218af6044SJoseph Koshy 
80318af6044SJoseph Koshy char *
update_options(char * opts,char * fstab,int curflags)8044796c6ccSJuli Mallett update_options(char *opts, char *fstab, int curflags)
80518af6044SJoseph Koshy {
80618af6044SJoseph Koshy 	char *o, *p;
80718af6044SJoseph Koshy 	char *cur;
80818af6044SJoseph Koshy 	char *expopt, *newopt, *tmpopt;
80918af6044SJoseph Koshy 
81018af6044SJoseph Koshy 	if (opts == NULL)
8114796c6ccSJuli Mallett 		return (strdup(""));
81218af6044SJoseph Koshy 
81318af6044SJoseph Koshy 	/* remove meta options from list */
81418af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
81518af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
81618af6044SJoseph Koshy 	cur = flags2opts(curflags);
81718af6044SJoseph Koshy 
81818af6044SJoseph Koshy 	/*
81918af6044SJoseph Koshy 	 * Expand all meta-options passed to us first.
82018af6044SJoseph Koshy 	 */
82118af6044SJoseph Koshy 	expopt = NULL;
82218af6044SJoseph Koshy 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
82318af6044SJoseph Koshy 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
82418af6044SJoseph Koshy 			expopt = catopt(expopt, fstab);
82518af6044SJoseph Koshy 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
82618af6044SJoseph Koshy 			expopt = catopt(expopt, cur);
82718af6044SJoseph Koshy 		else
82818af6044SJoseph Koshy 			expopt = catopt(expopt, o);
82918af6044SJoseph Koshy 	}
83018af6044SJoseph Koshy 	free(cur);
83118af6044SJoseph Koshy 	free(opts);
83218af6044SJoseph Koshy 
83318af6044SJoseph Koshy 	/*
83418af6044SJoseph Koshy 	 * Remove previous contradictory arguments. Given option "foo" we
83518af6044SJoseph Koshy 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
83618af6044SJoseph Koshy 	 * and "foo" - so we can deal with possible options like "notice".
83718af6044SJoseph Koshy 	 */
83818af6044SJoseph Koshy 	newopt = NULL;
83918af6044SJoseph Koshy 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
84018af6044SJoseph Koshy 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
841e725ee7eSCameron Katri 			xo_errx(1, "malloc failed");
84218af6044SJoseph Koshy 
84318af6044SJoseph Koshy 		strcpy(tmpopt, "no");
84418af6044SJoseph Koshy 		strcat(tmpopt, o);
84518af6044SJoseph Koshy 		remopt(newopt, tmpopt);
84618af6044SJoseph Koshy 		free(tmpopt);
84718af6044SJoseph Koshy 
84818af6044SJoseph Koshy 		if (strncmp("no", o, 2) == 0)
84918af6044SJoseph Koshy 			remopt(newopt, o+2);
85018af6044SJoseph Koshy 
85118af6044SJoseph Koshy 		newopt = catopt(newopt, o);
85218af6044SJoseph Koshy 	}
85318af6044SJoseph Koshy 	free(expopt);
85418af6044SJoseph Koshy 
8554796c6ccSJuli Mallett 	return (newopt);
85618af6044SJoseph Koshy }
85718af6044SJoseph Koshy 
85818af6044SJoseph Koshy void
remopt(char * string,const char * opt)8594796c6ccSJuli Mallett remopt(char *string, const char *opt)
86018af6044SJoseph Koshy {
86118af6044SJoseph Koshy 	char *o, *p, *r;
86218af6044SJoseph Koshy 
86318af6044SJoseph Koshy 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
86418af6044SJoseph Koshy 		return;
86518af6044SJoseph Koshy 
86618af6044SJoseph Koshy 	r = string;
86718af6044SJoseph Koshy 
86818af6044SJoseph Koshy 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
86918af6044SJoseph Koshy 		if (strcmp(opt, o) != 0) {
87018af6044SJoseph Koshy 			if (*r == ',' && *o != '\0')
87118af6044SJoseph Koshy 				r++;
87218af6044SJoseph Koshy 			while ((*r++ = *o++) != '\0')
87318af6044SJoseph Koshy 			    ;
87418af6044SJoseph Koshy 			*--r = ',';
87518af6044SJoseph Koshy 		}
87618af6044SJoseph Koshy 	}
87718af6044SJoseph Koshy 	*r = '\0';
87818af6044SJoseph Koshy }
87918af6044SJoseph Koshy 
8808fae3551SRodney W. Grimes void
usage(void)8814796c6ccSJuli Mallett usage(void)
8828fae3551SRodney W. Grimes {
8838fae3551SRodney W. Grimes 
884e725ee7eSCameron Katri 	xo_error("%s\n%s\n%s\n",
8854b4f9170SDag-Erling Smørgrav "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
8868d646af5SRuslan Ermilov "       mount [-dfpruvw] special | node",
8878d646af5SRuslan Ermilov "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
888*183e8aa7SYan-Hao Wang 	EXIT(EXIT_FAILURE);
8898fae3551SRodney W. Grimes }
890a257a45eSJordan K. Hubbard 
891a257a45eSJordan K. Hubbard void
putfsent(struct statfs * ent)892031ea52fSMatteo Riondato putfsent(struct statfs *ent)
893a257a45eSJordan K. Hubbard {
894a257a45eSJordan K. Hubbard 	struct fstab *fst;
89572da5470SJaakko Heinonen 	char *opts, *rw;
896306d73d6SPoul-Henning Kamp 	int l;
897a257a45eSJordan K. Hubbard 
89872da5470SJaakko Heinonen 	opts = NULL;
89972da5470SJaakko Heinonen 	/* flags2opts() doesn't return the "rw" option. */
90072da5470SJaakko Heinonen 	if ((ent->f_flags & MNT_RDONLY) != 0)
90172da5470SJaakko Heinonen 		rw = NULL;
90272da5470SJaakko Heinonen 	else
90372da5470SJaakko Heinonen 		rw = catopt(NULL, "rw");
90472da5470SJaakko Heinonen 
90518af6044SJoseph Koshy 	opts = flags2opts(ent->f_flags);
90672da5470SJaakko Heinonen 	opts = catopt(rw, opts);
9073ae7ea68SGiorgos Keramidas 
908031ea52fSMatteo Riondato 	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
909031ea52fSMatteo Riondato 	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
9106e24000aSAlan Somers 		strlcpy(ent->f_mntfromname,
9116e24000aSAlan Somers 		    (strnstr(ent->f_mntfromname, ":", 8) +1),
9126e24000aSAlan Somers 		    sizeof(ent->f_mntfromname));
913031ea52fSMatteo Riondato 	}
914031ea52fSMatteo Riondato 
915306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntfromname);
916e725ee7eSCameron Katri 	xo_emit("{:device}{P:/%s}{P:/%s}{P:/%s}",
917e725ee7eSCameron Katri 	    ent->f_mntfromname,
918306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
919306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
920306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
921306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntonname);
922e725ee7eSCameron Katri 	xo_emit("{:mntpoint}{P:/%s}{P:/%s}{P:/%s}",
923e725ee7eSCameron Katri 	    ent->f_mntonname,
924306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
925306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
926306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
927e725ee7eSCameron Katri 	xo_emit("{:fstype}{P:\t}", ent->f_fstypename);
928306d73d6SPoul-Henning Kamp 	l = strlen(opts);
929e725ee7eSCameron Katri 	xo_emit("{:opts}{P:/%s}", opts,
930306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : " ");
93118af6044SJoseph Koshy 	free(opts);
932c06fe0a0SPeter Wemm 
933fba1c154SSteve Price 	if ((fst = getfsspec(ent->f_mntfromname)))
934e725ee7eSCameron Katri 		xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
935e725ee7eSCameron Katri 		    fst->fs_freq, fst->fs_passno);
936fba1c154SSteve Price 	else if ((fst = getfsfile(ent->f_mntonname)))
937e725ee7eSCameron Katri 		xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
938e725ee7eSCameron Katri 		    fst->fs_freq, fst->fs_passno);
939ab80d6faSBrian Feldman 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
940ab80d6faSBrian Feldman 		if (strcmp(ent->f_mntonname, "/") == 0)
941e725ee7eSCameron Katri 			xo_emit("{P:\t}{n:dump/1}{P: }{n:pass/1}\n");
942a257a45eSJordan K. Hubbard 		else
943e725ee7eSCameron Katri 			xo_emit("{P:\t}{n:dump/2}{P: }{n:pass/2}\n");
944ab80d6faSBrian Feldman 	} else
945e725ee7eSCameron Katri 		xo_emit("{P:\t}{n:dump/0}{P: }{n:pass/0}\n");
946a257a45eSJordan K. Hubbard }
94718af6044SJoseph Koshy 
94818af6044SJoseph Koshy 
94918af6044SJoseph Koshy char *
flags2opts(int flags)9504796c6ccSJuli Mallett flags2opts(int flags)
95118af6044SJoseph Koshy {
95218af6044SJoseph Koshy 	char *res;
95318af6044SJoseph Koshy 
95418af6044SJoseph Koshy 	res = NULL;
95518af6044SJoseph Koshy 
95688e2c335SCraig Rodrigues 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
95718af6044SJoseph Koshy 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
95818af6044SJoseph Koshy 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
95918af6044SJoseph Koshy 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
96018af6044SJoseph Koshy 	if (flags & MNT_UNION)		res = catopt(res, "union");
96118af6044SJoseph Koshy 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
96218af6044SJoseph Koshy 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
96318af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
96418af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
96518af6044SJoseph Koshy 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
96618af6044SJoseph Koshy 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
967ba0fbe96SRobert Watson 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
96803d94b50SRobert Watson 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
9699340fc72SEdward Tomasz Napierala 	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
970daba4da8SKirk McKusick 	if (flags & MNT_UNTRUSTED)	res = catopt(res, "untrusted");
971ba7a55d9SSean Eric Fagan 	if (flags & MNT_NOCOVER)	res = catopt(res, "nocover");
972ba7a55d9SSean Eric Fagan 	if (flags & MNT_EMPTYDIR)	res = catopt(res, "emptydir");
97318af6044SJoseph Koshy 
9744796c6ccSJuli Mallett 	return (res);
97518af6044SJoseph Koshy }
976