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