File: signals.c

package info (click to toggle)
gnuit 4.9.5-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 5,068 kB
  • ctags: 4,821
  • sloc: ansic: 28,673; sh: 4,843; makefile: 328
file content (389 lines) | stat: -rw-r--r-- 8,057 bytes parent folder | download | duplicates (3)
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
/* signal.c -- Signals management for git*.  */

/* Copyright (C) 1993-1999, 2006-2007 Free Software Foundation, Inc.

 This file is part of gnuit.

 gnuit is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published
 by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 gnuit 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 General Public License for more details.

 You should have received a copy of the GNU General Public
 License along with this program. If not, see
 http://www.gnu.org/licenses/. */

/* Written by Tudor Hulubei and Andrei Pitis.  */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else /* !HAVE_STDLIB_H */
#include "ansi_stdlib.h"
#endif /* !HAVE_STDLIB_H */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <assert.h>

#include "signals.h"
#include "tty.h"
#include "misc.h"


/* Set on SIGINT.  Should be reset when detected.  */
int user_heart_attack;

/* Set if there are pending suspend/window change signals.  */
static int suspend_requested;
static int refresh_requested;
static int alarm_requested;

static int refresh_at_SIGCONT;
static int signals_allowed = OFF;
static int job_control = ON;

static void install_handler PROTO ((int));


extern void hide PROTO (());
extern void refresh PROTO ((int));
extern void clock_refresh PROTO ((int));


/*
 * Service pending signals (signals that can modify git at specific
 * times - in our case SIGTSTP (suspend) and SIGWINCH (window change)).
 */
void
service_pending_signals()
{
#ifdef SIGSTOP
    if (suspend_requested)
    {
	hide();
	kill(getpid(), SIGSTOP);
	suspend_requested = 0;
	refresh_requested = 0;
	alarm_requested = 0;
	return;
    }
#endif /* SIGSTOP */

#ifdef SIGWINCH
    if (refresh_requested)
    {
	tty_defaults();
	tty_io_clear();
	refresh(SIGWINCH);
	refresh_requested = 0;
	alarm_requested = 0;
	return;
    }
#endif /* SIGWINCH */

    if (alarm_requested)
    {
	/* FIXME: do something.  */
	/* clock_refresh(SIGALRM); ??? */
	alarm_requested = 0;
	return;
    }

    /* Add here as needed...  */
}


/*
 * Activate/deactivate suspend/resize signals.
 */
void
signals(mode)
    int mode;
{
    signals_allowed = mode;

    if (signals_allowed)
	service_pending_signals();
}


#ifdef SIGSTOP

static RETSIGTYPE
suspend(signum)
    int signum;
{
    if (signals_allowed)
    {
	refresh_at_SIGCONT = (tty_get_mode() == TTY_NONCANONIC);
	hide();
	kill(getpid(), SIGSTOP);
	suspend_requested = 0;
    }
    else
	suspend_requested = 1;

    /* Do this last, to avoid trouble on old Unix systems.  On newer
       systems it doesn't matter, and on older ones I rather quit
       because hide() is not reentrant.  */
    install_handler(signum);
}

#endif /* SIGSTOP */


#ifdef SIGWINCH

static RETSIGTYPE
window_change(signum)
    int signum;
{
    if (signals_allowed)
    {
	tty_defaults();
	tty_io_clear();
	refresh(signum);
	refresh_requested = 0;
    }
    else
	refresh_requested = 1;

    /* Do this last, to avoid trouble on old Unix systems.  On newer
       systems it doesn't matter, and on older ones I rather quit
       because refresh() is not reentrant.  */
    install_handler(signum);
}

#endif /* SIGWINCH */


static RETSIGTYPE
resume(signum)
    int signum;
{
    if (refresh_at_SIGCONT)
    {
	refresh(signum);
	refresh_requested = 0;
    }

    /* Do this last, to avoid trouble on old Unix systems.  On newer
       systems it doesn't matter, and on older ones I would rather
       quit because refresh() is not reentrant.  */
    install_handler(signum);
}


static RETSIGTYPE
time_change(signum)
    int signum;
{
    if (signals_allowed)
    {
	if (get_local_time()->tm_sec == 0)
	    clock_refresh(signum);
	tty_key_print_async();
	alarm_requested = 0;
    }
    else
	alarm_requested = 1;

    /* Do this last, to avoid trouble on old Unix systems.  On newer
       systems it doesn't matter, and on older ones I rather quit
       because clock_refresh() is not reentrant.  */
    install_handler(signum);

    /* Schedule the next alarm.  */
    alarm(60 - get_local_time()->tm_sec);
}


static RETSIGTYPE
panic(signum)
    int signum;
{
    signal(signum, panic);
    user_heart_attack = 1;
}


