summaryrefslogtreecommitdiffstats
path: root/private/crt32/misc/winsig.c
blob: 310f1c62fa19429aa876de22b0dd77f8591f57b2 (plain) (blame)
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/***
*winsig.c - C signal support
*
*	Copyright (c) 1991-1993, Microsoft Corporation. All rights reserved
*
*Purpose:
*	Defines signal(), raise() and supporting functions.
*
*Revision History:
*	10-21-91  GJF	Signal for Win32 and Dosx32. Copied from old signal.c
*			(the Cruiser implementation with some revisions for
*			Win32), then extensively rewritten.
*	11-08-91  GJF	Cleaned up header files usage.
*	12-13-91  GJF	Fixed multi-thread build.
*	09-30-92  SRW	Add WINAPI keyword to CtrlC handler
*	02-17-93  GJF	Changed for new _getptd().
*
*******************************************************************************/

#ifndef _POSIX_

#include <cruntime.h>
#include <errno.h>
#include <float.h>
#include <malloc.h>
#include <os2dll.h>
#include <oscalls.h>
#include <signal.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

/*
 * look up the first entry in the exception-action table corresponding to
 * the given signal
 */
#ifdef	MTHREAD
static struct _XCPT_ACTION * _CRTAPI3 siglookup(int, struct _XCPT_ACTION *);
#else	/* not MTHREAD */
static struct _XCPT_ACTION * _CRTAPI3 siglookup(int);
#endif	/* MTHREAD */

/*
 * variables holding action codes (and code pointers) for SIGINT, SIGBRK,
 * SIGABRT and SIGTERM.
 *
 * note that the disposition (i.e., action to be taken upon receipt) of
 * these signals is defined on a per-process basis (not per-thread)!!
 */

static _PHNDLR ctrlc_action	  = SIG_DFL;	/* SIGINT   */
static _PHNDLR ctrlbreak_action   = SIG_DFL;	/* SIGBREAK */
static _PHNDLR abort_action	  = SIG_DFL;	/* SIGABRT  */
static _PHNDLR term_action	  = SIG_DFL;	/* SIGTERM  */

/*
 * flag indicated whether or not a handler has been installed to capture
 * ^C and ^Break events.
 */
static int ConsoleCtrlHandler_Installed = 0;


/***
*static BOOL WINAPI ctrlevent_capture(DWORD CtrlType) - capture ^C and ^Break events
*
*Purpose:
*	Capture ^C and ^Break events from the console and dispose of them
*	according the values in ctrlc_action and ctrlbreak_action, resp.
*	This is the routine that evokes the user-defined action for SIGINT
*	(^C) or SIGBREAK (^Break) installed by a call to signal().
*
*Entry:
*       DWORD CtrlType  - indicates type of event, two values:
*				CTRL_C_EVENT
*				CTRL_BREAK_EVENT
*
*Exit:
*	Returns TRUE to indicate the event (signal) has been handled.
*	Otherwise, returns FALSE.
*
*Exceptions:
*
*******************************************************************************/

static BOOL WINAPI ctrlevent_capture (
        DWORD CtrlType
	)
{
	_PHNDLR ctrl_action;
	_PHNDLR *pctrl_action;
	int sigcode;

	_mlock(_SIGNAL_LOCK);

	/*
	 * Identify the type of event and fetch the corresponding action
	 * description.
	 */

	if ( CtrlType == CTRL_C_EVENT ) {
		ctrl_action = *(pctrl_action = &ctrlc_action);
		sigcode = SIGINT;
	}
	else {
		ctrl_action = *(pctrl_action = &ctrlbreak_action);
		sigcode = SIGBREAK;
	}

	if ( ctrl_action == SIG_DFL ) {
		/*
		 * return FALSE, indicating the event has NOT been handled
		 */
		_munlock(_SIGNAL_LOCK);
		return FALSE;
	}

	if ( ctrl_action != SIG_IGN ) {
		/*
		 * Reset the action to be SIG_DFL and call the user's handler.
		 */
		*pctrl_action = SIG_DFL;
		_munlock(_SIGNAL_LOCK);
		(*ctrl_action)(sigcode);
	}
	else
		/*
		 * SIG_IGN - nothing special to do except release the lock
		 */
		_munlock(_SIGNAL_LOCK);

	/*
	 * Return TRUE, indicating the event has been handled (which may
	 * mean it's being ignored)
	 */
	return TRUE;
}



