mount_unionfs.c revision 52055
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 52055 1999-10-09 11:54:14Z phk $"; 4928629Ssteve#endif 501558Srgrimes#endif /* not lint */ 511558Srgrimes 521558Srgrimes#include <sys/param.h> 531558Srgrimes#include <sys/mount.h> 541558Srgrimes 551558Srgrimes#include <miscfs/union/union.h> 561558Srgrimes 571558Srgrimes#include <err.h> 581558Srgrimes#include <stdio.h> 591558Srgrimes#include <stdlib.h> 601558Srgrimes#include <string.h> 6115770Swollman#include <sysexits.h> 621558Srgrimes#include <unistd.h> 631558Srgrimes 641558Srgrimes#include "mntopts.h" 651558Srgrimes 6615770Swollmanstatic struct mntopt mopts[] = { 671558Srgrimes MOPT_STDOPTS, 681558Srgrimes { NULL } 691558Srgrimes}; 701558Srgrimes 7115770Swollmanstatic int subdir __P((const char *, const char *)); 7218286Sbdestatic void usage __P((void)) __dead2; 731558Srgrimes 741558Srgrimesint 751558Srgrimesmain(argc, argv) 761558Srgrimes int argc; 771558Srgrimes char *argv[]; 781558Srgrimes{ 791558Srgrimes struct union_args args; 801558Srgrimes int ch, mntflags; 8125275Sdfr char source[MAXPATHLEN]; 821558Srgrimes char target[MAXPATHLEN]; 8323680Speter struct vfsconf vfc; 8423680Speter int error; 851558Srgrimes 861558Srgrimes mntflags = 0; 871558Srgrimes args.mntflags = UNMNT_ABOVE; 8824359Simp while ((ch = getopt(argc, argv, "bo:r")) != -1) 891558Srgrimes switch (ch) { 901558Srgrimes case 'b': 911558Srgrimes args.mntflags &= ~UNMNT_OPMASK; 921558Srgrimes args.mntflags |= UNMNT_BELOW; 931558Srgrimes break; 941558Srgrimes case 'o': 954065Swollman getmntopts(optarg, mopts, &mntflags, 0); 961558Srgrimes break; 971558Srgrimes case 'r': 981558Srgrimes args.mntflags &= ~UNMNT_OPMASK; 991558Srgrimes args.mntflags |= UNMNT_REPLACE; 1001558Srgrimes break; 1011558Srgrimes case '?': 1021558Srgrimes default: 1031558Srgrimes usage(); 1041558Srgrimes /* NOTREACHED */ 1051558Srgrimes } 1061558Srgrimes argc -= optind; 1071558Srgrimes argv += optind; 1081558Srgrimes 1091558Srgrimes if (argc != 2) 1101558Srgrimes usage(); 1111558Srgrimes 11252055Sphk /* resolve both target and source with realpath(3) */ 11352055Sphk (void)checkpath(argv[0], target); 11452055Sphk (void)checkpath(argv[1], source); 1151558Srgrimes 11625275Sdfr if (subdir(target, source) || subdir(source, target)) 11725275Sdfr errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths", 11825275Sdfr argv[0], target, argv[1], source); 11925275Sdfr 1201558Srgrimes args.target = target; 1211558Srgrimes 12223680Speter error = getvfsbyname("union", &vfc); 12323680Speter if (error && vfsisloadable("union")) { 12423680Speter if (vfsload("union")) 12523680Speter err(EX_OSERR, "vfsload(union)"); 1262999Swollman endvfsent(); /* flush cache */ 12723680Speter error = getvfsbyname("union", &vfc); 1282999Swollman } 12923680Speter if (error) 13015770Swollman errx(EX_OSERR, "union filesystem is not available"); 1312999Swollman 13225275Sdfr if (mount(vfc.vfc_name, source, mntflags, &args)) 13328629Ssteve err(EX_OSERR, "%s", target); 1341558Srgrimes exit(0); 1351558Srgrimes} 1361558Srgrimes 1371558Srgrimesint 1381558Srgrimessubdir(p, dir) 1391558Srgrimes const char *p; 1401558Srgrimes const char *dir; 1411558Srgrimes{ 1421558Srgrimes int l; 1431558Srgrimes 1441558Srgrimes l = strlen(dir); 1451558Srgrimes if (l <= 1) 1461558Srgrimes return (1); 1471558Srgrimes 1481558Srgrimes if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0')) 1491558Srgrimes return (1); 1501558Srgrimes 1511558Srgrimes return (0); 1521558Srgrimes} 1531558Srgrimes 1541558Srgrimesvoid 1551558Srgrimesusage() 1561558Srgrimes{ 1571558Srgrimes (void)fprintf(stderr, 1581558Srgrimes "usage: mount_union [-br] [-o options] target_fs mount_point\n"); 15915770Swollman exit(EX_USAGE); 1601558Srgrimes} 161