| 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 | */
|
|---|
| 32 | /** @file Signal handling.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #define LIBPOSIX_INTERNAL
|
|---|
| 36 |
|
|---|
| 37 | #include "signal.h"
|
|---|
| 38 | #include "internal/common.h"
|
|---|
| 39 | #include "limits.h"
|
|---|
| 40 | #include "stdlib.h"
|
|---|
| 41 | #include "string.h"
|
|---|
| 42 | #include "errno.h"
|
|---|
| 43 |
|
|---|
| 44 | #include "libc/fibril_synch.h"
|
|---|
| 45 | #include "libc/task.h"
|
|---|
| 46 |
|
|---|
| 47 | /* This file implements a fairly dumb and incomplete "simulation" of
|
|---|
| 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 | */
|
|---|
| 55 |
|
|---|
| 56 | /* Used to serialize signal handling. */
|
|---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(_signal_mutex);
|
|---|
| 58 |
|
|---|
| 59 | static LIST_INITIALIZE(_signal_queue);
|
|---|
| 60 |
|
|---|
| 61 | static posix_sigset_t _signal_mask = 0;
|
|---|
| 62 |
|
|---|
| 63 | #define DEFAULT_HANDLER { .sa_handler = SIG_DFL, \
|
|---|
| 64 | .sa_mask = 0, .sa_flags = 0, .sa_sigaction = NULL }
|
|---|
| 65 |
|
|---|
| 66 | /* Actions associated with each signal number. */
|
|---|
| 67 | static struct posix_sigaction _signal_actions[_TOP_SIGNAL + 1] = {
|
|---|
| 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,
|
|---|
| 74 | DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Default signal handler. Executes the default action for each signal,
|
|---|
| 79 | * as reasonable within HelenOS.
|
|---|
| 80 | *
|
|---|
| 81 | * @param signo Signal number.
|
|---|
| 82 | */
|
|---|
| 83 | void __posix_default_signal_handler(int signo)
|
|---|
| 84 | {
|
|---|
| 85 | switch (signo) {
|
|---|
| 86 | case SIGABRT:
|
|---|
| 87 | abort();
|
|---|
| 88 | case SIGQUIT:
|
|---|
| 89 | fprintf(stderr, "Quit signal raised. Exiting.\n");
|
|---|
| 90 | exit(EXIT_FAILURE);
|
|---|
| 91 | case SIGINT:
|
|---|
| 92 | fprintf(stderr, "Interrupt signal caught. Exiting.\n");
|
|---|
| 93 | exit(EXIT_FAILURE);
|
|---|
| 94 | case SIGTERM:
|
|---|
| 95 | fprintf(stderr, "Termination signal caught. Exiting.\n");
|
|---|
| 96 | exit(EXIT_FAILURE);
|
|---|
| 97 | case SIGSTOP:
|
|---|
| 98 | fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.\n");
|
|---|
| 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());
|
|---|
| 104 |
|
|---|
| 105 | /* Should not be reached. */
|
|---|
| 106 | abort();
|
|---|
| 107 | case SIGFPE:
|
|---|
| 108 | case SIGBUS:
|
|---|
| 109 | case SIGILL:
|
|---|
| 110 | case SIGSEGV:
|
|---|
| 111 | posix_psignal(signo, "Hardware exception raised by user code");
|
|---|
| 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:
|
|---|
| 124 | posix_psignal(signo, "Unsupported signal caught");
|
|---|
| 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 |
|
|---|
| 138 | /** Just an empty function to get an unique pointer value for comparison.
|
|---|
| 139 | *
|
|---|
| 140 | * @param signo
|
|---|
| 141 | */
|
|---|
| 142 | void __posix_hold_signal_handler(int signo)
|
|---|
| 143 | {
|
|---|
| 144 | /* Nothing */
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /** Empty function to be used as ignoring handler.
|
|---|
| 148 | *
|
|---|
| 149 | * @param signo
|
|---|
| 150 | */
|
|---|
| 151 | void __posix_ignore_signal_handler(int signo)
|
|---|
| 152 | {
|
|---|
| 153 | /* Nothing */
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /** Clear the signal set.
|
|---|
| 157 | *
|
|---|
| 158 | * @param set Pointer to the signal set.
|
|---|
| 159 | * @return Always returns zero.
|
|---|
| 160 | */
|
|---|
| 161 | int posix_sigemptyset(posix_sigset_t *set)
|
|---|
| 162 | {
|
|---|
| 163 | assert(set != NULL);
|
|---|
| 164 |
|
|---|
| 165 | *set = 0;
|
|---|
| 166 | return 0;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** Fill the signal set (i.e. add all signals).
|
|---|
| 170 | *
|
|---|
| 171 | * @param set Pointer to the signal set.
|
|---|
| 172 | * @return Always returns zero.
|
|---|
| 173 | */
|
|---|
| 174 | int posix_sigfillset(posix_sigset_t *set)
|
|---|
| 175 | {
|
|---|
| 176 | assert(set != NULL);
|
|---|
| 177 |
|
|---|
| 178 | *set = UINT32_MAX;
|
|---|
| 179 | return 0;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Add a signal to the set.
|
|---|
| 183 | *
|
|---|
| 184 | * @param set Pointer to the signal set.
|
|---|
| 185 | * @param signo Signal number to add.
|
|---|
| 186 | * @return Always returns zero.
|
|---|
| 187 | */
|
|---|
| 188 | int posix_sigaddset(posix_sigset_t *set, int signo)
|
|---|
| 189 | {
|
|---|
| 190 | assert(set != NULL);
|
|---|
| 191 |
|
|---|
| 192 | *set |= (1 << signo);
|
|---|
| 193 | return 0;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** Delete a signal from the set.
|
|---|
| 197 | *
|
|---|
| 198 | * @param set Pointer to the signal set.
|
|---|
| 199 | * @param signo Signal number to remove.
|
|---|
| 200 | * @return Always returns zero.
|
|---|
| 201 | */
|
|---|
| 202 | int posix_sigdelset(posix_sigset_t *set, int signo)
|
|---|
| 203 | {
|
|---|
| 204 | assert(set != NULL);
|
|---|
| 205 |
|
|---|
| 206 | *set &= ~(1 << signo);
|
|---|
| 207 | return 0;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /** Inclusion test for a signal set.
|
|---|
| 211 | *
|
|---|
| 212 | * @param set Pointer to the signal set.
|
|---|
| 213 | * @param signo Signal number to query.
|
|---|
| 214 | * @return 1 if the signal is in the set, 0 otherwise.
|
|---|
| 215 | */
|
|---|
| 216 | int posix_sigismember(const posix_sigset_t *set, int signo)
|
|---|
| 217 | {
|
|---|
| 218 | assert(set != NULL);
|
|---|
| 219 |
|
|---|
| 220 | return (*set & (1 << signo)) != 0;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /** Unsafe variant of the sigaction() function.
|
|---|
| 224 | * Doesn't do any checking of its arguments and
|
|---|
| 225 | * does not deal with thread-safety.
|
|---|
| 226 | *
|
|---|
| 227 | * @param sig
|
|---|
| 228 | * @param act
|
|---|
| 229 | * @param oact
|
|---|
| 230 | */
|
|---|
| 231 | static void _sigaction_unsafe(int sig, const struct posix_sigaction *restrict act,
|
|---|
| 232 | struct posix_sigaction *restrict oact)
|
|---|
| 233 | {
|
|---|
| 234 | if (oact != NULL) {
|
|---|
| 235 | memcpy(oact, &_signal_actions[sig],
|
|---|
| 236 | sizeof(struct posix_sigaction));
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | if (act != NULL) {
|
|---|
| 240 | memcpy(&_signal_actions[sig], act,
|
|---|
| 241 | sizeof(struct posix_sigaction));
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** Sets a new action for the given signal number.
|
|---|
| 246 | *
|
|---|
| 247 | * @param sig Signal number to set action for.
|
|---|
| 248 | * @param act If not NULL, contents of this structure are
|
|---|
| 249 | * used as the new action for the signal.
|
|---|
| 250 | * @param oact If not NULL, the original action associated with the signal
|
|---|
| 251 | * is stored in the structure pointer to.
|
|---|
| 252 | * @return -1 with errno set on failure, 0 on success.
|
|---|
| 253 | */
|
|---|
| 254 | int posix_sigaction(int sig, const struct posix_sigaction *restrict act,
|
|---|
| 255 | struct posix_sigaction *restrict oact)
|
|---|
| 256 | {
|
|---|
| 257 | if (sig > _TOP_SIGNAL || (act != NULL &&
|
|---|
| 258 | (sig == SIGKILL || sig == SIGSTOP))) {
|
|---|
| 259 | errno = EINVAL;
|
|---|
| 260 | return -1;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | if (sig > _TOP_CATCHABLE_SIGNAL) {
|
|---|
| 264 | posix_psignal(sig,
|
|---|
| 265 | "WARNING: registering handler for a partially"
|
|---|
| 266 | " or fully unsupported signal. This handler may only be"
|
|---|
| 267 | " invoked by the raise() function, which may not be what"
|
|---|
| 268 | " the application developer intended");
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | fibril_mutex_lock(&_signal_mutex);
|
|---|
| 272 | _sigaction_unsafe(sig, act, oact);
|
|---|
| 273 | fibril_mutex_unlock(&_signal_mutex);
|
|---|
| 274 |
|
|---|
| 275 | return 0;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /** Sets a new handler for the given signal number.
|
|---|
| 279 | *
|
|---|
| 280 | * @param sig Signal number to set handler for.
|
|---|
| 281 | * @param func Handler function.
|
|---|
| 282 | * @return SIG_ERR on failure, original handler on success.
|
|---|
| 283 | */
|
|---|
| 284 | void (*posix_signal(int sig, void (*func)(int)))(int)
|
|---|
| 285 | {
|
|---|
| 286 | struct posix_sigaction new = {
|
|---|
| 287 | .sa_handler = func,
|
|---|
| 288 | .sa_mask = 0,
|
|---|
| 289 | .sa_flags = 0,
|
|---|
| 290 | .sa_sigaction = NULL
|
|---|
| 291 | };
|
|---|
| 292 | struct posix_sigaction old;
|
|---|
| 293 | if (posix_sigaction(sig, func == NULL ? NULL : &new, &old) == 0) {
|
|---|
| 294 | return old.sa_handler;
|
|---|
| 295 | } else {
|
|---|
| 296 | return SIG_ERR;
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | typedef struct {
|
|---|
| 301 | link_t link;
|
|---|
| 302 | int signo;
|
|---|
| 303 | posix_siginfo_t siginfo;
|
|---|
| 304 | } signal_queue_item;
|
|---|
| 305 |
|
|---|
| 306 | /** Queue blocked signal.
|
|---|
| 307 | *
|
|---|
| 308 | * @param signo Signal number.
|
|---|
| 309 | * @param siginfo Additional information about the signal.
|
|---|
| 310 | */
|
|---|
| 311 | static void _queue_signal(int signo, posix_siginfo_t *siginfo)
|
|---|
| 312 | {
|
|---|
| 313 | assert(signo >= 0 && signo <= _TOP_SIGNAL);
|
|---|
| 314 | assert(siginfo != NULL);
|
|---|
| 315 |
|
|---|
| 316 | signal_queue_item *item = malloc(sizeof(signal_queue_item));
|
|---|
| 317 | link_initialize(&(item->link));
|
|---|
| 318 | item->signo = signo;
|
|---|
| 319 | memcpy(&item->siginfo, siginfo, sizeof(posix_siginfo_t));
|
|---|
| 320 | list_append(&(item->link), &_signal_queue);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 | /** Executes an action associated with the given signal.
|
|---|
| 325 | *
|
|---|
| 326 | * @param signo Signal number.
|
|---|
| 327 | * @param siginfo Additional information about the circumstances of this raise.
|
|---|
| 328 | * @return 0 if the action has been successfully executed. -1 if the signal is
|
|---|
| 329 | * blocked.
|
|---|
| 330 | */
|
|---|
| 331 | static int _raise_sigaction(int signo, posix_siginfo_t *siginfo)
|
|---|
| 332 | {
|
|---|
| 333 | assert(signo >= 0 && signo <= _TOP_SIGNAL);
|
|---|
| 334 | assert(siginfo != NULL);
|
|---|
| 335 |
|
|---|
| 336 | fibril_mutex_lock(&_signal_mutex);
|
|---|
| 337 |
|
|---|
| 338 | struct posix_sigaction action = _signal_actions[signo];
|
|---|
| 339 |
|
|---|
| 340 | if (posix_sigismember(&_signal_mask, signo) ||
|
|---|
| 341 | action.sa_handler == SIG_HOLD) {
|
|---|
| 342 | _queue_signal(signo, siginfo);
|
|---|
| 343 | fibril_mutex_unlock(&_signal_mutex);
|
|---|
| 344 | return -1;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /* Modifying signal mask is unnecessary,
|
|---|
| 348 | * signal handling is serialized.
|
|---|
| 349 | */
|
|---|
| 350 |
|
|---|
| 351 | if ((action.sa_flags & SA_RESETHAND) && signo != SIGILL && signo != SIGTRAP) {
|
|---|
| 352 | _signal_actions[signo] = (struct posix_sigaction) DEFAULT_HANDLER;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | if (action.sa_flags & SA_SIGINFO) {
|
|---|
| 356 | assert(action.sa_sigaction != NULL);
|
|---|
| 357 | action.sa_sigaction(signo, siginfo, NULL);
|
|---|
| 358 | } else {
|
|---|
| 359 | assert(action.sa_handler != NULL);
|
|---|
| 360 | action.sa_handler(signo);
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | fibril_mutex_unlock(&_signal_mutex);
|
|---|
| 364 |
|
|---|
| 365 | return 0;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | /** Raise all unblocked previously queued signals.
|
|---|
| 369 | */
|
|---|
| 370 | static void _dequeue_unblocked_signals()
|
|---|
| 371 | {
|
|---|
| 372 | link_t *iterator = _signal_queue.head.next;
|
|---|
| 373 | link_t *next;
|
|---|
| 374 |
|
|---|
| 375 | while (iterator != &(_signal_queue).head) {
|
|---|
| 376 | next = iterator->next;
|
|---|
| 377 |
|
|---|
| 378 | signal_queue_item *item =
|
|---|
| 379 | list_get_instance(iterator, signal_queue_item, link);
|
|---|
| 380 |
|
|---|
| 381 | if (!posix_sigismember(&_signal_mask, item->signo) &&
|
|---|
| 382 | _signal_actions[item->signo].sa_handler != SIG_HOLD) {
|
|---|
| 383 | list_remove(&(item->link));
|
|---|
| 384 | _raise_sigaction(item->signo, &(item->siginfo));
|
|---|
| 385 | free(item);
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | iterator = next;
|
|---|
| 389 | }
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | /** Raise a signal for the calling process.
|
|---|
| 393 | *
|
|---|
| 394 | * @param sig Signal number.
|
|---|
| 395 | * @return -1 with errno set on failure, 0 on success.
|
|---|
| 396 | */
|
|---|
| 397 | int posix_raise(int sig)
|
|---|
| 398 | {
|
|---|
| 399 | if (sig >= 0 && sig <= _TOP_SIGNAL) {
|
|---|
| 400 | posix_siginfo_t siginfo = {
|
|---|
| 401 | .si_signo = sig,
|
|---|
| 402 | .si_code = SI_USER
|
|---|
| 403 | };
|
|---|
| 404 | return _raise_sigaction(sig, &siginfo);
|
|---|
| 405 | } else {
|
|---|
| 406 | errno = EINVAL;
|
|---|
| 407 | return -1;
|
|---|
| 408 | }
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | /** Raises a signal for a selected process.
|
|---|
| 412 | *
|
|---|
| 413 | * @param pid PID of the process for which the signal shall be raised.
|
|---|
| 414 | * @param signo Signal to raise.
|
|---|
| 415 | * @return -1 with errno set on failure (possible errors include unsupported
|
|---|
| 416 | * action, invalid signal number, lack of permissions, etc.), 0 on success.
|
|---|
| 417 | */
|
|---|
| 418 | int posix_kill(posix_pid_t pid, int signo)
|
|---|
| 419 | {
|
|---|
| 420 | if (pid < 1) {
|
|---|
| 421 | // TODO
|
|---|
| 422 | errno = ENOTSUP;
|
|---|
| 423 | return -1;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | if (signo > _TOP_SIGNAL) {
|
|---|
| 427 | errno = EINVAL;
|
|---|
| 428 | return -1;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | if (pid == (posix_pid_t) task_get_id()) {
|
|---|
| 432 | return posix_raise(signo);
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | switch (signo) {
|
|---|
| 436 | case SIGKILL:
|
|---|
| 437 | task_kill(pid);
|
|---|
| 438 | break;
|
|---|
| 439 | default:
|
|---|
| 440 | /* Nothing else supported yet. */
|
|---|
| 441 | errno = ENOTSUP;
|
|---|
| 442 | return -1;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | return 0;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | /** Send a signal to a process group. Always fails at the moment because of
|
|---|
| 449 | * lack of this functionality in HelenOS.
|
|---|
| 450 | *
|
|---|
| 451 | * @param pid PID of the process group.
|
|---|
| 452 | * @param sig Signal number.
|
|---|
| 453 | * @return -1 on failure, 0 on success (see kill()).
|
|---|
| 454 | */
|
|---|
| 455 | int posix_killpg(posix_pid_t pid, int sig)
|
|---|
| 456 | {
|
|---|
| 457 | assert(pid > 1);
|
|---|
| 458 | return posix_kill(-pid, sig);
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | /** Outputs information about the signal to the standard error stream.
|
|---|
| 462 | *
|
|---|
| 463 | * @param pinfo SigInfo struct to write.
|
|---|
| 464 | * @param message String to output alongside human-readable signal description.
|
|---|
| 465 | */
|
|---|
| 466 | void posix_psiginfo(const posix_siginfo_t *pinfo, const char *message)
|
|---|
| 467 | {
|
|---|
| 468 | assert(pinfo != NULL);
|
|---|
| 469 | posix_psignal(pinfo->si_signo, message);
|
|---|
| 470 | // TODO: print si_code
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | /** Outputs information about the signal to the standard error stream.
|
|---|
| 474 | *
|
|---|
| 475 | * @param signum Signal number.
|
|---|
| 476 | * @param message String to output alongside human-readable signal description.
|
|---|
| 477 | */
|
|---|
| 478 | void posix_psignal(int signum, const char *message)
|
|---|
| 479 | {
|
|---|
| 480 | char *sigmsg = posix_strsignal(signum);
|
|---|
| 481 | if (message == NULL || *message == '\0') {
|
|---|
| 482 | fprintf(stderr, "%s\n", sigmsg);
|
|---|
| 483 | } else {
|
|---|
| 484 | fprintf(stderr, "%s: %s\n", message, sigmsg);
|
|---|
| 485 | }
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | /** Manipulate the signal mask of the calling thread.
|
|---|
| 489 | *
|
|---|
| 490 | * @param how What to do with the mask.
|
|---|
| 491 | * @param set Signal set to work with.
|
|---|
| 492 | * @param oset If not NULL, the original signal mask is coppied here.
|
|---|
| 493 | * @return 0 success, errorcode on failure.
|
|---|
| 494 | */
|
|---|
| 495 | int posix_thread_sigmask(int how, const posix_sigset_t *restrict set,
|
|---|
| 496 | posix_sigset_t *restrict oset)
|
|---|
| 497 | {
|
|---|
| 498 | fibril_mutex_lock(&_signal_mutex);
|
|---|
| 499 |
|
|---|
| 500 | if (oset != NULL) {
|
|---|
| 501 | *oset = _signal_mask;
|
|---|
| 502 | }
|
|---|
| 503 | if (set != NULL) {
|
|---|
| 504 | switch (how) {
|
|---|
| 505 | case SIG_BLOCK:
|
|---|
| 506 | _signal_mask |= *set;
|
|---|
| 507 | break;
|
|---|
| 508 | case SIG_UNBLOCK:
|
|---|
| 509 | _signal_mask &= ~*set;
|
|---|
| 510 | break;
|
|---|
| 511 | case SIG_SETMASK:
|
|---|
| 512 | _signal_mask = *set;
|
|---|
| 513 | break;
|
|---|
| 514 | default:
|
|---|
| 515 | fibril_mutex_unlock(&_signal_mutex);
|
|---|
| 516 | return EINVAL;
|
|---|
| 517 | }
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | _dequeue_unblocked_signals();
|
|---|
| 521 |
|
|---|
| 522 | fibril_mutex_unlock(&_signal_mutex);
|
|---|
| 523 |
|
|---|
| 524 | return 0;
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | /** Manipulate the signal mask of the process.
|
|---|
| 528 | *
|
|---|
| 529 | * @param how What to do with the mask.
|
|---|
| 530 | * @param set Signal set to work with.
|
|---|
| 531 | * @param oset If not NULL, the original signal mask is coppied here.
|
|---|
| 532 | * @return 0 on success, -1 with errno set on failure.
|
|---|
| 533 | */
|
|---|
| 534 | int posix_sigprocmask(int how, const posix_sigset_t *restrict set,
|
|---|
| 535 | posix_sigset_t *restrict oset)
|
|---|
| 536 | {
|
|---|
| 537 | int result = posix_thread_sigmask(how, set, oset);
|
|---|
| 538 | if (result != 0) {
|
|---|
| 539 | errno = result;
|
|---|
| 540 | return -1;
|
|---|
| 541 | }
|
|---|
| 542 | return 0;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | /** @}
|
|---|
| 546 | */
|
|---|