[7530a00] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
| 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
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libposix
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
[4cf8ca6] | 32 | /** @file Signal handling.
|
---|
[7530a00] | 33 | */
|
---|
| 34 |
|
---|
[a3da2b2] | 35 | #include "posix/signal.h"
|
---|
[7530a00] | 36 | #include "internal/common.h"
|
---|
[a3da2b2] | 37 | #include "posix/limits.h"
|
---|
| 38 | #include "posix/stdlib.h"
|
---|
| 39 | #include "posix/string.h"
|
---|
[0d0b319] | 40 |
|
---|
| 41 | #include <errno.h>
|
---|
[d3ce33fa] | 42 |
|
---|
| 43 | #include "libc/fibril_synch.h"
|
---|
| 44 | #include "libc/task.h"
|
---|
| 45 |
|
---|
[7c3fb9b] | 46 | /*
|
---|
| 47 | * This file implements a fairly dumb and incomplete "simulation" of
|
---|
[4419c34] | 48 | * POSIX signals. Since HelenOS doesn't support signals and mostly doesn't
|
---|
| 49 | * have any equivalent functionality, most of the signals are useless. The
|
---|
| 50 | * main purpose of this implementation is thus to help port applications using
|
---|
| 51 | * signals with minimal modification, but if the application uses signals for
|
---|
| 52 | * anything non-trivial, it's quite probable it won't work properly even if
|
---|
| 53 | * it builds without problems.
|
---|
| 54 | */
|
---|
[4cf8ca6] | 55 |
|
---|
[d3ce33fa] | 56 | /* Used to serialize signal handling. */
|
---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(_signal_mutex);
|
---|
| 58 |
|
---|
[4419c34] | 59 | static LIST_INITIALIZE(_signal_queue);
|
---|
| 60 |
|
---|
[7f9df7b9] | 61 | static sigset_t _signal_mask = 0;
|
---|
[d3ce33fa] | 62 |
|
---|
| 63 | #define DEFAULT_HANDLER { .sa_handler = SIG_DFL, \
|
---|
| 64 | .sa_mask = 0, .sa_flags = 0, .sa_sigaction = NULL }
|
---|
| 65 |
|
---|
[4419c34] | 66 | /* Actions associated with each signal number. */
|
---|
[7f9df7b9] | 67 | static struct sigaction _signal_actions[_TOP_SIGNAL + 1] = {
|
---|
[1b20da0] | 68 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
|
---|
| 69 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
|
---|
| 70 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
|
---|
| 71 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
|
---|
| 72 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
|
---|
| 73 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
|
---|
[d3ce33fa] | 74 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[4cf8ca6] | 77 | /**
|
---|
[f215bb5f] | 78 | * Default signal handler. Executes the default action for each signal,
|
---|
| 79 | * as reasonable within HelenOS.
|
---|
| 80 | *
|
---|
| 81 | * @param signo Signal number.
|
---|
[4cf8ca6] | 82 | */
|
---|
[d3ce33fa] | 83 | void __posix_default_signal_handler(int signo)
|
---|
| 84 | {
|
---|
| 85 | switch (signo) {
|
---|
| 86 | case SIGABRT:
|
---|
| 87 | abort();
|
---|
| 88 | case SIGQUIT:
|
---|
[f215bb5f] | 89 | fprintf(stderr, "Quit signal raised. Exiting.\n");
|
---|
[d3ce33fa] | 90 | exit(EXIT_FAILURE);
|
---|
| 91 | case SIGINT:
|
---|
[f215bb5f] | 92 | fprintf(stderr, "Interrupt signal caught. Exiting.\n");
|
---|
[d3ce33fa] | 93 | exit(EXIT_FAILURE);
|
---|
| 94 | case SIGTERM:
|
---|
[f215bb5f] | 95 | fprintf(stderr, "Termination signal caught. Exiting.\n");
|
---|
[d3ce33fa] | 96 | exit(EXIT_FAILURE);
|
---|
| 97 | case SIGSTOP:
|
---|
[f215bb5f] | 98 | fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.\n");
|
---|
[d3ce33fa] | 99 | break;
|
---|
| 100 | case SIGKILL:
|
---|
| 101 | /* This will only occur when raise or similar is called. */
|
---|
| 102 | /* Commit suicide. */
|
---|
| 103 | task_kill(task_get_id());
|
---|
[a35b458] | 104 |
|
---|
[d3ce33fa] | 105 | /* Should not be reached. */
|
---|
| 106 | abort();
|
---|
| 107 | case SIGFPE:
|
---|
| 108 | case SIGBUS:
|
---|
| 109 | case SIGILL:
|
---|
| 110 | case SIGSEGV:
|
---|
[7f9df7b9] | 111 | psignal(signo, "Hardware exception raised by user code");
|
---|
[d3ce33fa] | 112 | abort();
|
---|
| 113 | case SIGSYS:
|
---|
| 114 | case SIGXCPU:
|
---|
| 115 | case SIGXFSZ:
|
---|
| 116 | case SIGTRAP:
|
---|
| 117 | case SIGHUP:
|
---|
| 118 | case SIGPIPE:
|
---|
| 119 | case SIGPOLL:
|
---|
| 120 | case SIGURG:
|
---|
| 121 | case SIGTSTP:
|
---|
| 122 | case SIGTTIN:
|
---|
| 123 | case SIGTTOU:
|
---|
[7f9df7b9] | 124 | psignal(signo, "Unsupported signal caught");
|
---|
[d3ce33fa] | 125 | abort();
|
---|
| 126 | case SIGCHLD:
|
---|
| 127 | case SIGUSR1:
|
---|
| 128 | case SIGUSR2:
|
---|
| 129 | case SIGALRM:
|
---|
| 130 | case SIGVTALRM:
|
---|
| 131 | case SIGPROF:
|
---|
| 132 | case SIGCONT:
|
---|
| 133 | /* ignored */
|
---|
| 134 | break;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[55b1efd] | 138 | /**
|
---|
| 139 | * Just an empty function to get an unique pointer value for comparison.
|
---|
[4cf8ca6] | 140 | *
|
---|
[55b1efd] | 141 | * @param signo Signal number.
|
---|
[4cf8ca6] | 142 | */
|
---|
[d3ce33fa] | 143 | void __posix_hold_signal_handler(int signo)
|
---|
| 144 | {
|
---|
| 145 | /* Nothing */
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[55b1efd] | 148 | /**
|
---|
| 149 | * Empty function to be used as ignoring handler.
|
---|
[1b20da0] | 150 | *
|
---|
[55b1efd] | 151 | * @param signo Signal number.
|
---|
[4cf8ca6] | 152 | */
|
---|
[d3ce33fa] | 153 | void __posix_ignore_signal_handler(int signo)
|
---|
| 154 | {
|
---|
| 155 | /* Nothing */
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[55b1efd] | 158 | /**
|
---|
| 159 | * Clear the signal set.
|
---|
[1b20da0] | 160 | *
|
---|
[4419c34] | 161 | * @param set Pointer to the signal set.
|
---|
| 162 | * @return Always returns zero.
|
---|
[4cf8ca6] | 163 | */
|
---|
[7f9df7b9] | 164 | int sigemptyset(sigset_t *set)
|
---|
[d3ce33fa] | 165 | {
|
---|
| 166 | assert(set != NULL);
|
---|
| 167 |
|
---|
| 168 | *set = 0;
|
---|
| 169 | return 0;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[55b1efd] | 172 | /**
|
---|
| 173 | * Fill the signal set (i.e. add all signals).
|
---|
[1b20da0] | 174 | *
|
---|
[4419c34] | 175 | * @param set Pointer to the signal set.
|
---|
| 176 | * @return Always returns zero.
|
---|
[4cf8ca6] | 177 | */
|
---|
[7f9df7b9] | 178 | int sigfillset(sigset_t *set)
|
---|
[d3ce33fa] | 179 | {
|
---|
| 180 | assert(set != NULL);
|
---|
| 181 |
|
---|
| 182 | *set = UINT32_MAX;
|
---|
| 183 | return 0;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[55b1efd] | 186 | /**
|
---|
| 187 | * Add a signal to the set.
|
---|
[1b20da0] | 188 | *
|
---|
[4419c34] | 189 | * @param set Pointer to the signal set.
|
---|
| 190 | * @param signo Signal number to add.
|
---|
| 191 | * @return Always returns zero.
|
---|
[4cf8ca6] | 192 | */
|
---|
[7f9df7b9] | 193 | int sigaddset(sigset_t *set, int signo)
|
---|
[d3ce33fa] | 194 | {
|
---|
| 195 | assert(set != NULL);
|
---|
| 196 |
|
---|
| 197 | *set |= (1 << signo);
|
---|
| 198 | return 0;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[55b1efd] | 201 | /**
|
---|
| 202 | * Delete a signal from the set.
|
---|
[1b20da0] | 203 | *
|
---|
[4419c34] | 204 | * @param set Pointer to the signal set.
|
---|
| 205 | * @param signo Signal number to remove.
|
---|
| 206 | * @return Always returns zero.
|
---|
[4cf8ca6] | 207 | */
|
---|
[7f9df7b9] | 208 | int sigdelset(sigset_t *set, int signo)
|
---|
[d3ce33fa] | 209 | {
|
---|
| 210 | assert(set != NULL);
|
---|
| 211 |
|
---|
| 212 | *set &= ~(1 << signo);
|
---|
| 213 | return 0;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[55b1efd] | 216 | /**
|
---|
| 217 | * Inclusion test for a signal set.
|
---|
[1b20da0] | 218 | *
|
---|
[4419c34] | 219 | * @param set Pointer to the signal set.
|
---|
| 220 | * @param signo Signal number to query.
|
---|
| 221 | * @return 1 if the signal is in the set, 0 otherwise.
|
---|
[4cf8ca6] | 222 | */
|
---|
[7f9df7b9] | 223 | int sigismember(const sigset_t *set, int signo)
|
---|
[d3ce33fa] | 224 | {
|
---|
| 225 | assert(set != NULL);
|
---|
[a35b458] | 226 |
|
---|
[d3ce33fa] | 227 | return (*set & (1 << signo)) != 0;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[55b1efd] | 230 | /**
|
---|
| 231 | * Unsafe variant of the sigaction() function.
|
---|
| 232 | * Doesn't do any checking of its arguments and
|
---|
| 233 | * does not deal with thread-safety.
|
---|
[1b20da0] | 234 | *
|
---|
[4cf8ca6] | 235 | * @param sig
|
---|
| 236 | * @param act
|
---|
| 237 | * @param oact
|
---|
| 238 | */
|
---|
[7f9df7b9] | 239 | static void _sigaction_unsafe(int sig, const struct sigaction *restrict act,
|
---|
| 240 | struct sigaction *restrict oact)
|
---|
[d3ce33fa] | 241 | {
|
---|
| 242 | if (oact != NULL) {
|
---|
| 243 | memcpy(oact, &_signal_actions[sig],
|
---|
[7f9df7b9] | 244 | sizeof(struct sigaction));
|
---|
[d3ce33fa] | 245 | }
|
---|
| 246 |
|
---|
| 247 | if (act != NULL) {
|
---|
| 248 | memcpy(&_signal_actions[sig], act,
|
---|
[7f9df7b9] | 249 | sizeof(struct sigaction));
|
---|
[d3ce33fa] | 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[55b1efd] | 253 | /**
|
---|
| 254 | * Sets a new action for the given signal number.
|
---|
[1b20da0] | 255 | *
|
---|
[4419c34] | 256 | * @param sig Signal number to set action for.
|
---|
| 257 | * @param act If not NULL, contents of this structure are
|
---|
| 258 | * used as the new action for the signal.
|
---|
| 259 | * @param oact If not NULL, the original action associated with the signal
|
---|
[1b20da0] | 260 | * is stored in the structure pointer to.
|
---|
[4419c34] | 261 | * @return -1 with errno set on failure, 0 on success.
|
---|
[4cf8ca6] | 262 | */
|
---|
[7f9df7b9] | 263 | int sigaction(int sig, const struct sigaction *restrict act,
|
---|
| 264 | struct sigaction *restrict oact)
|
---|
[d3ce33fa] | 265 | {
|
---|
| 266 | if (sig > _TOP_SIGNAL || (act != NULL &&
|
---|
| 267 | (sig == SIGKILL || sig == SIGSTOP))) {
|
---|
| 268 | errno = EINVAL;
|
---|
| 269 | return -1;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | if (sig > _TOP_CATCHABLE_SIGNAL) {
|
---|
[7f9df7b9] | 273 | psignal(sig,
|
---|
[d3ce33fa] | 274 | "WARNING: registering handler for a partially"
|
---|
| 275 | " or fully unsupported signal. This handler may only be"
|
---|
| 276 | " invoked by the raise() function, which may not be what"
|
---|
[ffff746] | 277 | " the application developer intended");
|
---|
[d3ce33fa] | 278 | }
|
---|
| 279 |
|
---|
| 280 | fibril_mutex_lock(&_signal_mutex);
|
---|
| 281 | _sigaction_unsafe(sig, act, oact);
|
---|
| 282 | fibril_mutex_unlock(&_signal_mutex);
|
---|
| 283 |
|
---|
| 284 | return 0;
|
---|
| 285 | }
|
---|
[7530a00] | 286 |
|
---|
[55b1efd] | 287 | /**
|
---|
| 288 | * Sets a new handler for the given signal number.
|
---|
[1b20da0] | 289 | *
|
---|
[4419c34] | 290 | * @param sig Signal number to set handler for.
|
---|
| 291 | * @param func Handler function.
|
---|
| 292 | * @return SIG_ERR on failure, original handler on success.
|
---|
[4cf8ca6] | 293 | */
|
---|
[7f9df7b9] | 294 | void (*signal(int sig, void (*func)(int)))(int)
|
---|
[7530a00] | 295 | {
|
---|
[7f9df7b9] | 296 | struct sigaction new = {
|
---|
[d3ce33fa] | 297 | .sa_handler = func,
|
---|
| 298 | .sa_mask = 0,
|
---|
| 299 | .sa_flags = 0,
|
---|
| 300 | .sa_sigaction = NULL
|
---|
| 301 | };
|
---|
[7f9df7b9] | 302 | struct sigaction old;
|
---|
| 303 | if (sigaction(sig, func == NULL ? NULL : &new, &old) == 0) {
|
---|
[d3ce33fa] | 304 | return old.sa_handler;
|
---|
| 305 | } else {
|
---|
| 306 | return SIG_ERR;
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[4419c34] | 310 | typedef struct {
|
---|
| 311 | link_t link;
|
---|
| 312 | int signo;
|
---|
[7f9df7b9] | 313 | siginfo_t siginfo;
|
---|
[4419c34] | 314 | } signal_queue_item;
|
---|
| 315 |
|
---|
[55b1efd] | 316 | /**
|
---|
| 317 | * Queue blocked signal.
|
---|
[4419c34] | 318 | *
|
---|
| 319 | * @param signo Signal number.
|
---|
| 320 | * @param siginfo Additional information about the signal.
|
---|
| 321 | */
|
---|
[7f9df7b9] | 322 | static void _queue_signal(int signo, siginfo_t *siginfo)
|
---|
[4419c34] | 323 | {
|
---|
| 324 | assert(signo >= 0 && signo <= _TOP_SIGNAL);
|
---|
| 325 | assert(siginfo != NULL);
|
---|
[a35b458] | 326 |
|
---|
[4419c34] | 327 | signal_queue_item *item = malloc(sizeof(signal_queue_item));
|
---|
| 328 | link_initialize(&(item->link));
|
---|
| 329 | item->signo = signo;
|
---|
[7f9df7b9] | 330 | memcpy(&item->siginfo, siginfo, sizeof(siginfo_t));
|
---|
[4419c34] | 331 | list_append(&(item->link), &_signal_queue);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 |
|
---|
[55b1efd] | 335 | /**
|
---|
| 336 | * Executes an action associated with the given signal.
|
---|
[4419c34] | 337 | *
|
---|
| 338 | * @param signo Signal number.
|
---|
| 339 | * @param siginfo Additional information about the circumstances of this raise.
|
---|
| 340 | * @return 0 if the action has been successfully executed. -1 if the signal is
|
---|
| 341 | * blocked.
|
---|
[4cf8ca6] | 342 | */
|
---|
[7f9df7b9] | 343 | static int _raise_sigaction(int signo, siginfo_t *siginfo)
|
---|
[d3ce33fa] | 344 | {
|
---|
| 345 | assert(signo >= 0 && signo <= _TOP_SIGNAL);
|
---|
| 346 | assert(siginfo != NULL);
|
---|
| 347 |
|
---|
| 348 | fibril_mutex_lock(&_signal_mutex);
|
---|
| 349 |
|
---|
[7f9df7b9] | 350 | struct sigaction action = _signal_actions[signo];
|
---|
[d3ce33fa] | 351 |
|
---|
[7f9df7b9] | 352 | if (sigismember(&_signal_mask, signo) ||
|
---|
[d3ce33fa] | 353 | action.sa_handler == SIG_HOLD) {
|
---|
[4419c34] | 354 | _queue_signal(signo, siginfo);
|
---|
[d3ce33fa] | 355 | fibril_mutex_unlock(&_signal_mutex);
|
---|
| 356 | return -1;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[7c3fb9b] | 359 | /*
|
---|
| 360 | * Modifying signal mask is unnecessary,
|
---|
[d3ce33fa] | 361 | * signal handling is serialized.
|
---|
| 362 | */
|
---|
| 363 |
|
---|
| 364 | if ((action.sa_flags & SA_RESETHAND) && signo != SIGILL && signo != SIGTRAP) {
|
---|
[7f9df7b9] | 365 | _signal_actions[signo] = (struct sigaction) DEFAULT_HANDLER;
|
---|
[2aadf2b] | 366 | }
|
---|
[d3ce33fa] | 367 |
|
---|
| 368 | if (action.sa_flags & SA_SIGINFO) {
|
---|
| 369 | assert(action.sa_sigaction != NULL);
|
---|
| 370 | action.sa_sigaction(signo, siginfo, NULL);
|
---|
| 371 | } else {
|
---|
| 372 | assert(action.sa_handler != NULL);
|
---|
| 373 | action.sa_handler(signo);
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | fibril_mutex_unlock(&_signal_mutex);
|
---|
| 377 |
|
---|
| 378 | return 0;
|
---|
[7530a00] | 379 | }
|
---|
| 380 |
|
---|
[55b1efd] | 381 | /**
|
---|
| 382 | * Raise all unblocked previously queued signals.
|
---|
[4419c34] | 383 | */
|
---|
[193d280c] | 384 | static void _dequeue_unblocked_signals(void)
|
---|
[4419c34] | 385 | {
|
---|
| 386 | link_t *iterator = _signal_queue.head.next;
|
---|
| 387 | link_t *next;
|
---|
[a35b458] | 388 |
|
---|
[4419c34] | 389 | while (iterator != &(_signal_queue).head) {
|
---|
| 390 | next = iterator->next;
|
---|
[a35b458] | 391 |
|
---|
[4419c34] | 392 | signal_queue_item *item =
|
---|
| 393 | list_get_instance(iterator, signal_queue_item, link);
|
---|
[a35b458] | 394 |
|
---|
[7f9df7b9] | 395 | if (!sigismember(&_signal_mask, item->signo) &&
|
---|
[4419c34] | 396 | _signal_actions[item->signo].sa_handler != SIG_HOLD) {
|
---|
| 397 | list_remove(&(item->link));
|
---|
| 398 | _raise_sigaction(item->signo, &(item->siginfo));
|
---|
| 399 | free(item);
|
---|
| 400 | }
|
---|
[a35b458] | 401 |
|
---|
[4419c34] | 402 | iterator = next;
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[55b1efd] | 406 | /**
|
---|
| 407 | * Raise a signal for the calling process.
|
---|
[1b20da0] | 408 | *
|
---|
[4419c34] | 409 | * @param sig Signal number.
|
---|
| 410 | * @return -1 with errno set on failure, 0 on success.
|
---|
[4cf8ca6] | 411 | */
|
---|
[7f9df7b9] | 412 | int raise(int sig)
|
---|
[7530a00] | 413 | {
|
---|
[d3ce33fa] | 414 | if (sig >= 0 && sig <= _TOP_SIGNAL) {
|
---|
[7f9df7b9] | 415 | siginfo_t siginfo = {
|
---|
[d3ce33fa] | 416 | .si_signo = sig,
|
---|
| 417 | .si_code = SI_USER
|
---|
| 418 | };
|
---|
| 419 | return _raise_sigaction(sig, &siginfo);
|
---|
| 420 | } else {
|
---|
| 421 | errno = EINVAL;
|
---|
| 422 | return -1;
|
---|
| 423 | }
|
---|
[7530a00] | 424 | }
|
---|
| 425 |
|
---|
[55b1efd] | 426 | /**
|
---|
| 427 | * Raises a signal for a selected process.
|
---|
[1b20da0] | 428 | *
|
---|
[4419c34] | 429 | * @param pid PID of the process for which the signal shall be raised.
|
---|
| 430 | * @param signo Signal to raise.
|
---|
| 431 | * @return -1 with errno set on failure (possible errors include unsupported
|
---|
| 432 | * action, invalid signal number, lack of permissions, etc.), 0 on success.
|
---|
[4cf8ca6] | 433 | */
|
---|
[7f9df7b9] | 434 | int kill(pid_t pid, int signo)
|
---|
[7530a00] | 435 | {
|
---|
[d3ce33fa] | 436 | if (pid < 1) {
|
---|
| 437 | // TODO
|
---|
| 438 | errno = ENOTSUP;
|
---|
| 439 | return -1;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[ffff746] | 442 | if (signo > _TOP_SIGNAL) {
|
---|
[d3ce33fa] | 443 | errno = EINVAL;
|
---|
| 444 | return -1;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[7f9df7b9] | 447 | if (pid == (pid_t) task_get_id()) {
|
---|
| 448 | return raise(signo);
|
---|
[ffff746] | 449 | }
|
---|
| 450 |
|
---|
[d3ce33fa] | 451 | switch (signo) {
|
---|
| 452 | case SIGKILL:
|
---|
| 453 | task_kill(pid);
|
---|
| 454 | break;
|
---|
| 455 | default:
|
---|
| 456 | /* Nothing else supported yet. */
|
---|
| 457 | errno = ENOTSUP;
|
---|
| 458 | return -1;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | return 0;
|
---|
[7530a00] | 462 | }
|
---|
| 463 |
|
---|
[55b1efd] | 464 | /**
|
---|
| 465 | * Send a signal to a process group. Always fails at the moment because of
|
---|
| 466 | * lack of this functionality in HelenOS.
|
---|
[1b20da0] | 467 | *
|
---|
[4419c34] | 468 | * @param pid PID of the process group.
|
---|
| 469 | * @param sig Signal number.
|
---|
| 470 | * @return -1 on failure, 0 on success (see kill()).
|
---|
[4cf8ca6] | 471 | */
|
---|
[7f9df7b9] | 472 | int killpg(pid_t pid, int sig)
|
---|
[244d6fd] | 473 | {
|
---|
[d3ce33fa] | 474 | assert(pid > 1);
|
---|
[7f9df7b9] | 475 | return kill(-pid, sig);
|
---|
[244d6fd] | 476 | }
|
---|
| 477 |
|
---|
[55b1efd] | 478 | /**
|
---|
| 479 | * Outputs information about the signal to the standard error stream.
|
---|
[1b20da0] | 480 | *
|
---|
[4419c34] | 481 | * @param pinfo SigInfo struct to write.
|
---|
| 482 | * @param message String to output alongside human-readable signal description.
|
---|
[4cf8ca6] | 483 | */
|
---|
[7f9df7b9] | 484 | void psiginfo(const siginfo_t *pinfo, const char *message)
|
---|
[d3ce33fa] | 485 | {
|
---|
| 486 | assert(pinfo != NULL);
|
---|
[7f9df7b9] | 487 | psignal(pinfo->si_signo, message);
|
---|
[d3ce33fa] | 488 | // TODO: print si_code
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[55b1efd] | 491 | /**
|
---|
| 492 | * Outputs information about the signal to the standard error stream.
|
---|
[1b20da0] | 493 | *
|
---|
[4419c34] | 494 | * @param signum Signal number.
|
---|
| 495 | * @param message String to output alongside human-readable signal description.
|
---|
[4cf8ca6] | 496 | */
|
---|
[7f9df7b9] | 497 | void psignal(int signum, const char *message)
|
---|
[d3ce33fa] | 498 | {
|
---|
[7f9df7b9] | 499 | char *sigmsg = strsignal(signum);
|
---|
[d3ce33fa] | 500 | if (message == NULL || *message == '\0') {
|
---|
| 501 | fprintf(stderr, "%s\n", sigmsg);
|
---|
| 502 | } else {
|
---|
| 503 | fprintf(stderr, "%s: %s\n", message, sigmsg);
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[55b1efd] | 507 | /**
|
---|
| 508 | * Manipulate the signal mask of the calling thread.
|
---|
[1b20da0] | 509 | *
|
---|
[4419c34] | 510 | * @param how What to do with the mask.
|
---|
| 511 | * @param set Signal set to work with.
|
---|
| 512 | * @param oset If not NULL, the original signal mask is coppied here.
|
---|
| 513 | * @return 0 success, errorcode on failure.
|
---|
[4cf8ca6] | 514 | */
|
---|
[7f9df7b9] | 515 | int thread_sigmask(int how, const sigset_t *restrict set,
|
---|
| 516 | sigset_t *restrict oset)
|
---|
[7530a00] | 517 | {
|
---|
[d3ce33fa] | 518 | fibril_mutex_lock(&_signal_mutex);
|
---|
| 519 |
|
---|
| 520 | if (oset != NULL) {
|
---|
| 521 | *oset = _signal_mask;
|
---|
| 522 | }
|
---|
| 523 | if (set != NULL) {
|
---|
| 524 | switch (how) {
|
---|
| 525 | case SIG_BLOCK:
|
---|
| 526 | _signal_mask |= *set;
|
---|
| 527 | break;
|
---|
| 528 | case SIG_UNBLOCK:
|
---|
| 529 | _signal_mask &= ~*set;
|
---|
| 530 | break;
|
---|
| 531 | case SIG_SETMASK:
|
---|
| 532 | _signal_mask = *set;
|
---|
| 533 | break;
|
---|
| 534 | default:
|
---|
| 535 | fibril_mutex_unlock(&_signal_mutex);
|
---|
| 536 | return EINVAL;
|
---|
| 537 | }
|
---|
| 538 | }
|
---|
[a35b458] | 539 |
|
---|
[4419c34] | 540 | _dequeue_unblocked_signals();
|
---|
[d3ce33fa] | 541 |
|
---|
| 542 | fibril_mutex_unlock(&_signal_mutex);
|
---|
| 543 |
|
---|
| 544 | return 0;
|
---|
[7530a00] | 545 | }
|
---|
| 546 |
|
---|
[55b1efd] | 547 | /**
|
---|
| 548 | * Manipulate the signal mask of the process.
|
---|
[1b20da0] | 549 | *
|
---|
[4419c34] | 550 | * @param how What to do with the mask.
|
---|
| 551 | * @param set Signal set to work with.
|
---|
| 552 | * @param oset If not NULL, the original signal mask is coppied here.
|
---|
| 553 | * @return 0 on success, -1 with errno set on failure.
|
---|
[4cf8ca6] | 554 | */
|
---|
[7f9df7b9] | 555 | int sigprocmask(int how, const sigset_t *restrict set,
|
---|
| 556 | sigset_t *restrict oset)
|
---|
[7530a00] | 557 | {
|
---|
[7f9df7b9] | 558 | int result = thread_sigmask(how, set, oset);
|
---|
[d3ce33fa] | 559 | if (result != 0) {
|
---|
| 560 | errno = result;
|
---|
| 561 | return -1;
|
---|
| 562 | }
|
---|
| 563 | return 0;
|
---|
[7530a00] | 564 | }
|
---|
| 565 |
|
---|
| 566 | /** @}
|
---|
| 567 | */
|
---|