1 /* Copyright (C) 1991, 1994-2002, 2005, 2008-2011 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 <http://www.gnu.org/licenses/>. */
31 # define _(msgid) gettext (msgid)
32 # define N_(msgid) gettext_noop (msgid)
36 # include <bits/libc-lock.h>
38 # include "glthread/lock.h"
39 # include "glthread/tls.h"
40 # define __libc_once_define(CLASS, NAME) gl_once_define (CLASS, NAME)
41 # define __libc_once(NAME, INIT) gl_once ((NAME), (INIT))
42 # define __libc_key_t gl_tls_key_t
43 # define __libc_getspecific(NAME) gl_tls_get ((NAME))
44 # define __libc_setspecific(NAME, POINTER) gl_tls_set ((NAME), (POINTER))
45 # define __snprintf snprintf
50 /* Defined in siglist.c. */
51 extern const char *const _sys_siglist[];
52 extern const char *const _sys_siglist_internal[] attribute_hidden;
56 /* NetBSD declares sys_siglist in unistd.h. */
59 # define INTUSE(x) (x)
61 # if HAVE_DECL_SYS_SIGLIST
63 # define _sys_siglist sys_siglist
64 # else /* !HAVE_DECL_SYS_SIGLIST */
68 # if !HAVE_DECL__SYS_SIGLIST
69 static const char *_sys_siglist[NSIG];
71 # endif /* !HAVE_DECL_SYS_SIGLIST */
75 static __libc_key_t key;
77 /* If nonzero the key allocation failed and we should better use a
78 static buffer than fail. */
80 static char local_buf[BUFFERSIZ];
81 static char *static_buf;
83 /* Destructor for the thread-specific data. */
84 static void init (void);
85 static void free_key_mem (void *mem);
86 static char *getbuffer (void);
89 /* Return a string describing the meaning of the signal number SIGNUM. */
91 strsignal (int signum)
94 __libc_once_define (static, once);
96 /* If we have not yet initialized the buffer do it now. */
97 __libc_once (once, init);
101 (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
103 signum < 0 || signum >= NSIG
104 || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
106 char *buffer = getbuffer ();
109 if (signum >= SIGRTMIN && signum <= SIGRTMAX)
110 len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
111 signum - (int) SIGRTMIN);
114 len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
116 if (len >= BUFFERSIZ)
124 return (char *) _(desc);
128 /* Initialize buffer. */
133 if (__libc_key_create (&key, free_key_mem))
134 /* Creating the key failed. This means something really went
135 wrong. In any case use a static buffer which is better than
137 static_buf = local_buf;
139 gl_tls_key_init (key, free_key_mem);
141 # if !HAVE_DECL_SYS_SIGLIST
142 memset (_sys_siglist, 0, NSIG * sizeof *_sys_siglist);
144 /* No need to use a do {} while (0) here since init_sig(...) must expand
145 to a complete statement. (We cannot use the ISO C99 designated array
146 initializer syntax since it is not supported by ANSI C compilers and
147 since some signal numbers might exceed NSIG.) */
148 # define init_sig(sig, abbrev, desc) \
149 if (sig >= 0 && sig < NSIG) \
150 _sys_siglist[sig] = desc;
152 # include "siglist.h"
156 # endif /* !HAVE_DECL_SYS_SIGLIST */
161 /* Free the thread specific data, this is done if a thread terminates. */
163 free_key_mem (void *mem)
166 __libc_setspecific (key, NULL);
170 /* Return the buffer to be used. */
176 if (static_buf != NULL)
180 /* We don't use the static buffer and so we have a key. Use it
181 to get the thread-specific buffer. */
182 result = __libc_getspecific (key);
185 /* No buffer allocated so far. */
186 result = malloc (BUFFERSIZ);
188 /* No more memory available. We use the static buffer. */
191 __libc_setspecific (key, result);