signal.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <unistd.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <Kernel/Syscall.h>
  6. extern "C" {
  7. int kill(pid_t pid, int sig)
  8. {
  9. int rc = syscall(SC_kill, pid, sig);
  10. __RETURN_WITH_ERRNO(rc, rc, -1);
  11. }
  12. int killpg(int pgrp, int sig)
  13. {
  14. int rc = syscall(SC_killpg, pgrp, sig);
  15. __RETURN_WITH_ERRNO(rc, rc, -1);
  16. }
  17. int raise(int sig)
  18. {
  19. // FIXME: Support multi-threaded programs.
  20. return kill(getpid(), sig);
  21. }
  22. sighandler_t signal(int signum, sighandler_t handler)
  23. {
  24. struct sigaction new_act;
  25. struct sigaction old_act;
  26. new_act.sa_handler = handler;
  27. new_act.sa_flags = 0;
  28. new_act.sa_mask = 0;
  29. new_act.sa_restorer = nullptr;
  30. int rc = sigaction(signum, &new_act, &old_act);
  31. if (rc < 0)
  32. return SIG_ERR;
  33. return old_act.sa_handler;
  34. }
  35. int sigaction(int signum, const struct sigaction* act, struct sigaction* old_act)
  36. {
  37. int rc = syscall(SC_sigaction, signum, act, old_act);
  38. __RETURN_WITH_ERRNO(rc, rc, -1);
  39. }
  40. int sigemptyset(sigset_t* set)
  41. {
  42. *set = 0;
  43. return 0;
  44. }
  45. int sigfillset(sigset_t* set)
  46. {
  47. *set = 0xffffffff;
  48. return 0;
  49. }
  50. int sigaddset(sigset_t* set, int sig)
  51. {
  52. if (sig < 1 || sig > 32) {
  53. errno = EINVAL;
  54. return -1;
  55. }
  56. *set |= 1 << (sig);
  57. return 0;
  58. }
  59. int sigdelset(sigset_t* set, int sig)
  60. {
  61. if (sig < 1 || sig > 32) {
  62. errno = EINVAL;
  63. return -1;
  64. }
  65. *set &= ~(1 << (sig));
  66. return 0;
  67. }
  68. int sigismember(const sigset_t* set, int sig)
  69. {
  70. if (sig < 1 || sig > 32) {
  71. errno = EINVAL;
  72. return -1;
  73. }
  74. if (*set & (1 << (sig)))
  75. return 1;
  76. return 0;
  77. }
  78. int sigprocmask(int how, const sigset_t* set, sigset_t* old_set)
  79. {
  80. int rc = syscall(SC_sigprocmask, how, set, old_set);
  81. __RETURN_WITH_ERRNO(rc, rc, -1);
  82. }
  83. int sigpending(sigset_t* set)
  84. {
  85. int rc = syscall(SC_sigpending, set);
  86. __RETURN_WITH_ERRNO(rc, rc, -1);
  87. }
  88. const char* sys_siglist[NSIG] = {
  89. #undef __SIGNAL
  90. #define __SIGNAL(a, b) b,
  91. __ENUMERATE_ALL_SIGNALS
  92. #undef __SIGNAL
  93. };
  94. }