1145247Sdamien/*
2145247Sdamien * Copyright (c) 1992, 1993, 1994
3145247Sdamien *	The Regents of the University of California.  All rights reserved.
4156599Sdamien *
5145247Sdamien * This code is derived from software donated to Berkeley by
6172567Sthompsa * Jan-Simon Pendry.
7172567Sthompsa *
8145247Sdamien * Redistribution and use in source and binary forms, with or without
9145247Sdamien * modification, are permitted provided that the following conditions
10145247Sdamien * are met:
11145247Sdamien * 1. Redistributions of source code must retain the above copyright
12145247Sdamien *    notice, this list of conditions and the following disclaimer.
13145247Sdamien * 2. Redistributions in binary form must reproduce the above copyright
14145247Sdamien *    notice, this list of conditions and the following disclaimer in the
15145247Sdamien *    documentation and/or other materials provided with the distribution.
16145247Sdamien * 4. Neither the name of the University nor the names of its contributors
17145247Sdamien *    may be used to endorse or promote products derived from this software
18145247Sdamien *    without specific prior written permission.
19145247Sdamien *
20145247Sdamien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21145247Sdamien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22145247Sdamien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23145247Sdamien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24145247Sdamien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25145247Sdamien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26145247Sdamien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27145247Sdamien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28145247Sdamien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29145247Sdamien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30145247Sdamien * SUCH DAMAGE.
31145247Sdamien */
32145247Sdamien
33145247Sdamien#ifndef lint
34145247Sdamienstatic const char copyright[] =
35145247Sdamien"@(#) Copyright (c) 1992, 1993, 1994\n\
36145247Sdamien	The Regents of the University of California.  All rights reserved.\n";
37145247Sdamien#endif /* not lint */
38145247Sdamien
39145247Sdamien#ifndef lint
40145247Sdamien#if 0
41145247Sdamienstatic char sccsid[] = "@(#)mount_null.c	8.6 (Berkeley) 4/26/95";
42145247Sdamien#endif
43145247Sdamienstatic const char rcsid[] =
44145247Sdamien  "$FreeBSD: releng/10.3/sbin/mount_nullfs/mount_nullfs.c 267808 2014-06-23 22:35:41Z rodrigc $";
45145247Sdamien#endif /* not lint */
46145247Sdamien
47145247Sdamien#include <sys/param.h>
48156599Sdamien#include <sys/mount.h>
49156599Sdamien#include <sys/uio.h>
50145247Sdamien
51145247Sdamien#include <err.h>
52145247Sdamien#include <stdio.h>
53156599Sdamien#include <stdlib.h>
54156599Sdamien#include <string.h>
55145247Sdamien#include <sysexits.h>
56145247Sdamien#include <unistd.h>
57145247Sdamien
58145247Sdamien#include "mntopts.h"
59145247Sdamien
60145247Sdamienint	subdir(const char *, const char *);
61145247Sdamienstatic void	usage(void) __dead2;
62145247Sdamien
63145247Sdamienint
64145247Sdamienmain(int argc, char *argv[])
65145247Sdamien{
66145247Sdamien	struct iovec *iov;
67145247Sdamien	char *p, *val;
68145247Sdamien	char source[MAXPATHLEN];
69145247Sdamien	char target[MAXPATHLEN];
70145247Sdamien	char errmsg[255];
71156599Sdamien	int ch, iovlen;
72156599Sdamien	char nullfs[] = "nullfs";
73156599Sdamien
74145247Sdamien	iov = NULL;
75145247Sdamien	iovlen = 0;
76145247Sdamien	errmsg[0] = '\0';
77145247Sdamien	while ((ch = getopt(argc, argv, "o:")) != -1)
78145247Sdamien		switch(ch) {
79145247Sdamien		case 'o':
80145247Sdamien			val = strdup("");
81145247Sdamien			p = strchr(optarg, '=');
82145247Sdamien			if (p != NULL) {
83172567Sthompsa				free(val);
84145247Sdamien				*p = '\0';
85145247Sdamien				val = p + 1;
86145247Sdamien			}
87145247Sdamien			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
88145247Sdamien			break;
89145247Sdamien		case '?':
90145247Sdamien		default:
91145247Sdamien			usage();
92145247Sdamien		}
93145247Sdamien	argc -= optind;
94145247Sdamien	argv += optind;
95145247Sdamien
96156599Sdamien	if (argc != 2)
97145247Sdamien		usage();
98145247Sdamien
99145247Sdamien	/* resolve target and source with realpath(3) */
100145247Sdamien	if (checkpath(argv[0], target) != 0)
101145247Sdamien		err(EX_USAGE, "%s", target);
102145247Sdamien	if (checkpath(argv[1], source) != 0)
103145247Sdamien		err(EX_USAGE, "%s", source);
104145247Sdamien
105145247Sdamien	if (subdir(target, source) || subdir(source, target))
106145247Sdamien		errx(EX_USAGE, "%s (%s) and %s are not distinct paths",
107145247Sdamien		    argv[0], target, argv[1]);
108145247Sdamien
109145247Sdamien	build_iovec(&iov, &iovlen, "fstype", nullfs, (size_t)-1);
110178354Ssam	build_iovec(&iov, &iovlen, "fspath", source, (size_t)-1);
111228621Sbschmidt	build_iovec(&iov, &iovlen, "target", target, (size_t)-1);
112228621Sbschmidt	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
113228621Sbschmidt	if (nmount(iov, iovlen, 0) < 0) {
114178354Ssam		if (errmsg[0] != 0)
115145247Sdamien			err(1, "%s: %s", source, errmsg);
116145247Sdamien		else
117145247Sdamien			err(1, "%s", source);
118178354Ssam	}
119145247Sdamien	exit(0);
120172567Sthompsa}
121172567Sthompsa
122172567Sthompsaint
123145247Sdamiensubdir(const char *p, const char *dir)
124145247Sdamien{
125145247Sdamien	int l;
126145247Sdamien
127145247Sdamien	l = strlen(dir);
128145247Sdamien	if (l <= 1)
129172567Sthompsa		return (1);
130145247Sdamien
131145247Sdamien	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
132145247Sdamien		return (1);
133178354Ssam
134178354Ssam	return (0);
135145247Sdamien}
136172567Sthompsa
137172567Sthompsastatic void
138145247Sdamienusage(void)
139145247Sdamien{
140172567Sthompsa	(void)fprintf(stderr,
141172567Sthompsa		"usage: mount_nullfs [-o options] target mount-point\n");
142145247Sdamien	exit(1);
143156599Sdamien}
144156599Sdamien