File: signals.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (687 lines) | stat: -rw-r--r-- 15,781 bytes parent folder | download
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
 * signals.c --
 *
 * Handles signals, such as stop, start, interrupt.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/utils/signals.c,v 1.3 2010/03/08 13:33:08 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>	/* for getpid() */
#include <sys/time.h>

#include "utils/main.h"

#ifndef	SIGEMT
#define	SIGEMT	7	/* EMT instruction (SIGUNUSED) */
#endif

#ifndef	SIGBUS
#define	SIGBUS	10	/* bus error (SIGUSR1) */
#endif

#ifndef	SIGSYS
#define	SIGSYS	12	/* bad argument to system call (SIGUSR2) */
#endif

#if !defined(SIGIOT) && defined(SIGABRT)
#define SIGIOT SIGABRT	/* io-trap signal redefined */
#endif

#ifdef	linux
#if SIGBUS == SIGUNUSED
#undef SIGBUS
#define SIGBUS SIGUSR1
#endif

#if SIGSYS == SIGUNUSED
#undef SIGSYS
#define SIGSYS SIGUSR2
#endif
#endif

#include <fcntl.h>

#include "utils/magic.h"
#include "utils/magsgtty.h"
#include "textio/textio.h"
#include "utils/geometry.h"
#include "utils/signals.h"
#include "windows/windows.h"
#include "graphics/graphics.h"


#ifndef FASYNC
#  define	FASYNC	00100	/* kludge for SUN2s */
#endif

/* specially imported */
extern bool DBWriteBackup();

/* macs support BSD4.2 signals, so turn off the SYSV flag for this module */
#ifdef macosx
#undef SYSV
#endif

void sigSetAction(int, sigRetVal (*)(int));

/* becomes true when we get an interrupt */
global bool SigInterruptPending = FALSE;

/* Becomes true when IO is possible on one of the files passed to SigWatchFile.
 * Spurious signals are sometimes generated -- use select() to make
 * sure that what you want is really there.
 */
global bool SigIOReady = FALSE;

/* If true, we will set SigInterruptPending whenever we set SigIOReady. */
global bool SigInterruptOnSigIO;

/*
 * Set to true when we recieve a SIGWINCH/SIGWINDOW signal 
 * (indicating that a window has changed size or otherwise needs attention).
 */
global bool SigGotSigWinch = FALSE;


/* 
 * Local data structures
 */
static bool sigInterruptReceived = FALSE;
static int sigNumDisables = 0;

/*---------------------------------------------------------
 * While we can conveniently run Ctrl-C interrupts from
 * the terminal at any time, we can't do this from the
 * window in Tcl/Tk because the GUI event handler is not
 * a separate process.  So we run a timer process during
 * WindUpdate() which periodically processes Tk events
 * in the window.
 *
 * This timer can be set for "secs" second intervals.  If
 * "secs" is zero, it defaults to a 1/4 second interval.
 *---------------------------------------------------------
 */

void
SigSetTimer(int secs)
{
    struct itimerval subsecond;	/* one-quarter second interval */
 
    /*
    if (GrDisplayStatus == DISPLAY_IDLE)
	fprintf(stderr, "Timer start\n");
    else
	fprintf(stderr, "Timer repeat\n");
    fflush(stderr);
    */
   
    subsecond.it_interval.tv_sec = 0;
    subsecond.it_interval.tv_usec = 0;
    subsecond.it_value.tv_sec = secs;
    subsecond.it_value.tv_usec = (secs == 0) ? 250000 : 0;

    setitimer(ITIMER_REAL, &subsecond, NULL);
}

/*---------------------------*/
/* Remove any existing timer */
/*---------------------------*/

void
SigRemoveTimer()
{
    struct itimerval zero;	/* zero time to stop the timer */
   
    /* fprintf(stderr, "Timer stop\n"); fflush(stderr); */

    zero.it_value.tv_sec = 0;
    zero.it_value.tv_usec = 0;
    zero.it_interval.tv_sec = 0;
    zero.it_interval.tv_usec = 0;

    setitimer(ITIMER_REAL, &zero, NULL);
}

sigRetVal
sigOnAlarm(int signo)
{
    if (GrDisplayStatus == DISPLAY_IN_PROGRESS)
	GrDisplayStatus = DISPLAY_BREAK_PENDING;

    sigReturn;
}

/*-----------------------------------------*/
/* Set timer to act as a display interrupt */
/*-----------------------------------------*/

void
SigTimerDisplay()
{
    sigSetAction(SIGALRM,  sigOnAlarm);
}

/*------------------------------------------*/
/* Set timer to act like a Ctrl-C interrupt */
/*------------------------------------------*/

void
SigTimerInterrupts()
{
    sigSetAction(SIGALRM,  sigOnInterrupt);
}

/*---------------------------------------------------------
 * sigOnStop:
 *	This procedure handles stop signals.
 *
 * Results:
 *	sigReturn result (see signals.h)
 *
 * Side Effects:
 *	The text display is reset, and we stop
 *---------------------------------------------------------
 */

sigRetVal
sigOnStop(int signo)
{
    /* fix things up */
    TxResetTerminal();
    GrStop();

    /* restore the default action and resend the signal */

    sigSetAction(SIGTSTP, SIG_DFL);
    kill(getpid(), 
#ifdef linux
    	SIGSTOP
#else
    	SIGTSTP
#endif
	); 
    
    /* -- we stop here -- */

    /* NOTE:  The following code really belongs in a routine that is
     * called in response to a SIGCONT signal, but it doesn't seem to
     * work that way.  Maybe there is a Unix bug with this???
     */

    GrResume();
    TxSetTerminal();
    TxReprint();

    /* catch future stops now that we have finished resuming */

    sigSetAction(SIGTSTP, sigOnStop);
    sigReturn;
}

/*
 *---------------------------------------------------------
 * SigCheckProcess ---
 *
 *	Check if a process exists by sending it a signal.
 *	Perhaps there are better ways to do this?
 *
 * Results:
 *	TRUE if the process exists, FALSE if not.
 *
 * Side effects:
 *	Whatever happens when the process gets SIGCONT;
 *	hopefully nothing.
 *---------------------------------------------------------
 */

bool
SigCheckProcess(pid)
    int pid;
{
    int result;
    result = kill((pid_t)pid, SIGCONT);

    if (result == 0) return TRUE;
    else return FALSE;
}

/*---------------------------------------------------------
 * sigEnableInterrupts:
 *	This procedure reenables our handling of interrupts.
 *
 * Results:	None.
 *
 * Side Effects:
 *	None.
 *---------------------------------------------------------
 */

void
SigEnableInterrupts()
{
    /*sigNumDisables--;*/
    if (sigNumDisables == 1)
    {
	SigInterruptPending = sigInterruptReceived;
	sigInterruptReceived = FALSE;
    }
    sigNumDisables--;
}


/*---------------------------------------------------------
 * sigDisableInterrupts:
 *	This procedure disables our handling of interrupts.
 *
 * Results:	None.
 *
 * Side Effects:
 *	None.
 *---------------------------------------------------------
 */

void
SigDisableInterrupts()
{
    sigNumDisables++;
    if (sigNumDisables == 1)
    {
	sigInterruptReceived = SigInterruptPending;
	SigInterruptPending = FALSE;
    }
    /*sigNumDisables++;*/
}


/*
 * ----------------------------------------------------------------------------
 * SigWatchFile --
 *
 *	Take interrupts on a given IO stream.  
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	SigIOReady will be set when the IO stream becomes ready.  It is
 *	the responsibility of the client to clear that flag when needed.
 * ----------------------------------------------------------------------------
 */

void
SigWatchFile(filenum, filename)
    int filenum;		/* A file descriptor number */
    char *filename;		/* Used to recognize special files that
				 * don't support a full range of fcntl
				 * calls (such as windows: /dev/winXX).
				 */
{
    int flags;
    bool iswindow;

    iswindow = (filename && (strncmp(filename, "/dev/win", 8) == 0));

    flags = fcntl(filenum, F_GETFL, 0);
    if (flags == -1)
    {
	perror("(Magic) SigWatchFile1");
	return;
    }

    if (!mainDebug)
    {
	/* turn on FASYNC */
#ifndef SYSV
#ifdef F_SETOWN
	if (!iswindow)
	{
	    if (fcntl(filenum, F_SETOWN, -getpid()) == -1)
		perror("(Magic) SigWatchFile2"); 
	}
#endif
#endif /* SYSV */
#ifdef FASYNC
	if (fcntl(filenum, F_SETFL, flags | FASYNC) == -1) 
	    perror("(Magic) SigWatchFile3");
#else
# ifdef FIOASYNC
	flags = 1;
	if (ioctl(filenum, FIOASYNC, &flags) == -1) 
	    perror("(Magic) SigWatchFile3a");
# endif
#endif
    }
    else
    {
#ifdef FASYNC
	/* turn off FASYNC */
	if (fcntl(filenum, F_SETFL, flags & (~FASYNC)) == -1) 
	    perror("(Magic) SigWatchFile4");
#else
# ifdef FIOASYNC
	flags = 0;
	if (ioctl(filenum, FIOASYNC, &flags) == -1) 
	    perror("(Magic) SigWatchFile3b");
# endif
#endif
    }
}


