openat-syscall.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <unistd.h>
  11. #include "thread_map.h"
  12. #include "evsel.h"
  13. #include "debug.h"
  14. #include "tests.h"
  15. #include "util/counts.h"
  16. static int test__openat_syscall_event(struct test_suite *test __maybe_unused,
  17. int subtest __maybe_unused)
  18. {
  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_by_tid(getpid());
  23. char sbuf[STRERR_BUFSIZE];
  24. char errbuf[BUFSIZ];
  25. if (threads == NULL) {
  26. pr_debug("thread_map__new\n");
  27. return TEST_FAIL;
  28. }
  29. evsel = evsel__newtp("syscalls", "sys_enter_openat");
  30. if (IS_ERR(evsel)) {
  31. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
  32. pr_debug("%s\n", errbuf);
  33. err = TEST_SKIP;
  34. goto out_thread_map_delete;
  35. }
  36. if (evsel__open_per_thread(evsel, threads) < 0) {
  37. pr_debug("failed to open counter: %s, "
  38. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  39. str_error_r(errno, sbuf, sizeof(sbuf)));
  40. err = TEST_SKIP;
  41. goto out_evsel_delete;
  42. }
  43. for (i = 0; i < nr_openat_calls; ++i) {
  44. fd = openat(0, "/etc/passwd", O_RDONLY);
  45. close(fd);
  46. }
  47. if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
  48. pr_debug("evsel__read_on_cpu\n");
  49. goto out_close_fd;
  50. }
  51. if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
  52. pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
  53. nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
  54. goto out_close_fd;
  55. }
  56. err = TEST_OK;
  57. out_close_fd:
  58. perf_evsel__close_fd(&evsel->core);
  59. out_evsel_delete:
  60. evsel__delete(evsel);
  61. out_thread_map_delete:
  62. perf_thread_map__put(threads);
  63. return err;
  64. }
  65. static struct test_case tests__openat_syscall_event[] = {
  66. TEST_CASE_REASON("Detect openat syscall event",
  67. openat_syscall_event,
  68. "permissions"),
  69. { .name = NULL, }
  70. };
  71. struct test_suite suite__openat_syscall_event = {
  72. .desc = "Detect openat syscall event",
  73. .test_cases = tests__openat_syscall_event,
  74. };