uxsignal.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /*
  5. * Calling signal() is non-portable, as it varies in meaning
  6. * between platforms and depending on feature macros, and has
  7. * stupid semantics at least some of the time.
  8. *
  9. * This function provides the same interface as the libc function,
  10. * but provides consistent semantics. It assumes POSIX semantics
  11. * for sigaction() (so you might need to do some more work if you
  12. * port to something ancient like SunOS 4)
  13. */
  14. void (*putty_signal(int sig, void (*func)(int)))(int) {
  15. struct sigaction sa;
  16. struct sigaction old;
  17. sa.sa_handler = func;
  18. if(sigemptyset(&sa.sa_mask) < 0)
  19. return SIG_ERR;
  20. sa.sa_flags = SA_RESTART;
  21. if(sigaction(sig, &sa, &old) < 0)
  22. return SIG_ERR;
  23. return old.sa_handler;
  24. }
  25. void block_signal(int sig, int block_it)
  26. {
  27. sigset_t ss;
  28. sigemptyset(&ss);
  29. sigaddset(&ss, sig);
  30. if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
  31. perror("sigprocmask");
  32. exit(1);
  33. }
  34. }
  35. /*
  36. Local Variables:
  37. c-basic-offset:4
  38. comment-column:40
  39. End:
  40. */