signal.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016, Cyril Bur, IBM Corp.
  4. *
  5. * Sending one self a signal should always get delivered.
  6. */
  7. #include <signal.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. #include <altivec.h>
  15. #include "utils.h"
  16. #define MAX_ATTEMPT 500000
  17. #define TIMEOUT 5
  18. extern long signal_self(pid_t pid, int sig);
  19. static sig_atomic_t signaled;
  20. static sig_atomic_t fail;
  21. static void signal_handler(int sig)
  22. {
  23. if (sig == SIGUSR1)
  24. signaled = 1;
  25. else
  26. fail = 1;
  27. }
  28. static int test_signal()
  29. {
  30. int i;
  31. struct sigaction act;
  32. pid_t ppid = getpid();
  33. pid_t pid;
  34. act.sa_handler = signal_handler;
  35. act.sa_flags = 0;
  36. sigemptyset(&act.sa_mask);
  37. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  38. perror("sigaction SIGUSR1");
  39. exit(1);
  40. }
  41. if (sigaction(SIGALRM, &act, NULL) < 0) {
  42. perror("sigaction SIGALRM");
  43. exit(1);
  44. }
  45. /* Don't do this for MAX_ATTEMPT, its simply too long */
  46. for(i = 0; i < 1000; i++) {
  47. pid = fork();
  48. if (pid == -1) {
  49. perror("fork");
  50. exit(1);
  51. }
  52. if (pid == 0) {
  53. signal_self(ppid, SIGUSR1);
  54. exit(1);
  55. } else {
  56. alarm(0); /* Disable any pending */
  57. alarm(2);
  58. while (!signaled && !fail)
  59. asm volatile("": : :"memory");
  60. if (!signaled) {
  61. fprintf(stderr, "Didn't get signal from child\n");
  62. FAIL_IF(1); /* For the line number */
  63. }
  64. /* Otherwise we'll loop too fast and fork() will eventually fail */
  65. waitpid(pid, NULL, 0);
  66. }
  67. }
  68. for (i = 0; i < MAX_ATTEMPT; i++) {
  69. long rc;
  70. alarm(0); /* Disable any pending */
  71. signaled = 0;
  72. alarm(TIMEOUT);
  73. rc = signal_self(ppid, SIGUSR1);
  74. if (rc) {
  75. fprintf(stderr, "(%d) Fail reason: %d rc=0x%lx",
  76. i, fail, rc);
  77. FAIL_IF(1); /* For the line number */
  78. }
  79. while (!signaled && !fail)
  80. asm volatile("": : :"memory");
  81. if (!signaled) {
  82. fprintf(stderr, "(%d) Fail reason: %d rc=0x%lx",
  83. i, fail, rc);
  84. FAIL_IF(1); /* For the line number */
  85. }
  86. }
  87. return 0;
  88. }
  89. int main(void)
  90. {
  91. test_harness_set_timeout(300);
  92. return test_harness(test_signal, "signal");
  93. }