static void
install_handler(signum)
    int signum;
{
#ifdef HAVE_POSIX_SIGNALS
    sigset_t mask;
    struct sigaction action;

    sigemptyset(&mask);

    /* Block other terminal-generated signals while handler runs.  */
    sigaddset(&mask, SIGINT);
    action.sa_mask = mask;
    action.sa_flags = 0;

    switch (signum)
    {
#if defined(SIGTSTP) && defined (SIGCONT)
	case SIGTSTP:
#ifdef SIGWINCH
	    sigaddset(&mask, SIGWINCH);
#endif /* SIGWINCH */
	    sigaddset(&mask, SIGCONT);
	    sigaddset(&mask, SIGALRM);
	    action.sa_handler = suspend;
	    break;

	case SIGCONT:
#ifdef SIGWINCH
	    sigaddset(&mask, SIGWINCH);
#endif /* SIGWINCH */
	    sigaddset(&mask, SIGTSTP);
	    sigaddset(&mask, SIGALRM);
	    action.sa_handler = resume;
	    break;
#endif /* SIGTSTP && SIGCONT */

#ifdef SIGWINCH
	case SIGWINCH:
#if defined(SIGTSTP) && defined (SIGCONT)
	    sigaddset(&mask, SIGTSTP);
	    sigaddset(&mask, SIGCONT);
#endif /* SIGTSTP && SIGCONT */
	    sigaddset(&mask, SIGALRM);
	    action.sa_handler = window_change;
	    break;
#endif /* SIGWINCH */

	case SIGALRM:
#if defined(SIGTSTP) && defined (SIGCONT)
	    sigaddset(&mask, SIGTSTP);
	    sigaddset(&mask, SIGCONT);
#endif /* SIGTSTP && SIGCONT */
#ifdef SIGWINCH
	    sigaddset(&mask, SIGWINCH);
#endif /* SIGWINCH */
	    action.sa_handler = time_change;
	    break;

	default:
	    assert(0);
    }

    sigaction(signum, &action, NULL);
#else /* ! HAVE_POSIX_SIGNALS */
    /* No POSIX signals.  That's bad, we have no way of blocking
       signals during the execution of the signal handlers.
       Theoretically, it is possible for a signal to be delivered
       during the execution of another handler, potentially leading to
       disastrous results.  */
    switch (signum)
    {
#if defined(SIGTSTP) && defined (SIGCONT)
	case SIGTSTP:
	    signal(SIGTSTP, suspend);
	    break;

	case SIGCONT:
	    signal(SIGCONT, resume);
	    break;
#endif /* SIGTSTP && SIGCONT */

#ifdef SIGWINCH
	case SIGWINCH:
	    signal(SIGWINCH, window_change);
	    break;
#endif /* SIGWINCH */

	case SIGALRM:
	    signal(SIGALRM, time_change);
	    break;

	default:
	    assert(0);
    }
#endif /* HAVE_POSIX_SIGNALS */
}


void
signal_handlers(status)
    int status;
{
    if (status == ON)
    {
#if defined(SIGTSTP) && defined(SIGCONT)
	/* Job control stuff. */
	if (job_control)
	{
	    install_handler(SIGTSTP);
	    install_handler(SIGCONT);
	}
#endif /* SIGTSTP && SIGCONT */

#ifdef SIGWINCH
	/* Handle window changes.  */
	install_handler(SIGWINCH);
#endif /* SIGWINCH */

	install_handler(SIGALRM);
    }
    else
    {
#if defined(SIGTSTP) && defined(SIGCONT)
	/* Job control stuff. */
	if (job_control)
	{
	    signal(SIGTSTP, SIG_IGN);
	    signal(SIGCONT, SIG_IGN);
	}
#endif /* SIGTSTP && SIGCONT */

#ifdef SIGWINCH
	/* Ignore window changes.  */
	signal(SIGWINCH, SIG_IGN);
#endif /* SIGWINCH */

	signal(SIGALRM, SIG_IGN);
    }
}


/*
 * Call this at the very beginning to avoid receiving signals before
 * being able to handle them.
 */
void
signals_init()
{
#if defined(SIGTSTP) && defined(SIGCONT)
    /* Job control stuff. */
    job_control = (signal(SIGTSTP, SIG_IGN) != SIG_IGN);
    signal(SIGCONT, SIG_IGN);
#endif /* SIGTSTP && SIGCONT */

#ifdef SIGWINCH
    /* Ignore window changes for now.  */
    signal(SIGWINCH, SIG_IGN);
#endif /* SIGWINCH */

    /* Miscelaneous signals that we want to handle.  */
    signal(SIGSEGV, fatal_signal);
    signal(SIGHUP,  fatal_signal);
    signal(SIGTERM, fatal_signal);
    signal(SIGQUIT, SIG_IGN);
    signal(SIGINT,  panic);
}