signal.c 2.3 KB

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