/*
 * ----------------------------------------------------------------------------
 * SigUnWatchFile --
 *
 *	Do not take interrupts on a given IO stream.  
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	SigIOReady will be not set when the IO stream becomes ready.  
 * ----------------------------------------------------------------------------
 */
 /*ARGSUSED*/

void
SigUnWatchFile(filenum, filename)
    int filenum;		/* A file descriptor number */
    char *filename;		/* Used to recognize special files that
				 * don't support a full range of fcntl
				 * calls (such as windows: /dev/winXX).
				 */
{
    int flags;

    flags = fcntl(filenum, F_GETFL, 0);
    if (flags == -1)
    {
	perror("(Magic) SigUnWatchFile1");
	return;
    }

#ifdef FASYNC
    /* turn off FASYNC */
    if (fcntl(filenum, F_SETFL, flags & (~FASYNC)) == -1) 
	perror("(Magic) SigUnWatchFile4");
#else
# ifdef FIOASYNC
    flags = 0;
    if (ioctl(filenum, FIOASYNC, &flags) == -1) 
	perror("(Magic) SigWatchFile3");
# endif
#endif
}


/*---------------------------------------------------------
 * sigOnInterrupt:
 *	This procedure handles interupt signals.
 *
 * Results:
 *	sigReturn result (see signals.h)
 *
 * Side Effects:
 *    A global flag is set
 *---------------------------------------------------------
 */

sigRetVal
sigOnInterrupt(int signo)
{
    if (sigNumDisables != 0)
	sigInterruptReceived = TRUE;
    else
	SigInterruptPending = TRUE;
    sigReturn;
}


/*
 * ----------------------------------------------------------------------------
 * sigOnTerm:
 *
 *	Catch the terminate (SIGTERM) signal.
 *	Force all modified cells to be written to disk (in new files,
 *	of course).
 *
 * Results:
 *	Function does not return.
 *
 * Side effects:
 *	Writes cells out to disk (by calling DBWriteBackup()).
 *	Exits.
 * ----------------------------------------------------------------------------
 */

sigRetVal
sigOnTerm(int signo)
{
    DBWriteBackup(NULL);
    exit (1);
}



/*
 * ----------------------------------------------------------------------------
 * sigOnWinch --
 *
 *	A window has changed size or otherwise needs attention.
 *
 * Results:
 *	sigReturn result (see signals.h)
 *
 * Side effects:
 *	Sets a global flag.
 * ----------------------------------------------------------------------------
 */

sigRetVal
sigOnWinch(int signo)
{
    SigGotSigWinch = TRUE;
    sigReturn;
}

/*
 * ----------------------------------------------------------------------------
 * sigIO --
 *
 *	Some IO device is ready (probably the keyboard or the mouse).
 *
 * Results:
 *	sigReturn result (see signals.h)
 *
 * Side effects:
 *	Sets a global flag.
 * ----------------------------------------------------------------------------
 */

sigRetVal
sigIO(int signo)
{
    SigIOReady = TRUE;
    if (SigInterruptOnSigIO) sigOnInterrupt(0);
    sigReturn;
}

/*
 * ----------------------------------------------------------------------------
 *
 * sigCrash --
 *
 *	Something when wrong, reset the terminal and die.
 *
 * Results:
 *	Function never returns.
 *
 * Side Effects:
 *	We die.
 *
 * ----------------------------------------------------------------------------
 */

