openat-syscall.c (1136fa0c07de570dc17858745af8be169d1440ba) openat-syscall.c (f9b10c82faf5859262de10ea30261391e117b9bd)
1// SPDX-License-Identifier: GPL-2.0
2#include <errno.h>
3#include <inttypes.h>
4#include <api/fs/tracing_path.h>
5#include <linux/err.h>
6#include <linux/string.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include "thread_map.h"
11#include "evsel.h"
12#include "debug.h"
13#include "tests.h"
14#include "util/counts.h"
15
16static int test__openat_syscall_event(struct test_suite *test __maybe_unused,
17 int subtest __maybe_unused)
18{
1// SPDX-License-Identifier: GPL-2.0
2#include <errno.h>
3#include <inttypes.h>
4#include <api/fs/tracing_path.h>
5#include <linux/err.h>
6#include <linux/string.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include "thread_map.h"
11#include "evsel.h"
12#include "debug.h"
13#include "tests.h"
14#include "util/counts.h"
15
16static int test__openat_syscall_event(struct test_suite *test __maybe_unused,
17 int subtest __maybe_unused)
18{
19 int err = -1, fd;
19 int err = TEST_FAIL, fd;
20 struct evsel *evsel;
21 unsigned int nr_openat_calls = 111, i;
22 struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
23 char sbuf[STRERR_BUFSIZE];
24 char errbuf[BUFSIZ];
25
26 if (threads == NULL) {
27 pr_debug("thread_map__new\n");
20 struct evsel *evsel;
21 unsigned int nr_openat_calls = 111, i;
22 struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
23 char sbuf[STRERR_BUFSIZE];
24 char errbuf[BUFSIZ];
25
26 if (threads == NULL) {
27 pr_debug("thread_map__new\n");
28 return -1;
28 return TEST_FAIL;
29 }
30
31 evsel = evsel__newtp("syscalls", "sys_enter_openat");
32 if (IS_ERR(evsel)) {
33 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
34 pr_debug("%s\n", errbuf);
29 }
30
31 evsel = evsel__newtp("syscalls", "sys_enter_openat");
32 if (IS_ERR(evsel)) {
33 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
34 pr_debug("%s\n", errbuf);
35 err = TEST_SKIP;
35 goto out_thread_map_delete;
36 }
37
38 if (evsel__open_per_thread(evsel, threads) < 0) {
39 pr_debug("failed to open counter: %s, "
40 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
41 str_error_r(errno, sbuf, sizeof(sbuf)));
36 goto out_thread_map_delete;
37 }
38
39 if (evsel__open_per_thread(evsel, threads) < 0) {
40 pr_debug("failed to open counter: %s, "
41 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
42 str_error_r(errno, sbuf, sizeof(sbuf)));
43 err = TEST_SKIP;
42 goto out_evsel_delete;
43 }
44
45 for (i = 0; i < nr_openat_calls; ++i) {
46 fd = openat(0, "/etc/passwd", O_RDONLY);
47 close(fd);
48 }
49
50 if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
51 pr_debug("evsel__read_on_cpu\n");
52 goto out_close_fd;
53 }
54
55 if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
56 pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
57 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
58 goto out_close_fd;
59 }
60
44 goto out_evsel_delete;
45 }
46
47 for (i = 0; i < nr_openat_calls; ++i) {
48 fd = openat(0, "/etc/passwd", O_RDONLY);
49 close(fd);
50 }
51
52 if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
53 pr_debug("evsel__read_on_cpu\n");
54 goto out_close_fd;
55 }
56
57 if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
58 pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
59 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
60 goto out_close_fd;
61 }
62
61 err = 0;
63 err = TEST_OK;
62out_close_fd:
63 perf_evsel__close_fd(&evsel->core);
64out_evsel_delete:
65 evsel__delete(evsel);
66out_thread_map_delete:
67 perf_thread_map__put(threads);
68 return err;
69}
70
64out_close_fd:
65 perf_evsel__close_fd(&evsel->core);
66out_evsel_delete:
67 evsel__delete(evsel);
68out_thread_map_delete:
69 perf_thread_map__put(threads);
70 return err;
71}
72
71DEFINE_SUITE("Detect openat syscall event", openat_syscall_event);
73static struct test_case tests__openat_syscall_event[] = {
74 TEST_CASE_REASON("Detect openat syscall event",
75 openat_syscall_event,
76 "permissions"),
77 { .name = NULL, }
78};
79
80struct test_suite suite__openat_syscall_event = {
81 .desc = "Detect openat syscall event",
82 .test_cases = tests__openat_syscall_event,
83};