xref: /freebsd/lib/libc/string/strsignal.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "namespace.h"
33 #if defined(NLS)
34 #include <nl_types.h>
35 #endif
36 #include <limits.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <signal.h>
41 #include "reentrant.h"
42 #include "un-namespace.h"
43 
44 #define	UPREFIX		"Unknown signal"
45 
46 static char		sig_ebuf[NL_TEXTMAX];
47 static char		sig_ebuf_err[NL_TEXTMAX];
48 static once_t		sig_init_once = ONCE_INITIALIZER;
49 static thread_key_t	sig_key;
50 static int		sig_keycreated = 0;
51 
52 static void
53 sig_keycreate(void)
54 {
55 	sig_keycreated = (thr_keycreate(&sig_key, free) == 0);
56 }
57 
58 static char *
59 sig_tlsalloc(void)
60 {
61 	char *ebuf = NULL;
62 
63 	if (thr_main() != 0)
64 		ebuf = sig_ebuf;
65 	else {
66 		if (thr_once(&sig_init_once, sig_keycreate) != 0 ||
67 		    !sig_keycreated)
68 			goto thr_err;
69 		if ((ebuf = thr_getspecific(sig_key)) == NULL) {
70 			if ((ebuf = malloc(sizeof(sig_ebuf))) == NULL)
71 				goto thr_err;
72 			if (thr_setspecific(sig_key, ebuf) != 0) {
73 				free(ebuf);
74 				ebuf = NULL;
75 				goto thr_err;
76 			}
77 		}
78 	}
79 thr_err:
80 	if (ebuf == NULL)
81 		ebuf = sig_ebuf_err;
82 	return (ebuf);
83 }
84 
85 /* XXX: negative 'num' ? (REGR) */
86 char *
87 strsignal(int num)
88 {
89 	char *ebuf;
90 	char tmp[20];
91 	size_t n;
92 	int signum;
93 	char *t, *p;
94 
95 #if defined(NLS)
96 	int saved_errno = errno;
97 	nl_catd catd;
98 	catd = catopen("libc", NL_CAT_LOCALE);
99 #endif
100 
101 	ebuf = sig_tlsalloc();
102 
103 	if (num > 0 && num < sys_nsig) {
104 		n = strlcpy(ebuf,
105 #if defined(NLS)
106 			catgets(catd, 2, num, sys_siglist[num]),
107 #else
108 			sys_siglist[num],
109 #endif
110 			sizeof(sig_ebuf));
111 	} else {
112 		n = strlcpy(ebuf,
113 #if defined(NLS)
114 			catgets(catd, 2, 0xffff, UPREFIX),
115 #else
116 			UPREFIX,
117 #endif
118 			sizeof(sig_ebuf));
119 
120 		signum = num;
121 		if (num < 0)
122 			signum = -signum;
123 
124 		t = tmp;
125 		do {
126 			*t++ = "0123456789"[signum % 10];
127 		} while (signum /= 10);
128 		if (num < 0)
129 			*t++ = '-';
130 
131 		p = (ebuf + n);
132 		*p++ = ':';
133 		*p++ = ' ';
134 
135 		for (;;) {
136 			*p++ = *--t;
137 			if (t <= tmp)
138 				break;
139 		}
140 		*p = '\0';
141 	}
142 
143 #if defined(NLS)
144 	catclose(catd);
145 	errno = saved_errno;
146 #endif
147 	return (ebuf);
148 }
149