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