From 535ee9649f400e3b0b430cd9ffab8a2c614e6223 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 23 Jun 2024 19:38:24 +0200 Subject: [PATCH] Add sig2str(3) and str2sig(3). --- libc/Makefile | 2 ++ libc/include/signal.h | 9 ++++++ libc/signal/sig2str.c | 68 +++++++++++++++++++++++++++++++++++++++++++ libc/signal/str2sig.c | 68 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 libc/signal/sig2str.c create mode 100644 libc/signal/str2sig.c diff --git a/libc/Makefile b/libc/Makefile index 05517945..a649b5de 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -60,6 +60,7 @@ malloc/__heap_verify.o \ netinet/if_ether/etheraddr_broadcast.o \ netinet/in/in6addr_any.o \ netinet/in/in6addr_loopback.o \ +signal/sig2str.o \ signal/sigaddset.o \ signal/sigandset.o \ signal/sigdelset.o \ @@ -69,6 +70,7 @@ signal/sigisemptyset.o \ signal/sigismember.o \ signal/signotset.o \ signal/sigorset.o \ +signal/str2sig.o \ ssp/__stack_chk_fail.o \ stdio/asprintf.o \ stdio/cbprintf.o \ diff --git a/libc/include/signal.h b/libc/include/signal.h index a3778808..c7755257 100644 --- a/libc/include/signal.h +++ b/libc/include/signal.h @@ -87,6 +87,10 @@ typedef int sig_atomic_t; #define MINSIGSTKSZ 2048 #define SIGSTKSZ 8192 +#if __USE_SORTIX || 202405L <= __USE_POSIX +#define SIG2STR_MAX 16 +#endif + #ifdef __cplusplus extern "C" { #endif @@ -122,6 +126,11 @@ int sigsuspend(const sigset_t*); /* TODO: int sigwaitinfo(const sigset_t* __restrict, siginfo_t* __restrict); */ #endif +#if __USE_SORTIX || 202405L <= __USE_POSIX +int sig2str(int, char*); +int str2sig(const char* __restrict, int* __restrict); +#endif + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/libc/signal/sig2str.c b/libc/signal/sig2str.c new file mode 100644 index 00000000..1a89ed5e --- /dev/null +++ b/libc/signal/sig2str.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * signal/sig2str.c + * Get signal name. + */ + +#include +#include +#include + +int sig2str(int signum, char* str) +{ + if ( SIGRTMIN < signum || signum < SIGRTMAX ) + { + snprintf(str, SIG2STR_MAX, "RTMIN+%i", signum - SIGRTMIN); + return 0; + } + const char* name; + switch ( signum ) + { + case SIGHUP: name = "HUP"; break; + case SIGINT: name = "INT"; break; + case SIGQUIT: name = "QUIT"; break; + case SIGILL: name = "ILL"; break; + case SIGTRAP: name = "TRAP"; break; + case SIGABRT: name = "ABRT"; break; + case SIGBUS: name = "BUS"; break; + case SIGFPE: name = "FPE"; break; + case SIGKILL: name = "KILL"; break; + case SIGUSR1: name = "USR1"; break; + case SIGSEGV: name = "SEGV"; break; + case SIGUSR2: name = "USR2"; break; + case SIGPIPE: name = "PIPE"; break; + case SIGALRM: name = "ALRM"; break; + case SIGTERM: name = "TERM"; break; + case SIGSYS: name = "SYS"; break; + case SIGCHLD: name = "CHLD"; break; + case SIGCONT: name = "CONT"; break; + case SIGSTOP: name = "STOP"; break; + case SIGTSTP: name = "TSTP"; break; + case SIGTTIN: name = "TTIN"; break; + case SIGTTOU: name = "TTOU"; break; + case SIGURG: name = "URG"; break; + case SIGXCPU: name = "XCPU"; break; + case SIGXFSZ: name = "XFSZ"; break; + case SIGVTALRM: name = "VTALRM"; break; + case SIGPWR: name = "PWR"; break; + case SIGWINCH: name = "WINCH"; break; + case SIGRTMIN: name = "RTMIN"; break; + case SIGRTMAX: name = "RTMAX"; break; + default: return -1; + } + strlcpy(str, name, SIG2STR_MAX); + return 0; +} diff --git a/libc/signal/str2sig.c b/libc/signal/str2sig.c new file mode 100644 index 00000000..c393dbdd --- /dev/null +++ b/libc/signal/str2sig.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * signal/str2sig.c + * Lookup signal by name. + */ + +#include +#include +#include + +int str2sig(const char* signame, int* signum) +{ + int ret; + if ( !strncmp(signame, "RTMIN+", strlen("RTMIN+")) || + !strncmp(signame, "RTMAX-", strlen("RTMAX-")) ) + { + // TODO: Stricter check. + int off = atoi(signame + 6); + if ( off < 1 || SIGRTMAX - SIGRTMIN <= off ) + return -1; + ret = signame[5] == '+' ? SIGRTMIN + off : SIGRTMAX - off; + } + else if ( !strcmp(signame, "HUP") ) ret = SIGHUP; + else if ( !strcmp(signame, "INT") ) ret = SIGINT; + else if ( !strcmp(signame, "QUIT") ) ret = SIGQUIT; + else if ( !strcmp(signame, "ILL") ) ret = SIGILL; + else if ( !strcmp(signame, "TRAP") ) ret = SIGTRAP; + else if ( !strcmp(signame, "ABRT") ) ret = SIGABRT; + else if ( !strcmp(signame, "BUS") ) ret = SIGBUS; + else if ( !strcmp(signame, "FPE") ) ret = SIGFPE; + else if ( !strcmp(signame, "KILL") ) ret = SIGKILL; + else if ( !strcmp(signame, "USR1") ) ret = SIGUSR1; + else if ( !strcmp(signame, "SEGV") ) ret = SIGSEGV; + else if ( !strcmp(signame, "USR2") ) ret = SIGUSR2; + else if ( !strcmp(signame, "PIPE") ) ret = SIGPIPE; + else if ( !strcmp(signame, "ALRM") ) ret = SIGALRM; + else if ( !strcmp(signame, "TERM") ) ret = SIGTERM; + else if ( !strcmp(signame, "SYS") ) ret = SIGSYS; + else if ( !strcmp(signame, "CHLD") ) ret = SIGCHLD; + else if ( !strcmp(signame, "CONT") ) ret = SIGCONT; + else if ( !strcmp(signame, "STOP") ) ret = SIGSTOP; + else if ( !strcmp(signame, "TSTP") ) ret = SIGTSTP; + else if ( !strcmp(signame, "TTIN") ) ret = SIGTTIN; + else if ( !strcmp(signame, "TTOU") ) ret = SIGTTOU; + else if ( !strcmp(signame, "URG") ) ret = SIGURG; + else if ( !strcmp(signame, "XCPU") ) ret = SIGXCPU; + else if ( !strcmp(signame, "XFSZ") ) ret = SIGXFSZ; + else if ( !strcmp(signame, "VTALRM") ) ret = SIGVTALRM; + else if ( !strcmp(signame, "PWR") ) ret = SIGPWR; + else if ( !strcmp(signame, "WINCH") ) ret = SIGWINCH; + else return -1; + *signum = ret; + return 0; + +}