2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California.
6 * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
7 * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org>
10 * This code is derived from software donated to Berkeley by
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char copyright[] =
40 "@(#) Copyright (c) 1992, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n";
46 static char sccsid[] = "@(#)mount_union.c 8.5 (Berkeley) 3/27/94";
48 static const char rcsid[] =
53 #include <sys/param.h>
54 #include <sys/mount.h>
56 #include <sys/errno.h>
70 subdir(const char *p, const char *dir)
78 if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
88 "usage: mount_unionfs [-o options] directory uniondir\n");
93 parse_gid(const char *s, char *buf, size_t bufsize)
98 if ((gr = getgrnam(s)) != NULL)
99 snprintf(buf, bufsize, "%d", gr->gr_gid);
101 strtol(s, &inval, 10);
103 errx(EX_NOUSER, "unknown group id: %s", s);
106 strncpy(buf, s, bufsize);
112 parse_uid(const char *s, char *buf, size_t bufsize)
117 if ((pw = getpwnam(s)) != NULL)
118 snprintf(buf, bufsize, "%d", pw->pw_uid);
120 strtol(s, &inval, 10);
122 errx(EX_NOUSER, "unknown user id: %s", s);
125 strncpy(buf, s, bufsize);
131 main(int argc, char *argv[])
135 char source [MAXPATHLEN], target[MAXPATHLEN], errmsg[255];
136 char uid_str[20], gid_str[20];
137 char fstype[] = "unionfs";
142 memset(errmsg, 0, sizeof(errmsg));
144 while ((ch = getopt(argc, argv, "bo:")) != -1) {
147 printf("\n -b is deprecated. Use \"-o below\" instead\n");
148 build_iovec(&iov, &iovlen, "below", NULL, 0);
151 p = strchr(optarg, '=');
156 if (strcmp(optarg, "gid") == 0) {
157 parse_gid(val, gid_str, sizeof(gid_str));
160 else if (strcmp(optarg, "uid") == 0) {
161 parse_uid(val, uid_str, sizeof(uid_str));
165 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
179 /* resolve both target and source with realpath(3) */
180 if (checkpath(argv[0], target) != 0)
181 err(EX_USAGE, "%s", target);
182 if (checkpath(argv[1], source) != 0)
183 err(EX_USAGE, "%s", source);
185 if (subdir(target, source) || subdir(source, target))
186 errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths",
187 argv[0], target, argv[1], source);
189 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
190 build_iovec(&iov, &iovlen, "fspath", source, (size_t)-1);
191 build_iovec(&iov, &iovlen, "from", target, (size_t)-1);
192 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
194 if (nmount(iov, iovlen, 0))
195 err(EX_OSERR, "%s: %s", source, errmsg);