Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <Kernel/Syscall.h>
28#include <assert.h>
29#include <errno.h>
30#include <setjmp.h>
31#include <signal.h>
32#include <stdio.h>
33#include <unistd.h>
34
35extern "C" {
36
37int kill(pid_t pid, int sig)
38{
39 int rc = syscall(SC_kill, pid, sig);
40 __RETURN_WITH_ERRNO(rc, rc, -1);
41}
42
43int killpg(int pgrp, int sig)
44{
45 int rc = syscall(SC_killpg, pgrp, sig);
46 __RETURN_WITH_ERRNO(rc, rc, -1);
47}
48
49int raise(int sig)
50{
51 // FIXME: Support multi-threaded programs.
52 return kill(getpid(), sig);
53}
54
55sighandler_t signal(int signum, sighandler_t handler)
56{
57 struct sigaction new_act;
58 struct sigaction old_act;
59 new_act.sa_handler = handler;
60 new_act.sa_flags = 0;
61 new_act.sa_mask = 0;
62 int rc = sigaction(signum, &new_act, &old_act);
63 if (rc < 0)
64 return SIG_ERR;
65 return old_act.sa_handler;
66}
67
68int sigaction(int signum, const struct sigaction* act, struct sigaction* old_act)
69{
70 int rc = syscall(SC_sigaction, signum, act, old_act);
71 __RETURN_WITH_ERRNO(rc, rc, -1);
72}
73
74int sigemptyset(sigset_t* set)
75{
76 *set = 0;
77 return 0;
78}
79
80int sigfillset(sigset_t* set)
81{
82 *set = 0xffffffff;
83 return 0;
84}
85
86int sigaddset(sigset_t* set, int sig)
87{
88 if (sig < 1 || sig > 32) {
89 errno = EINVAL;
90 return -1;
91 }
92 *set |= 1 << (sig - 1);
93 return 0;
94}
95
96int sigdelset(sigset_t* set, int sig)
97{
98 if (sig < 1 || sig > 32) {
99 errno = EINVAL;
100 return -1;
101 }
102 *set &= ~(1 << (sig - 1));
103 return 0;
104}
105
106int sigismember(const sigset_t* set, int sig)
107{
108 if (sig < 1 || sig > 32) {
109 errno = EINVAL;
110 return -1;
111 }
112 if (*set & (1 << (sig - 1)))
113 return 1;
114 return 0;
115}
116
117int sigprocmask(int how, const sigset_t* set, sigset_t* old_set)
118{
119 int rc = syscall(SC_sigprocmask, how, set, old_set);
120 __RETURN_WITH_ERRNO(rc, rc, -1);
121}
122
123int sigpending(sigset_t* set)
124{
125 int rc = syscall(SC_sigpending, set);
126 __RETURN_WITH_ERRNO(rc, rc, -1);
127}
128
129const char* sys_siglist[NSIG] = {
130 "Invalid signal number",
131 "Hangup",
132 "Interrupt",
133 "Quit",
134 "Illegal instruction",
135 "Trap",
136 "Aborted",
137 "Bus error",
138 "Division by zero",
139 "Killed",
140 "User signal 1",
141 "Segmentation violation",
142 "User signal 2",
143 "Broken pipe",
144 "Alarm clock",
145 "Terminated",
146 "Stack fault",
147 "Child exited",
148 "Continued",
149 "Stopped (signal)",
150 "Stopped",
151 "Stopped (tty input)",
152 "Stopped (tty output)",
153 "Urgent I/O condition)",
154 "CPU limit exceeded",
155 "File size limit exceeded",
156 "Virtual timer expired",
157 "Profiling timer expired",
158 "Window changed",
159 "I/O possible",
160 "Power failure",
161 "Bad system call",
162};
163
164int sigsetjmp(jmp_buf env, int savesigs)
165{
166 if (savesigs) {
167 int rc = sigprocmask(0, nullptr, &env->saved_signal_mask);
168 assert(rc == 0);
169 env->did_save_signal_mask = true;
170 } else {
171 env->did_save_signal_mask = false;
172 }
173 return setjmp(env);
174}
175void siglongjmp(jmp_buf env, int val)
176{
177 if (env->did_save_signal_mask) {
178 int rc = sigprocmask(SIG_SETMASK, &env->saved_signal_mask, nullptr);
179 assert(rc == 0);
180 }
181 longjmp(env, val);
182}
183
184int sigsuspend(const sigset_t*)
185{
186 dbgprintf("FIXME: Implement sigsuspend()\n");
187 ASSERT_NOT_REACHED();
188}
189}