1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 #include "fexit_test.lskel.h"
5 
6 static int fexit_test(struct fexit_test_lskel *fexit_skel)
7 {
8 	int err, prog_fd, i;
9 	int link_fd;
10 	__u64 *result;
11 	LIBBPF_OPTS(bpf_test_run_opts, topts);
12 
13 	err = fexit_test_lskel__attach(fexit_skel);
14 	if (!ASSERT_OK(err, "fexit_attach"))
15 		return err;
16 
17 	/* Check that already linked program can't be attached again. */
18 	link_fd = fexit_test_lskel__test1__attach(fexit_skel);
19 	if (!ASSERT_LT(link_fd, 0, "fexit_attach_link"))
20 		return -1;
21 
22 	prog_fd = fexit_skel->progs.test1.prog_fd;
23 	err = bpf_prog_test_run_opts(prog_fd, &topts);
24 	ASSERT_OK(err, "test_run");
25 	ASSERT_EQ(topts.retval, 0, "test_run");
26 
27 	result = (__u64 *)fexit_skel->bss;
28 	for (i = 0; i < sizeof(*fexit_skel->bss) / sizeof(__u64); i++) {
29 		if (!ASSERT_EQ(result[i], 1, "fexit_result"))
30 			return -1;
31 	}
32 
33 	fexit_test_lskel__detach(fexit_skel);
34 
35 	/* zero results for re-attach test */
36 	memset(fexit_skel->bss, 0, sizeof(*fexit_skel->bss));
37 	return 0;
38 }
39 
40 void test_fexit_test(void)
41 {
42 	struct fexit_test_lskel *fexit_skel = NULL;
43 	int err;
44 
45 	fexit_skel = fexit_test_lskel__open_and_load();
46 	if (!ASSERT_OK_PTR(fexit_skel, "fexit_skel_load"))
47 		goto cleanup;
48 
49 	err = fexit_test(fexit_skel);
50 	if (!ASSERT_OK(err, "fexit_first_attach"))
51 		goto cleanup;
52 
53 	err = fexit_test(fexit_skel);
54 	ASSERT_OK(err, "fexit_second_attach");
55 
56 cleanup:
57 	fexit_test_lskel__destroy(fexit_skel);
58 }
59