mirror of
https://git.FreeBSD.org/src.git
synced 2025-02-01 17:00:36 +00:00
Make strsignal(3) thread-safe.
MFC after: 2 weeks
This commit is contained in:
parent
681ffdf935
commit
4f86602c03
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=202916
@ -33,22 +33,64 @@ static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93";
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include "namespace.h"
|
||||||
#if defined(NLS)
|
#if defined(NLS)
|
||||||
#include <nl_types.h>
|
#include <nl_types.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include "reentrant.h"
|
||||||
|
#include "un-namespace.h"
|
||||||
|
|
||||||
#define UPREFIX "Unknown signal"
|
#define UPREFIX "Unknown signal"
|
||||||
|
|
||||||
|
static char sig_ebuf[NL_TEXTMAX];
|
||||||
|
static char sig_ebuf_err[NL_TEXTMAX];
|
||||||
|
static once_t sig_init_once = ONCE_INITIALIZER;
|
||||||
|
static thread_key_t sig_key;
|
||||||
|
static int sig_keycreated = 0;
|
||||||
|
|
||||||
|
static void
|
||||||
|
sig_keycreate(void)
|
||||||
|
{
|
||||||
|
sig_keycreated = (thr_keycreate(&sig_key, free) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
sig_tlsalloc(void)
|
||||||
|
{
|
||||||
|
char *ebuf = NULL;
|
||||||
|
|
||||||
|
if (thr_main() != 0)
|
||||||
|
ebuf = sig_ebuf;
|
||||||
|
else {
|
||||||
|
if (thr_once(&sig_init_once, sig_keycreate) != 0 ||
|
||||||
|
!sig_keycreated)
|
||||||
|
goto thr_err;
|
||||||
|
if ((ebuf = thr_getspecific(sig_key)) == NULL) {
|
||||||
|
if ((ebuf = malloc(sizeof(sig_ebuf))) == NULL)
|
||||||
|
goto thr_err;
|
||||||
|
if (thr_setspecific(sig_key, ebuf) != 0) {
|
||||||
|
free(ebuf);
|
||||||
|
ebuf = NULL;
|
||||||
|
goto thr_err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thr_err:
|
||||||
|
if (ebuf == NULL)
|
||||||
|
ebuf = sig_ebuf_err;
|
||||||
|
return (ebuf);
|
||||||
|
}
|
||||||
|
|
||||||
/* XXX: negative 'num' ? (REGR) */
|
/* XXX: negative 'num' ? (REGR) */
|
||||||
char *
|
char *
|
||||||
strsignal(int num)
|
strsignal(int num)
|
||||||
{
|
{
|
||||||
static char ebuf[NL_TEXTMAX];
|
char *ebuf;
|
||||||
char tmp[20];
|
char tmp[20];
|
||||||
size_t n;
|
size_t n;
|
||||||
int signum;
|
int signum;
|
||||||
@ -60,6 +102,8 @@ strsignal(int num)
|
|||||||
catd = catopen("libc", NL_CAT_LOCALE);
|
catd = catopen("libc", NL_CAT_LOCALE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
ebuf = sig_tlsalloc();
|
||||||
|
|
||||||
if (num > 0 && num < sys_nsig) {
|
if (num > 0 && num < sys_nsig) {
|
||||||
n = strlcpy(ebuf,
|
n = strlcpy(ebuf,
|
||||||
#if defined(NLS)
|
#if defined(NLS)
|
||||||
@ -67,7 +111,7 @@ strsignal(int num)
|
|||||||
#else
|
#else
|
||||||
sys_siglist[num],
|
sys_siglist[num],
|
||||||
#endif
|
#endif
|
||||||
sizeof(ebuf));
|
sizeof(sig_ebuf));
|
||||||
} else {
|
} else {
|
||||||
n = strlcpy(ebuf,
|
n = strlcpy(ebuf,
|
||||||
#if defined(NLS)
|
#if defined(NLS)
|
||||||
@ -75,7 +119,7 @@ strsignal(int num)
|
|||||||
#else
|
#else
|
||||||
UPREFIX,
|
UPREFIX,
|
||||||
#endif
|
#endif
|
||||||
sizeof(ebuf));
|
sizeof(sig_ebuf));
|
||||||
}
|
}
|
||||||
|
|
||||||
signum = num;
|
signum = num;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user