Luna/libc/src/signal.cpp
2023-07-10 21:17:25 +02:00

92 lines
1.9 KiB
C++

#include <bits/errno-return.h>
#include <signal.h>
#include <sys/syscall.h>
#include <unistd.h>
extern "C" void sigreturn();
extern "C"
{
int sigaction(int signo, const struct sigaction* act, struct sigaction* oldact)
{
long rc = syscall(SYS_sigaction, signo, act, oldact, sigreturn);
__errno_return(rc, int);
}
__simple_sighandler_t signal(int signo, __simple_sighandler_t handler)
{
struct sigaction act, oldact;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(signo, &act, &oldact) < 0) return SIG_ERR;
return oldact.sa_handler;
}
int kill(pid_t pid, int signo)
{
long rc = syscall(SYS_kill, pid, signo);
__errno_return(rc, int);
}
int raise(int signo)
{
return kill(getpid(), signo);
}
int sigprocmask(int how, const sigset_t* set, sigset_t* oldset)
{
long rc = syscall(SYS_sigprocmask, how, set, oldset);
__errno_return(rc, int);
}
int sigemptyset(sigset_t* set)
{
*set = 0;
return 0;
}
int sigfillset(sigset_t* set)
{
*set = 0xffffffff;
return 0;
}
int sigaddset(sigset_t* set, int signo)
{
if (signo <= 0 || signo > NSIG)
{
errno = EINVAL;
return -1;
}
*set |= (1 << (signo - 1));
return 0;
}
int sigdelset(sigset_t* set, int signo)
{
if (signo <= 0 || signo > NSIG)
{
errno = EINVAL;
return -1;
}
*set &= ~(1 << (signo - 1));
return 0;
}
int sigismember(const sigset_t* set, int signo)
{
if (signo <= 0 || signo > NSIG)
{
errno = EINVAL;
return -1;
}
return (*set & (1 << (signo - 1))) > 0;
}
}