blob: 7e05b8b5cc95ff1322b298e3e9e23cac2a1a10e6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Jiri Olsafbf99622015-09-02 09:56:45 +02004#include <api/fs/tracing_path.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02005#include <linux/err.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -03006#include <linux/string.h>
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -03007#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
Jiri Olsad3b59a32012-11-10 01:46:42 +010010#include "thread_map.h"
11#include "evsel.h"
12#include "debug.h"
13#include "tests.h"
Arnaldo Carvalho de Meloe4aec1b2019-08-21 14:01:24 -030014#include "util/counts.h"
Jiri Olsad3b59a32012-11-10 01:46:42 +010015
Ian Rogers33f44bf2021-11-03 23:41:51 -070016static int test__openat_syscall_event(struct test_suite *test __maybe_unused,
Ian Rogersd68f0362021-11-03 23:41:50 -070017 int subtest __maybe_unused)
Jiri Olsad3b59a32012-11-10 01:46:42 +010018{
Ian Rogersf9b10c82022-05-17 21:20:22 -070019 int err = TEST_FAIL, fd;
Jiri Olsa32dcd022019-07-21 13:23:51 +020020 struct evsel *evsel;
Riku Voipio43f322b2015-04-16 16:52:53 +030021 unsigned int nr_openat_calls = 111, i;
Jiri Olsa9749b902019-07-21 13:23:50 +020022 struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000023 char sbuf[STRERR_BUFSIZE];
Jiri Olsafbf99622015-09-02 09:56:45 +020024 char errbuf[BUFSIZ];
Jiri Olsad3b59a32012-11-10 01:46:42 +010025
Jiri Olsad3b59a32012-11-10 01:46:42 +010026 if (threads == NULL) {
27 pr_debug("thread_map__new\n");
Ian Rogersf9b10c82022-05-17 21:20:22 -070028 return TEST_FAIL;
Jiri Olsad3b59a32012-11-10 01:46:42 +010029 }
30
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -030031 evsel = evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020032 if (IS_ERR(evsel)) {
Jiri Olsafbf99622015-09-02 09:56:45 +020033 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
Namhyung Kim87191382015-10-20 00:23:48 +090034 pr_debug("%s\n", errbuf);
Ian Rogersf9b10c82022-05-17 21:20:22 -070035 err = TEST_SKIP;
Jiri Olsad3b59a32012-11-10 01:46:42 +010036 goto out_thread_map_delete;
37 }
38
Arnaldo Carvalho de Meloaa8c4062020-04-29 16:21:03 -030039 if (evsel__open_per_thread(evsel, threads) < 0) {
Jiri Olsad3b59a32012-11-10 01:46:42 +010040 pr_debug("failed to open counter: %s, "
41 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030042 str_error_r(errno, sbuf, sizeof(sbuf)));
Ian Rogersf9b10c82022-05-17 21:20:22 -070043 err = TEST_SKIP;
Jiri Olsad3b59a32012-11-10 01:46:42 +010044 goto out_evsel_delete;
45 }
46
Riku Voipio43f322b2015-04-16 16:52:53 +030047 for (i = 0; i < nr_openat_calls; ++i) {
48 fd = openat(0, "/etc/passwd", O_RDONLY);
Jiri Olsad3b59a32012-11-10 01:46:42 +010049 close(fd);
50 }
51
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -030052 if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
53 pr_debug("evsel__read_on_cpu\n");
Jiri Olsad3b59a32012-11-10 01:46:42 +010054 goto out_close_fd;
55 }
56
Jiri Olsaa6fa0032015-06-26 11:29:11 +020057 if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -030058 pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
Jiri Olsaa6fa0032015-06-26 11:29:11 +020059 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
Jiri Olsad3b59a32012-11-10 01:46:42 +010060 goto out_close_fd;
61 }
62
Ian Rogersf9b10c82022-05-17 21:20:22 -070063 err = TEST_OK;
Jiri Olsad3b59a32012-11-10 01:46:42 +010064out_close_fd:
Jiri Olsa88761fa2019-07-21 13:24:50 +020065 perf_evsel__close_fd(&evsel->core);
Jiri Olsad3b59a32012-11-10 01:46:42 +010066out_evsel_delete:
Jiri Olsa5eb2dd22019-07-21 13:23:57 +020067 evsel__delete(evsel);
Jiri Olsad3b59a32012-11-10 01:46:42 +010068out_thread_map_delete:
Jiri Olsa7836e522019-07-21 13:24:20 +020069 perf_thread_map__put(threads);
Jiri Olsad3b59a32012-11-10 01:46:42 +010070 return err;
71}
Ian Rogersd68f0362021-11-03 23:41:50 -070072
Ian Rogersf9b10c82022-05-17 21:20:22 -070073static 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};