clone3_clear_sighand.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <sched.h>
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <linux/sched.h>
  11. #include <linux/types.h>
  12. #include <sys/syscall.h>
  13. #include <sys/wait.h>
  14. #include "kselftest.h"
  15. #include "clone3_selftests.h"
  16. static void nop_handler(int signo)
  17. {
  18. }
  19. static int wait_for_pid(pid_t pid)
  20. {
  21. int status, ret;
  22. again:
  23. ret = waitpid(pid, &status, 0);
  24. if (ret == -1) {
  25. if (errno == EINTR)
  26. goto again;
  27. return -1;
  28. }
  29. if (!WIFEXITED(status))
  30. return -1;
  31. return WEXITSTATUS(status);
  32. }
  33. static void test_clone3_clear_sighand(void)
  34. {
  35. int ret;
  36. pid_t pid;
  37. struct __clone_args args = {};
  38. struct sigaction act;
  39. /*
  40. * Check that CLONE_CLEAR_SIGHAND and CLONE_SIGHAND are mutually
  41. * exclusive.
  42. */
  43. args.flags |= CLONE_CLEAR_SIGHAND | CLONE_SIGHAND;
  44. args.exit_signal = SIGCHLD;
  45. pid = sys_clone3(&args, sizeof(args));
  46. if (pid > 0)
  47. ksft_exit_fail_msg(
  48. "clone3(CLONE_CLEAR_SIGHAND | CLONE_SIGHAND) succeeded\n");
  49. act.sa_handler = nop_handler;
  50. ret = sigemptyset(&act.sa_mask);
  51. if (ret < 0)
  52. ksft_exit_fail_msg("%s - sigemptyset() failed\n",
  53. strerror(errno));
  54. act.sa_flags = 0;
  55. /* Register signal handler for SIGUSR1 */
  56. ret = sigaction(SIGUSR1, &act, NULL);
  57. if (ret < 0)
  58. ksft_exit_fail_msg(
  59. "%s - sigaction(SIGUSR1, &act, NULL) failed\n",
  60. strerror(errno));
  61. /* Register signal handler for SIGUSR2 */
  62. ret = sigaction(SIGUSR2, &act, NULL);
  63. if (ret < 0)
  64. ksft_exit_fail_msg(
  65. "%s - sigaction(SIGUSR2, &act, NULL) failed\n",
  66. strerror(errno));
  67. /* Check that CLONE_CLEAR_SIGHAND works. */
  68. args.flags = CLONE_CLEAR_SIGHAND;
  69. pid = sys_clone3(&args, sizeof(args));
  70. if (pid < 0)
  71. ksft_exit_fail_msg("%s - clone3(CLONE_CLEAR_SIGHAND) failed\n",
  72. strerror(errno));
  73. if (pid == 0) {
  74. ret = sigaction(SIGUSR1, NULL, &act);
  75. if (ret < 0)
  76. exit(EXIT_FAILURE);
  77. if (act.sa_handler != SIG_DFL)
  78. exit(EXIT_FAILURE);
  79. ret = sigaction(SIGUSR2, NULL, &act);
  80. if (ret < 0)
  81. exit(EXIT_FAILURE);
  82. if (act.sa_handler != SIG_DFL)
  83. exit(EXIT_FAILURE);
  84. exit(EXIT_SUCCESS);
  85. }
  86. ret = wait_for_pid(pid);
  87. if (ret)
  88. ksft_exit_fail_msg(
  89. "Failed to clear signal handler for child process\n");
  90. ksft_test_result_pass("Cleared signal handlers for child process\n");
  91. }
  92. int main(int argc, char **argv)
  93. {
  94. ksft_print_header();
  95. ksft_set_plan(1);
  96. test_clone3_supported();
  97. test_clone3_clear_sighand();
  98. ksft_exit_pass();
  99. }