pidfd_test.c 15 KB

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