mount_unionfs.c revision 101270
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
3828629Sstevestatic const char 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
4428629Ssteve#if 0
451558Srgrimesstatic char sccsid[] = "@(#)mount_union.c	8.5 (Berkeley) 3/27/94";
4628629Ssteve#else
4728629Sstevestatic const char rcsid[] =
4850476Speter  "$FreeBSD: head/sbin/mount_unionfs/mount_unionfs.c 101270 2002-08-03 16:03:21Z mux $";
4928629Ssteve#endif
501558Srgrimes#endif /* not lint */
511558Srgrimes
521558Srgrimes#include <sys/param.h>
531558Srgrimes#include <sys/mount.h>
541558Srgrimes
551558Srgrimes#include <err.h>
561558Srgrimes#include <stdio.h>
571558Srgrimes#include <stdlib.h>
581558Srgrimes#include <string.h>
5915770Swollman#include <sysexits.h>
601558Srgrimes#include <unistd.h>
611558Srgrimes
621558Srgrimes#include "mntopts.h"
631558Srgrimes
6415770Swollmanstatic struct mntopt mopts[] = {
651558Srgrimes	MOPT_STDOPTS,
661558Srgrimes	{ NULL }
671558Srgrimes};
681558Srgrimes
6992882Simpstatic int	subdir(const char *, const char *);
7092882Simpstatic void	usage (void) __dead2;
711558Srgrimes
721558Srgrimesint
731558Srgrimesmain(argc, argv)
741558Srgrimes	int argc;
751558Srgrimes	char *argv[];
761558Srgrimes{
7797196Smux	struct iovec iov[8];
7898266Smux	int ch, mntflags;
7925275Sdfr	char source[MAXPATHLEN];
801558Srgrimes	char target[MAXPATHLEN];
81101270Smux	int iovcnt;
821558Srgrimes
8398266Smux	iovcnt = 6;
841558Srgrimes	mntflags = 0;
8524359Simp	while ((ch = getopt(argc, argv, "bo:r")) != -1)
861558Srgrimes		switch (ch) {
871558Srgrimes		case 'b':
8898266Smux			iov[6].iov_base = "below";
8998266Smux			iov[6].iov_len = strlen(iov[6].iov_base) + 1;
9098266Smux			iov[7].iov_base = NULL;
9198266Smux			iov[7].iov_len = 0;
9298266Smux			iovcnt = 8;
931558Srgrimes			break;
941558Srgrimes		case 'o':
954065Swollman			getmntopts(optarg, mopts, &mntflags, 0);
961558Srgrimes			break;
971558Srgrimes		case 'r':
9898266Smux			iov[6].iov_base = "replace";
9998266Smux			iov[6].iov_len = strlen(iov[6].iov_base) + 1;
10098266Smux			iov[7].iov_base = NULL;
10198266Smux			iov[7].iov_len = 0;
10298266Smux			iovcnt = 8;
1031558Srgrimes			break;
1041558Srgrimes		case '?':
1051558Srgrimes		default:
1061558Srgrimes			usage();
1071558Srgrimes			/* NOTREACHED */
1081558Srgrimes		}
1091558Srgrimes	argc -= optind;
1101558Srgrimes	argv += optind;
1111558Srgrimes
1121558Srgrimes	if (argc != 2)
1131558Srgrimes		usage();
1141558Srgrimes
11552055Sphk	/* resolve both target and source with realpath(3) */
11652055Sphk	(void)checkpath(argv[0], target);
11752055Sphk	(void)checkpath(argv[1], source);
1181558Srgrimes
11925275Sdfr	if (subdir(target, source) || subdir(source, target))
12025275Sdfr		errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths",
12125275Sdfr		    argv[0], target, argv[1], source);
12225275Sdfr
12397196Smux	iov[0].iov_base = "fstype";
12498266Smux	iov[0].iov_len = strlen(iov[0].iov_base) + 1;
125101270Smux	iov[1].iov_base = "unionfs";
126101270Smux	iov[1].iov_len = strlen(iov[1].iov_base) + 1;
12797196Smux	iov[2].iov_base = "fspath";
12898266Smux	iov[2].iov_len = strlen(iov[2].iov_base) + 1;
12997196Smux	iov[3].iov_base = source;
13097196Smux	iov[3].iov_len = strlen(source) + 1;
13197196Smux	iov[4].iov_base = "target";
13298266Smux	iov[4].iov_len = strlen(iov[4].iov_base) + 1;
13397196Smux	iov[5].iov_base = target;
13497196Smux	iov[5].iov_len = strlen(target) + 1;
13598266Smux	if (nmount(iov, iovcnt, mntflags))
13628629Ssteve		err(EX_OSERR, "%s", target);
1371558Srgrimes	exit(0);
1381558Srgrimes}
1391558Srgrimes
1401558Srgrimesint
1411558Srgrimessubdir(p, dir)
1421558Srgrimes	const char *p;
1431558Srgrimes	const char *dir;
1441558Srgrimes{
1451558Srgrimes	int l;
1461558Srgrimes
1471558Srgrimes	l = strlen(dir);
1481558Srgrimes	if (l <= 1)
1491558Srgrimes		return (1);
1501558Srgrimes
1511558Srgrimes	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
1521558Srgrimes		return (1);
1531558Srgrimes
1541558Srgrimes	return (0);
1551558Srgrimes}
1561558Srgrimes
1571558Srgrimesvoid
1581558Srgrimesusage()
1591558Srgrimes{
1601558Srgrimes	(void)fprintf(stderr,
16177042Sru		"usage: mount_unionfs [-br] [-o options] target_fs mount_point\n");
16215770Swollman	exit(EX_USAGE);
1631558Srgrimes}
164