summaryrefslogtreecommitdiff
path: root/e2fsck/sigcatcher.c
blob: 10b93287d5b2b4da1fb43d34fafd848d06c01645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * sigcatcher.c --- print a backtrace on a SIGSEGV, et. al
 *
 * Copyright (C) 2011 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif

#include "e2fsck.h"

struct str_table {
	int	num;
	const char	*name;
};

#define DEFINE_ENTRY(SYM)	{ SYM, #SYM },
#define END_TABLE		{ 0, 0 }

static struct str_table sig_table[] = {
#ifdef SIGHUP
	DEFINE_ENTRY(SIGHUP)
#endif
#ifdef SIGINT
	DEFINE_ENTRY(SIGINT)
#endif
#ifdef SIGQUIT
	DEFINE_ENTRY(SIGQUIT)
#endif
#ifdef SIGILL
	DEFINE_ENTRY(SIGILL)
#endif
#ifdef SIGTRAP
	DEFINE_ENTRY(SIGTRAP)
#endif
#ifdef SIGABRT
	DEFINE_ENTRY(SIGABRT)
#endif
#ifdef SIGIOT
	DEFINE_ENTRY(SIGIOT)
#endif
#ifdef SIGBUS
	DEFINE_ENTRY(SIGBUS)
#endif
#ifdef SIGFPE
	DEFINE_ENTRY(SIGFPE)
#endif
#ifdef SIGKILL
	DEFINE_ENTRY(SIGKILL)
#endif
#ifdef SIGUSR1
	DEFINE_ENTRY(SIGUSR1)
#endif
#ifdef SIGSEGV
	DEFINE_ENTRY(SIGSEGV)
#endif
#ifdef SIGUSR2
	DEFINE_ENTRY(SIGUSR2)
#endif
#ifdef SIGPIPE
	DEFINE_ENTRY(SIGPIPE)
#endif
#ifdef SIGALRM
	DEFINE_ENTRY(SIGALRM)
#endif
#ifdef SIGTERM
	DEFINE_ENTRY(SIGTERM)
#endif
#ifdef SIGSTKFLT
	DEFINE_ENTRY(SIGSTKFLT)
#endif
#ifdef SIGCHLD
	DEFINE_ENTRY(SIGCHLD)
#endif
#ifdef SIGCONT
	DEFINE_ENTRY(SIGCONT)
#endif
#ifdef SIGSTOP
	DEFINE_ENTRY(SIGSTOP)
#endif
#ifdef SIGTSTP
	DEFINE_ENTRY(SIGTSTP)
#endif
#ifdef SIGTTIN
	DEFINE_ENTRY(SIGTTIN)
#endif
#ifdef SIGTTOU
	DEFINE_ENTRY(SIGTTOU)
#endif
#ifdef SIGURG
	DEFINE_ENTRY(SIGURG)
#endif
#ifdef SIGXCPU
	DEFINE_ENTRY(SIGXCPU)
#endif
#ifdef SIGXFSZ
	DEFINE_ENTRY(SIGXFSZ)
#endif
#ifdef SIGVTALRM
	DEFINE_ENTRY(SIGVTALRM)
#endif
#ifdef SIGPROF
	DEFINE_ENTRY(SIGPROF)
#endif
#ifdef SIGWINCH
	DEFINE_ENTRY(SIGWINCH)
#endif
#ifdef SIGIO
	DEFINE_ENTRY(SIGIO)
#endif
#ifdef SIGPOLL
	DEFINE_ENTRY(SIGPOLL)
#endif
#ifdef SIGPWR
	DEFINE_ENTRY(SIGPWR)
#endif
#ifdef SIGSYS
	DEFINE_ENTRY(SIGSYS)
#endif
	END_TABLE
};

static struct str_table generic_code_table[] = {
#ifdef SI_ASYNCNL
	DEFINE_ENTRY(SI_ASYNCNL)
#endif
#ifdef SI_TKILL
	DEFINE_ENTRY(SI_TKILL)
#endif
#ifdef SI_SIGIO
	DEFINE_ENTRY(SI_SIGIO)
#endif
#ifdef SI_ASYNCIO
	DEFINE_ENTRY(SI_ASYNCIO)
#endif
#ifdef SI_MESGQ
	DEFINE_ENTRY(SI_MESGQ)
#endif
#ifdef SI_TIMER
	DEFINE_ENTRY(SI_TIMER)
#endif
#ifdef SI_QUEUE
	DEFINE_ENTRY(SI_QUEUE)
#endif
#ifdef SI_USER
	DEFINE_ENTRY(SI_USER)
#endif
#ifdef SI_KERNEL
	DEFINE_ENTRY(SI_KERNEL)
#endif
	END_TABLE
};

