mount_unionfs.c revision 26071
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
38char 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
44static char sccsid[] = "@(#)mount_union.c	8.5 (Berkeley) 3/27/94";
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/mount.h>
49
50#include <miscfs/union/union.h>
51
52#include <err.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <sysexits.h>
57#include <unistd.h>
58
59#include "mntopts.h"
60
61static struct mntopt mopts[] = {
62	MOPT_STDOPTS,
63	{ NULL }
64};
65
66static int	subdir __P((const char *, const char *));
67static void	usage __P((void)) __dead2;
68
69int
70main(argc, argv)
71	int argc;
72	char *argv[];
73{
74	struct union_args args;
75	int ch, mntflags;
76	char source[MAXPATHLEN];
77	char target[MAXPATHLEN];
78	struct vfsconf vfc;
79	int error;
80
81	mntflags = 0;
82	args.mntflags = UNMNT_ABOVE;
83	while ((ch = getopt(argc, argv, "bo:r")) != -1)
84		switch (ch) {
85		case 'b':
86			args.mntflags &= ~UNMNT_OPMASK;
87			args.mntflags |= UNMNT_BELOW;
88			break;
89		case 'o':
90			getmntopts(optarg, mopts, &mntflags, 0);
91			break;
92		case 'r':
93			args.mntflags &= ~UNMNT_OPMASK;
94			args.mntflags |= UNMNT_REPLACE;
95			break;
96		case '?':
97		default:
98			usage();
99			/* NOTREACHED */
100		}
101	argc -= optind;
102	argv += optind;
103
104	if (argc != 2)
105		usage();
106
107	if (realpath(argv[0], target) == 0)
108		err(EX_OSERR, "%s", target);
109
110	if (realpath(argv[1], source) == 0)
111		err(EX_OSERR, "%s", source);
112
113	if (subdir(target, source) || subdir(source, target))
114		errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths",
115		    argv[0], target, argv[1], source);
116
117	args.target = target;
118
119	error = getvfsbyname("union", &vfc);
120	if (error && vfsisloadable("union")) {
121		if (vfsload("union"))
122			err(EX_OSERR, "vfsload(union)");
123		endvfsent();	/* flush cache */
124		error = getvfsbyname("union", &vfc);
125	}
126	if (error)
127		errx(EX_OSERR, "union filesystem is not available");
128
129	if (mount(vfc.vfc_name, source, mntflags, &args))
130		err(EX_OSERR, target);
131	exit(0);
132}
133
134int
135subdir(p, dir)
136	const char *p;
137	const char *dir;
138{
139	int l;
140
141	l = strlen(dir);
142	if (l <= 1)
143		return (1);
144
145	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
146		return (1);
147
148	return (0);
149}
150
151void
152usage()
153{
154	(void)fprintf(stderr,
155		"usage: mount_union [-br] [-o options] target_fs mount_point\n");
156	exit(EX_USAGE);
157}
158