lib.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2014, Michael Ellerman, IBM Corp.
  4. */
  5. #define _GNU_SOURCE /* For CPU_ZERO etc. */
  6. #include <errno.h>
  7. #include <sched.h>
  8. #include <setjmp.h>
  9. #include <stdlib.h>
  10. #include <sys/wait.h>
  11. #include "utils.h"
  12. #include "lib.h"
  13. #define PARENT_TOKEN 0xAA
  14. #define CHILD_TOKEN 0x55
  15. int sync_with_child(union pipe read_pipe, union pipe write_pipe)
  16. {
  17. char c = PARENT_TOKEN;
  18. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  19. FAIL_IF(read(read_pipe.read_fd, &c, 1) != 1);
  20. if (c != CHILD_TOKEN) /* sometimes expected */
  21. return 1;
  22. return 0;
  23. }
  24. int wait_for_parent(union pipe read_pipe)
  25. {
  26. char c;
  27. FAIL_IF(read(read_pipe.read_fd, &c, 1) != 1);
  28. FAIL_IF(c != PARENT_TOKEN);
  29. return 0;
  30. }
  31. int notify_parent(union pipe write_pipe)
  32. {
  33. char c = CHILD_TOKEN;
  34. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  35. return 0;
  36. }
  37. int notify_parent_of_error(union pipe write_pipe)
  38. {
  39. char c = ~CHILD_TOKEN;
  40. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  41. return 0;
  42. }
  43. int wait_for_child(pid_t child_pid)
  44. {
  45. int rc;
  46. if (waitpid(child_pid, &rc, 0) == -1) {
  47. perror("waitpid");
  48. return 1;
  49. }
  50. if (WIFEXITED(rc))
  51. rc = WEXITSTATUS(rc);
  52. else
  53. rc = 1; /* Signal or other */
  54. return rc;
  55. }
  56. int kill_child_and_wait(pid_t child_pid)
  57. {
  58. kill(child_pid, SIGTERM);
  59. return wait_for_child(child_pid);
  60. }
  61. static int eat_cpu_child(union pipe read_pipe, union pipe write_pipe)
  62. {
  63. volatile int i = 0;
  64. /*
  65. * We are just here to eat cpu and die. So make sure we can be killed,
  66. * and also don't do any custom SIGTERM handling.
  67. */
  68. signal(SIGTERM, SIG_DFL);
  69. notify_parent(write_pipe);
  70. wait_for_parent(read_pipe);
  71. /* Soak up cpu forever */
  72. while (1) i++;
  73. return 0;
  74. }
  75. pid_t eat_cpu(int (test_function)(void))
  76. {
  77. union pipe read_pipe, write_pipe;
  78. int rc;
  79. pid_t pid;
  80. FAIL_IF(bind_to_cpu(BIND_CPU_ANY) < 0);
  81. if (pipe(read_pipe.fds) == -1)
  82. return -1;
  83. if (pipe(write_pipe.fds) == -1)
  84. return -1;
  85. pid = fork();
  86. if (pid == 0)
  87. exit(eat_cpu_child(write_pipe, read_pipe));
  88. if (sync_with_child(read_pipe, write_pipe)) {
  89. rc = -1;
  90. goto out;
  91. }
  92. printf("main test running as pid %d\n", getpid());
  93. rc = test_function();
  94. out:
  95. kill(pid, SIGKILL);
  96. return rc;
  97. }
  98. struct addr_range libc, vdso;
  99. int parse_proc_maps(void)
  100. {
  101. unsigned long start, end;
  102. char execute, name[128];
  103. FILE *f;
  104. int rc;
  105. f = fopen("/proc/self/maps", "r");
  106. if (!f) {
  107. perror("fopen");
  108. return -1;
  109. }
  110. do {
  111. /* This skips line with no executable which is what we want */
  112. rc = fscanf(f, "%lx-%lx %*c%*c%c%*c %*x %*d:%*d %*d %127s\n",
  113. &start, &end, &execute, name);
  114. if (rc <= 0)
  115. break;
  116. if (execute != 'x')
  117. continue;
  118. if (strstr(name, "libc")) {
  119. libc.first = start;
  120. libc.last = end - 1;
  121. } else if (strstr(name, "[vdso]")) {
  122. vdso.first = start;
  123. vdso.last = end - 1;
  124. }
  125. } while(1);
  126. fclose(f);
  127. return 0;
  128. }
  129. #define PARANOID_PATH "/proc/sys/kernel/perf_event_paranoid"
  130. bool require_paranoia_below(int level)
  131. {
  132. int err;
  133. long current;
  134. err = read_long(PARANOID_PATH, &current, 10);
  135. if (err) {
  136. printf("Couldn't parse " PARANOID_PATH "?\n");
  137. return false;
  138. }
  139. return current < level;
  140. }