mount_unionfs.c revision 4065
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1992, 1993, 1994
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software donated to Berkeley by
61558Srgrimes * Jan-Simon Pendry.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
381558Srgrimeschar copyright[] =
391558Srgrimes"@(#) Copyright (c) 1992, 1993, 1994\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411558Srgrimes#endif /* not lint */
421558Srgrimes
431558Srgrimes#ifndef lint
441558Srgrimesstatic char sccsid[] = "@(#)mount_union.c	8.5 (Berkeley) 3/27/94";
451558Srgrimes#endif /* not lint */
461558Srgrimes
471558Srgrimes#include <sys/param.h>
481558Srgrimes#include <sys/mount.h>
491558Srgrimes
501558Srgrimes#include <miscfs/union/union.h>
511558Srgrimes
521558Srgrimes#include <err.h>
531558Srgrimes#include <stdio.h>
541558Srgrimes#include <stdlib.h>
551558Srgrimes#include <string.h>
561558Srgrimes#include <unistd.h>
571558Srgrimes
581558Srgrimes#include "mntopts.h"
591558Srgrimes
601558Srgrimesstruct mntopt mopts[] = {
611558Srgrimes	MOPT_STDOPTS,
621558Srgrimes	{ NULL }
631558Srgrimes};
641558Srgrimes
651558Srgrimesint	subdir __P((const char *, const char *));
661558Srgrimesvoid	usage __P((void));
671558Srgrimes
681558Srgrimesint
691558Srgrimesmain(argc, argv)
701558Srgrimes	int argc;
711558Srgrimes	char *argv[];
721558Srgrimes{
731558Srgrimes	struct union_args args;
741558Srgrimes	int ch, mntflags;
751558Srgrimes	char target[MAXPATHLEN];
762999Swollman	struct vfsconf *vfc;
771558Srgrimes
781558Srgrimes	mntflags = 0;
791558Srgrimes	args.mntflags = UNMNT_ABOVE;
801558Srgrimes	while ((ch = getopt(argc, argv, "bo:r")) != EOF)
811558Srgrimes		switch (ch) {
821558Srgrimes		case 'b':
831558Srgrimes			args.mntflags &= ~UNMNT_OPMASK;
841558Srgrimes			args.mntflags |= UNMNT_BELOW;
851558Srgrimes			break;
861558Srgrimes		case 'o':
874065Swollman			getmntopts(optarg, mopts, &mntflags, 0);
881558Srgrimes			break;
891558Srgrimes		case 'r':
901558Srgrimes			args.mntflags &= ~UNMNT_OPMASK;
911558Srgrimes			args.mntflags |= UNMNT_REPLACE;
921558Srgrimes			break;
931558Srgrimes		case '?':
941558Srgrimes		default:
951558Srgrimes			usage();
961558Srgrimes			/* NOTREACHED */
971558Srgrimes		}
981558Srgrimes	argc -= optind;
991558Srgrimes	argv += optind;
1001558Srgrimes
1011558Srgrimes	if (argc != 2)
1021558Srgrimes		usage();
1031558Srgrimes
1041558Srgrimes	if (realpath(argv[0], target) == 0)
1051558Srgrimes		err(1, "%s", target);
1061558Srgrimes
1071558Srgrimes	if (subdir(target, argv[1]) || subdir(argv[1], target))
1081558Srgrimes		errx(1, "%s (%s) and %s are not distinct paths",
1091558Srgrimes		    argv[0], target, argv[1]);
1101558Srgrimes
1111558Srgrimes	args.target = target;
1121558Srgrimes
1132999Swollman	vfc = getvfsbyname("union");
1142999Swollman	if(!vfc && vfsisloadable("union")) {
1152999Swollman		if(vfsload("union"))
1162999Swollman			err(1, "vfsload(union)");
1172999Swollman		endvfsent();	/* flush cache */
1182999Swollman		vfc = getvfsbyname("union");
1192999Swollman	}
1202999Swollman
1212999Swollman	if (mount(vfc ? vfc->vfc_index : MOUNT_UNION, argv[1], mntflags, &args))
1221558Srgrimes		err(1, NULL);
1231558Srgrimes	exit(0);
1241558Srgrimes}
1251558Srgrimes
1261558Srgrimesint
1271558Srgrimessubdir(p, dir)
1281558Srgrimes	const char *p;
1291558Srgrimes	const char *dir;
1301558Srgrimes{
1311558Srgrimes	int l;
1321558Srgrimes
1331558Srgrimes	l = strlen(dir);
1341558Srgrimes	if (l <= 1)
1351558Srgrimes		return (1);
1361558Srgrimes
1371558Srgrimes	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
1381558Srgrimes		return (1);
1391558Srgrimes
1401558Srgrimes	return (0);
1411558Srgrimes}
1421558Srgrimes
1431558Srgrimesvoid
1441558Srgrimesusage()
1451558Srgrimes{
1461558Srgrimes	(void)fprintf(stderr,
1471558Srgrimes		"usage: mount_union [-br] [-o options] target_fs mount_point\n");
1481558Srgrimes	exit(1);
1491558Srgrimes}
150