Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f19e29b

Browse filesBrowse files
Add the POSIX sig2str(3) & str2sig(3) calls
Signed-off-by: Ricardo Branco <rbranco@suse.de>
1 parent fed5475 commit f19e29b
Copy full SHA for f19e29b

File tree

Expand file treeCollapse file tree

5 files changed

+123
-2
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+123
-2
lines changed

‎include/signal.h

Copy file name to clipboardExpand all lines: include/signal.h
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
#include <sys/_ucontext.h>
4141
#endif
4242

43+
#if __POSIX_VISIBLE >= 202405
44+
#define SIG2STR_MAX 32 /* size of buffer required for sig2str() */
45+
#endif
46+
4347
__NULLABILITY_PRAGMA_PUSH
4448

4549
#if __BSD_VISIBLE
@@ -119,6 +123,11 @@ void psiginfo(const siginfo_t *, const char *);
119123
void psignal(int, const char *);
120124
#endif
121125

126+
#if __POSIX_VISIBLE >= 202405
127+
int sig2str(int, char *);
128+
int str2sig(const char * __restrict, int * __restrict);
129+
#endif
130+
122131
#if __BSD_VISIBLE
123132
int sigandset(sigset_t *dest, const sigset_t *left, const sigset_t *right);
124133
int sigblock(int);

‎lib/libc/gen/Makefile.inc

Copy file name to clipboardExpand all lines: lib/libc/gen/Makefile.inc
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ MLINKS+=posix_spawn.3 posix_spawnp.3 \
470470
posix_spawnattr_getsigmask.3 posix_spawnattr_setsigmask.3 \
471471
posix_spawnattr_init.3 posix_spawnattr_destroy.3
472472
MLINKS+=psignal.3 psiginfo.3 \
473+
psignal.3 sig2str.3 \
474+
psignal.3 str2sig.3 \
473475
psignal.3 strsignal.3 \
474476
psignal.3 sys_siglist.3 \
475477
psignal.3 sys_signame.3

‎lib/libc/gen/Symbol.map

Copy file name to clipboardExpand all lines: lib/libc/gen/Symbol.map
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ FBSD_1.8 {
462462
psiginfo;
463463
rtld_get_var;
464464
rtld_set_var;
465+
sig2str;
466+
str2sig;
465467
};
466468

