mount_unionfs.c revision 101270
1/*
2 * Copyright (c) 1992, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1992, 1993, 1994\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)mount_union.c	8.5 (Berkeley) 3/27/94";
46#else
47static const char rcsid[] =
48  "$FreeBSD: head/sbin/mount_unionfs/mount_unionfs.c 101270 2002-08-03 16:03:21Z mux $";
49#endif
50#endif /* not lint */
51
52#include <sys/param.h>
53#include <sys/mount.h>
54
55#include <err.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <sysexits.h>
60#include <unistd.h>
61
62#include "mntopts.h"
63
64static struct mntopt mopts[] = {
65	MOPT_STDOPTS,
66	{ NULL }
67};
68
69static int	subdir(const char *, const char *);
70static void	usage (void) __dead2;
71
72int
73main(argc, argv)
74	int argc;
75	char *argv[];
76{
77	struct iovec iov[8];
78	int ch, mntflags;
79	char source[MAXPATHLEN];
80	char target[MAXPATHLEN];
81	int iovcnt;
82
83	iovcnt = 6;
84	mntflags = 0;
85	while ((ch = getopt(argc, argv, "bo:r")) != -1)
86		switch (ch) {
87		case 'b':
88			iov[6].iov_base = "below";
89			iov[6].iov_len = strlen(iov[6].iov_base) + 1;
90			iov[7].iov_base = NULL;
91			iov[7].iov_len = 0;
92			iovcnt = 8;
93			break;
94		case 'o':
95			getmntopts(optarg, mopts, &mntflags, 0);
96			break;
97		case 'r':
98			iov[6].iov_base = "replace";
99			iov[6].iov_len = strlen(iov[6].iov_base) + 1;
100			iov[7].iov_base = NULL;
101			iov[7].iov_len = 0;
102			iovcnt = 8;
103			break;
104		case '?':
105		default:
106			usage();
107			/* NOTREACHED */
108		}
109	argc -= optind;
110	argv += optind;
111
112	if (argc != 2)
113		usage();
114
115	/* resolve both target and source with realpath(3) */
116	(void)checkpath(argv[0], target);
117	(void)checkpath(argv[1], source);
118
119	if (subdir(target, source) || subdir(source, target))
120		errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths",
121		    argv[0], target, argv[1], source);
122
123	iov[0].iov_base = "fstype";
124	iov[0].iov_len = strlen(iov[0].iov_base) + 1;
125	iov[1].iov_base = "unionfs";
126	iov[1].iov_len = strlen(iov[1].iov_base) + 1;
127	iov[2].iov_base = "fspath";
128	iov[2].iov_len = strlen(iov[2].iov_base) + 1;
129	iov[3].iov_base = source;
130	iov[3].iov_len = strlen(source) + 1;
131	iov[4].iov_base = "target";
132	iov[4].iov_len = strlen(iov[4].iov_base) + 1;
133	iov[5].iov_base = target;
134	iov[5].iov_len = strlen(target) + 1;
135	if (nmount(iov, iovcnt, mntflags))
136		err(EX_OSERR, "%s", target);
137	exit(0);
138}
139
140int
141subdir(p, dir)
142	const char *p;
143	const char *dir;
144{
145	int l;
146
147	l = strlen(dir);
148	if (l <= 1)
149		return (1);
150
151	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
152		return (1);
153
154	return (0);
155}
156
157void
158usage()
159{
160	(void)fprintf(stderr,
161		"usage: mount_unionfs [-br] [-o options] target_fs mount_point\n");
162	exit(EX_USAGE);
163}
164