xref: /freebsd/sbin/mount_nullfs/mount_nullfs.c (revision a123502ef781ea052e842af25c7aeb64fd9fe217)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/uio.h>
39 
40 #include <err.h>
41 #include <mntopts.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sysexits.h>
46 #include <unistd.h>
47 
48 static void	usage(void) __dead2;
49 
50 static int
stat_realpath(const char * path,char * resolved,struct stat * sbp)51 stat_realpath(const char *path, char *resolved, struct stat *sbp)
52 {
53 	if (realpath(path, resolved) == NULL || stat(resolved, sbp) != 0)
54 		return (1);
55 	return (0);
56 }
57 
58 int
main(int argc,char * argv[])59 main(int argc, char *argv[])
60 {
61 	struct iovec *iov;
62 	char *p, *val;
63 	char mountpoint[MAXPATHLEN];
64 	char target[MAXPATHLEN];
65 	char errmsg[255];
66 	int ch, iovlen;
67 	char nullfs[] = "nullfs";
68 	struct stat target_stat;
69 	struct stat mountpoint_stat;
70 
71 	iov = NULL;
72 	iovlen = 0;
73 	errmsg[0] = '\0';
74 	while ((ch = getopt(argc, argv, "o:")) != -1)
75 		switch(ch) {
76 		case 'o':
77 			val = strdup("");
78 			p = strchr(optarg, '=');
79 			if (p != NULL) {
80 				free(val);
81 				*p = '\0';
82 				val = p + 1;
83 			}
84 			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
85 			break;
86 		case '?':
87 		default:
88 			usage();
89 		}
90 	argc -= optind;
91 	argv += optind;
92 
93 	if (argc != 2)
94 		usage();
95 
96 	/* resolve target and mountpoint with realpath(3) */
97 	if (stat_realpath(argv[0], target, &target_stat) != 0)
98 		err(EX_USAGE, "%s", target);
99 	if (stat_realpath(argv[1], mountpoint, &mountpoint_stat) != 0)
100 		err(EX_USAGE, "%s", mountpoint);
101 	if (!S_ISDIR(target_stat.st_mode) && !S_ISREG(target_stat.st_mode))
102 		errx(EX_USAGE, "%s: must be either a file or directory",
103 		    target);
104 	if ((target_stat.st_mode & S_IFMT) !=
105 	    (mountpoint_stat.st_mode & S_IFMT))
106 		errx(EX_USAGE,
107 		    "%s: must be same type as %s (file or directory)",
108 		    mountpoint, target);
109 
110 	build_iovec(&iov, &iovlen, "fstype", nullfs, (size_t)-1);
111 	build_iovec(&iov, &iovlen, "fspath", mountpoint, (size_t)-1);
112 	build_iovec(&iov, &iovlen, "target", target, (size_t)-1);
113 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
114 	if (nmount(iov, iovlen, 0) < 0) {
115 		if (errmsg[0] != 0)
116 			err(1, "%s: %s", mountpoint, errmsg);
117 		else
118 			err(1, "%s", mountpoint);
119 	}
120 	exit(0);
121 }
122 
123 static void
usage(void)124 usage(void)
125 {
126 	(void)fprintf(stderr,
127 		"usage: mount_nullfs [-o options] target mount-point\n");
128 	exit(1);
129 }
130