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