File: gtmci_signals.c

package info (click to toggle)
fis-gtm 7.1-006-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,908 kB
  • sloc: ansic: 344,906; asm: 5,184; csh: 4,859; sh: 2,000; awk: 294; makefile: 73; sed: 13
file content (103 lines) | stat: -rwxr-xr-x 3,337 bytes parent folder | download | duplicates (7)
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
/****************************************************************
 *								*
 *	Copyright 2001, 2009 Fidelity Information Services, Inc	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"
#include "gtm_unistd.h"
#include "gtmci_signals.h"
#include "gtmsiginfo.h"
#include "invocation_mode.h"

/* These routines implement signal handling protocol between gtm and
   external user (if C is base the application):
   +	by switching the active signal handlers between gtm and external user
   +	if signal received in gtm context, it is the responsibility of
   	gtm to execute external handlers.
   + 	similary, external user is responsible to save and execute the gtm
   	signal handlers if signalled in external context.
 */

/* info of the last signal handled by generic_signal_handler */
GBLREF int4 		exi_condition;
GBLREF siginfo_t        exi_siginfo;
GBLDEF gtm_sigcontext_t exi_context;

static struct sigaction *gtm_sig_h; 	/* storage for GT.M handlers */
static struct sigaction *ext_sig_h; 	/* handlers defined in external application */
static boolean_t sig_gtm_ctxt = FALSE;	/* if current signal context is of GT.M */

/* initialize all sig handlers to 'act' and save external handlers if C is the
   base routine in call-ins. Otherwise, if M is the base routine, don't retain the
   old handlers */
void 	sig_save_ext(struct sigaction* act)
{
	int	i;
	if (MUMPS_CALLIN & invocation_mode)
	{  /* C  is the base */
		gtm_sig_h = (struct sigaction*)malloc(NSIG * SIZEOF(struct sigaction));
		ext_sig_h = (struct sigaction*)malloc(NSIG * SIZEOF(struct sigaction));
		for (i = 1; i <= NSIG; i++)
			sigaction(i, act, &ext_sig_h[i-1]);
	} else {  /* GT.M is the base */
		for (i = 1; i <= NSIG; i++)
			sigaction(i, act, 0);
	}
}

void 	sig_save_gtm(void)
{
	int	i;
	if (MUMPS_CALLIN & invocation_mode) {
		for (i = 1; i <= NSIG; i++)
			sigaction(i, 0, &gtm_sig_h[i-1]);
		sig_gtm_ctxt = TRUE;
	}
}

void	sig_switch_gtm(void) /* switch to GT.M signal context */
{
	int	i;
	if ((MUMPS_CALLIN & invocation_mode) && !sig_gtm_ctxt) {
		for (i = 1; i <= NSIG; i++)
			sigaction(i, &gtm_sig_h[i-1], &ext_sig_h[i-1]);
		sig_gtm_ctxt = TRUE;
	}
}

void	sig_switch_ext(void) /* switch to external signal context */
{
	int	i;
	if ((MUMPS_CALLIN & invocation_mode) && sig_gtm_ctxt) {
		for (i = 1; i <= NSIG; i++)
			sigaction(i, &ext_sig_h[i-1], &gtm_sig_h[i-1]);
		sig_gtm_ctxt = FALSE;
	}
}

/* identify any signal received in GT.M context, and handle them in the context
   of external user */
void 	gtmci_exit_handler(void)
{
	static boolean_t 	handler_active = FALSE;
	struct sigaction 	*act, ignore;

	if ((0 >= exi_condition && NSIG < exi_condition) || !(MUMPS_CALLIN & invocation_mode) ||
			!(MUMPS_GTMCI & invocation_mode) || handler_active)
		return;
	handler_active = TRUE;
	sig_switch_ext();
	act = &ext_sig_h[exi_condition-1];
	if (SIG_IGN == act->sa_handler || SIG_DFL == act->sa_handler)
		return;
	if (act->sa_flags & SA_SIGINFO)
		(*act->sa_sigaction)(exi_condition, &exi_siginfo, &exi_context);
	else
		(*act->sa_handler)(exi_condition);
}