From: Joakim Sindholt <opensource@zhasha.com>
To: musl@lists.openwall.com
Subject: Re: [musl] [PATCH 2/2] signal: add str2sig(3) from POSIX.1-2024
Date: Sun, 4 Aug 2024 16:02:44 +0200 [thread overview]
Message-ID: <20240804160244.16aaa35b@eclair> (raw)
In-Reply-To: <CAKbZUD1WAZ=xsY2WtykFbqU9R+jg66FSW10NfB-ZDbSh_Ct3pQ@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 638 bytes --]
On Sun, 4 Aug 2024 14:39:21 +0100 Pedro Falcato <pedro.falcato@gmail.com> wrote:
> To me it looks like the best way to implement these two functions is
> to create a table of some sorts (indexed by signal, so array[SIGINT] =
> "INT") and then use it.
> str2sig would just iterate the whole array with strcmps, which is a
> _little_ slow but this is probably not performance critical anyway.
>
> Special casing would be required for RT signals, but that's no biggie.
> What do you think?
I was thinking the same thing for the sake of size.
There are a few more special cases though as some signal names refer to the
same signal numbers.
[-- Attachment #2: sig2str.c --]
[-- Type: text/x-c++src, Size: 2719 bytes --]
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#define SIG(s) [SIG##s] = #s
static const char signames[][8] = {
SIG(HUP),
SIG(INT),
SIG(QUIT),
SIG(ILL),
SIG(TRAP),
SIG(ABRT),
SIG(BUS),
SIG(FPE),
SIG(KILL),
SIG(USR1),
SIG(SEGV),
SIG(USR2),
SIG(PIPE),
SIG(ALRM),
SIG(TERM),
SIG(STKFLT),
SIG(CHLD),
SIG(CONT),
SIG(STOP),
SIG(TSTP),
SIG(TTIN),
SIG(TTOU),
SIG(URG),
SIG(XCPU),
SIG(XFSZ),
SIG(VTALRM),
SIG(PROF),
SIG(WINCH),
SIG(POLL),
SIG(PWR),
SIG(SYS),
[32] = "TIMER",
[33] = "CANCEL",
[34] = "SYNCCALL",
[35] = "RTMIN",
#define RTMIN 35
#define RTMAX SIGRTMAX
};
int sig2str(int sig, char *str)
{
const char *name;
int i, num;
if (sig < sizeof signames/sizeof *signames) {
for (i = 0; i < sizeof *signames; i++)
str[i] = signames[sig][i];
str[sizeof *signames] = 0;
return 0;
}
if (sig == RTMAX) {
for (i = 0; i < 6; i++)
str[i] = "RTMAX"[i];
return 0;
}
if (sig > RTMIN && sig <= RTMAX) {
if (sig <= (RTMIN+RTMAX)/2) {
name = "RTMIN+";
num = sig-RTMIN;
} else {
name = sig==RTMAX?"RTMAX":"RTMAX-";
num = RTMAX-sig;
}
for (i = 0; i < 6; i++)
str[i] = name[i];
if (num > 10)
str[i++] = '0'+num/10;
if (num > 0)
str[i++] = '0'+num%10;
str[i] = 0;
return 0;
}
errno = EINVAL;
return -1;
}
int str2sig(const char *restrict str, int *restrict sig)
{
int i, num;
if (strnlen(str, sizeof *signames) <= sizeof *signames)
for (i = 0; i < sizeof signames/sizeof *signames; i++)
if (strncmp(str, signames[i], sizeof *signames) == 0)
return (*sig = i, 0);
if (strcmp(str, "IO") == 0)
return (*sig = SIGIO, 0);
if (strcmp(str, "IOT") == 0)
return (*sig = SIGIOT, 0);
if (strcmp(str, "UNUSED") == 0)
return (*sig = SIGUNUSED, 0);
if (strcmp(str, "RTMAX") == 0)
return (*sig = RTMAX, 0);
if (strncmp(str, "RTMIN+", 6) == 0 || strncmp(str, "RTMAX-", 6) == 0)
i = 6;
else if (isdigit(*str))
i = 0;
else
return -1;
if (str[i] < '1' || str[i] > '9')
return -1;
num = str[i++]-'0';
if (isdigit(str[i]))
num = num*10+str[i++]-'0';
if (str[i])
return -1;
if (*str != 'R')
return num<=RTMAX?(*sig = num, 0):-1;
else if (num < RTMAX-RTMIN)
return (*sig = str[5]=='+'?RTMIN+num:RTMAX-num, 0);
else
return -1;
}
next prev parent reply other threads:[~2024-08-04 14:03 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-04 12:41 [musl] [PATCH 1/2] signal: add sig2str(3) " contact
2024-08-04 12:41 ` [musl] [PATCH 2/2] signal: add str2sig(3) " contact
2024-08-04 13:39 ` Pedro Falcato
2024-08-04 14:02 ` Joakim Sindholt [this message]
2024-08-04 14:23 ` [musl] [PATCH 1/2] signal: add sig2str(3) " Joakim Sindholt
2024-08-04 14:32 ` Rich Felker
2024-08-04 14:29 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240804160244.16aaa35b@eclair \
--to=opensource@zhasha.com \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).