signal.cpp 6.2 KB

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