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 * 4. Neither the name of the University nor the names of its contributors
171558Srgrimes *    may be used to endorse or promote products derived from this software
181558Srgrimes *    without specific prior written permission.
191558Srgrimes *
201558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301558Srgrimes * SUCH DAMAGE.
311558Srgrimes */
321558Srgrimes
331558Srgrimes#ifndef lint
3437428Scharnierstatic const char copyright[] =
351558Srgrimes"@(#) Copyright (c) 1992, 1993, 1994\n\
361558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
371558Srgrimes#endif /* not lint */
381558Srgrimes
391558Srgrimes#ifndef lint
4037428Scharnier#if 0
4123680Speterstatic char sccsid[] = "@(#)mount_null.c	8.6 (Berkeley) 4/26/95";
4237428Scharnier#endif
4315770Swollmanstatic const char rcsid[] =
4450476Speter  "$FreeBSD$";
451558Srgrimes#endif /* not lint */
461558Srgrimes
471558Srgrimes#include <sys/param.h>
481558Srgrimes#include <sys/mount.h>
49101829Smux#include <sys/uio.h>
501558Srgrimes
511558Srgrimes#include <err.h>
521558Srgrimes#include <stdio.h>
531558Srgrimes#include <stdlib.h>
541558Srgrimes#include <string.h>
5515770Swollman#include <sysexits.h>
5615770Swollman#include <unistd.h>
571558Srgrimes
581558Srgrimes#include "mntopts.h"
591558Srgrimes
6092882Simpint	subdir(const char *, const char *);
6192882Simpstatic void	usage(void) __dead2;
621558Srgrimes
631558Srgrimesint
64152670Srodrigcmain(int argc, char *argv[])
651558Srgrimes{
66245005Skib	struct iovec *iov;
67245005Skib	char *p, *val;
6826072Sdfr	char source[MAXPATHLEN];
691558Srgrimes	char target[MAXPATHLEN];
70245005Skib	char errmsg[255];
71247856Sjkim	int ch, iovlen;
72245005Skib	char nullfs[] = "nullfs";
731558Srgrimes
74245005Skib	iov = NULL;
75245005Skib	iovlen = 0;
76245005Skib	errmsg[0] = '\0';
7724359Simp	while ((ch = getopt(argc, argv, "o:")) != -1)
781558Srgrimes		switch(ch) {
791558Srgrimes		case 'o':
80245005Skib			val = strdup("");
81245005Skib			p = strchr(optarg, '=');
82245005Skib			if (p != NULL) {
83245005Skib				free(val);
84245005Skib				*p = '\0';
85245005Skib				val = p + 1;
86245005Skib			}
87245005Skib			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
881558Srgrimes			break;
891558Srgrimes		case '?':
901558Srgrimes		default:
911558Srgrimes			usage();
921558Srgrimes		}
931558Srgrimes	argc -= optind;
941558Srgrimes	argv += optind;
951558Srgrimes
961558Srgrimes	if (argc != 2)
971558Srgrimes		usage();
981558Srgrimes
9952055Sphk	/* resolve target and source with realpath(3) */
100230226Sjh	if (checkpath(argv[0], target) != 0)
101230226Sjh		err(EX_USAGE, "%s", target);
102230226Sjh	if (checkpath(argv[1], source) != 0)
103230226Sjh		err(EX_USAGE, "%s", source);
1041558Srgrimes
10526072Sdfr	if (subdir(target, source) || subdir(source, target))
10615770Swollman		errx(EX_USAGE, "%s (%s) and %s are not distinct paths",
1071558Srgrimes		    argv[0], target, argv[1]);
1081558Srgrimes
109245005Skib	build_iovec(&iov, &iovlen, "fstype", nullfs, (size_t)-1);
110245005Skib	build_iovec(&iov, &iovlen, "fspath", source, (size_t)-1);
111245005Skib	build_iovec(&iov, &iovlen, "target", target, (size_t)-1);
112245005Skib	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
113247856Sjkim	if (nmount(iov, iovlen, 0) < 0) {
114245005Skib		if (errmsg[0] != 0)
115245005Skib			err(1, "%s: %s", source, errmsg);
116245005Skib		else
117245005Skib			err(1, "%s", source);
118245005Skib	}
1191558Srgrimes	exit(0);
1201558Srgrimes}
1211558Srgrimes
1221558Srgrimesint
123201227Sedsubdir(const char *p, const char *dir)
1241558Srgrimes{
1251558Srgrimes	int l;
1261558Srgrimes
1271558Srgrimes	l = strlen(dir);
1281558Srgrimes	if (l <= 1)
1291558Srgrimes		return (1);
1301558Srgrimes
1311558Srgrimes	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
1321558Srgrimes		return (1);
1331558Srgrimes
1341558Srgrimes	return (0);
1351558Srgrimes}
1361558Srgrimes
13715770Swollmanstatic void
138201227Sedusage(void)
1391558Srgrimes{
1401558Srgrimes	(void)fprintf(stderr,
141141611Sru		"usage: mount_nullfs [-o options] target mount-point\n");
1421558Srgrimes	exit(1);
1431558Srgrimes}
144