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
38 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <sys/errno.h>
55 subdir(const char *p, const char *dir)
63 if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
73 "usage: mount_unionfs [-o options] directory uniondir\n");
78 parse_gid(const char *s, char *buf, size_t bufsize)
83 if ((gr = getgrnam(s)) != NULL)
84 snprintf(buf, bufsize, "%d", gr->gr_gid);
86 strtol(s, &inval, 10);
88 errx(EX_NOUSER, "unknown group id: %s", s);
91 strncpy(buf, s, bufsize);
97 parse_uid(const char *s, char *buf, size_t bufsize)
102 if ((pw = getpwnam(s)) != NULL)
103 snprintf(buf, bufsize, "%d", pw->pw_uid);
105 strtol(s, &inval, 10);
107 errx(EX_NOUSER, "unknown user id: %s", s);
110 strncpy(buf, s, bufsize);
116 main(int argc, char *argv[])
120 char source [MAXPATHLEN], target[MAXPATHLEN], errmsg[255];
121 char uid_str[20], gid_str[20];
122 char fstype[] = "unionfs";
127 memset(errmsg, 0, sizeof(errmsg));
129 while ((ch = getopt(argc, argv, "bo:")) != -1) {
132 printf("\n -b is deprecated. Use \"-o below\" instead\n");
133 build_iovec(&iov, &iovlen, "below", NULL, 0);
136 p = strchr(optarg, '=');
141 if (strcmp(optarg, "gid") == 0) {
142 parse_gid(val, gid_str, sizeof(gid_str));
145 else if (strcmp(optarg, "uid") == 0) {
146 parse_uid(val, uid_str, sizeof(uid_str));
150 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
164 /* resolve both target and source with realpath(3) */
165 if (checkpath(argv[0], target) != 0)
166 err(EX_USAGE, "%s", target);
167 if (checkpath(argv[1], source) != 0)
168 err(EX_USAGE, "%s", source);
170 if (subdir(target, source) || subdir(source, target))
171 errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths",
172 argv[0], target, argv[1], source);
174 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
175 build_iovec(&iov, &iovlen, "fspath", source, (size_t)-1);
176 build_iovec(&iov, &iovlen, "from", target, (size_t)-1);
177 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
179 if (nmount(iov, iovlen, 0))
180 err(EX_OSERR, "%s: %s", source, errmsg);