static struct str_table sigill_code_table[] = {
#ifdef ILL_ILLOPC
	DEFINE_ENTRY(ILL_ILLOPC)
#endif
#ifdef ILL_ILLOPN
	DEFINE_ENTRY(ILL_ILLOPN)
#endif
#ifdef ILL_ILLADR
	DEFINE_ENTRY(ILL_ILLADR)
#endif
#ifdef ILL_ILLTRP
	DEFINE_ENTRY(ILL_ILLTRP)
#endif
#ifdef ILL_PRVOPC
	DEFINE_ENTRY(ILL_PRVOPC)
#endif
#ifdef ILL_PRVREG
	DEFINE_ENTRY(ILL_PRVREG)
#endif
#ifdef ILL_COPROC
	DEFINE_ENTRY(ILL_COPROC)
#endif
#ifdef ILL_BADSTK
	DEFINE_ENTRY(ILL_BADSTK)
#endif
#ifdef BUS_ADRALN
	DEFINE_ENTRY(BUS_ADRALN)
#endif
#ifdef BUS_ADRERR
	DEFINE_ENTRY(BUS_ADRERR)
#endif
#ifdef BUS_OBJERR
	DEFINE_ENTRY(BUS_OBJERR)
#endif
	END_TABLE
};

static struct str_table sigfpe_code_table[] = {
#ifdef FPE_INTDIV
	DEFINE_ENTRY(FPE_INTDIV)
#endif
#ifdef FPE_INTOVF
	DEFINE_ENTRY(FPE_INTOVF)
#endif
#ifdef FPE_FLTDIV
	DEFINE_ENTRY(FPE_FLTDIV)
#endif
#ifdef FPE_FLTOVF
	DEFINE_ENTRY(FPE_FLTOVF)
#endif
#ifdef FPE_FLTUND
	DEFINE_ENTRY(FPE_FLTUND)
#endif
#ifdef FPE_FLTRES
	DEFINE_ENTRY(FPE_FLTRES)
#endif
#ifdef FPE_FLTINV
	DEFINE_ENTRY(FPE_FLTINV)
#endif
#ifdef FPE_FLTSUB
	DEFINE_ENTRY(FPE_FLTSUB)
#endif
	END_TABLE
};

static struct str_table sigsegv_code_table[] = {
#ifdef SEGV_MAPERR
	DEFINE_ENTRY(SEGV_MAPERR)
#endif
#ifdef SEGV_ACCERR
	DEFINE_ENTRY(SEGV_ACCERR)
#endif
	END_TABLE
};


static struct str_table sigbus_code_table[] = {
#ifdef BUS_ADRALN
	DEFINE_ENTRY(BUS_ADRALN)
#endif
#ifdef BUS_ADRERR
	DEFINE_ENTRY(BUS_ADRERR)
#endif
#ifdef BUS_OBJERR
	DEFINE_ENTRY(BUS_OBJERR)
#endif
	END_TABLE
};

static struct str_table sigstrap_code_table[] = {
#ifdef TRAP_BRKPT
	DEFINE_ENTRY(TRAP_BRKPT)
#endif
#ifdef TRAP_TRACE
	DEFINE_ENTRY(TRAP_TRACE)
#endif
	END_TABLE
};

static struct str_table sigcld_code_table[] = {
#ifdef CLD_EXITED
	DEFINE_ENTRY(CLD_EXITED)
#endif
#ifdef CLD_KILLED
	DEFINE_ENTRY(CLD_KILLED)
#endif
#ifdef CLD_DUMPED
	DEFINE_ENTRY(CLD_DUMPED)
#endif
#ifdef CLD_TRAPPED
	DEFINE_ENTRY(CLD_TRAPPED)
#endif
#ifdef CLD_STOPPED
	DEFINE_ENTRY(CLD_STOPPED)
#endif
#ifdef CLD_CONTINUED
	DEFINE_ENTRY(CLD_CONTINUED)
#endif
	END_TABLE
};

