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