pidfd_test.c 14 KB

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