lib.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  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. int bind_to_cpu(int cpu)
  14. {
  15. cpu_set_t mask;
  16. printf("Binding to cpu %d\n", cpu);
  17. CPU_ZERO(&mask);
  18. CPU_SET(cpu, &mask);
  19. return sched_setaffinity(0, sizeof(mask), &mask);
  20. }
  21. #define PARENT_TOKEN 0xAA
  22. #define CHILD_TOKEN 0x55
  23. int sync_with_child(union pipe read_pipe, union pipe write_pipe)
  24. {
  25. char c = PARENT_TOKEN;
  26. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  27. FAIL_IF(read(read_pipe.read_fd, &c, 1) != 1);
  28. if (c != CHILD_TOKEN) /* sometimes expected */
  29. return 1;
  30. return 0;
  31. }
  32. int wait_for_parent(union pipe read_pipe)
  33. {
  34. char c;
  35. FAIL_IF(read(read_pipe.read_fd, &c, 1) != 1);
  36. FAIL_IF(c != PARENT_TOKEN);
  37. return 0;
  38. }
  39. int notify_parent(union pipe write_pipe)
  40. {
  41. char c = CHILD_TOKEN;
  42. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  43. return 0;
  44. }
  45. int notify_parent_of_error(union pipe write_pipe)
  46. {
  47. char c = ~CHILD_TOKEN;
  48. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  49. return 0;
  50. }
  51. int wait_for_child(pid_t child_pid)
  52. {
  53. int rc;
  54. if (waitpid(child_pid, &rc, 0) == -1) {
  55. perror("waitpid");
  56. return 1;
  57. }
  58. if (WIFEXITED(rc))
  59. rc = WEXITSTATUS(rc);
  60. else
  61. rc = 1; /* Signal or other */
  62. return rc;
  63. }
  64. int kill_child_and_wait(pid_t child_pid)
  65. {
  66. kill(child_pid, SIGTERM);
  67. return wait_for_child(child_pid);
  68. }
  69. static int eat_cpu_child(union pipe read_pipe, union pipe write_pipe)
  70. {
  71. volatile int i = 0;
  72. /*
  73. * We are just here to eat cpu and die. So make sure we can be killed,
  74. * and also don't do any custom SIGTERM handling.
  75. */
  76. signal(SIGTERM, SIG_DFL);
  77. notify_parent(write_pipe);
  78. wait_for_parent(read_pipe);
  79. /* Soak up cpu forever */
  80. while (1) i++;
  81. return 0;
  82. }
  83. pid_t eat_cpu(int (test_function)(void))
  84. {
  85. union pipe read_pipe, write_pipe;
  86. int cpu, rc;
  87. pid_t pid;
  88. cpu = pick_online_cpu();
  89. FAIL_IF(cpu < 0);
  90. FAIL_IF(bind_to_cpu(cpu));
  91. if (pipe(read_pipe.fds) == -1)
  92. return -1;
  93. if (pipe(write_pipe.fds) == -1)
  94. return -1;
  95. pid = fork();
  96. if (pid == 0)
  97. exit(eat_cpu_child(write_pipe, read_pipe));
  98. if (sync_with_child(read_pipe, write_pipe)) {
  99. rc = -1;
  100. goto out;
  101. }
  102. printf("main test running as pid %d\n", getpid());
  103. rc = test_function();
  104. out:
  105. kill(pid, SIGKILL);
  106. return rc;
  107. }
  108. struct addr_range libc, vdso;
  109. int parse_proc_maps(void)
  110. {
  111. unsigned long start, end;
  112. char execute, name[128];
  113. FILE *f;
  114. int rc;
  115. f = fopen("/proc/self/maps", "r");
  116. if (!f) {
  117. perror("fopen");
  118. return -1;
  119. }
  120. do {
  121. /* This skips line with no executable which is what we want */
  122. rc = fscanf(f, "%lx-%lx %*c%*c%c%*c %*x %*d:%*d %*d %127s\n",
  123. &start, &end, &execute, name);
  124. if (rc <= 0)
  125. break;
  126. if (execute != 'x')
  127. continue;
  128. if (strstr(name, "libc")) {
  129. libc.first = start;
  130. libc.last = end - 1;
  131. } else if (strstr(name, "[vdso]")) {
  132. vdso.first = start;
  133. vdso.last = end - 1;
  134. }
  135. } while(1);
  136. fclose(f);
  137. return 0;
  138. }
  139. #define PARANOID_PATH "/proc/sys/kernel/perf_event_paranoid"
  140. bool require_paranoia_below(int level)
  141. {
  142. long current;
  143. char *end, buf[16];
  144. FILE *f;
  145. bool rc;
  146. rc = false;
  147. f = fopen(PARANOID_PATH, "r");
  148. if (!f) {
  149. perror("fopen");
  150. goto out;
  151. }
  152. if (!fgets(buf, sizeof(buf), f)) {
  153. printf("Couldn't read " PARANOID_PATH "?\n");
  154. goto out_close;
  155. }
  156. current = strtol(buf, &end, 10);
  157. if (end == buf) {
  158. printf("Couldn't parse " PARANOID_PATH "?\n");
  159. goto out_close;
  160. }
  161. if (current >= level)
  162. goto out_close;
  163. rc = true;
  164. out_close:
  165. fclose(f);
  166. out:
  167. return rc;
  168. }