Add sig2str(3) and str2sig(3).
This commit is contained in:
parent
230b816702
commit
535ee9649f
4 changed files with 147 additions and 0 deletions
|
@ -60,6 +60,7 @@ malloc/__heap_verify.o \
|
||||||
netinet/if_ether/etheraddr_broadcast.o \
|
netinet/if_ether/etheraddr_broadcast.o \
|
||||||
netinet/in/in6addr_any.o \
|
netinet/in/in6addr_any.o \
|
||||||
netinet/in/in6addr_loopback.o \
|
netinet/in/in6addr_loopback.o \
|
||||||
|
signal/sig2str.o \
|
||||||
signal/sigaddset.o \
|
signal/sigaddset.o \
|
||||||
signal/sigandset.o \
|
signal/sigandset.o \
|
||||||
signal/sigdelset.o \
|
signal/sigdelset.o \
|
||||||
|
@ -69,6 +70,7 @@ signal/sigisemptyset.o \
|
||||||
signal/sigismember.o \
|
signal/sigismember.o \
|
||||||
signal/signotset.o \
|
signal/signotset.o \
|
||||||
signal/sigorset.o \
|
signal/sigorset.o \
|
||||||
|
signal/str2sig.o \
|
||||||
ssp/__stack_chk_fail.o \
|
ssp/__stack_chk_fail.o \
|
||||||
stdio/asprintf.o \
|
stdio/asprintf.o \
|
||||||
stdio/cbprintf.o \
|
stdio/cbprintf.o \
|
||||||
|
|
|
@ -87,6 +87,10 @@ typedef int sig_atomic_t;
|
||||||
#define MINSIGSTKSZ 2048
|
#define MINSIGSTKSZ 2048
|
||||||
#define SIGSTKSZ 8192
|
#define SIGSTKSZ 8192
|
||||||
|
|
||||||
|
#if __USE_SORTIX || 202405L <= __USE_POSIX
|
||||||
|
#define SIG2STR_MAX 16
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
@ -122,6 +126,11 @@ int sigsuspend(const sigset_t*);
|
||||||
/* TODO: int sigwaitinfo(const sigset_t* __restrict, siginfo_t* __restrict); */
|
/* TODO: int sigwaitinfo(const sigset_t* __restrict, siginfo_t* __restrict); */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if __USE_SORTIX || 202405L <= __USE_POSIX
|
||||||
|
int sig2str(int, char*);
|
||||||
|
int str2sig(const char* __restrict, int* __restrict);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} /* extern "C" */
|
} /* extern "C" */
|
||||||
#endif
|
#endif
|
||||||
|
|
68
libc/signal/sig2str.c
Normal file
68
libc/signal/sig2str.c
Normal file
|
@ -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 <signal.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
68
libc/signal/str2sig.c
Normal file
68
libc/signal/str2sig.c
Normal file
|
@ -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 <signal.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue