mount_unionfs.c revision 25275
1249423Sdim/*
2224135Sdim * Copyright (c) 1992, 1993, 1994
3224135Sdim *	The Regents of the University of California.  All rights reserved.
4224135Sdim *
5224135Sdim * This code is derived from software donated to Berkeley by
6224135Sdim * Jan-Simon Pendry.
7224135Sdim *
8224135Sdim * Redistribution and use in source and binary forms, with or without
9224135Sdim * modification, are permitted provided that the following conditions
10224135Sdim * are met:
11224135Sdim * 1. Redistributions of source code must retain the above copyright
12239462Sdim *    notice, this list of conditions and the following disclaimer.
13224135Sdim * 2. Redistributions in binary form must reproduce the above copyright
14224135Sdim *    notice, this list of conditions and the following disclaimer in the
15249423Sdim *    documentation and/or other materials provided with the distribution.
16239462Sdim * 3. All advertising materials mentioning features or use of this software
17249423Sdim *    must display the following acknowledgement:
18224135Sdim *	This product includes software developed by the University of
19239462Sdim *	California, Berkeley and its contributors.
20239462Sdim * 4. Neither the name of the University nor the names of its contributors
21249423Sdim *    may be used to endorse or promote products derived from this software
22224135Sdim *    without specific prior written permission.
23224135Sdim *
24224135Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25224135Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26224135Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27224135Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28224135Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29234353Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30234353Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31239462Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32239462Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33239462Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34239462Sdim * SUCH DAMAGE.
35239462Sdim */
36239462Sdim
37239462Sdim#ifndef lint
38224135Sdimchar copyright[] =
39224135Sdim"@(#) Copyright (c) 1992, 1993, 1994\n\
40224135Sdim	The Regents of the University of California.  All rights reserved.\n";
41224135Sdim#endif /* not lint */
42234353Sdim
43234353Sdim#ifndef lint
44243830Sdimstatic char sccsid[] = "@(#)mount_union.c	8.5 (Berkeley) 3/27/94";
45224135Sdim#endif /* not lint */
46224135Sdim
47234353Sdim#include <sys/param.h>
48234353Sdim#include <sys/mount.h>
49234353Sdim
50224135Sdim#include <miscfs/union/union.h>
51234353Sdim
52234353Sdim#include <err.h>
53234353Sdim#include <stdio.h>
54224135Sdim#include <stdlib.h>
55224135Sdim#include <string.h>
56224135Sdim#include <sysexits.h>
57224135Sdim#include <unistd.h>
58224135Sdim
59234353Sdim#include "mntopts.h"
60224135Sdim
61234353Sdimstatic struct mntopt mopts[] = {
62224135Sdim	MOPT_STDOPTS,
63243830Sdim	{ NULL }
64224135Sdim};
65224135Sdim
66224135Sdimstatic int	subdir __P((const char *, const char *));
67224135Sdimstatic void	usage __P((void)) __dead2;
68224135Sdim
69224135Sdimint
70239462Sdimmain(argc, argv)
71239462Sdim	int argc;
72239462Sdim	char *argv[];
73239462Sdim{
74249423Sdim	struct union_args args;
75249423Sdim	int ch, mntflags;
76249423Sdim	char source[MAXPATHLEN];
77249423Sdim	char target[MAXPATHLEN];
78249423Sdim	struct vfsconf vfc;
79249423Sdim	int error;
80249423Sdim
81249423Sdim	mntflags = 0;
82249423Sdim	args.mntflags = UNMNT_ABOVE;
83239462Sdim	while ((ch = getopt(argc, argv, "bo:r")) != -1)
84249423Sdim		switch (ch) {
85239462Sdim		case 'b':
86239462Sdim			args.mntflags &= ~UNMNT_OPMASK;
87239462Sdim			args.mntflags |= UNMNT_BELOW;
88239462Sdim			break;
89249423Sdim		case 'o':
90239462Sdim			getmntopts(optarg, mopts, &mntflags, 0);
91239462Sdim			break;
92239462Sdim		case 'r':
93239462Sdim			args.mntflags &= ~UNMNT_OPMASK;
94239462Sdim			args.mntflags |= UNMNT_REPLACE;
95239462Sdim			break;
96239462Sdim		case '?':
97249423Sdim		default:
98239462Sdim			usage();
99239462Sdim			/* NOTREACHED */
100239462Sdim		}
101239462Sdim	argc -= optind;
102239462Sdim	argv += optind;
103239462Sdim
104239462Sdim	if (argc != 2)
105239462Sdim		usage();
106239462Sdim
107239462Sdim	if (realpath(argv[0], target) == 0)
108239462Sdim		err(EX_OSERR, "%s", target);
109239462Sdim
110249423Sdim	if (realpath(argv[1], source) == 0)
111239462Sdim		err(EX_OSERR, "%s", target);
112239462Sdim
113239462Sdim	if (subdir(target, source) || subdir(source, target))
114239462Sdim		errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths",
115239462Sdim		    argv[0], target, argv[1], source);
116239462Sdim
117239462Sdim	args.target = target;
118239462Sdim
119239462Sdim	error = getvfsbyname("union", &vfc);
120224135Sdim	if (error && vfsisloadable("union")) {
121224135Sdim		if (vfsload("union"))
122224135Sdim			err(EX_OSERR, "vfsload(union)");
123224135Sdim		endvfsent();	/* flush cache */
124224135Sdim		error = getvfsbyname("union", &vfc);
125224135Sdim	}
126226633Sdim	if (error)
127226633Sdim		errx(EX_OSERR, "union filesystem is not available");
128226633Sdim
129226633Sdim	if (mount(vfc.vfc_name, source, mntflags, &args))
130226633Sdim		err(EX_OSERR, target);
131226633Sdim	exit(0);
132226633Sdim}
133226633Sdim
134226633Sdimint
135226633Sdimsubdir(p, dir)
136226633Sdim	const char *p;
137226633Sdim	const char *dir;
138224135Sdim{
139224135Sdim	int l;
140234353Sdim
141224135Sdim	l = strlen(dir);
142224135Sdim	if (l <= 1)
143234353Sdim		return (1);
144224135Sdim
145224135Sdim	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
146224135Sdim		return (1);
147224135Sdim
148224135Sdim	return (0);
149224135Sdim}
150226633Sdim
151224135Sdimvoid
152224135Sdimusage()
153224135Sdim{
154224135Sdim	(void)fprintf(stderr,
155224135Sdim		"usage: mount_union [-br] [-o options] target_fs mount_point\n");
156224135Sdim	exit(EX_USAGE);
157224135Sdim}
158234353Sdim