summaryrefslogtreecommitdiff
path: root/9bind.c
blob: 620d092ddb3337da34276ebe861667b5ae5a4bea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* © 2008 sqweek <sqweek@gmail.com>
 * See COPYING for details.
 */
#include <err.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/mount.h>

int
main(int argc, char **argv)
{
	char *old = NULL, *new = NULL;
	struct stat stbuf;

	while (*++argv) {
		if (!old) {
			old = *argv;
		} else if (!new) {
			new = *argv;
		} else {
			errx(1, "%s: too many arguments", *argv);
		}
	}

	if (!old || !new) {
		errx(1, "usage: 9bind old new");
	}

	/* Make sure mount exists, is writable, and not sticky */
	if (stat(new, &stbuf) || access(new, W_OK)) {
		err(1, "%s", new);
	}
	if (stbuf.st_mode & S_ISVTX) {
		errx(1, "%s: refusing to bind over sticky directory", new);
	}

	if (mount(old, new, NULL, MS_BIND, NULL)) {
		err(1, "mount");
	}

	return 0;
}