xref: /freebsd/sbin/mount_nullfs/mount_nullfs.c (revision e5b786625f7f82a1fa91e41823332459ea5550f9)
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 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1992, 1993, 1994\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)mount_null.c	8.6 (Berkeley) 4/26/95";
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/mount.h>
49 #include <sys/stat.h>
50 #include <sys/uio.h>
51 
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sysexits.h>
57 #include <unistd.h>
58 
59 #include "mntopts.h"
60 
61 static void	usage(void) __dead2;
62 
63 static int
64 stat_realpath(const char *path, char *resolved, struct stat *sbp)
65 {
66 	if (realpath(path, resolved) == NULL || stat(resolved, sbp) != 0)
67 		return (1);
68 	return (0);
69 }
70 
71 int
72 main(int argc, char *argv[])
73 {
74 	struct iovec *iov;
75 	char *p, *val;
76 	char mountpoint[MAXPATHLEN];
77 	char target[MAXPATHLEN];
78 	char errmsg[255];
79 	int ch, iovlen;
80 	char nullfs[] = "nullfs";
81 	struct stat target_stat;
82 	struct stat mountpoint_stat;
83 
84 	iov = NULL;
85 	iovlen = 0;
86 	errmsg[0] = '\0';
87 	while ((ch = getopt(argc, argv, "o:")) != -1)
88 		switch(ch) {
89 		case 'o':
90 			val = strdup("");
91 			p = strchr(optarg, '=');
92 			if (p != NULL) {
93 				free(val);
94 				*p = '\0';
95 				val = p + 1;
96 			}
97 			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
98 			break;
99 		case '?':
100 		default:
101 			usage();
102 		}
103 	argc -= optind;
104 	argv += optind;
105 
106 	if (argc != 2)
107 		usage();
108 
109 	/* resolve target and mountpoint with realpath(3) */
110 	if (stat_realpath(argv[0], target, &target_stat) != 0)
111 		err(EX_USAGE, "%s", target);
112 	if (stat_realpath(argv[1], mountpoint, &mountpoint_stat) != 0)
113 		err(EX_USAGE, "%s", mountpoint);
114 	if (!S_ISDIR(target_stat.st_mode) && !S_ISREG(target_stat.st_mode))
115 		errx(EX_USAGE, "%s: must be either a file or directory",
116 		    target);
117 	if ((target_stat.st_mode & S_IFMT) !=
118 	    (mountpoint_stat.st_mode & S_IFMT))
119 		errx(EX_USAGE,
120 		    "%s: must be same type as %s (file or directory)",
121 		    mountpoint, target);
122 
123 	build_iovec(&iov, &iovlen, "fstype", nullfs, (size_t)-1);
124 	build_iovec(&iov, &iovlen, "fspath", mountpoint, (size_t)-1);
125 	build_iovec(&iov, &iovlen, "target", target, (size_t)-1);
126 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
127 	if (nmount(iov, iovlen, 0) < 0) {
128 		if (errmsg[0] != 0)
129 			err(1, "%s: %s", mountpoint, errmsg);
130 		else
131 			err(1, "%s", mountpoint);
132 	}
133 	exit(0);
134 }
135 
136 static void
137 usage(void)
138 {
139 	(void)fprintf(stderr,
140 		"usage: mount_nullfs [-o options] target mount-point\n");
141 	exit(1);
142 }
143