mount_nullfs.c revision 15770
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
4415770Swollman/*
451558Srgrimesstatic char sccsid[] = "@(#)mount_null.c	8.5 (Berkeley) 3/27/94";
4615770Swollman*/
4715770Swollmanstatic const char rcsid[] =
4815770Swollman	"$Id$";
491558Srgrimes#endif /* not lint */
501558Srgrimes
511558Srgrimes#include <sys/param.h>
521558Srgrimes#include <sys/mount.h>
531558Srgrimes#include <miscfs/nullfs/null.h>
541558Srgrimes
551558Srgrimes#include <err.h>
561558Srgrimes#include <stdio.h>
571558Srgrimes#include <stdlib.h>
581558Srgrimes#include <string.h>
5915770Swollman#include <sysexits.h>
6015770Swollman#include <unistd.h>
611558Srgrimes
621558Srgrimes#include "mntopts.h"
631558Srgrimes
641558Srgrimesstruct mntopt mopts[] = {
651558Srgrimes	MOPT_STDOPTS,
661558Srgrimes	{ NULL }
671558Srgrimes};
681558Srgrimes
691558Srgrimesint	subdir __P((const char *, const char *));
7015770Swollmanstatic __dead void	usage __P((void)) __dead2;
711558Srgrimes
721558Srgrimesint
731558Srgrimesmain(argc, argv)
741558Srgrimes	int argc;
751558Srgrimes	char *argv[];
761558Srgrimes{
771558Srgrimes	struct null_args args;
781558Srgrimes	int ch, mntflags;
791558Srgrimes	char target[MAXPATHLEN];
802999Swollman	struct vfsconf *vfc;
811558Srgrimes
821558Srgrimes	mntflags = 0;
831558Srgrimes	while ((ch = getopt(argc, argv, "o:")) != EOF)
841558Srgrimes		switch(ch) {
851558Srgrimes		case 'o':
864065Swollman			getmntopts(optarg, mopts, &mntflags, 0);
871558Srgrimes			break;
881558Srgrimes		case '?':
891558Srgrimes		default:
901558Srgrimes			usage();
911558Srgrimes		}
921558Srgrimes	argc -= optind;
931558Srgrimes	argv += optind;
941558Srgrimes
951558Srgrimes	if (argc != 2)
961558Srgrimes		usage();
971558Srgrimes
981558Srgrimes	if (realpath(argv[0], target) == 0)
9915770Swollman		err(EX_OSERR, "%s", target);
1001558Srgrimes
1011558Srgrimes	if (subdir(target, argv[1]) || subdir(argv[1], target))
10215770Swollman		errx(EX_USAGE, "%s (%s) and %s are not distinct paths",
1031558Srgrimes		    argv[0], target, argv[1]);
1041558Srgrimes
1051558Srgrimes	args.target = target;
1061558Srgrimes
1072999Swollman	vfc = getvfsbyname("null");
1082999Swollman	if(!vfc && vfsisloadable("null")) {
1092999Swollman		if(vfsload("null"))
11015770Swollman			err(EX_OSERR, "vfsload(null)");
1112999Swollman		endvfsent();	/* flush cache */
1122999Swollman		vfc = getvfsbyname("null");
1132999Swollman	}
11415770Swollman	if (!vfc)
11515770Swollman		errx(EX_OSERR, "null filesystem is not available");
1162999Swollman
11715770Swollman	if (mount(vfc->vfc_index, argv[1], mntflags, &args))
11815770Swollman		err(EX_OSERR, target);
1191558Srgrimes	exit(0);
1201558Srgrimes}
1211558Srgrimes
1221558Srgrimesint
1231558Srgrimessubdir(p, dir)
1241558Srgrimes	const char *p;
1251558Srgrimes	const char *dir;
1261558Srgrimes{
1271558Srgrimes	int l;
1281558Srgrimes
1291558Srgrimes	l = strlen(dir);
1301558Srgrimes	if (l <= 1)
1311558Srgrimes		return (1);
1321558Srgrimes
1331558Srgrimes	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
1341558Srgrimes		return (1);
1351558Srgrimes
1361558Srgrimes	return (0);
1371558Srgrimes}
1381558Srgrimes
13915770Swollmanstatic void
1401558Srgrimesusage()
1411558Srgrimes{
1421558Srgrimes	(void)fprintf(stderr,
1431558Srgrimes		"usage: mount_null [-o options] target_fs mount_point\n");
1441558Srgrimes	exit(1);
1451558Srgrimes}
146