mount_nullfs.c revision 26072
1139815Simp/* 2206361Sjoel * Copyright (c) 1992, 1993, 1994 375332Sbp * The Regents of the University of California. All rights reserved. 475332Sbp * 575332Sbp * This code is derived from software donated to Berkeley by 675332Sbp * Jan-Simon Pendry. 775332Sbp * 875332Sbp * Redistribution and use in source and binary forms, with or without 975332Sbp * modification, are permitted provided that the following conditions 1075332Sbp * are met: 1175332Sbp * 1. Redistributions of source code must retain the above copyright 1275332Sbp * notice, this list of conditions and the following disclaimer. 1375332Sbp * 2. Redistributions in binary form must reproduce the above copyright 1475332Sbp * notice, this list of conditions and the following disclaimer in the 1575332Sbp * documentation and/or other materials provided with the distribution. 1675332Sbp * 3. All advertising materials mentioning features or use of this software 1775332Sbp * must display the following acknowledgement: 1875332Sbp * This product includes software developed by the University of 1975332Sbp * California, Berkeley and its contributors. 2075332Sbp * 4. Neither the name of the University nor the names of its contributors 2175332Sbp * may be used to endorse or promote products derived from this software 2275332Sbp * without specific prior written permission. 2375332Sbp * 2475332Sbp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2575332Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26116189Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27116189Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28116189Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29116189Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3075332Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3175332Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3275332Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3375332Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3475332Sbp * SUCH DAMAGE. 35120492Sfjoe */ 36185652Sjhb 37120492Sfjoe#ifndef lint 3875332Sbpchar copyright[] = 3975332Sbp"@(#) Copyright (c) 1992, 1993, 1994\n\ 4075332Sbp The Regents of the University of California. All rights reserved.\n"; 4175332Sbp#endif /* not lint */ 4275332Sbp 4375332Sbp#ifndef lint 4475332Sbp/* 45151897Srwatsonstatic char sccsid[] = "@(#)mount_null.c 8.6 (Berkeley) 4/26/95"; 46227293Sed*/ 4775332Sbpstatic const char rcsid[] = 48120492Sfjoe "$Id: mount_null.c,v 1.9 1997/03/29 03:32:42 imp Exp $"; 4975332Sbp#endif /* not lint */ 50185652Sjhb 51185652Sjhb#include <sys/param.h> 5275332Sbp#include <sys/mount.h> 5375332Sbp#include <miscfs/nullfs/null.h> 5475332Sbp 5575332Sbp#include <err.h> 5675332Sbp#include <stdio.h> 5775332Sbp#include <stdlib.h> 5875332Sbp#include <string.h> 5975332Sbp#include <sysexits.h> 6075332Sbp#include <unistd.h> 6175332Sbp 6275332Sbp#include "mntopts.h" 6375332Sbp 6475332Sbpstruct mntopt mopts[] = { 6575332Sbp MOPT_STDOPTS, 6675332Sbp { NULL } 6775332Sbp}; 6875332Sbp 6975332Sbpint subdir __P((const char *, const char *)); 7075332Sbpstatic void usage __P((void)) __dead2; 7175332Sbp 7275332Sbpint 7375332Sbpmain(argc, argv) 7475332Sbp int argc; 7575332Sbp char *argv[]; 7675332Sbp{ 7775332Sbp struct null_args args; 7875332Sbp int ch, mntflags; 7975332Sbp char source[MAXPATHLEN]; 8075332Sbp char target[MAXPATHLEN]; 8175332Sbp struct vfsconf vfc; 8275332Sbp int error; 8375332Sbp 8475332Sbp mntflags = 0; 8575332Sbp while ((ch = getopt(argc, argv, "o:")) != -1) 86185652Sjhb switch(ch) { 87236899Smjg case 'o': 88236899Smjg getmntopts(optarg, mopts, &mntflags, 0); 89236899Smjg break; 9075332Sbp case '?': 91236899Smjg default: 92185652Sjhb usage(); 93185652Sjhb } 94185652Sjhb argc -= optind; 9575332Sbp argv += optind; 96185652Sjhb 97185652Sjhb if (argc != 2) 9875332Sbp usage(); 9975332Sbp 10075332Sbp if (realpath(argv[0], target) == 0) 10175332Sbp err(EX_OSERR, "%s", target); 10275332Sbp 10375332Sbp if (realpath(argv[1], source) == 0) 10475332Sbp err(EX_OSERR, "%s", source); 10575332Sbp 10675332Sbp if (subdir(target, source) || subdir(source, target)) 10775332Sbp errx(EX_USAGE, "%s (%s) and %s are not distinct paths", 10875332Sbp argv[0], target, argv[1]); 109185652Sjhb 11075332Sbp args.target = target; 11175332Sbp 11275332Sbp error = getvfsbyname("null", &vfc); 11375332Sbp if (error && vfsisloadable("null")) { 11475332Sbp if(vfsload("null")) 11575332Sbp err(EX_OSERR, "vfsload(null)"); 11675332Sbp endvfsent(); 11775332Sbp error = getvfsbyname("null", &vfc); 11875332Sbp } 11975332Sbp if (error) 12075332Sbp errx(EX_OSERR, "null/loopback filesystem is not available"); 12175332Sbp 12275332Sbp if (mount(vfc.vfc_name, source, mntflags, &args)) 12375332Sbp err(1, NULL); 12475332Sbp exit(0); 12575332Sbp} 12675332Sbp 12775332Sbpint 12875332Sbpsubdir(p, dir) 12975332Sbp const char *p; 13075332Sbp const char *dir; 13175332Sbp{ 13275332Sbp int l; 13375332Sbp 13475332Sbp l = strlen(dir); 13575332Sbp if (l <= 1) 13675332Sbp return (1); 13775332Sbp 138235712Skevlo if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0')) 13975332Sbp return (1); 140258752Seadler 14175332Sbp return (0); 14275332Sbp} 14375332Sbp 14475332Sbpstatic void 14575332Sbpusage() 14675332Sbp{ 14775332Sbp (void)fprintf(stderr, 14875332Sbp "usage: mount_null [-o options] target_fs mount_point\n"); 14975332Sbp exit(1); 15075332Sbp} 15175332Sbp