xref: /haiku/src/system/libroot/posix/signal/signal.cpp (revision 24df65921befcd0ad0c5c7866118f922da61cb96)
1*24df6592SIngo Weinhold /*
2*24df6592SIngo Weinhold  * Copyright 2002-2011, Haiku, Inc. All rights reserved.
3*24df6592SIngo Weinhold  * Distributed under the terms of the MIT license.
4*24df6592SIngo Weinhold  *
5*24df6592SIngo Weinhold  * Author:
6*24df6592SIngo Weinhold  *		Daniel Reinhold, danielre@users.sf.net
7*24df6592SIngo Weinhold  *		Ingo Weinhold, ingo_weinhold@gmx.de
8*24df6592SIngo Weinhold  */
9*24df6592SIngo Weinhold 
10*24df6592SIngo Weinhold 
11*24df6592SIngo Weinhold #include <signal.h>
12*24df6592SIngo Weinhold 
13*24df6592SIngo Weinhold #include <errno.h>
14*24df6592SIngo Weinhold #include <stdio.h>
15*24df6592SIngo Weinhold 
16*24df6592SIngo Weinhold #include <symbol_versioning.h>
17*24df6592SIngo Weinhold 
18*24df6592SIngo Weinhold #include <signal_private.h>
19*24df6592SIngo Weinhold 
20*24df6592SIngo Weinhold 
21*24df6592SIngo Weinhold static __sighandler_t
22*24df6592SIngo Weinhold signal_common(int signal, __sighandler_t signalHandler, int flags)
23*24df6592SIngo Weinhold {
24*24df6592SIngo Weinhold 	struct sigaction newAction, oldAction;
25*24df6592SIngo Weinhold 
26*24df6592SIngo Weinhold 	// setup the replacement sigaction
27*24df6592SIngo Weinhold 	newAction.sa_handler = signalHandler;
28*24df6592SIngo Weinhold 	newAction.sa_mask = 0;
29*24df6592SIngo Weinhold 	newAction.sa_flags = flags;
30*24df6592SIngo Weinhold 
31*24df6592SIngo Weinhold 	if (__sigaction(signal, &newAction, &oldAction) != 0)
32*24df6592SIngo Weinhold 		return SIG_ERR;
33*24df6592SIngo Weinhold 
34*24df6592SIngo Weinhold 	// success, return the original handler
35*24df6592SIngo Weinhold 	return oldAction.sa_handler;
36*24df6592SIngo Weinhold }
37*24df6592SIngo Weinhold 
38*24df6592SIngo Weinhold 
39*24df6592SIngo Weinhold __sighandler_t
40*24df6592SIngo Weinhold __signal_beos(int signal, __sighandler_t signalHandler)
41*24df6592SIngo Weinhold {
42*24df6592SIngo Weinhold 	// check signal range
43*24df6592SIngo Weinhold 	if (signal < 0 || signal > MAX_SIGNAL_NUMBER_BEOS) {
44*24df6592SIngo Weinhold 		errno = EINVAL;
45*24df6592SIngo Weinhold 		return SIG_ERR;
46*24df6592SIngo Weinhold 	}
47*24df6592SIngo Weinhold 
48*24df6592SIngo Weinhold 	// set the signal handler
49*24df6592SIngo Weinhold 	__sighandler_t result = signal_common(signal, signalHandler,
50*24df6592SIngo Weinhold 		SA_BEOS_COMPATIBLE_HANDLER);
51*24df6592SIngo Weinhold 	if (result == SIG_ERR)
52*24df6592SIngo Weinhold 		return SIG_ERR;
53*24df6592SIngo Weinhold 
54*24df6592SIngo Weinhold 	// If the signal is SIGSEGV, set the same signal handler for SIGBUS. Those
55*24df6592SIngo Weinhold 	// constants had the same value under BeOS.
56*24df6592SIngo Weinhold 	if (signal == SIGSEGV)
57*24df6592SIngo Weinhold 		signal_common(SIGBUS, signalHandler, SA_BEOS_COMPATIBLE_HANDLER);
58*24df6592SIngo Weinhold 
59*24df6592SIngo Weinhold 	return result;
60*24df6592SIngo Weinhold }
61*24df6592SIngo Weinhold 
62*24df6592SIngo Weinhold 
63*24df6592SIngo Weinhold __sighandler_t
64*24df6592SIngo Weinhold __signal(int signal, __sighandler_t signalHandler)
65*24df6592SIngo Weinhold {
66*24df6592SIngo Weinhold 	return signal_common(signal, signalHandler, 0);
67*24df6592SIngo Weinhold }
68*24df6592SIngo Weinhold 
69*24df6592SIngo Weinhold 
70*24df6592SIngo Weinhold DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("__signal_beos", "signal@", "BASE");
71*24df6592SIngo Weinhold 
72*24df6592SIngo Weinhold DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("__signal", "signal@@", "1_ALPHA4");
73