0001
0002 #include <errno.h>
0003 #include <inttypes.h>
0004 #include <api/fs/tracing_path.h>
0005 #include <linux/err.h>
0006 #include <linux/string.h>
0007 #include <sys/types.h>
0008 #include <sys/stat.h>
0009 #include <fcntl.h>
0010 #include "thread_map.h"
0011 #include "evsel.h"
0012 #include "debug.h"
0013 #include "tests.h"
0014 #include "util/counts.h"
0015
0016 static int test__openat_syscall_event(struct test_suite *test __maybe_unused,
0017 int subtest __maybe_unused)
0018 {
0019 int err = TEST_FAIL, fd;
0020 struct evsel *evsel;
0021 unsigned int nr_openat_calls = 111, i;
0022 struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
0023 char sbuf[STRERR_BUFSIZE];
0024 char errbuf[BUFSIZ];
0025
0026 if (threads == NULL) {
0027 pr_debug("thread_map__new\n");
0028 return TEST_FAIL;
0029 }
0030
0031 evsel = evsel__newtp("syscalls", "sys_enter_openat");
0032 if (IS_ERR(evsel)) {
0033 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
0034 pr_debug("%s\n", errbuf);
0035 err = TEST_SKIP;
0036 goto out_thread_map_delete;
0037 }
0038
0039 if (evsel__open_per_thread(evsel, threads) < 0) {
0040 pr_debug("failed to open counter: %s, "
0041 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
0042 str_error_r(errno, sbuf, sizeof(sbuf)));
0043 err = TEST_SKIP;
0044 goto out_evsel_delete;
0045 }
0046
0047 for (i = 0; i < nr_openat_calls; ++i) {
0048 fd = openat(0, "/etc/passwd", O_RDONLY);
0049 close(fd);
0050 }
0051
0052 if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
0053 pr_debug("evsel__read_on_cpu\n");
0054 goto out_close_fd;
0055 }
0056
0057 if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
0058 pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
0059 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
0060 goto out_close_fd;
0061 }
0062
0063 err = TEST_OK;
0064 out_close_fd:
0065 perf_evsel__close_fd(&evsel->core);
0066 out_evsel_delete:
0067 evsel__delete(evsel);
0068 out_thread_map_delete:
0069 perf_thread_map__put(threads);
0070 return err;
0071 }
0072
0073 static struct test_case tests__openat_syscall_event[] = {
0074 TEST_CASE_REASON("Detect openat syscall event",
0075 openat_syscall_event,
0076 "permissions"),
0077 { .name = NULL, }
0078 };
0079
0080 struct test_suite suite__openat_syscall_event = {
0081 .desc = "Detect openat syscall event",
0082 .test_cases = tests__openat_syscall_event,
0083 };