Remove a spurious const in tst-fmemopen.
[platform/upstream/glibc.git] / stdio-common / psiginfo.c
1 /* Copyright (C) 2009, 2010 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 /* Defined in sys_siglist.c.  */
30 extern const char *const _sys_siglist[];
31 extern const char *const _sys_siglist_internal[] attribute_hidden;
32
33
34 #define MF(l) MF1 (l)
35 #define MF1(l) str_##l
36 #define C(s1, s2) C1 (s1, s2)
37 #define C1(s1, s2) s1##s2
38
39 #define NOW SIGILL
40 #include "psiginfo-define.h"
41
42 #define NOW SIGFPE
43 #include "psiginfo-define.h"
44
45 #define NOW SIGSEGV
46 #include "psiginfo-define.h"
47
48 #define NOW SIGBUS
49 #include "psiginfo-define.h"
50
51 #define NOW SIGTRAP
52 #include "psiginfo-define.h"
53
54 #define NOW SIGCLD
55 #include "psiginfo-define.h"
56
57 #define NOW SIGPOLL
58 #include "psiginfo-define.h"
59
60
61 /* Print out on stderr a line consisting of the test in S, a colon, a space,
62    a message describing the meaning of the signal number PINFO and a newline.
63    If S is NULL or "", the colon and space are omitted.  */
64 void
65 psiginfo (const siginfo_t *pinfo, const char *s)
66 {
67   char buf[512];
68   FILE *fp = fmemopen (buf, sizeof (buf), "w");
69   if (fp == NULL)
70     {
71       const char *colon;
72
73       if (s == NULL || *s == '\0')
74         s = colon = "";
75       else
76         colon = ": ";
77
78       __fxprintf (NULL, "%s%ssignal %d\n", s, colon, pinfo->si_signo);
79       return;
80     }
81
82   if (s != NULL && *s != '\0')
83     fprintf (fp, "%s: ", s);
84
85   const char *desc;
86   if (pinfo->si_signo >= 0 && pinfo->si_signo < NSIG
87       && ((desc = INTUSE(_sys_siglist)[pinfo->si_signo]) != NULL
88 #ifdef SIGRTMIN
89           || (pinfo->si_signo >= SIGRTMIN && pinfo->si_signo < SIGRTMAX)
90 #endif
91          ))
92     {
93 #ifdef SIGRTMIN
94       if (desc == NULL)
95         {
96           if (pinfo->si_signo - SIGRTMIN < SIGRTMAX - pinfo->si_signo)
97             {
98               if (pinfo->si_signo == SIGRTMIN)
99                 fprintf (fp, "SIGRTMIN (");
100               else
101                 fprintf (fp, "SIGRTMIN+%d (", pinfo->si_signo - SIGRTMIN);
102             }
103           else
104             {
105               if (pinfo->si_signo == SIGRTMAX)
106                 fprintf (fp, "SIGRTMAX (");
107               else
108                 fprintf (fp, "SIGRTMAX-%d (", SIGRTMAX - pinfo->si_signo);
109             }
110         }
111       else
112 #endif
113         fprintf (fp, "%s (", _(desc));
114
115       const char *base = NULL;
116       const uint8_t *offarr = NULL;
117       size_t offarr_len = 0;
118       switch (pinfo->si_signo)
119         {
120 #define H(sig) \
121         case sig:                                                             \
122           base = C(codestrs_, sig).str;                                       \
123           offarr = C (codes_, sig);                                           \
124           offarr_len = sizeof (C (codes_, sig)) / sizeof (C (codes_, sig)[0]);\
125           break
126
127           H (SIGILL);
128           H (SIGFPE);
129           H (SIGSEGV);
130           H (SIGBUS);
131           H (SIGTRAP);
132           H (SIGCHLD);
133           H (SIGPOLL);
134         }
135
136       const char *str = NULL;
137       if (offarr != NULL
138           && pinfo->si_code >= 1 && pinfo->si_code <= offarr_len)
139         str = base + offarr[pinfo->si_code - 1];
140       else
141         switch (pinfo->si_code)
142           {
143           case SI_USER:
144             str = N_("Signal sent by kill()");
145             break;
146           case SI_QUEUE:
147             str = N_("Signal sent by sigqueue()");
148             break;
149           case SI_TIMER:
150             str = N_("Signal generated by the expiration of a timer");
151             break;
152           case SI_ASYNCIO:
153             str = N_("\
154 Signal generated by the completion of an asynchronous I/O request");
155             break;
156           case SI_MESGQ:
157             str = N_("\
158 Signal generated by the arrival of a message on an empty message queue");
159             break;
160 #ifdef SI_TKILL
161           case SI_TKILL:
162             str = N_("Signal sent by tkill()");
163             break;
164 #endif
165 #ifdef SI_ASYNCNL
166           case SI_ASYNCNL:
167             str = N_("\
168 Signal generated by the completion of an asynchronous name lookup request");
169             break;
170 #endif
171 #ifdef SI_SIGIO
172           case SI_SIGIO:
173             str = N_("\
174 Signal generated by the completion of an I/O request");
175             break;
176 #endif
177 #ifdef SI_KERNEL
178           case SI_KERNEL:
179             str = N_("Signal sent by the kernel");
180             break;
181 #endif
182           }
183
184       if (str != NULL)
185         fprintf (fp, "%s ", _(str));
186       else
187         fprintf (fp, "%d ", pinfo->si_code);
188
189       if (pinfo->si_signo == SIGILL || pinfo->si_signo == SIGFPE
190           || pinfo->si_signo == SIGSEGV || pinfo->si_signo == SIGBUS)
191         fprintf (fp, "[%p])\n", pinfo->si_addr);
192       else if (pinfo->si_signo == SIGCHLD)
193         fprintf (fp, "%ld %d %ld)\n",
194                  (long int) pinfo->si_pid, pinfo->si_status,
195                  (long int) pinfo->si_uid);
196       else if (pinfo->si_signo == SIGPOLL)
197         fprintf (fp, "%ld)\n", (long int) pinfo->si_band);
198       else
199         fprintf (fp, "%ld %ld)\n",
200                  (long int) pinfo->si_pid, (long int) pinfo->si_uid);
201     }
202   else
203     fprintf (fp, _("Unknown signal %d\n"),  pinfo->si_signo);
204
205   fclose (fp);
206
207   write_not_cancel (STDERR_FILENO, buf, strlen (buf));
208 }