Blame view

samples/bpf/test_current_task_under_cgroup_user.c 1.53 KB
25763b3c8   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
2
  /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
3
4
5
6
7
8
   */
  
  #define _GNU_SOURCE
  #include <stdio.h>
  #include <linux/bpf.h>
  #include <unistd.h>
2bf3e2ef4   Jakub Kicinski   samples: bpf: inc...
9
  #include <bpf/bpf.h>
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
10
  #include "bpf_load.h"
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
11
  #include "cgroup_helpers.h"
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
12

1a922fee6   Sargun Dhillon   samples, bpf: Ref...
13
  #define CGROUP_PATH		"/my-cgroup"
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
14
15
16
  
  int main(int argc, char **argv)
  {
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
17
  	pid_t remote_pid, local_pid = getpid();
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
18
19
  	int cg2, idx = 0, rc = 0;
  	char filename[256];
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
20
21
22
23
24
25
  
  	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  	if (load_bpf_file(filename)) {
  		printf("%s", bpf_log_buf);
  		return 1;
  	}
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
26
27
  	if (setup_cgroup_environment())
  		goto err;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
28

1a922fee6   Sargun Dhillon   samples, bpf: Ref...
29
  	cg2 = create_and_get_cgroup(CGROUP_PATH);
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
30

a8911d6d5   Stanislav Fomichev   selftests/bpf: fi...
31
  	if (cg2 < 0)
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
32
  		goto err;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
33

d40fc181e   Joe Stringer   samples/bpf: Make...
34
  	if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
35
  		log_err("Adding target cgroup to map");
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
36
  		goto err;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
37
  	}
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
38
39
  	if (join_cgroup(CGROUP_PATH))
  		goto err;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
40
41
42
43
44
45
  	/*
  	 * The installed helper program catched the sync call, and should
  	 * write it to the map.
  	 */
  
  	sync();
d40fc181e   Joe Stringer   samples/bpf: Make...
46
  	bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
47
48
49
50
51
52
  
  	if (local_pid != remote_pid) {
  		fprintf(stderr,
  			"BPF Helper didn't write correct PID to map, but: %d
  ",
  			remote_pid);
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
53
  		goto err;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
54
55
56
  	}
  
  	/* Verify the negative scenario; leave the cgroup */
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
57
58
  	if (join_cgroup("/"))
  		goto err;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
59
60
  
  	remote_pid = 0;
d40fc181e   Joe Stringer   samples/bpf: Make...
61
  	bpf_map_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY);
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
62
63
  
  	sync();
d40fc181e   Joe Stringer   samples/bpf: Make...
64
  	bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
65
66
67
68
  
  	if (local_pid == remote_pid) {
  		fprintf(stderr, "BPF cgroup negative test did not work
  ");
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
69
  		goto err;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
70
  	}
1a922fee6   Sargun Dhillon   samples, bpf: Ref...
71
72
73
  	goto out;
  err:
  	rc = 1;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
74

1a922fee6   Sargun Dhillon   samples, bpf: Ref...
75
76
77
78
  out:
  	close(cg2);
  	cleanup_cgroup_environment();
  	return rc;
9e6e60ecb   Sargun Dhillon   samples/bpf: Add ...
79
  }