2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 static const char copyright[] =
37 "@(#) Copyright (c) 1992, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)mount_null.c 8.6 (Berkeley) 4/26/95";
45 static const char rcsid[] =
49 #include <sys/param.h>
50 #include <sys/mount.h>
63 static void usage(void) __dead2;
66 stat_realpath(const char *path, char *resolved, struct stat *sbp)
68 if (realpath(path, resolved) == NULL || stat(resolved, sbp) != 0)
74 main(int argc, char *argv[])
78 char mountpoint[MAXPATHLEN];
79 char target[MAXPATHLEN];
82 char nullfs[] = "nullfs";
83 struct stat target_stat;
84 struct stat mountpoint_stat;
89 while ((ch = getopt(argc, argv, "o:")) != -1)
93 p = strchr(optarg, '=');
99 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
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",
119 if ((target_stat.st_mode & S_IFMT) !=
120 (mountpoint_stat.st_mode & S_IFMT))
122 "%s: must be same type as %s (file or directory)",
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) {
131 err(1, "%s: %s", mountpoint, errmsg);
133 err(1, "%s", mountpoint);
141 (void)fprintf(stderr,
142 "usage: mount_nullfs [-o options] target mount-point\n");