sigRetVal
sigCrash(signum)
    int signum;
{
    static int magicNumber = 1239987;
    char *msg;
    extern bool AbortFatal;

#ifndef	linux
    if (magicNumber == 1239987) {
	/* Things aren't screwed up that badly, try to reset the terminal */
	magicNumber = 0;
	switch (signum) {
	    case SIGILL: {msg = "Illegal Instruction"; break;};
	    case SIGTRAP: {msg = "Instruction Trap"; break;};
	    case SIGIOT: {msg = "IO Trap"; break;};
	    case SIGEMT: {msg = "EMT Trap"; break;};
	    case SIGFPE: {msg = "Floating Point Exception"; break;};
	    case SIGBUS: {msg = "Bus Error"; break;};
	    case SIGSEGV: {msg = "Segmentation Violation"; break;};
	    case SIGSYS: {msg = "Bad System Call"; break;};
	    default: {msg = "Unknown signal"; break;};
	};
	strcpy(AbortMessage, msg);
	AbortFatal = TRUE;
	niceabort();
	TxResetTerminal();
    }
#else
	if (magicNumber == 1239987) {
	  magicNumber = 0;
	  /* Don't use switch statement.  These values aren't mutually exclusive
	  * under Linux.
	  */
	  if (signum == SIGILL) msg = "Illegal Instruction";
	  else if (signum == SIGTRAP) msg = "Instruction Trap";
	  else if (signum == SIGIOT) msg = "IO Trap";
	  else if (signum == SIGEMT) msg = "EMT Trap";
	  else if (signum == SIGFPE) msg = "Floating Point Exception";
	  else if (signum == SIGBUS) msg = "Bus Error";
	  else if (signum == SIGSEGV) msg = "Segmentation Violation";
	  else if (signum == SIGSYS) msg = "Bad System Call";
	  else msg = "Unknown signal";
	  strcpy(AbortMessage, msg);
	  AbortFatal = TRUE;
	  niceabort();
	  TxResetTerminal();
	}
#endif

    /* Crash & burn */
    magicNumber = 0;
    exit(12);
}


/*
 * ----------------------------------------------------------------------------
 * SigInit:
 *
 *	Set up signal handling for all signals.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Signal handling is set up.
 * ----------------------------------------------------------------------------
 */

void
SigInit()
{
  /* fprintf(stderr, "Establishing signal handlers.\n"); fflush(stderr); */

  sigSetAction(SIGINT,  sigOnInterrupt);
  sigSetAction(SIGTERM, sigOnTerm);

/* Under Tcl, sigOnStop just causes Tcl to hang forever.  So don't set	*/
/* any new actions.							*/

#ifndef MAGIC_WRAPPER
#ifdef SIGTSTP
  sigSetAction(SIGTSTP, sigOnStop);
#endif

#ifdef SIGWINCH
  sigSetAction(SIGWINCH, sigOnWinch);
#endif

#ifdef SIGWINDOW
  sigSetAction(SIGWINDOW, sigOnWinch);
#endif
#endif	/* MAGIC_WRAPPER */

  if( !mainDebug ) {
    sigSetAction(SIGIO, sigIO);
#ifdef MAGIC_WRAPPER
    SigTimerDisplay();
#else
    sigSetAction(SIGALRM,  SIG_IGN);
#endif
    sigSetAction(SIGPIPE,  SIG_IGN);
#ifdef SIGPOLL
	if( SIGIO != SIGPOLL ) {
      sigSetAction(SIGPOLL,  SIG_IGN);
	}
#endif

#ifdef FANCY_ABORT
    sigSetAction(SIGILL,  sigCrash);
    sigSetAction(SIGTRAP, sigCrash);
    sigSetAction(SIGIOT,  sigCrash);
    sigSetAction(SIGEMT,  sigCrash);
    sigSetAction(SIGFPE,  sigCrash);
    sigSetAction(SIGBUS,  sigCrash);
    sigSetAction(SIGSEGV, sigCrash);
    sigSetAction(SIGSYS,  sigCrash);
#endif /* FANCY_ABORT */
  }

#if !defined(SYSV) && !defined(CYGWIN)
  sigsetmask(0);
#endif
}

void
sigSetAction(int signo, sigRetVal (*handler)(int))
{
#if defined(SYSV) || defined(CYGWIN) || defined(__NetBSD__)
  struct sigaction sa;

  sa.sa_handler = handler;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sigaction(signo, &sa, (struct sigaction *)NULL);
#else
  struct sigvec sv;

  sv.sv_handler = handler;
  sv.sv_mask    = 0;
  sv.sv_flags   = 0;
  sigvec(signo, &sv, (struct sigvec *)NULL);
#endif
}