/***
*_PHNDLR signal(signum, sigact) - Define a signal handler
*
*Purpose:
*	The signal routine allows the user to define what action should
*	be taken when various signals occur. The Win32/Dosx32 implementation
*	supports seven signals, divided up into three general groups
*
*	1. Signals corresponding to OS exceptions. These are:
*			SIGFPE
*			SIGILL
*			SIGSEGV
*	   Signal actions for these signals are installed by altering the
*	   XcptAction and SigAction fields for the appropriate entry in the
*	   exception-action table (XcptActTab[]).
*
*	2. Signals corresponding to ^C and ^Break. These are:
*			SIGINT
*			SIGBREAK
*	   Signal actions for these signals are installed by altering the
*	   _ctrlc_action and _ctrlbreak_action variables.
*
*	3. Signals which are implemented only in the runtime. That is, they
*	   occur only as the result of a call to raise().
*			SIGABRT
*			SIGTERM
*
*
*Entry:
*	int signum	signal type. recognized signal types are:
*
*			SIGABRT 	(ANSI)
*			SIGBREAK
*			SIGFPE		(ANSI)
*			SIGILL		(ANSI)
*			SIGINT		(ANSI)
*			SIGSEGV 	(ANSI)
*			SIGTERM 	(ANSI)
*
*	_PHNDLR sigact	signal handling function or action code. the action
*			codes are:
*
*			SIG_DFL - take the default action, whatever that may
*			be, upon receipt of this type type of signal.
*
*			SIG_DIE - *** ILLEGAL ***
*			special code used in the XcptAction field of an
*			XcptActTab[] entry to indicate that the runtime is
*			to terminate the process upon receipt of the exception.
*			not accepted as a value for sigact.
*
*			SIG_IGN - ignore this type of signal
*
*			[function address] - transfer control to this address
*			when a signal of this type occurs.
*
*Exit:
*	Good return:
*	Signal returns the previous value of the signal handling function
*	(e.g., SIG_DFL, SIG_IGN, etc., or [function address]). This value is
*	returned in DX:AX.
*
*	Error return:
*	Signal returns -1 and errno is set to EINVAL. The error return is
*	generally taken if the user submits bogus input values.
*
*Exceptions:
*	None.
*
*******************************************************************************/

_PHNDLR _CRTAPI1 signal(
	int signum,
	_PHNDLR sigact
	)
{
	struct _XCPT_ACTION *pxcptact;
	_PHNDLR oldsigact;
#ifdef	MTHREAD
	_ptiddata ptd;
#endif

	/*
	 * Check for values of sigact supported on other platforms but not
	 * on this one. Also, make sure sigact is not SIG_DIE
	 */
	if ( (sigact == SIG_ACK) || (sigact == SIG_SGE) )
		goto sigreterror;

	/*
	 * Take care of all signals which do not correspond to exceptions
	 * in the host OS. Those are:
	 *
	 *			SIGINT
	 *			SIGBREAK
	 *			SIGABRT
	 *			SIGTERM
	 *
	 */
	if ( (signum == SIGINT) || (signum == SIGBREAK) || (signum == SIGABRT)
	    || (signum == SIGTERM) ) {

		_mlock(_SIGNAL_LOCK);

		/*
		 * if SIGINT or SIGBREAK, make sure the handler is installed
		 * to capture ^C and ^Break events.
		 */
		if ( ((signum == SIGINT) || (signum == SIGBREAK)) &&
		    !ConsoleCtrlHandler_Installed )
			if ( SetConsoleCtrlHandler(ctrlevent_capture, TRUE)
			    == TRUE )
				ConsoleCtrlHandler_Installed = TRUE;
			else {
				_doserrno = GetLastError();
				_munlock(_SIGNAL_LOCK);
				goto sigreterror;
			}

		switch (signum) {

			case SIGINT:
				oldsigact = ctrlc_action;
				ctrlc_action = sigact;
				break;

			case SIGBREAK:
				oldsigact = ctrlbreak_action;
				ctrlbreak_action = sigact;
				break;

			case SIGABRT:
				oldsigact = abort_action;
				abort_action = sigact;
				break;

			case SIGTERM:
				oldsigact = term_action;
				term_action = sigact;
				break;
		}

		_munlock(_SIGNAL_LOCK);
		goto sigretok;
	}

	/*
	 * If we reach here, signum is supposed to be one the signals which
	 * correspond to exceptions in the host OS. Those are:
	 *
	 *			SIGFPE
	 *			SIGILL
	 *			SIGSEGV
	 */

	/*
	 * Make sure signum is one of the remaining supported signals.
	 */
	if ( (signum != SIGFPE) && (signum != SIGILL) && (signum != SIGSEGV) )
		goto sigreterror;


#ifdef	MTHREAD
	/*
	 * Fetch the tid data table entry for this thread
	 */
	ptd = _getptd();

	/*
	 * Check that there a per-thread instance of the exception-action
	 * table for this thread. if there isn't, create one.
	 */
	if ( ptd->_pxcptacttab == _XcptActTab )
		/*
		 * allocate space for an exception-action table
		 */
		if ( (ptd->_pxcptacttab = malloc(_XcptActTabSize))
		    != NULL )
			/*
			 * initialize the table by copying over the contents
			 * of _XcptActTab[]
			 */
			(void) memcpy(ptd->_pxcptacttab, _XcptActTab,
			    _XcptActTabSize);
		else
			/*
			 * cannot create exception-action table, return
			 * error to caller
			 */
			goto sigreterror;

#endif	/* MTHREAD */

	/*
	 * look up the proper entry in the exception-action table. note that
	 * if several exceptions are mapped to the same signal, this returns
	 * the pointer to first such entry in the exception action table. it
	 * is assumed that the other entries immediately follow this one.
	 */
#ifdef	MTHREAD
	if ( (pxcptact = siglookup(signum, ptd->_pxcptacttab)) == NULL )
#else	/* not MTHREAD */
	if ( (pxcptact = siglookup(signum)) == NULL )
#endif	/* MTHREAD */
		goto sigreterror;

	/*
	 * SIGSEGV, SIGILL and SIGFPE all have more than one exception mapped
	 * to them. the code below depends on the exceptions corresponding to
	 * the same signal being grouped together in the exception-action
	 * table.
	 */

	/*
	 * store old signal action code for return value
	 */
	oldsigact = pxcptact->XcptAction;

	/*
	 * loop through all entries corresponding to the
	 * given signal and update the SigAction and XcptAction
	 * fields as appropriate
	 *
	 * should add a test to make sure pxcptact never runs
	 * off the end of the table (works okay for now because
	 * of the order of the entries).
	 */
	while ( pxcptact->SigNum == signum ) {
		/*
		 * take care of the SIG_IGN and SIG_DFL action
		 * codes
		 */
		pxcptact->XcptAction = sigact;
		pxcptact++;
	}

sigretok:
	return(oldsigact);

sigreterror:
	errno = EINVAL;
	return(SIG_ERR);
}