467469
FBSDprivate_1.0 {

‎lib/libc/gen/psignal.3

Copy file name to clipboardExpand all lines: lib/libc/gen/psignal.3
+35-2Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@
2525
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2626
.\" SUCH DAMAGE.
2727
.\"
28-
.Dd Apr 16, 2025
28+
.Dd May 10, 2025
2929
.Dt PSIGNAL 3
3030
.Os
3131
.Sh NAME
3232
.Nm psignal ,
3333
.Nm psiginfo ,
3434
.Nm strsignal ,
3535
.Nm sys_siglist ,
36-
.Nm sys_signame
36+
.Nm sys_signame ,
37+
.Nm sig2str ,
38+
.Nm str2sig
3739
.Nd system signal messages
3840
.Sh LIBRARY
3941
.Lb libc
@@ -48,6 +50,10 @@
4850
.In string.h
4951
.Ft "char *"
5052
.Fn strsignal "int sig"
53+
.Ft int
54+
.Fn sig2str "int signum" "char *str"
55+
.Ft int
56+
.Fn str2sig "char *str" "int *pnum"
5157
.Sh DESCRIPTION
5258
The
5359
.Fn psignal
@@ -108,6 +114,27 @@ contains a count of the strings in
108114
.Va sys_siglist
109115
and
110116
.Va sys_signame .
117+
.Pp
118+
The
119+
.Fn sig2str
120+
translates the signal number
121+
.Fa signum
122+
to the signal name, without the "SIG" prefix, and stores it at the location specified by
123+
.Fa str ,
124+
which should be large enough to hold the name and the terminating NUL byte.
125+
The symbol
126+
.Dv SIG2STR_MAX
127+
gives the maximum size in bytes required.
128+
.Pp
129+
The
130+
.Fn str2sig
131+
translates the signal name
132+
.Fa str
133+
to a signal number, and stores it in the location referenced by
134+
.Fa pnum .
135+
The name in
136+
.Fa str
137+
can be either the name of the signal, without the "SIG" prefix, or a decimal number.
111138
.Sh SEE ALSO
112139
.Xr sigaction 2 ,
113140
.Xr perror 3 ,
@@ -124,3 +151,9 @@ function appeared in
124151
.Nx 6.0 ,
125152
and
126153
.Dx 4.1 .
154+
The
155+
.Fn sig2str
156+
and
157+
.Fn str2sig
158+
functions appeared in
159+
.Fx 15.0

‎lib/libc/gen/psignal.c

Copy file name to clipboardExpand all lines: lib/libc/gen/psignal.c
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@
3434
* along with the supplied message.
3535
*/
3636
#include "namespace.h"
37+
#include <ctype.h>
3738
#include <signal.h>
39+
#include <stdio.h>
40+
#include <stdlib.h>
3841
#include <string.h>
3942
#include <unistd.h>
4043
#include "un-namespace.h"
4144

45+
static const char rtmin_str[] = "RTMIN";
46+
static const char rtmax_str[] = "RTMAX";
47+
4248
void
4349
psignal(int sig, const char *s)
4450
{
@@ -61,3 +67,72 @@ psiginfo(const siginfo_t *si, const char *s)
6167
{
6268
psignal(si->si_signo, s);
6369
}
70+
71+
int
72+
sig2str(int signum, char *str)
73+
{
74+
int n;
75+
76+
if (signum <= 0 || signum > SIGRTMAX)
77+
return (-1);
78+
79+
if (signum < sys_nsig)
80+
(void)strcpy(str, sys_signame[signum]);
81+
else if (signum < SIGRTMIN)
82+
(void)sprintf(str, "%d", signum);
83+
else {
84+
if (signum <= (SIGRTMIN + SIGRTMAX) / 2) {
85+
(void)strcpy(str, rtmin_str);
86+
n = signum - SIGRTMIN;
87+
} else {
88+
(void)strcpy(str, rtmax_str);
89+
n = signum - SIGRTMAX;
90+
}
91+
if (n != 0)
92+
(void)sprintf(str + sizeof(rtmin_str) - 1, "%+d", n);
93+
}
94+
95+
return (0);
96+
}
97+
98+
int
99+
str2sig(const char * restrict str, int * restrict pnum)
100+
{
101+
int n, sig;
102+
char *end;
103+
int rtend = sizeof(rtmin_str) - 1;
104+
105+
if (strncmp(str, rtmin_str, sizeof(rtmin_str) - 1) == 0 ||
106+
strncmp(str, rtmax_str, sizeof(rtmax_str) - 1) == 0) {
107+
sig = (str[4] == 'X') ? SIGRTMAX : SIGRTMIN;
108+
n = 0;
109+
if (str[rtend] == '+' || str[rtend] == '-') {
110+
n = (int) strtol(str + rtend, &end, 10);
111+
if (*end != '\0' || n == 0)
112+
return (-1);
113+
} else if (str[rtend] != '\0')
114+
return (-1);
115+
sig += n;
116+
if (sig < SIGRTMIN || sig > SIGRTMAX)
117+
return (-1);
118+
*pnum = sig;
119+
return (0);
120+
}
121+
122+
if (isdigit(str[0])) {
123+
sig = (int)strtol(str, &end, 10);
124+
if (*end == '\0' && sig > 0 && sig < SIGRTMAX) {
125+
*pnum = sig;
126+
return (0);
127+
}
128+
}
129+
130+
for (sig = 1; sig < sys_nsig; sig++) {
131+
if (strcmp(sys_signame[sig], str) == 0) {
132+
*pnum = sig;
133+
return (0);
134+
}
135+
}
136+
137+
return (-1);
138+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.