pidfd_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <linux/types.h>
  6. #include <pthread.h>
  7. #include <sched.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <syscall.h>
  14. #include <sys/epoll.h>
  15. #include <sys/mman.h>
  16. #include <sys/mount.h>
  17. #include <sys/wait.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include "pidfd.h"
  21. #include "../kselftest.h"
  22. #define str(s) _str(s)
  23. #define _str(s) #s
  24. #define CHILD_THREAD_MIN_WAIT 3 /* seconds */
  25. #define MAX_EVENTS 5
  26. static bool have_pidfd_send_signal;
  27. static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *))
  28. {
  29. size_t stack_size = 1024;
  30. char *stack[1024] = { 0 };
  31. #ifdef __ia64__
  32. return __clone2(fn, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
  33. #else
  34. return clone(fn, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
  35. #endif
  36. }
  37. static int signal_received;
  38. static void set_signal_received_on_sigusr1(int sig)
  39. {
  40. if (sig == SIGUSR1)
  41. signal_received = 1;
  42. }
  43. /*
  44. * Straightforward test to see whether pidfd_send_signal() works is to send
  45. * a signal to ourself.
  46. */
  47. static int test_pidfd_send_signal_simple_success(void)
  48. {
  49. int pidfd, ret;
  50. const char *test_name = "pidfd_send_signal send SIGUSR1";
  51. if (!have_pidfd_send_signal) {
  52. ksft_test_result_skip(
  53. "%s test: pidfd_send_signal() syscall not supported\n",
  54. test_name);
  55. return 0;
  56. }
  57. pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
  58. if (pidfd < 0)
  59. ksft_exit_fail_msg(
  60. "%s test: Failed to open process file descriptor\n",
  61. test_name);
  62. signal(SIGUSR1, set_signal_received_on_sigusr1);
  63. ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0);
  64. close(pidfd);
  65. if (ret < 0)
  66. ksft_exit_fail_msg("%s test: Failed to send signal\n",
  67. test_name);
  68. if (signal_received != 1)
  69. ksft_exit_fail_msg("%s test: Failed to receive signal\n",
  70. test_name);
  71. signal_received = 0;
  72. ksft_test_result_pass("%s test: Sent signal\n", test_name);
  73. return 0;
  74. }
  75. static int test_pidfd_send_signal_exited_fail(void)
  76. {
  77. int pidfd, ret, saved_errno;
  78. char buf[256];
  79. pid_t pid;
  80. const char *test_name = "pidfd_send_signal signal exited process";
  81. if (!have_pidfd_send_signal) {
  82. ksft_test_result_skip(
  83. "%s test: pidfd_send_signal() syscall not supported\n",
  84. test_name);
  85. return 0;
  86. }
  87. pid = fork();
  88. if (pid < 0)
  89. ksft_exit_fail_msg("%s test: Failed to create new process\n",
  90. test_name);
  91. if (pid == 0)
  92. _exit(EXIT_SUCCESS);
  93. snprintf(buf, sizeof(buf), "/proc/%d", pid);
  94. pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
  95. (void)wait_for_pid(pid);
  96. if (pidfd < 0)
  97. ksft_exit_fail_msg(
  98. "%s test: Failed to open process file descriptor\n",
  99. test_name);
  100. ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
  101. saved_errno = errno;
  102. close(pidfd);
  103. if (ret == 0)
  104. ksft_exit_fail_msg(
  105. "%s test: Managed to send signal to process even though it should have failed\n",
  106. test_name);
  107. if (saved_errno != ESRCH)
  108. ksft_exit_fail_msg(
  109. "%s test: Expected to receive ESRCH as errno value but received %d instead\n",
  110. test_name, saved_errno);
  111. ksft_test_result_pass("%s test: Failed to send signal as expected\n",
  112. test_name);
  113. return 0;
  114. }
  115. /*
  116. * Maximum number of cycles we allow. This is equivalent to PID_MAX_DEFAULT.
  117. * If users set a higher limit or we have cycled PIDFD_MAX_DEFAULT number of
  118. * times then we skip the test to not go into an infinite loop or block for a
  119. * long time.
  120. */
  121. #define PIDFD_MAX_DEFAULT 0x8000
  122. static int test_pidfd_send_signal_recycled_pid_fail(void)
  123. {
  124. int i, ret;
  125. pid_t pid1;
  126. const char *test_name = "pidfd_send_signal signal recycled pid";
  127. if (!have_pidfd_send_signal) {
  128. ksft_test_result_skip(
  129. "%s test: pidfd_send_signal() syscall not supported\n",
  130. test_name);
  131. return 0;
  132. }
  133. ret = unshare(CLONE_NEWPID);
  134. if (ret < 0) {
  135. if (errno == EPERM) {
  136. ksft_test_result_skip("%s test: Unsharing pid namespace not permitted\n",
  137. test_name);
  138. return 0;
  139. }
  140. ksft_exit_fail_msg("%s test: Failed to unshare pid namespace\n",
  141. test_name);
  142. }
  143. ret = unshare(CLONE_NEWNS);
  144. if (ret < 0) {
  145. if (errno == EPERM) {
  146. ksft_test_result_skip("%s test: Unsharing mount namespace not permitted\n",
  147. test_name);
  148. return 0;
  149. }
  150. ksft_exit_fail_msg("%s test: Failed to unshare mount namespace\n",
  151. test_name);
  152. }
  153. ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
  154. if (ret < 0)
  155. ksft_exit_fail_msg("%s test: Failed to remount / private\n",
  156. test_name);
  157. /* pid 1 in new pid namespace */
  158. pid1 = fork();
  159. if (pid1 < 0)
  160. ksft_exit_fail_msg("%s test: Failed to create new process\n",
  161. test_name);
  162. if (pid1 == 0) {
  163. char buf[256];
  164. pid_t pid2;
  165. int pidfd = -1;
  166. (void)umount2("/proc", MNT_DETACH);
  167. ret = mount("proc", "/proc", "proc", 0, NULL);
  168. if (ret < 0)
  169. _exit(PIDFD_ERROR);
  170. /* grab pid PID_RECYCLE */
  171. for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
  172. pid2 = fork();
  173. if (pid2 < 0)
  174. _exit(PIDFD_ERROR);
  175. if (pid2 == 0)
  176. _exit(PIDFD_PASS);
  177. if (pid2 == PID_RECYCLE) {
  178. snprintf(buf, sizeof(buf), "/proc/%d", pid2);
  179. ksft_print_msg("pid to recycle is %d\n", pid2);
  180. pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
  181. }
  182. if (wait_for_pid(pid2))
  183. _exit(PIDFD_ERROR);
  184. if (pid2 >= PID_RECYCLE)
  185. break;
  186. }
  187. /*
  188. * We want to be as predictable as we can so if we haven't been
  189. * able to grab pid PID_RECYCLE skip the test.
  190. */
  191. if (pid2 != PID_RECYCLE) {
  192. /* skip test */
  193. close(pidfd);
  194. _exit(PIDFD_SKIP);
  195. }
  196. if (pidfd < 0)
  197. _exit(PIDFD_ERROR);
  198. for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
  199. char c;
  200. int pipe_fds[2];
  201. pid_t recycled_pid;
  202. int child_ret = PIDFD_PASS;
  203. ret = pipe2(pipe_fds, O_CLOEXEC);
  204. if (ret < 0)
  205. _exit(PIDFD_ERROR);
  206. recycled_pid = fork();
  207. if (recycled_pid < 0)
  208. _exit(PIDFD_ERROR);
  209. if (recycled_pid == 0) {
  210. close(pipe_fds[1]);
  211. (void)read(pipe_fds[0], &c, 1);
  212. close(pipe_fds[0]);
  213. _exit(PIDFD_PASS);
  214. }
  215. /*
  216. * Stop the child so we can inspect whether we have
  217. * recycled pid PID_RECYCLE.
  218. */
  219. close(pipe_fds[0]);
  220. ret = kill(recycled_pid, SIGSTOP);
  221. close(pipe_fds[1]);
  222. if (ret) {
  223. (void)wait_for_pid(recycled_pid);
  224. _exit(PIDFD_ERROR);
  225. }
  226. /*
  227. * We have recycled the pid. Try to signal it. This
  228. * needs to fail since this is a different process than
  229. * the one the pidfd refers to.
  230. */
  231. if (recycled_pid == PID_RECYCLE) {
  232. ret = sys_pidfd_send_signal(pidfd, SIGCONT,
  233. NULL, 0);
  234. if (ret && errno == ESRCH)
  235. child_ret = PIDFD_XFAIL;
  236. else
  237. child_ret = PIDFD_FAIL;
  238. }
  239. /* let the process move on */
  240. ret = kill(recycled_pid, SIGCONT);
  241. if (ret)
  242. (void)kill(recycled_pid, SIGKILL);
  243. if (wait_for_pid(recycled_pid))
  244. _exit(PIDFD_ERROR);
  245. switch (child_ret) {
  246. case PIDFD_FAIL:
  247. /* fallthrough */
  248. case PIDFD_XFAIL:
  249. _exit(child_ret);
  250. case PIDFD_PASS:
  251. break;
  252. default:
  253. /* not reached */
  254. _exit(PIDFD_ERROR);
  255. }
  256. /*
  257. * If the user set a custom pid_max limit we could be
  258. * in the millions.
  259. * Skip the test in this case.
  260. */
  261. if (recycled_pid > PIDFD_MAX_DEFAULT)
  262. _exit(PIDFD_SKIP);
  263. }
  264. /* failed to recycle pid */
  265. _exit(PIDFD_SKIP);
  266. }
  267. ret = wait_for_pid(pid1);
  268. switch (ret) {
  269. case PIDFD_FAIL:
  270. ksft_exit_fail_msg(
  271. "%s test: Managed to signal recycled pid %d\n",
  272. test_name, PID_RECYCLE);
  273. case PIDFD_PASS:
  274. ksft_exit_fail_msg("%s test: Failed to recycle pid %d\n",
  275. test_name, PID_RECYCLE);
  276. case PIDFD_SKIP:
  277. ksft_test_result_skip("%s test: Skipping test\n", test_name);
  278. ret = 0;
  279. break;
  280. case PIDFD_XFAIL:
  281. ksft_test_result_pass(
  282. "%s test: Failed to signal recycled pid as expected\n",
  283. test_name);
  284. ret = 0;
  285. break;
  286. default /* PIDFD_ERROR */:
  287. ksft_exit_fail_msg("%s test: Error while running tests\n",
  288. test_name);
  289. }
  290. return ret;
  291. }
  292. static int test_pidfd_send_signal_syscall_support(void)
  293. {
  294. int pidfd, ret;
  295. const char *test_name = "pidfd_send_signal check for support";
  296. pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
  297. if (pidfd < 0)
  298. ksft_exit_fail_msg(
  299. "%s test: Failed to open process file descriptor\n",
  300. test_name);
  301. ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
  302. if (ret < 0) {
  303. if (errno == ENOSYS) {
  304. ksft_test_result_skip(
  305. "%s test: pidfd_send_signal() syscall not supported\n",
  306. test_name);
  307. return 0;
  308. }
  309. ksft_exit_fail_msg("%s test: Failed to send signal\n",
  310. test_name);
  311. }
  312. have_pidfd_send_signal = true;
  313. close(pidfd);
  314. ksft_test_result_pass(
  315. "%s test: pidfd_send_signal() syscall is supported. Tests can be executed\n",
  316. test_name);
  317. return 0;
  318. }
  319. static void *test_pidfd_poll_exec_thread(void *priv)
  320. {
  321. ksft_print_msg("Child Thread: starting. pid %d tid %ld ; and sleeping\n",
  322. getpid(), syscall(SYS_gettid));
  323. ksft_print_msg("Child Thread: doing exec of sleep\n");
  324. execl("/bin/sleep", "sleep", str(CHILD_THREAD_MIN_WAIT), (char *)NULL);
  325. ksft_print_msg("Child Thread: DONE. pid %d tid %ld\n",
  326. getpid(), syscall(SYS_gettid));
  327. return NULL;
  328. }
  329. static void poll_pidfd(const char *test_name, int pidfd)
  330. {
  331. int c;
  332. int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
  333. struct epoll_event event, events[MAX_EVENTS];
  334. if (epoll_fd == -1)
  335. ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
  336. "(errno %d)\n",
  337. test_name, errno);
  338. event.events = EPOLLIN;
  339. event.data.fd = pidfd;
  340. if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
  341. ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
  342. "(errno %d)\n",
  343. test_name, errno);
  344. }
  345. c = epoll_wait(epoll_fd, events, MAX_EVENTS, 5000);
  346. if (c != 1 || !(events[0].events & EPOLLIN))
  347. ksft_exit_fail_msg("%s test: Unexpected epoll_wait result (c=%d, events=%x) "
  348. "(errno %d)\n",
  349. test_name, c, events[0].events, errno);
  350. close(epoll_fd);
  351. return;
  352. }
  353. static int child_poll_exec_test(void *args)
  354. {
  355. pthread_t t1;
  356. ksft_print_msg("Child (pidfd): starting. pid %d tid %ld\n", getpid(),
  357. syscall(SYS_gettid));
  358. pthread_create(&t1, NULL, test_pidfd_poll_exec_thread, NULL);
  359. /*
  360. * Exec in the non-leader thread will destroy the leader immediately.
  361. * If the wait in the parent returns too soon, the test fails.
  362. */
  363. while (1)
  364. sleep(1);
  365. return 0;
  366. }
  367. static void test_pidfd_poll_exec(int use_waitpid)
  368. {
  369. int pid, pidfd = 0;
  370. int status, ret;
  371. time_t prog_start = time(NULL);
  372. const char *test_name = "pidfd_poll check for premature notification on child thread exec";
  373. ksft_print_msg("Parent: pid: %d\n", getpid());
  374. pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_exec_test);
  375. if (pid < 0)
  376. ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
  377. test_name, pid, errno);
  378. ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
  379. if (use_waitpid) {
  380. ret = waitpid(pid, &status, 0);
  381. if (ret == -1)
  382. ksft_print_msg("Parent: error\n");
  383. if (ret == pid)
  384. ksft_print_msg("Parent: Child process waited for.\n");
  385. } else {
  386. poll_pidfd(test_name, pidfd);
  387. }
  388. time_t prog_time = time(NULL) - prog_start;
  389. ksft_print_msg("Time waited for child: %lu\n", prog_time);
  390. close(pidfd);
  391. if (prog_time < CHILD_THREAD_MIN_WAIT || prog_time > CHILD_THREAD_MIN_WAIT + 2)
  392. ksft_exit_fail_msg("%s test: Failed\n", test_name);
  393. else
  394. ksft_test_result_pass("%s test: Passed\n", test_name);
  395. }
  396. static void *test_pidfd_poll_leader_exit_thread(void *priv)
  397. {
  398. ksft_print_msg("Child Thread: starting. pid %d tid %ld ; and sleeping\n",
  399. getpid(), syscall(SYS_gettid));
  400. sleep(CHILD_THREAD_MIN_WAIT);
  401. ksft_print_msg("Child Thread: DONE. pid %d tid %ld\n", getpid(), syscall(SYS_gettid));
  402. return NULL;
  403. }
  404. static time_t *child_exit_secs;
  405. static int child_poll_leader_exit_test(void *args)
  406. {
  407. pthread_t t1, t2;
  408. ksft_print_msg("Child: starting. pid %d tid %ld\n", getpid(), syscall(SYS_gettid));
  409. pthread_create(&t1, NULL, test_pidfd_poll_leader_exit_thread, NULL);
  410. pthread_create(&t2, NULL, test_pidfd_poll_leader_exit_thread, NULL);
  411. /*
  412. * glibc exit calls exit_group syscall, so explicity call exit only
  413. * so that only the group leader exits, leaving the threads alone.
  414. */
  415. *child_exit_secs = time(NULL);
  416. syscall(SYS_exit, 0);
  417. /* Never reached, but appeases compiler thinking we should return. */
  418. exit(0);
  419. }
  420. static void test_pidfd_poll_leader_exit(int use_waitpid)
  421. {
  422. int pid, pidfd = 0;
  423. int status, ret = 0;
  424. const char *test_name = "pidfd_poll check for premature notification on non-empty"
  425. "group leader exit";
  426. child_exit_secs = mmap(NULL, sizeof *child_exit_secs, PROT_READ | PROT_WRITE,
  427. MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  428. if (child_exit_secs == MAP_FAILED)
  429. ksft_exit_fail_msg("%s test: mmap failed (errno %d)\n",
  430. test_name, errno);
  431. ksft_print_msg("Parent: pid: %d\n", getpid());
  432. pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_leader_exit_test);
  433. if (pid < 0)
  434. ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
  435. test_name, pid, errno);
  436. ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
  437. if (use_waitpid) {
  438. ret = waitpid(pid, &status, 0);
  439. if (ret == -1)
  440. ksft_print_msg("Parent: error\n");
  441. } else {
  442. /*
  443. * This sleep tests for the case where if the child exits, and is in
  444. * EXIT_ZOMBIE, but the thread group leader is non-empty, then the poll
  445. * doesn't prematurely return even though there are active threads
  446. */
  447. sleep(1);
  448. poll_pidfd(test_name, pidfd);
  449. }
  450. if (ret == pid)
  451. ksft_print_msg("Parent: Child process waited for.\n");
  452. time_t since_child_exit = time(NULL) - *child_exit_secs;
  453. ksft_print_msg("Time since child exit: %lu\n", since_child_exit);
  454. close(pidfd);
  455. if (since_child_exit < CHILD_THREAD_MIN_WAIT ||
  456. since_child_exit > CHILD_THREAD_MIN_WAIT + 2)
  457. ksft_exit_fail_msg("%s test: Failed\n", test_name);
  458. else
  459. ksft_test_result_pass("%s test: Passed\n", test_name);
  460. }
  461. int main(int argc, char **argv)
  462. {
  463. ksft_print_header();
  464. ksft_set_plan(8);
  465. test_pidfd_poll_exec(0);
  466. test_pidfd_poll_exec(1);
  467. test_pidfd_poll_leader_exit(0);
  468. test_pidfd_poll_leader_exit(1);
  469. test_pidfd_send_signal_syscall_support();
  470. test_pidfd_send_signal_simple_success();
  471. test_pidfd_send_signal_exited_fail();
  472. test_pidfd_send_signal_recycled_pid_fail();
  473. return ksft_exit_pass();
  474. }