1
0

sigaction.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdlib.h>
  2. #include <signal.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include "syscall.h"
  6. #include "pthread_impl.h"
  7. #include "libc.h"
  8. #include "ksigaction.h"
  9. void __restore(), __restore_rt();
  10. static pthread_t dummy(void) { return 0; }
  11. weak_alias(dummy, __pthread_self_def);
  12. static unsigned long handler_set[_NSIG/(8*sizeof(long))];
  13. void __get_handler_set(sigset_t *set)
  14. {
  15. memcpy(set, handler_set, sizeof handler_set);
  16. }
  17. int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
  18. {
  19. struct k_sigaction ksa, ksa_old;
  20. if (sig >= (unsigned)_NSIG) {
  21. errno = EINVAL;
  22. return -1;
  23. }
  24. if (sa) {
  25. if ((uintptr_t)sa->sa_handler > 1UL) {
  26. a_or_l(handler_set+(sig-1)/(8*sizeof(long)),
  27. 1UL<<(sig-1)%(8*sizeof(long)));
  28. __pthread_self_def();
  29. }
  30. ksa.handler = sa->sa_handler;
  31. ksa.flags = sa->sa_flags | SA_RESTORER;
  32. ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore;
  33. memcpy(&ksa.mask, &sa->sa_mask, sizeof ksa.mask);
  34. }
  35. if (syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, sizeof ksa.mask))
  36. return -1;
  37. if (old) {
  38. old->sa_handler = ksa_old.handler;
  39. old->sa_flags = ksa_old.flags;
  40. memcpy(&old->sa_mask, &ksa_old.mask, sizeof ksa_old.mask);
  41. }
  42. return 0;
  43. }
  44. int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
  45. {
  46. if (sig-32U < 3) {
  47. errno = EINVAL;
  48. return -1;
  49. }
  50. return __libc_sigaction(sig, sa, old);
  51. }
  52. weak_alias(__sigaction, sigaction);