[go: up one dir, main page]

File: signal.c

package info (click to toggle)
libforms 1.2.3-1.7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,904 kB
  • sloc: ansic: 97,669; sh: 11,156; makefile: 951
file content (248 lines) | stat: -rw-r--r-- 5,711 bytes parent folder | download | duplicates (5)
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
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file signal.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao
 *  All rights reserved.
 *
 *  Simple minded quick&dirty signal handling.
 *
 *  Only permit a specific signal to have one handler
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include "flinternal.h"
#include <stdlib.h>
#include <signal.h>


void ( * fli_handle_signal )( void ) = NULL;   /* also needed in handling.c */


/***************************************
 ***************************************/

static void
handle_signal( void )
{
    FLI_SIGNAL_REC *rec = fli_context->signal_rec;

    for ( ; rec; rec = rec->next )
        while ( rec->caught )
        {
            rec->caught--;
            rec->callback( rec->signum, rec->data );
        }
}


#ifndef FL_WIN32
#define IsDangerous( s ) (    ( s ) == SIGBUS   \
                           || ( s ) == SIGSEGV  \
                           || ( s ) == SIGILL   \
                           || ( s ) == SIGFPE )
#else
#define IsDangerous( s )  0
#endif

static int sig_direct;


/***************************************
 ***************************************/

static RETSIGTYPE
default_signal_handler( int sig )
{
    fl_signal_caught( sig );

    /* if the signal is one of those that can cause infinite loops, handle it
       and then exit */

    if ( IsDangerous( sig ) )
    {
        handle_signal( );
        fprintf( stderr, "Can't continue upon receiving signal %d\n", sig );
        exit( sig );
    }

#ifndef RETSIGTYPE_IS_VOID
    return 0;
#endif
}


/***************************************
 ***************************************/

void
fl_add_signal_callback( int                 s,
                        FL_SIGNAL_HANDLER   cb,
                        void              * data )
{
    FLI_SIGNAL_REC *sig_rec,
                   *rec = fli_context->signal_rec;

    if ( ! fli_handle_signal )
        fli_handle_signal = handle_signal;

    while ( rec && rec->signum != s )
        rec = rec->next;

    if ( rec )
    {
        rec->callback = cb;
        rec->data = data;
    }
    else
    {
        sig_rec = fl_malloc( sizeof *sig_rec );
        sig_rec->next      = NULL;
        sig_rec->data      = data;
        sig_rec->callback  = cb;
        sig_rec->signum    = s;
        sig_rec->caught    = 0;

        if ( ! sig_direct )
        {
#if defined HAVE_SIGACTION
            struct sigaction sact;

            sact.sa_handler = default_signal_handler;
            sigemptyset( &sact.sa_mask );
            sact.sa_flags = 0;

            if ( sigaction( s, &sact, &sig_rec->old_sigact ) < 0 )
#else
            errno = 0;
            sig_rec->ocallback = signal( s, default_signal_handler );
            if ( sig_rec->ocallback == ( FLI_OSSIG_HANDLER ) - 1L || errno )
#endif
            {
                M_err( "fl_add_signal_callback", "Can't add handler for "
                       "signal %d", s );
                fl_free( sig_rec );
                return;
            }
        }

        if ( fli_context->signal_rec )
            sig_rec->next = fli_context->signal_rec;
        fli_context->signal_rec = sig_rec;
    }
}


/***************************************
 ***************************************/

void
fl_remove_signal_callback( int s )
{
    FLI_SIGNAL_REC *last,
                   *rec = fli_context->signal_rec;

    for ( last = rec; rec && rec->signum != s; last = rec, rec = rec->next )
        /* empty */ ;

    if ( ! rec )
    {
        M_err( "fl_remove_signal_callback", "No handler exists for signal %d",
               s );
        return;
    }

    if ( rec == fli_context->signal_rec )
        fli_context->signal_rec = rec->next;
    else
        last->next = rec->next;

    if ( ! sig_direct )
    {
#if defined HAVE_SIGACTION
        sigaction( s, &rec->old_sigact, NULL );
#else
        signal( s, rec->ocallback );
#endif
    }

    fli_safe_free( rec );
}


/***************************************
 ***************************************/

void
fl_signal_caught( int s )
{
    FLI_SIGNAL_REC *rec = fli_context->signal_rec;

    while ( rec && rec->signum != s )
        rec = rec->next;

    if ( ! rec )
    {
        M_err( "fl_signal_caught", "Caught bogus signal %d", s );
        return;
    }

    rec->caught++;

#if ! defined HAVE_SIGACTION
    if ( ! sig_direct && ! IsDangerous( s ) )
        signal( s, default_signal_handler );
#endif
}


/***************************************
 ***************************************/

void
fl_app_signal_direct( int y )
{
    if ( ! fli_handle_signal )
        fli_handle_signal = handle_signal;
    sig_direct = y;
}


/***************************************
 ***************************************/

void
fli_remove_all_signal_callbacks( void )
{
    while ( fli_context->signal_rec )
        fl_remove_signal_callback( fli_context->signal_rec->signum );
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */