xref: /AOO41X/main/sal/osl/os2/signal.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir /* system headers */
30*cdf0e10cSrcweir #include "system.h"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <osl/diagnose.h>
33*cdf0e10cSrcweir #include <osl/mutex.h>
34*cdf0e10cSrcweir #include <osl/signal.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir typedef struct _oslSignalHandlerImpl
37*cdf0e10cSrcweir {
38*cdf0e10cSrcweir 	oslSignalHandlerFunction      Handler;
39*cdf0e10cSrcweir 	void*			        	  pData;
40*cdf0e10cSrcweir 	struct _oslSignalHandlerImpl* pNext;
41*cdf0e10cSrcweir } oslSignalHandlerImpl;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir static sal_Bool				  bErrorReportingEnabled = sal_True;
44*cdf0e10cSrcweir static sal_Bool  			  bInitSignal = sal_False;
45*cdf0e10cSrcweir static oslMutex 			  SignalListMutex;
46*cdf0e10cSrcweir static oslSignalHandlerImpl*  SignalList;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /*static*//* ULONG _Export APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
49*cdf0e10cSrcweir                                             PEXCEPTIONREGISTRATIONRECORD,
50*cdf0e10cSrcweir                                             PCONTEXTRECORD, PVOID);
51*cdf0e10cSrcweir */
52*cdf0e10cSrcweir /*static*/ ULONG __declspec(dllexport) APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
53*cdf0e10cSrcweir                                             PEXCEPTIONREGISTRATIONRECORD,
54*cdf0e10cSrcweir                                             PCONTEXTRECORD, PVOID);
55*cdf0e10cSrcweir static EXCEPTIONREGISTRATIONRECORD ExcptHandler = { 0, SignalHandlerFunction };
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir static sal_Bool InitSignal( void )
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir 	SignalListMutex = osl_createMutex();
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	ExcptHandler.ExceptionHandler = (_ERR *) &SignalHandlerFunction;
62*cdf0e10cSrcweir 	/* DosSetExceptionHandler(&ExcptHandler); */
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	return sal_True;
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir static sal_Bool DeInitSignal( void )
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	/* DosUnsetExceptionHandler(&ExcptHandler); */
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	osl_destroyMutex(SignalListMutex);
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	return sal_False;
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir static oslSignalAction CallSignalHandler(oslSignalInfo *pInfo)
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir 	oslSignalHandlerImpl* pHandler = SignalList;
79*cdf0e10cSrcweir 	oslSignalAction Action = osl_Signal_ActCallNextHdl;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	while (pHandler != NULL)
82*cdf0e10cSrcweir 	{
83*cdf0e10cSrcweir 		if ((Action = pHandler->Handler(pHandler->pData, pInfo)) != osl_Signal_ActCallNextHdl)
84*cdf0e10cSrcweir 			break;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 		pHandler = pHandler->pNext;
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	return Action;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir /*****************************************************************************/
93*cdf0e10cSrcweir /* SignalHandlerFunction	*/
94*cdf0e10cSrcweir /*****************************************************************************/
95*cdf0e10cSrcweir /*static*/ ULONG __declspec(dllexport) APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
96*cdf0e10cSrcweir                                             PEXCEPTIONREGISTRATIONRECORD pERegRec,
97*cdf0e10cSrcweir                                             PCONTEXTRECORD pConRec, PVOID pReserved)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir 	oslSignalInfo	 Info;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 	Info.UserSignal = pERepRec->ExceptionNum;
102*cdf0e10cSrcweir 	Info.UserData   = NULL;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	switch (pERepRec->ExceptionNum)
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		case XCPT_ACCESS_VIOLATION:
107*cdf0e10cSrcweir 			Info.Signal = osl_Signal_AccessViolation;
108*cdf0e10cSrcweir 			break;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 		case XCPT_INTEGER_DIVIDE_BY_ZERO:
111*cdf0e10cSrcweir 			Info.Signal = osl_Signal_IntegerDivideByZero;
112*cdf0e10cSrcweir 			break;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 		case XCPT_BREAKPOINT:
115*cdf0e10cSrcweir 			Info.Signal = osl_Signal_DebugBreak;
116*cdf0e10cSrcweir 			break;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		default:
119*cdf0e10cSrcweir 			Info.Signal = osl_Signal_System;
120*cdf0e10cSrcweir 			break;
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	switch (CallSignalHandler(&Info))
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir 		case osl_Signal_ActCallNextHdl:
126*cdf0e10cSrcweir 			return (XCPT_CONTINUE_SEARCH);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 		case osl_Signal_ActAbortApp:
129*cdf0e10cSrcweir 			return (XCPT_CONTINUE_SEARCH);
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		case osl_Signal_ActKillApp:
132*cdf0e10cSrcweir 			exit(255);
133*cdf0e10cSrcweir 			break;
134*cdf0e10cSrcweir 	}
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	return (XCPT_CONTINUE_SEARCH);
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir /*****************************************************************************/
140*cdf0e10cSrcweir /* osl_addSignalHandler */
141*cdf0e10cSrcweir /*****************************************************************************/
142*cdf0e10cSrcweir oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction Handler, void* pData)
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir 	oslSignalHandlerImpl* pHandler;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 	OSL_ASSERT(Handler != NULL);
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	if (! bInitSignal)
149*cdf0e10cSrcweir 		bInitSignal = InitSignal();
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	pHandler = (oslSignalHandlerImpl*) calloc(1, sizeof(oslSignalHandlerImpl));
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 	if (pHandler != NULL)
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		pHandler->Handler = Handler;
156*cdf0e10cSrcweir 		pHandler->pData   = pData;
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 		osl_acquireMutex(SignalListMutex);
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 		pHandler->pNext = SignalList;
161*cdf0e10cSrcweir 		SignalList      = pHandler;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 		osl_releaseMutex(SignalListMutex);
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 		return (pHandler);
166*cdf0e10cSrcweir 	}
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	return (NULL);
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir /*****************************************************************************/
172*cdf0e10cSrcweir /* osl_removeSignalHandler */
173*cdf0e10cSrcweir /*****************************************************************************/
174*cdf0e10cSrcweir sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler Handler)
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir 	oslSignalHandlerImpl *pHandler, *pPrevious = NULL;
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	OSL_ASSERT(Handler != NULL);
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	if (! bInitSignal)
181*cdf0e10cSrcweir 		bInitSignal = InitSignal();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	osl_acquireMutex(SignalListMutex);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	pHandler = SignalList;
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	while (pHandler != NULL)
188*cdf0e10cSrcweir 	{
189*cdf0e10cSrcweir 		if (pHandler == Handler)
190*cdf0e10cSrcweir 		{
191*cdf0e10cSrcweir 			if (pPrevious)
192*cdf0e10cSrcweir 				pPrevious->pNext = pHandler->pNext;
193*cdf0e10cSrcweir 			else
194*cdf0e10cSrcweir 				SignalList = pHandler->pNext;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 			osl_releaseMutex(SignalListMutex);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 			if (SignalList == NULL )
199*cdf0e10cSrcweir 				bInitSignal = DeInitSignal();
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 			free(pHandler);
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 			return (sal_True);
204*cdf0e10cSrcweir 		}
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 		pPrevious = pHandler;
207*cdf0e10cSrcweir 		pHandler  = pHandler->pNext;
208*cdf0e10cSrcweir 	}
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	osl_releaseMutex(SignalListMutex);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	return (sal_False);
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir /*****************************************************************************/
216*cdf0e10cSrcweir /* osl_raiseSignal */
217*cdf0e10cSrcweir /*****************************************************************************/
218*cdf0e10cSrcweir oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 UserSignal, void* UserData)
219*cdf0e10cSrcweir {
220*cdf0e10cSrcweir 	oslSignalInfo   Info;
221*cdf0e10cSrcweir 	oslSignalAction Action;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 	if (! bInitSignal)
224*cdf0e10cSrcweir 		bInitSignal = InitSignal();
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	osl_acquireMutex(SignalListMutex);
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 	Info.Signal     = osl_Signal_User;
229*cdf0e10cSrcweir 	Info.UserSignal = UserSignal;
230*cdf0e10cSrcweir 	Info.UserData   = UserData;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	Action = CallSignalHandler(&Info);
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	osl_releaseMutex(SignalListMutex);
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	return (Action);
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir /*****************************************************************************/
240*cdf0e10cSrcweir /* osl_setErrorReporting */
241*cdf0e10cSrcweir /*****************************************************************************/
242*cdf0e10cSrcweir sal_Bool SAL_CALL osl_setErrorReporting( sal_Bool bEnable )
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir 	sal_Bool bOld = bErrorReportingEnabled;
245*cdf0e10cSrcweir 	bErrorReportingEnabled = bEnable;
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 	return bOld;
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir 
250