1/* Copyright (C) 2009-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <libintl.h>
20#include <signal.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <not-cancel.h>
27
28
29#define MF(l) MF1 (l)
30#define MF1(l) str_##l
31#define C(s1, s2) C1 (s1, s2)
32#define C1(s1, s2) s1##s2
33
34#define NOW SIGILL
35#include "psiginfo-define.h"
36
37#define NOW SIGFPE
38#include "psiginfo-define.h"
39
40#define NOW SIGSEGV
41#include "psiginfo-define.h"
42
43#define NOW SIGBUS
44#include "psiginfo-define.h"
45
46#define NOW SIGTRAP
47#include "psiginfo-define.h"
48
49#define NOW SIGCLD
50#include "psiginfo-define.h"
51
52#define NOW SIGPOLL
53#include "psiginfo-define.h"
54
55
56/* Print out on stderr a line consisting of the test in S, a colon, a space,
57 a message describing the meaning of the signal number PINFO and a newline.
58 If S is NULL or "", the colon and space are omitted. */
59void
60psiginfo (const siginfo_t *pinfo, const char *s)
61{
62 char buf[512];
63 FILE *fp = __fmemopen (buf, sizeof (buf), "w");
64 if (fp == NULL)
65 {
66 const char *colon;
67
68 if (s == NULL || *s == '\0')
69 s = colon = "";
70 else
71 colon = ": ";
72
73 __fxprintf (NULL, "%s%ssignal %d\n", s, colon, pinfo->si_signo);
74 return;
75 }
76
77 if (s != NULL && *s != '\0')
78 fprintf (fp, "%s: ", s);
79
80 const char *desc;
81 if (pinfo->si_signo >= 0 && pinfo->si_signo < NSIG
82 && ((desc = _sys_siglist[pinfo->si_signo]) != NULL
83#ifdef SIGRTMIN
84 || (pinfo->si_signo >= SIGRTMIN && pinfo->si_signo < SIGRTMAX)
85#endif
86 ))
87 {
88#ifdef SIGRTMIN
89 if (desc == NULL)
90 {
91 if (pinfo->si_signo - SIGRTMIN < SIGRTMAX - pinfo->si_signo)
92 {
93 if (pinfo->si_signo == SIGRTMIN)
94 fprintf (fp, "SIGRTMIN (");
95 else
96 fprintf (fp, "SIGRTMIN+%d (", pinfo->si_signo - SIGRTMIN);
97 }
98 else
99 {
100 if (pinfo->si_signo == SIGRTMAX)
101 fprintf (fp, "SIGRTMAX (");
102 else
103 fprintf (fp, "SIGRTMAX-%d (", SIGRTMAX - pinfo->si_signo);
104 }
105 }
106 else
107#endif
108 fprintf (fp, "%s (", _(desc));
109
110 const char *base = NULL;
111 const uint8_t *offarr = NULL;
112 size_t offarr_len = 0;
113 switch (pinfo->si_signo)
114 {
115#define H(sig) \
116 case sig: \
117 base = C(codestrs_, sig).str; \
118 offarr = C (codes_, sig); \
119 offarr_len = sizeof (C (codes_, sig)) / sizeof (C (codes_, sig)[0]);\
120 break
121
122 H (SIGILL);
123 H (SIGFPE);
124 H (SIGSEGV);
125 H (SIGBUS);
126 H (SIGTRAP);
127 H (SIGCHLD);
128 H (SIGPOLL);
129 }
130
131 const char *str = NULL;
132 if (offarr != NULL
133 && pinfo->si_code >= 1 && pinfo->si_code <= offarr_len)
134 str = base + offarr[pinfo->si_code - 1];
135 else
136 switch (pinfo->si_code)
137 {
138 case SI_USER:
139 str = N_("Signal sent by kill()");
140 break;
141 case SI_QUEUE:
142 str = N_("Signal sent by sigqueue()");
143 break;
144 case SI_TIMER:
145 str = N_("Signal generated by the expiration of a timer");
146 break;
147 case SI_ASYNCIO:
148 str = N_("\
149Signal generated by the completion of an asynchronous I/O request");
150 break;
151 case SI_MESGQ:
152 str = N_("\
153Signal generated by the arrival of a message on an empty message queue");
154 break;
155#ifdef SI_TKILL
156 case SI_TKILL:
157 str = N_("Signal sent by tkill()");
158 break;
159#endif
160#ifdef SI_ASYNCNL
161 case SI_ASYNCNL:
162 str = N_("\
163Signal generated by the completion of an asynchronous name lookup request");
164 break;
165#endif
166#ifdef SI_SIGIO
167 case SI_SIGIO:
168 str = N_("\
169Signal generated by the completion of an I/O request");
170 break;
171#endif
172#ifdef SI_KERNEL
173 case SI_KERNEL:
174 str = N_("Signal sent by the kernel");
175 break;
176#endif
177 }
178
179 if (str != NULL)
180 fprintf (fp, "%s ", _(str));
181 else
182 fprintf (fp, "%d ", pinfo->si_code);
183
184 if (pinfo->si_signo == SIGILL || pinfo->si_signo == SIGFPE
185 || pinfo->si_signo == SIGSEGV || pinfo->si_signo == SIGBUS)
186 fprintf (fp, "[%p])\n", pinfo->si_addr);
187 else if (pinfo->si_signo == SIGCHLD)
188 fprintf (fp, "%ld %d %ld)\n",
189 (long int) pinfo->si_pid, pinfo->si_status,
190 (long int) pinfo->si_uid);
191 else if (pinfo->si_signo == SIGPOLL)
192 fprintf (fp, "%ld)\n", (long int) pinfo->si_band);
193 else
194 fprintf (fp, "%ld %ld)\n",
195 (long int) pinfo->si_pid, (long int) pinfo->si_uid);
196 }
197 else
198 fprintf (fp, _("Unknown signal %d\n"), pinfo->si_signo);
199
200 fclose (fp);
201
202 write_not_cancel (STDERR_FILENO, buf, strlen (buf));
203}
204