Fix buffer allocation in files initgroups handler
[glibc.git] / stdio-common / psiginfo.c
bloba10780d05b436c2f917483d674dcc72a54cbd915
1 /* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <libintl.h>
21 #include <signal.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <not-cancel.h>
30 /* Defined in sys_siglist.c. */
31 extern const char *const _sys_siglist[];
32 extern const char *const _sys_siglist_internal[] attribute_hidden;
35 #define MF(l) MF1 (l)
36 #define MF1(l) str_##l
37 #define C(s1, s2) C1 (s1, s2)
38 #define C1(s1, s2) s1##s2
40 #define NOW SIGILL
41 #include "psiginfo-define.h"
43 #define NOW SIGFPE
44 #include "psiginfo-define.h"
46 #define NOW SIGSEGV
47 #include "psiginfo-define.h"
49 #define NOW SIGBUS
50 #include "psiginfo-define.h"
52 #define NOW SIGTRAP
53 #include "psiginfo-define.h"
55 #define NOW SIGCLD
56 #include "psiginfo-define.h"
58 #define NOW SIGPOLL
59 #include "psiginfo-define.h"
62 /* Print out on stderr a line consisting of the test in S, a colon, a space,
63 a message describing the meaning of the signal number PINFO and a newline.
64 If S is NULL or "", the colon and space are omitted. */
65 void
66 psiginfo (const siginfo_t *pinfo, const char *s)
68 char buf[512];
69 FILE *fp = fmemopen (buf, sizeof (buf), "w");
70 if (fp == NULL)
72 const char *colon;
74 if (s == NULL || *s == '\0')
75 s = colon = "";
76 else
77 colon = ": ";
79 __fxprintf (NULL, "%s%ssignal %d\n", s, colon, pinfo->si_signo);
80 return;
83 if (s != NULL && *s != '\0')
84 fprintf (fp, "%s: ", s);
86 const char *desc;
87 if (pinfo->si_signo >= 0 && pinfo->si_signo < NSIG
88 && ((desc = INTUSE(_sys_siglist)[pinfo->si_signo]) != NULL
89 #ifdef SIGRTMIN
90 || (pinfo->si_signo >= SIGRTMIN && pinfo->si_signo < SIGRTMAX)
91 #endif
94 #ifdef SIGRTMIN
95 if (desc == NULL)
97 if (pinfo->si_signo - SIGRTMIN < SIGRTMAX - pinfo->si_signo)
99 if (pinfo->si_signo == SIGRTMIN)
100 fprintf (fp, "SIGRTMIN (");
101 else
102 fprintf (fp, "SIGRTMIN+%d (", pinfo->si_signo - SIGRTMIN);
104 else
106 if (pinfo->si_signo == SIGRTMAX)
107 fprintf (fp, "SIGRTMAX (");
108 else
109 fprintf (fp, "SIGRTMAX-%d (", SIGRTMAX - pinfo->si_signo);
112 else
113 #endif
114 fprintf (fp, "%s (", _(desc));
116 const char *base = NULL;
117 const uint8_t *offarr = NULL;
118 size_t offarr_len = 0;
119 switch (pinfo->si_signo)
121 #define H(sig) \
122 case sig: \
123 base = C(codestrs_, sig).str; \
124 offarr = C (codes_, sig); \
125 offarr_len = sizeof (C (codes_, sig)) / sizeof (C (codes_, sig)[0]);\
126 break
128 H (SIGILL);
129 H (SIGFPE);
130 H (SIGSEGV);
131 H (SIGBUS);
132 H (SIGTRAP);
133 H (SIGCHLD);
134 H (SIGPOLL);
137 const char *str = NULL;
138 if (offarr != NULL
139 && pinfo->si_code >= 1 && pinfo->si_code <= offarr_len)
140 str = base + offarr[pinfo->si_code - 1];
141 else
142 switch (pinfo->si_code)
144 case SI_USER:
145 str = N_("Signal sent by kill()");
146 break;
147 case SI_QUEUE:
148 str = N_("Signal sent by sigqueue()");
149 break;
150 case SI_TIMER:
151 str = N_("Signal generated by the expiration of a timer");
152 break;
153 case SI_ASYNCIO:
154 str = N_("\
155 Signal generated by the completion of an asynchronous I/O request");
156 break;
157 case SI_MESGQ:
158 str = N_("\
159 Signal generated by the arrival of a message on an empty message queue");
160 break;
161 #ifdef SI_TKILL
162 case SI_TKILL:
163 str = N_("Signal sent by tkill()");
164 break;
165 #endif
166 #ifdef SI_ASYNCNL
167 case SI_ASYNCNL:
168 str = N_("\
169 Signal generated by the completion of an asynchronous name lookup request");
170 break;
171 #endif
172 #ifdef SI_SIGIO
173 case SI_SIGIO:
174 str = N_("\
175 Signal generated by the completion of an I/O request");
176 break;
177 #endif
178 #ifdef SI_KERNEL
179 case SI_KERNEL:
180 str = N_("Signal sent by the kernel");
181 break;
182 #endif
185 if (str != NULL)
186 fprintf (fp, "%s ", _(str));
187 else
188 fprintf (fp, "%d ", pinfo->si_code);
190 if (pinfo->si_signo == SIGILL || pinfo->si_signo == SIGFPE
191 || pinfo->si_signo == SIGSEGV || pinfo->si_signo == SIGBUS)
192 fprintf (fp, "[%p])\n", pinfo->si_addr);
193 else if (pinfo->si_signo == SIGCHLD)
194 fprintf (fp, "%ld %d %ld)\n",
195 (long int) pinfo->si_pid, pinfo->si_status,
196 (long int) pinfo->si_uid);
197 else if (pinfo->si_signo == SIGPOLL)
198 fprintf (fp, "%ld)\n", (long int) pinfo->si_band);
199 else
200 fprintf (fp, "%ld %ld)\n",
201 (long int) pinfo->si_pid, (long int) pinfo->si_uid);
203 else
204 fprintf (fp, _("Unknown signal %d\n"), pinfo->si_signo);
206 fclose (fp);
208 write_not_cancel (STDERR_FILENO, buf, strlen (buf));