/***
*int raise(signum) - Raise a signal
*
*Purpose:
*	This routine raises a signal (i.e., performs the action currently
*	defined for this signal). The action associated with the signal is
*	evoked directly without going through intermediate dispatching or
*	handling.
*
*Entry:
*	int signum - signal type (e.g., SIGINT)
*
*Exit:
*	returns 0 on good return, -1 on bad return.
*
*Exceptions:
*	May not return.  Raise has no control over the action
*	routines defined for the various signals.  Those routines may
*	abort, terminate, etc.	In particular, the default actions for
*	certain signals will terminate the program.
*
*******************************************************************************/


int _CRTAPI1 raise (
	int signum
	)
{
	_PHNDLR sigact;
	_PHNDLR *psigact;
	PEXCEPTION_POINTERS oldpxcptinfoptrs;
	int oldfpecode;

#ifdef	MTHREAD
	int siglock = 0;
	_ptiddata ptd;
#endif

	switch (signum) {

		case SIGINT:
			sigact = *(psigact = &ctrlc_action);
#ifdef	MTHREAD
			siglock++;
#endif
			break;

		case SIGBREAK:
			sigact = *(psigact = &ctrlbreak_action);
#ifdef	MTHREAD
			siglock++;
#endif
			break;

		case SIGABRT:
			sigact = *(psigact = &abort_action);
#ifdef	MTHREAD
			siglock++;
#endif
			break;

		case SIGTERM:
			sigact = *(psigact = &term_action);
#ifdef	MTHREAD
			siglock++;
#endif
			break;

		case SIGFPE:
		case SIGILL:
		case SIGSEGV:
#ifdef	MTHREAD
			ptd = _getptd();
			sigact = *(psigact = &(siglookup( signum,
			    ptd->_pxcptacttab )->XcptAction));
#else
			sigact = *(psigact = &(siglookup( signum )->
			    XcptAction));
#endif
			break;

		default:
			/*
			 * unsupported signal, return an error
			 */
			return (-1);
	}

#ifdef	MTHREAD
	/*
	 * if signum is one of the 'process-wide' signals (i.e., SIGINT,
	 * SIGBREAK, SIGABRT or SIGTERM), assert _SIGNAL_LOCK.
	 */
	if ( siglock )
		_mlock(_SIGNAL_LOCK);
#endif

	/*
	 * If the current action is SIG_IGN, just return
	 */
	if ( sigact == SIG_IGN ) {

#ifdef	MTHREAD
		if ( siglock )
			_munlock(_SIGNAL_LOCK);
#endif
		return(0);
	}

	/*
	 * If the current action is SIG_DFL, take the default action
	 */
	if ( sigact == SIG_DFL ) {

#ifdef	MTHREAD
		if ( siglock )
			_munlock(_SIGNAL_LOCK);
#endif
		/*
		 * The current default action for all of the supported
		 * signals is to terminate with an exit code of 3.
		 *
		 * BUG, BUG!!!! THIS IS ALMOST CERTAINLY THE WRONG EXIT
		 * CODE FOR SOME OF THE SIGNALS IN NT!
		 */
		_exit(3);
	}

	/*
	 * From here on, sigact is assumed to be a pointer to a user-supplied
	 * handler.
	 */

	/*
	 * For signals which correspond to exceptions, set the pointer
	 * to the EXCEPTION_POINTERS structure to NULL
	 */
	if ( (signum == SIGFPE) || (signum == SIGSEGV) ||
	    (signum == SIGILL) ) {
#ifdef	MTHREAD
		oldpxcptinfoptrs = ptd->_tpxcptinfoptrs;
		ptd->_tpxcptinfoptrs = NULL;
#else
		oldpxcptinfoptrs = _pxcptinfoptrs;
		_pxcptinfoptrs = NULL;
#endif

		 /*
		  * If signum is SIGFPE, also set _fpecode to
		  * _FPE_EXPLICITGEN
		  */
		if ( signum == SIGFPE ) {
#ifdef	MTHREAD
			oldfpecode = ptd->_tfpecode;
			ptd->_tfpecode = _FPE_EXPLICITGEN;
#else
			oldfpecode = _fpecode;
			_fpecode = _FPE_EXPLICITGEN;
#endif
		}
	}

	/*
	 * Reset the action to SIG_DFL and call the user specified handler
	 * routine.
	 */
	*psigact = SIG_DFL;

#ifdef	MTHREAD
	if ( siglock )
		_munlock(_SIGNAL_LOCK);
#endif

	if ( signum == SIGFPE )
		/*
		 * Special hack to support old SIGFPE handlers which
		 * expect the value of _fpecode as the second argument.
		 */
#ifdef	MTHREAD
		(*(void (* _CRTAPI1)(int,int))sigact)(SIGFPE,
		    ptd->_tfpecode);
#else
		(*(void (* _CRTAPI1)(int,int))sigact)(SIGFPE, _fpecode);
#endif
	else
		(*sigact)(signum);

	/*
	 * For signals which correspond to exceptions, restore the pointer
	 * to the EXCEPTION_POINTERS structure.
	 */
	if ( (signum == SIGFPE) || (signum == SIGSEGV) ||
	    (signum == SIGILL) ) {
#ifdef	MTHREAD
		ptd->_tpxcptinfoptrs = oldpxcptinfoptrs;
#else
		_pxcptinfoptrs = oldpxcptinfoptrs;
#endif

		 /*
		  * If signum is SIGFPE, also restore _fpecode
		  */
		if ( signum == SIGFPE )
#ifdef	MTHREAD
			ptd->_tfpecode = oldfpecode;
#else
			_fpecode = oldfpecode;
#endif
	}

	return(0);
}