static struct str_table sigpoll_code_table[] = {
#ifdef POLL_IN
	DEFINE_ENTRY(POLL_IN)
#endif
#ifdef POLL_OUT
	DEFINE_ENTRY(POLL_OUT)
#endif
#ifdef POLL_MSG
	DEFINE_ENTRY(POLL_MSG)
#endif
#ifdef POLL_ERR
	DEFINE_ENTRY(POLL_ERR)
#endif
#ifdef POLL_PRI
	DEFINE_ENTRY(POLL_PRI)
#endif
#ifdef POLL_HUP
	DEFINE_ENTRY(POLL_HUP)
#endif
	END_TABLE
};

static const char *lookup_table(int num, struct str_table *table)
{
	struct str_table *p;

	for (p=table; p->name; p++)
		if (num == p->num)
			return(p->name);
	return NULL;
}

static const char *lookup_table_fallback(int num, struct str_table *table)
{
	static char buf[32];
	const char *ret = lookup_table(num, table);

	if (ret)
		return ret;
	snprintf(buf, sizeof(buf), "%d", num);
	buf[sizeof(buf)-1] = 0;
	return buf;
}

static void die_signal_handler(int signum, siginfo_t *siginfo, void *context)
{
       void *stack_syms[32];
       int frames;
       const char *cp;

       fprintf(stderr, "Signal (%d) %s ", signum,
	       lookup_table_fallback(signum, sig_table));
       if (siginfo->si_code == SI_USER)
	       fprintf(stderr, "(sent from pid %u) ", siginfo->si_pid);
       cp = lookup_table(siginfo->si_code, generic_code_table);
       if (cp)
	       fprintf(stderr, "si_code=%s ", cp);
       else if (signum == SIGILL)
	       fprintf(stderr, "si_code=%s ",
		       lookup_table_fallback(siginfo->si_code,
					     sigill_code_table));
       else if (signum == SIGFPE)
	       fprintf(stderr, "si_code=%s ",
		       lookup_table_fallback(siginfo->si_code,
					     sigfpe_code_table));
       else if (signum == SIGSEGV)
	       fprintf(stderr, "si_code=%s ",
		       lookup_table_fallback(siginfo->si_code,
					     sigsegv_code_table));
       else if (signum == SIGBUS)
	       fprintf(stderr, "si_code=%s ",
		       lookup_table_fallback(siginfo->si_code,
					     sigbus_code_table));
       else if (signum == SIGCHLD)
	       fprintf(stderr, "si_code=%s ",
		       lookup_table_fallback(siginfo->si_code,
					     sigcld_code_table));
       else
	       fprintf(stderr, "si code=%d ", siginfo->si_code);
       if ((siginfo->si_code != SI_USER) &&
	   (signum == SIGILL || signum == SIGFPE ||
	    signum == SIGSEGV || signum == SIGBUS))
	       fprintf(stderr, "fault addr=%p", siginfo->si_addr);
       fprintf(stderr, "\n");

#ifdef HAVE_BACKTRACE
       frames = backtrace(stack_syms, 32);
       backtrace_symbols_fd(stack_syms, frames, 2);
#endif
       exit(FSCK_ERROR);
}

void sigcatcher_setup(void)
{
	struct sigaction	sa;
	
	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = die_signal_handler;
	sa.sa_flags = SA_SIGINFO;

	sigaction(SIGFPE, &sa, 0);
	sigaction(SIGILL, &sa, 0);
	sigaction(SIGBUS, &sa, 0);
	sigaction(SIGSEGV, &sa, 0);
}	


#ifdef DEBUG
#include <getopt.h>

void usage(void)
{
	fprintf(stderr, "tst_sigcatcher: [-akfn]\n");
	exit(1);
}

int main(int argc, char** argv)
{
	struct sigaction	sa;
	char			*p = 0;
	int 			i, c;
	volatile		x=0;

	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = die_signal_handler;
	sa.sa_flags = SA_SIGINFO;
	for (i=1; i < 31; i++)
		sigaction(i, &sa, 0);

	while ((c = getopt (argc, argv, "afkn")) != EOF)
		switch (c) {
		case 'a':
			abort();
			break;
		case 'f':
			printf("%d\n", 42/x);
		case 'k':
			kill(getpid(), SIGTERM);
			break;
		case 'n':
			*p = 42;
		default:
			usage ();
		}

	printf("Sleeping for 10 seconds, send kill signal to pid %u...\n",
	       getpid());
	fflush(stdout);
	sleep(10);
	exit(0);
}
#endif