xref: /original-bsd/sbin/mount_null/mount_null.c (revision e66e06db)
1 /*
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 char copyright[] =
13 "@(#) Copyright (c) 1992, 1993, 1994\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)mount_null.c	8.5 (Berkeley) 03/27/94";
19 #endif /* not lint */
20 
21 #include <sys/param.h>
22 #include <sys/mount.h>
23 #include <miscfs/nullfs/null.h>
24 
25 #include <err.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "mntopts.h"
32 
33 struct mntopt mopts[] = {
34 	MOPT_STDOPTS,
35 	{ NULL }
36 };
37 
38 int	subdir __P((const char *, const char *));
39 void	usage __P((void));
40 
41 int
42 main(argc, argv)
43 	int argc;
44 	char *argv[];
45 {
46 	struct null_args args;
47 	int ch, mntflags;
48 	char target[MAXPATHLEN];
49 
50 	mntflags = 0;
51 	while ((ch = getopt(argc, argv, "o:")) != EOF)
52 		switch(ch) {
53 		case 'o':
54 			getmntopts(optarg, mopts, &mntflags);
55 			break;
56 		case '?':
57 		default:
58 			usage();
59 		}
60 	argc -= optind;
61 	argv += optind;
62 
63 	if (argc != 2)
64 		usage();
65 
66 	if (realpath(argv[0], target) == 0)
67 		err(1, "%s", target);
68 
69 	if (subdir(target, argv[1]) || subdir(argv[1], target))
70 		errx(1, "%s (%s) and %s are not distinct paths",
71 		    argv[0], target, argv[1]);
72 
73 	args.target = target;
74 
75 	if (mount(MOUNT_NULL, argv[1], mntflags, &args))
76 		err(1, NULL);
77 	exit(0);
78 }
79 
80 int
81 subdir(p, dir)
82 	const char *p;
83 	const char *dir;
84 {
85 	int l;
86 
87 	l = strlen(dir);
88 	if (l <= 1)
89 		return (1);
90 
91 	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
92 		return (1);
93 
94 	return (0);
95 }
96 
97 void
98 usage()
99 {
100 	(void)fprintf(stderr,
101 		"usage: mount_null [-o options] target_fs mount_point\n");
102 	exit(1);
103 }
104