/***
*struct _XCPT_ACTION *siglookup(int signum) - look up exception-action table
*	entry for signal.
*
*Purpose:
*	Find the first entry int _XcptActTab[] whose SigNum field is signum.
*
*Entry:
*	int signum - C signal type (e.g., SIGINT)
*
*Exit:
*	If successful, pointer to the table entry. If no such entry, NULL is
*	returned.
*
*Exceptions:
*
*******************************************************************************/

#ifdef	MTHREAD

static struct _XCPT_ACTION * _CRTAPI3 siglookup (
	int signum,
	struct _XCPT_ACTION *pxcptacttab
	)
{
	struct _XCPT_ACTION *pxcptact = pxcptacttab;

#else	/* not MTHREAD */

static struct _XCPT_ACTION * _CRTAPI3 siglookup(int signum)
{
	struct _XCPT_ACTION *pxcptact = _XcptActTab;

#endif	/* MTHREAD */
	/*
	 * walk thru the _xcptactab table looking for the proper entry. note
	 * that in the case where more than one exception corresponds to the
	 * same signal, the first such instance in the table is the one
	 * returned.
	 */
#ifdef	MTHREAD

	while ( (pxcptact->SigNum != signum) && (++pxcptact <
	pxcptacttab + _XcptActTabCount) ) ;

#else	/* not MTHREAD */

	while ( (pxcptact->SigNum != signum) && (++pxcptact <
	_XcptActTab + _XcptActTabCount) ) ;

#endif	/* MTHREAD */

	if ( pxcptact->SigNum == signum )
		/*
		 * found a table entry corresponding to the signal
		 */
		return(pxcptact);
	else
		/*
		 * found no table entry corresponding to the signal
		 */
		return(NULL);
}

#ifdef	MTHREAD

/***
*int *__fpecode(void) - return pointer to _fpecode field of the tidtable entry
*	for the current thread
*
*Purpose:
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

int * _CRTAPI1 __fpecode (
	void
	)
{
	return( &(_getptd()->_tfpecode) );
}


/***
*void **__pxcptinfoptrs(void) - return pointer to _pxcptinfoptrs field of the
*	tidtable entry for the current thread
*
*Purpose:
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

void ** _CRTAPI1 __pxcptinfoptrs (
	void
	)
{
	return( &(_getptd()->_tpxcptinfoptrs) );
}

#endif

#endif  /* _POSIX_ */