HelenOS sources
This source file includes following definitions.
- __posix_default_signal_handler
- __posix_hold_signal_handler
- __posix_ignore_signal_handler
- sigemptyset
- sigfillset
- sigaddset
- sigdelset
- sigismember
- _sigaction_unsafe
- sigaction
- signal
- _queue_signal
- _raise_sigaction
- _dequeue_unblocked_signals
- raise
- kill
- killpg
- psiginfo
- psignal
- thread_sigmask
- sigprocmask
#include <signal.h>
#include "internal/common.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fibril_synch.h>
#include <task.h>
static FIBRIL_MUTEX_INITIALIZE(_signal_mutex);
static LIST_INITIALIZE(_signal_queue);
static sigset_t _signal_mask = 0;
#define DEFAULT_HANDLER { .sa_handler = SIG_DFL, \
.sa_mask = 0, .sa_flags = 0, .sa_sigaction = NULL }
static struct sigaction _signal_actions[_TOP_SIGNAL + 1] = {
DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER,
DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER, DEFAULT_HANDLER
};
void __posix_default_signal_handler(int signo)
{
switch (signo) {
case SIGABRT:
abort();
case SIGQUIT:
fprintf(stderr, "Quit signal raised. Exiting.\n");
exit(EXIT_FAILURE);
case SIGINT:
fprintf(stderr, "Interrupt signal caught. Exiting.\n");
exit(EXIT_FAILURE);
case SIGTERM:
fprintf(stderr, "Termination signal caught. Exiting.\n");
exit(EXIT_FAILURE);
case SIGSTOP:
fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.\n");
break;
case SIGKILL:
task_kill(task_get_id());
abort();
case SIGFPE:
case SIGBUS:
case SIGILL:
case SIGSEGV:
psignal(signo, "Hardware exception raised by user code");
abort();
case SIGSYS:
case SIGXCPU:
case SIGXFSZ:
case SIGTRAP:
case SIGHUP:
case SIGPIPE:
case SIGPOLL:
case SIGURG:
case SIGTSTP:
case SIGTTIN:
case SIGTTOU:
psignal(signo, "Unsupported signal caught");
abort();
case SIGCHLD:
case SIGUSR1:
case SIGUSR2:
case SIGALRM:
case SIGVTALRM:
case SIGPROF:
case SIGCONT:
break;
}
}
void __posix_hold_signal_handler(int signo)
{
}
void __posix_ignore_signal_handler(int signo)
{
}
int sigemptyset(sigset_t *set)
{
assert(set != NULL);
*set = 0;
return 0;
}
int sigfillset(sigset_t *set)
{
assert(set != NULL);
*set = UINT32_MAX;
return 0;
}
int sigaddset(sigset_t *set, int signo)
{
assert(set != NULL);
*set |= (1 << signo);
return 0;
}
int sigdelset(sigset_t *set, int signo)
{
assert(set != NULL);
*set &= ~(1 << signo);
return 0;
}
int sigismember(const sigset_t *set, int signo)
{
assert(set != NULL);
return (*set & (1 << signo)) != 0;
}
static void _sigaction_unsafe(int sig, const struct sigaction *restrict act,
struct sigaction *restrict oact)
{
if (oact != NULL) {
memcpy(oact, &_signal_actions[sig],
sizeof(struct sigaction));
}
if (act != NULL) {
memcpy(&_signal_actions[sig], act,
sizeof(struct sigaction));
}
}
int sigaction(int sig, const struct sigaction *restrict act,
struct sigaction *restrict oact)
{
if (sig > _TOP_SIGNAL || (act != NULL &&
(sig == SIGKILL || sig == SIGSTOP))) {
errno = EINVAL;
return -1;
}
if (sig > _TOP_CATCHABLE_SIGNAL) {
psignal(sig,
"WARNING: registering handler for a partially"
" or fully unsupported signal. This handler may only be"
" invoked by the raise() function, which may not be what"
" the application developer intended");
}
fibril_mutex_lock(&_signal_mutex);
_sigaction_unsafe(sig, act, oact);
fibril_mutex_unlock(&_signal_mutex);
return 0;
}
void (*signal(int sig, void (*func)(int)))(int)
{
struct sigaction new = {
.sa_handler = func,
.sa_mask = 0,
.sa_flags = 0,
.sa_sigaction = NULL
};
struct sigaction old;
if (sigaction(sig, func == NULL ? NULL : &new, &old) == 0) {
return old.sa_handler;
} else {
return SIG_ERR;
}
}
typedef struct {
link_t link;
int signo;
siginfo_t siginfo;
} signal_queue_item;
static void _queue_signal(int signo, siginfo_t *siginfo)
{
assert(signo >= 0 && signo <= _TOP_SIGNAL);
assert(siginfo != NULL);
signal_queue_item *item = malloc(sizeof(signal_queue_item));
link_initialize(&(item->link));
item->signo = signo;
memcpy(&item->siginfo, siginfo, sizeof(siginfo_t));
list_append(&(item->link), &_signal_queue);
}
static int _raise_sigaction(int signo, siginfo_t *siginfo)
{
assert(signo >= 0 && signo <= _TOP_SIGNAL);
assert(siginfo != NULL);
fibril_mutex_lock(&_signal_mutex);
struct sigaction action = _signal_actions[signo];
if (sigismember(&_signal_mask, signo) ||
action.sa_handler == SIG_HOLD) {
_queue_signal(signo, siginfo);
fibril_mutex_unlock(&_signal_mutex);
return -1;
}
if ((action.sa_flags & SA_RESETHAND) && signo != SIGILL && signo != SIGTRAP) {
_signal_actions[signo] = (struct sigaction) DEFAULT_HANDLER;
}
if (action.sa_flags & SA_SIGINFO) {
assert(action.sa_sigaction != NULL);
action.sa_sigaction(signo, siginfo, NULL);
} else {
assert(action.sa_handler != NULL);
action.sa_handler(signo);
}
fibril_mutex_unlock(&_signal_mutex);
return 0;
}
static void _dequeue_unblocked_signals(void)
{
link_t *iterator = _signal_queue.head.next;
link_t *next;
while (iterator != &(_signal_queue).head) {
next = iterator->next;
signal_queue_item *item =
list_get_instance(iterator, signal_queue_item, link);
if (!sigismember(&_signal_mask, item->signo) &&
_signal_actions[item->signo].sa_handler != SIG_HOLD) {
list_remove(&(item->link));
_raise_sigaction(item->signo, &(item->siginfo));
free(item);
}
iterator = next;
}
}
int raise(int sig)
{
if (sig >= 0 && sig <= _TOP_SIGNAL) {
siginfo_t siginfo = {
.si_signo = sig,
.si_code = SI_USER
};
return _raise_sigaction(sig, &siginfo);
} else {
errno = EINVAL;
return -1;
}
}
int kill(pid_t pid, int signo)
{
if (pid < 1) {
errno = ENOTSUP;
return -1;
}
if (signo > _TOP_SIGNAL) {
errno = EINVAL;
return -1;
}
if (pid == (pid_t) task_get_id()) {
return raise(signo);
}
switch (signo) {
case SIGKILL:
task_kill(pid);
break;
default:
errno = ENOTSUP;
return -1;
}
return 0;
}
int killpg(pid_t pid, int sig)
{
assert(pid > 1);
return kill(-pid, sig);
}
void psiginfo(const siginfo_t *pinfo, const char *message)
{
assert(pinfo != NULL);
psignal(pinfo->si_signo, message);
}
void psignal(int signum, const char *message)
{
char *sigmsg = strsignal(signum);
if (message == NULL || *message == '\0') {
fprintf(stderr, "%s\n", sigmsg);
} else {
fprintf(stderr, "%s: %s\n", message, sigmsg);
}
}
int thread_sigmask(int how, const sigset_t *restrict set,
sigset_t *restrict oset)
{
fibril_mutex_lock(&_signal_mutex);
if (oset != NULL) {
*oset = _signal_mask;
}
if (set != NULL) {
switch (how) {
case SIG_BLOCK:
_signal_mask |= *set;
break;
case SIG_UNBLOCK:
_signal_mask &= ~*set;
break;
case SIG_SETMASK:
_signal_mask = *set;
break;
default:
fibril_mutex_unlock(&_signal_mutex);
return EINVAL;
}
}
_dequeue_unblocked_signals();
fibril_mutex_unlock(&_signal_mutex);
return 0;
}
int sigprocmask(int how, const sigset_t *restrict set,
sigset_t *restrict oset)
{
int result = thread_sigmask(how, set, oset);
if (result != 0) {
errno = result;
return -1;
}
return 0;
}
HelenOS homepage, sources at GitHub