signal.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <setjmp.h>
  10. #include <signal.h>
  11. #include <string.h>
  12. #include <sys/select.h>
  13. #include <syscall.h>
  14. #include <unistd.h>
  15. extern "C" {
  16. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/kill.html
  17. int kill(pid_t pid, int sig)
  18. {
  19. int rc = syscall(SC_kill, pid, sig);
  20. __RETURN_WITH_ERRNO(rc, rc, -1);
  21. }
  22. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/killpg.html
  23. int killpg(int pgrp, int sig)
  24. {
  25. int rc = syscall(SC_killpg, pgrp, sig);
  26. __RETURN_WITH_ERRNO(rc, rc, -1);
  27. }
  28. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/raise.html
  29. int raise(int sig)
  30. {
  31. // FIXME: Support multi-threaded programs.
  32. return kill(getpid(), sig);
  33. }
  34. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/signal.html
  35. sighandler_t signal(int signum, sighandler_t handler)
  36. {
  37. struct sigaction new_act;
  38. struct sigaction old_act;
  39. new_act.sa_handler = handler;
  40. new_act.sa_flags = 0;
  41. new_act.sa_mask = 0;
  42. int rc = sigaction(signum, &new_act, &old_act);
  43. if (rc < 0)
  44. return SIG_ERR;
  45. return old_act.sa_handler;
  46. }
  47. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html
  48. int sigaction(int signum, const struct sigaction* act, struct sigaction* old_act)
  49. {
  50. int rc = syscall(SC_sigaction, signum, act, old_act);
  51. __RETURN_WITH_ERRNO(rc, rc, -1);
  52. }
  53. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigemptyset.html
  54. int sigemptyset(sigset_t* set)
  55. {
  56. *set = 0;
  57. return 0;
  58. }
  59. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigfillset.html
  60. int sigfillset(sigset_t* set)
  61. {
  62. *set = 0xffffffff;
  63. return 0;
  64. }
  65. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaddset.html
  66. int sigaddset(sigset_t* set, int sig)
  67. {
  68. if (sig < 1 || sig > 32) {
  69. errno = EINVAL;
  70. return -1;
  71. }
  72. *set |= 1 << (sig - 1);
  73. return 0;
  74. }
  75. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaltstack.html
  76. int sigaltstack(stack_t const* ss, stack_t* old_ss)
  77. {
  78. int rc = syscall(SC_sigaltstack, ss, old_ss);
  79. __RETURN_WITH_ERRNO(rc, rc, -1);
  80. }
  81. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigdelset.html
  82. int sigdelset(sigset_t* set, int sig)
  83. {
  84. if (sig < 1 || sig > 32) {
  85. errno = EINVAL;
  86. return -1;
  87. }
  88. *set &= ~(1 << (sig - 1));
  89. return 0;
  90. }
  91. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigismember.html
  92. int sigismember(sigset_t const* set, int sig)
  93. {
  94. if (sig < 1 || sig > 32) {
  95. errno = EINVAL;
  96. return -1;
  97. }
  98. if (*set & (1 << (sig - 1)))
  99. return 1;
  100. return 0;
  101. }
  102. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigprocmask.html
  103. int sigprocmask(int how, sigset_t const* set, sigset_t* old_set)
  104. {
  105. int rc = syscall(SC_sigprocmask, how, set, old_set);
  106. __RETURN_WITH_ERRNO(rc, rc, -1);
  107. }
  108. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigpending.html
  109. int sigpending(sigset_t* set)
  110. {
  111. int rc = syscall(SC_sigpending, set);
  112. __RETURN_WITH_ERRNO(rc, rc, -1);
  113. }
  114. char const* sys_siglist[NSIG] = {
  115. "Invalid signal number",
  116. "Hangup",
  117. "Interrupt",
  118. "Quit",
  119. "Illegal instruction",
  120. "Trap",
  121. "Aborted",
  122. "Bus error",
  123. "Division by zero",
  124. "Killed",
  125. "User signal 1",
  126. "Segmentation violation",
  127. "User signal 2",
  128. "Broken pipe",
  129. "Alarm clock",
  130. "Terminated",
  131. "Stack fault",
  132. "Child exited",
  133. "Continued",
  134. "Stopped (signal)",
  135. "Stopped",
  136. "Stopped (tty input)",
  137. "Stopped (tty output)",
  138. "Urgent I/O condition)",
  139. "CPU limit exceeded",
  140. "File size limit exceeded",
  141. "Virtual timer expired",
  142. "Profiling timer expired",
  143. "Window changed",
  144. "I/O possible",
  145. "Power failure",
  146. "Bad system call",
  147. };
  148. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/siglongjmp.html
  149. void siglongjmp(jmp_buf env, int val)
  150. {
  151. if (env->did_save_signal_mask) {
  152. int rc = sigprocmask(SIG_SETMASK, &env->saved_signal_mask, nullptr);
  153. assert(rc == 0);
  154. }
  155. longjmp(env, val);
  156. }
  157. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigsuspend.html
  158. int sigsuspend(sigset_t const* set)
  159. {
  160. int rc = syscall(SC_sigsuspend, set);
  161. __RETURN_WITH_ERRNO(rc, rc, -1);
  162. }
  163. // https://pubs.opengroup.org/onlinepubs/009604499/functions/sigwait.html
  164. int sigwait(sigset_t const* set, int* sig)
  165. {
  166. int rc = syscall(Syscall::SC_sigtimedwait, set, nullptr, nullptr);
  167. VERIFY(rc != 0);
  168. if (rc < 0)
  169. return -rc;
  170. *sig = rc;
  171. return 0;
  172. }
  173. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigwaitinfo.html
  174. int sigwaitinfo(sigset_t const* set, siginfo_t* info)
  175. {
  176. return sigtimedwait(set, info, nullptr);
  177. }
  178. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigtimedwait.html
  179. int sigtimedwait(sigset_t const* set, siginfo_t* info, struct timespec const* timeout)
  180. {
  181. int rc = syscall(Syscall::SC_sigtimedwait, set, info, timeout);
  182. __RETURN_WITH_ERRNO(rc, rc, -1);
  183. }
  184. char const* sys_signame[] = {
  185. "INVAL",
  186. "HUP",
  187. "INT",
  188. "QUIT",
  189. "ILL",
  190. "TRAP",
  191. "ABRT",
  192. "BUS",
  193. "FPE",
  194. "KILL",
  195. "USR1",
  196. "SEGV",
  197. "USR2",
  198. "PIPE",
  199. "ALRM",
  200. "TERM",
  201. "STKFLT",
  202. "CHLD",
  203. "CONT",
  204. "STOP",
  205. "TSTP",
  206. "TTIN",
  207. "TTOU",
  208. "URG",
  209. "XCPU",
  210. "XFSZ",
  211. "VTALRM",
  212. "PROF",
  213. "WINCH",
  214. "IO",
  215. "INFO",
  216. "SYS",
  217. };
  218. static_assert(sizeof(sys_signame) == sizeof(char const*) * NSIG);
  219. int getsignalbyname(char const* name)
  220. {
  221. VERIFY(name);
  222. StringView name_sv { name, strlen(name) };
  223. for (size_t i = 0; i < NSIG; ++i) {
  224. StringView signal_name { sys_signame[i], sizeof(sys_signame[i]) - 1 };
  225. if (signal_name == name_sv || (name_sv.starts_with("SIG"sv) && signal_name == name_sv.substring_view(3)))
  226. return i;
  227. }
  228. errno = EINVAL;
  229. return -1;
  230. }
  231. char const* getsignalname(int signal)
  232. {
  233. if (signal < 0 || signal >= NSIG) {
  234. errno = EINVAL;
  235. return nullptr;
  236. }
  237. return sys_signame[signal];
  238. }
  239. }