4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Test that "mount -t proc -o subset=pid" hides everything but pids,
18 * /proc/self and /proc/thread-self.
27 #include <sys/mount.h>
28 #include <sys/types.h>
35 static inline bool streq(const char *a, const char *b)
37 return strcmp(a, b) == 0;
40 static void make_private_proc(void)
42 if (unshare(CLONE_NEWNS) == -1) {
43 if (errno == ENOSYS || errno == EPERM) {
48 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
51 if (mount(NULL, "/proc", "proc", 0, "subset=pid") == -1) {
56 static bool string_is_pid(const char *s)
60 case '0':case '1':case '2':case '3':case '4':
61 case '5':case '6':case '7':case '8':case '9':
77 DIR *d = opendir("/proc");
85 bool thread_self = false;
87 while ((de = readdir(d))) {
88 if (streq(de->d_name, ".")) {
91 assert(de->d_type == DT_DIR);
92 } else if (streq(de->d_name, "..")) {
95 assert(de->d_type == DT_DIR);
96 } else if (streq(de->d_name, "self")) {
99 assert(de->d_type == DT_LNK);
100 } else if (streq(de->d_name, "thread-self")) {
101 assert(!thread_self);
103 assert(de->d_type == DT_LNK);
105 if (!string_is_pid(de->d_name)) {
106 fprintf(stderr, "d_name '%s'\n", de->d_name);
109 assert(de->d_type == DT_DIR);
114 int rv = readlink("/proc/cpuinfo", &c, 1);
115 assert(rv == -1 && errno == ENOENT);
117 int fd = open("/proc/cpuinfo", O_RDONLY);
118 assert(fd == -1 && errno == ENOENT);