source: zzuf/trunk/src/load-signal.c @ 1532

Last change on this file since 1532 was 1532, checked in by Sam Hocevar, 16 years ago
  • Implemented signal handling.
  • Updated documentation accordingly and improved a few parts.
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: load-signal.c 1532 2007-01-01 21:35:54Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  load-signal.c: loaded signal functions
17 */
18
19#include "config.h"
20
21/* needed for sighandler_t */
22#define _GNU_SOURCE
23
24#if defined HAVE_STDINT_H
25#   include <stdint.h>
26#elif defined HAVE_INTTYPES_H
27#   include <inttypes.h>
28#endif
29#include <stdlib.h>
30#include <dlfcn.h>
31
32#include <string.h>
33#include <signal.h>
34
35#include "libzzuf.h"
36#include "debug.h"
37#include "fuzz.h"
38#include "load.h"
39
40/* Library functions that we divert */
41static sighandler_t (*signal_orig)    (int signum, sighandler_t handler);
42static int          (*sigaction_orig) (int signum, const struct sigaction *act,
43                                       struct sigaction *oldact);
44/* Local functions */
45static int isfatal(int signum);
46
47void _zz_load_signal(void)
48{
49    LOADSYM(signal);
50    LOADSYM(sigaction);
51}
52
53static int isfatal(int signum)
54{
55    switch(signum)
56    {
57        case SIGABRT:
58        case SIGFPE:
59        case SIGILL:
60        case SIGQUIT:
61        case SIGSEGV:
62        case SIGTRAP:
63#ifdef SIGSYS
64        case SIGSYS:
65#endif
66#ifdef SIGEMT
67        case SIGEMT:
68#endif
69#ifdef SIGBUS
70        case SIGBUS:
71#endif
72#ifdef SIGXCPU
73        case SIGXCPU:
74#endif
75#ifdef SIGXFSZ
76        case SIGXFSZ:
77#endif
78            return 1;
79        default:
80            return 0;
81    }
82}
83
84sighandler_t signal(int signum, sighandler_t handler)
85{
86    sighandler_t ret;
87
88    if(!_zz_ready)
89        LOADSYM(signal);
90
91    if(!_zz_signal)
92        return signal_orig(signum, handler);
93
94    ret = signal_orig(signum, isfatal(signum) ? SIG_DFL : handler);
95
96    debug("signal(%i, %p) = %p", signum, handler, ret);
97
98    return ret;
99}
100
101int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
102{
103    int ret;
104
105    if(!_zz_ready)
106        LOADSYM(sigaction);
107
108    if(!_zz_signal)
109        return sigaction_orig(signum, act, oldact);
110
111    if(act && isfatal(signum))
112    {
113        struct sigaction newact;
114        memcpy(&newact, act, sizeof(struct sigaction));
115        newact.sa_handler = SIG_DFL;
116        ret = sigaction_orig(signum, &newact, oldact);
117    }
118    else
119        ret = sigaction_orig(signum, act, oldact);
120
121    debug("sigaction(%i, %p, %p) = %i", signum, act, oldact, ret);
122
123    return ret;
124}
125
Note: See TracBrowser for help on using the repository browser.