source: zzuf/trunk/src/lib-signal.c @ 1695

Last change on this file since 1695 was 1695, checked in by Sam Hocevar, 15 years ago
  • Added enough autoconf checks and #ifdefs so that libzzuf builds on Win32. zzuf itself doesn't build yet, though.
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: lib-signal.c 1695 2007-01-18 00:21:07Z 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
31#include <string.h>
32#include <signal.h>
33
34#include "libzzuf.h"
35#include "lib-load.h"
36#include "debug.h"
37#include "fuzz.h"
38
39#if defined HAVE_SIGHANDLER_T
40#   define SIG_T sighandler_t
41#elif defined HAVE_SIG_T
42#   define SIG_T sig_t
43#else
44    typedef void (*SIG_T) (int);
45#endif
46
47/* Library functions that we divert */
48static SIG_T (*signal_orig)    (int signum, SIG_T handler);
49#if defined HAVE_SIGACTION
50static int   (*sigaction_orig) (int signum, const struct sigaction *act,
51                                struct sigaction *oldact);
52#endif
53/* Local functions */
54static int isfatal(int signum);
55
56static int isfatal(int signum)
57{
58    switch(signum)
59    {
60        case SIGABRT:
61        case SIGFPE:
62        case SIGILL:
63#if defined SIGQUIT
64        case SIGQUIT:
65#endif
66        case SIGSEGV:
67#if defined SIGTRAP
68        case SIGTRAP:
69#endif
70#if defined SIGSYS
71        case SIGSYS:
72#endif
73#if defined SIGEMT
74        case SIGEMT:
75#endif
76#if defined SIGBUS
77        case SIGBUS:
78#endif
79#if defined SIGXCPU
80        case SIGXCPU:
81#endif
82#if defined SIGXFSZ
83        case SIGXFSZ:
84#endif
85            return 1;
86        default:
87            return 0;
88    }
89}
90
91SIG_T signal(int signum, SIG_T handler)
92{
93    SIG_T ret;
94
95    LOADSYM(signal);
96
97    if(!_zz_signal)
98        return signal_orig(signum, handler);
99
100    ret = signal_orig(signum, isfatal(signum) ? SIG_DFL : handler);
101
102    debug("%s(%i, %p) = %p", __func__, signum, handler, ret);
103
104    return ret;
105}
106
107#if defined HAVE_SIGACTION
108int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
109{
110    int ret;
111
112    LOADSYM(sigaction);
113
114    if(!_zz_signal)
115        return sigaction_orig(signum, act, oldact);
116
117    if(act && isfatal(signum))
118    {
119        struct sigaction newact;
120        memcpy(&newact, act, sizeof(struct sigaction));
121        newact.sa_handler = SIG_DFL;
122        ret = sigaction_orig(signum, &newact, oldact);
123    }
124    else
125        ret = sigaction_orig(signum, act, oldact);
126
127    debug("%s(%i, %p, %p) = %i", __func__, signum, act, oldact, ret);
128
129    return ret;
130}
131#endif
132
Note: See TracBrowser for help on using the repository browser.