1 /* Copyright (C) 1991, 1994-2002, 2005, 2008-2018 Free Software Foundation,
3 This file is part of the GNU C Library.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
33 # define _(msgid) gettext (msgid)
34 # define N_(msgid) gettext_noop (msgid)
38 # include <bits/libc-lock.h>
40 # include "glthread/lock.h"
41 # include "glthread/tls.h"
42 # define __libc_once_define(CLASS, NAME) gl_once_define (CLASS, NAME)
43 # define __libc_once(NAME, INIT) gl_once ((NAME), (INIT))
44 # define __libc_key_t gl_tls_key_t
45 # define __libc_getspecific(NAME) gl_tls_get ((NAME))
46 # define __libc_setspecific(NAME, POINTER) gl_tls_set ((NAME), (POINTER))
47 # define __snprintf snprintf
52 /* Defined in siglist.c. */
53 extern const char *const _sys_siglist
[];
54 extern const char *const _sys_siglist_internal
[] attribute_hidden
;
58 /* NetBSD declares sys_siglist in unistd.h. */
63 # define INTUSE(x) (x)
65 # if HAVE_DECL_SYS_SIGLIST
67 # define _sys_siglist sys_siglist
68 # else /* !HAVE_DECL_SYS_SIGLIST */
72 # if !HAVE_DECL__SYS_SIGLIST
73 static const char *_sys_siglist
[NSIG
];
75 # endif /* !HAVE_DECL_SYS_SIGLIST */
79 static __libc_key_t key
;
81 /* If nonzero the key allocation failed and we should better use a
82 static buffer than fail. */
84 static char local_buf
[BUFFERSIZ
];
85 static char *static_buf
;
87 /* Destructor for the thread-specific data. */
88 static void init (void);
89 static void free_key_mem (void *mem
);
90 static char *getbuffer (void);
93 /* Return a string describing the meaning of the signal number SIGNUM. */
95 strsignal (int signum
)
98 __libc_once_define (static, once
);
100 /* If we have not yet initialized the buffer do it now. */
101 __libc_once (once
, init
);
105 (signum
>= SIGRTMIN
&& signum
<= SIGRTMAX
) ||
107 signum
< 0 || signum
>= NSIG
108 || (desc
= INTUSE(_sys_siglist
)[signum
]) == NULL
)
110 char *buffer
= getbuffer ();
113 if (signum
>= SIGRTMIN
&& signum
<= SIGRTMAX
)
114 len
= __snprintf (buffer
, BUFFERSIZ
- 1, _("Real-time signal %d"),
115 signum
- (int) SIGRTMIN
);
118 len
= __snprintf (buffer
, BUFFERSIZ
- 1, _("Unknown signal %d"),
120 if (len
>= BUFFERSIZ
)
128 return (char *) _(desc
);
132 /* Initialize buffer. */
137 if (__libc_key_create (&key
, free_key_mem
))
138 /* Creating the key failed. This means something really went
139 wrong. In any case use a static buffer which is better than
141 static_buf
= local_buf
;
143 gl_tls_key_init (key
, free_key_mem
);
145 # if !HAVE_DECL_SYS_SIGLIST
146 memset (_sys_siglist
, 0, NSIG
* sizeof *_sys_siglist
);
148 /* No need to use a do {} while (0) here since init_sig(...) must expand
149 to a complete statement. (We cannot use the ISO C99 designated array
150 initializer syntax since it is not supported by ANSI C compilers and
151 since some signal numbers might exceed NSIG.) */
152 # define init_sig(sig, abbrev, desc) \
153 if (sig >= 0 && sig < NSIG) \
154 _sys_siglist[sig] = desc;
156 # include "siglist.h"
160 # endif /* !HAVE_DECL_SYS_SIGLIST */
165 /* Free the thread specific data, this is done if a thread terminates. */
167 free_key_mem (void *mem
)
170 __libc_setspecific (key
, NULL
);
174 /* Return the buffer to be used. */
180 if (static_buf
!= NULL
)
184 /* We don't use the static buffer and so we have a key. Use it
185 to get the thread-specific buffer. */
186 result
= __libc_getspecific (key
);
189 /* No buffer allocated so far. */
190 result
= malloc (BUFFERSIZ
);
192 /* No more memory available. We use the static buffer. */
195 __libc_setspecific (key
, result
);