sigaction.c 1006 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include "test_helpers.h"
  7. void handler1(int sig) {
  8. ERROR_IF(handler, sig, != SIGUSR1);
  9. puts("Signal handler1 called!");
  10. }
  11. void handler2(int sig) {
  12. ERROR_IF(handler, sig, != SIGUSR1);
  13. puts("Signal handler2 called!");
  14. }
  15. int main(void) {
  16. struct sigaction sa1 = { .sa_handler = handler1 };
  17. struct sigaction sa2 = { .sa_handler = handler2 };
  18. struct sigaction saold = {0};
  19. sigemptyset(&sa1.sa_mask);
  20. sigemptyset(&sa2.sa_mask);
  21. int rcode = sigaction(SIGUSR1, &sa1, NULL);
  22. ERROR_IF(signal, rcode, != 0);
  23. puts("Raising...");
  24. int raise_status = raise(SIGUSR1);
  25. ERROR_IF(raise, raise_status, < 0);
  26. rcode = sigaction(SIGUSR1, &sa2, &saold);
  27. ERROR_IF(signal, rcode, != 0);
  28. ERROR_IF(signal, saold.sa_handler, != sa1.sa_handler);
  29. puts("Raising...");
  30. raise_status = raise(SIGUSR1);
  31. ERROR_IF(raise, raise_status, < 0);
  32. puts("Raised.");
  33. }