summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
blob: 172c999e523c10440814cea1334b1e4534d88ad2 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#define _GNU_SOURCE
#include <sched.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <test_progs.h>

#define TDIR "/sys/kernel/debug"

static int read_iter(char *file)
{
	/* 1024 should be enough to get contiguous 4 "iter" letters at some point */
	char buf[1024];
	int fd, len;

	fd = open(file, 0);
	if (fd < 0)
		return -1;
	while ((len = read(fd, buf, sizeof(buf))) > 0)
		if (strstr(buf, "iter")) {
			close(fd);
			return 0;
		}
	close(fd);
	return -1;
}

static int fn(void)
{
	int err, duration = 0;

	err = unshare(CLONE_NEWNS);
	if (CHECK(err, "unshare", "failed: %d\n", errno))
		goto out;

	err = mount("", "/", "", MS_REC | MS_PRIVATE, NULL);
	if (CHECK(err, "mount /", "failed: %d\n", errno))
		goto out;

	err = umount(TDIR);
	if (CHECK(err, "umount " TDIR, "failed: %d\n", errno))
		goto out;

	err = mount("none", TDIR, "tmpfs", 0, NULL);
	if (CHECK(err, "mount", "mount root failed: %d\n", errno))
		goto out;

	err = mkdir(TDIR "/fs1", 0777);
	if (CHECK(err, "mkdir "TDIR"/fs1", "failed: %d\n", errno))
		goto out;
	err = mkdir(TDIR "/fs2", 0777);
	if (CHECK(err, "mkdir "TDIR"/fs2", "failed: %d\n", errno))
		goto out;

	err = mount("bpf", TDIR "/fs1", "bpf", 0, NULL);
	if (CHECK(err, "mount bpffs "TDIR"/fs1", "failed: %d\n", errno))
		goto out;
	err = mount("bpf", TDIR "/fs2", "bpf", 0, NULL);
	if (CHECK(err, "mount bpffs " TDIR "/fs2", "failed: %d\n", errno))
		goto out;

	err = read_iter(TDIR "/fs1/maps.debug");
	if (CHECK(err, "reading " TDIR "/fs1/maps.debug", "failed\n"))
		goto out;
	err = read_iter(TDIR "/fs2/progs.debug");
	if (CHECK(err, "reading " TDIR "/fs2/progs.debug", "failed\n"))
		goto out;
out:
	umount(TDIR "/fs1");
	umount(TDIR "/fs2");
	rmdir(TDIR "/fs1");
	rmdir(TDIR "/fs2");
	umount(TDIR);
	exit(err);
}

void test_test_bpffs(void)
{
	int err, duration = 0, status = 0;
	pid_t pid;

	pid = fork();
	if (CHECK(pid == -1, "clone", "clone failed %d", errno))
		return;
	if (pid == 0)
		fn();
	err = waitpid(pid, &status, 0);
	if (CHECK(err == -1 && errno != ECHILD, "waitpid", "failed %d", errno))
		return;
	if (CHECK(WEXITSTATUS(status), "bpffs test ", "failed %d", WEXITSTATUS(status)))
		return;
}