File: 9bind.c

package info (click to toggle)
9mount 1.3-10
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 104 kB
  • sloc: ansic: 402; sh: 53; makefile: 32
file content (45 lines) | stat: -rw-r--r-- 798 bytes parent folder | download | duplicates (2)
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;
}