xref: /linux/tools/testing/selftests/bpf/progs/cgroup_getset_retval_setsockopt.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
4  * Copyright 2021 Google LLC.
5  */
6 
7 #include <errno.h>
8 #include <linux/bpf.h>
9 #include <bpf/bpf_helpers.h>
10 
11 __u32 invocations = 0;
12 __u32 assertion_error = 0;
13 __u32 retval_value = 0;
14 
15 SEC("cgroup/setsockopt")
16 int get_retval(struct bpf_sockopt *ctx)
17 {
18 	retval_value = bpf_get_retval();
19 	__sync_fetch_and_add(&invocations, 1);
20 
21 	return 1;
22 }
23 
24 SEC("cgroup/setsockopt")
25 int set_eunatch(struct bpf_sockopt *ctx)
26 {
27 	__sync_fetch_and_add(&invocations, 1);
28 
29 	if (bpf_set_retval(-EUNATCH))
30 		assertion_error = 1;
31 
32 	return 0;
33 }
34 
35 SEC("cgroup/setsockopt")
36 int set_eisconn(struct bpf_sockopt *ctx)
37 {
38 	__sync_fetch_and_add(&invocations, 1);
39 
40 	if (bpf_set_retval(-EISCONN))
41 		assertion_error = 1;
42 
43 	return 0;
44 }
45 
46 SEC("cgroup/setsockopt")
47 int legacy_eperm(struct bpf_sockopt *ctx)
48 {
49 	__sync_fetch_and_add(&invocations, 1);
50 
51 	return 0;
52 }
53