1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /*
18 FUNCTION
19 <<signal>>---specify handler subroutine for a signal
20
21 INDEX
22 signal
23
24 SYNOPSIS
25 #include <signal.h>
26 void (*signal(int <[sig]>, void(*<[func]>)(int))) (int);
27
28 DESCRIPTION
29 <<signal>> provides a simple signal-handling implementation for embedded
30 targets.
31
32 <<signal>> allows you to request changed treatment for a particular
33 signal <[sig]>. You can use one of the predefined macros <<SIG_DFL>>
34 (select system default handling) or <<SIG_IGN>> (ignore this signal)
35 as the value of <[func]>; otherwise, <[func]> is a function pointer
36 that identifies a subroutine in your program as the handler for this signal.
37
38 Some of the execution environment for signal handlers is
39 unpredictable; notably, the only library function required to work
40 correctly from within a signal handler is <<signal>> itself, and
41 only when used to redefine the handler for the current signal value.
42
43 Static storage is likewise unreliable for signal handlers, with one
44 exception: if you declare a static storage location as `<<volatile
45 sig_atomic_t>>', then you may use that location in a signal handler to
46 store signal values.
47
48 If your signal handler terminates using <<return>> (or implicit
49 return), your program's execution continues at the point
50 where it was when the signal was raised (whether by your program
51 itself, or by an external event). Signal handlers can also
52 use functions such as <<exit>> and <<abort>> to avoid returning.
53
54 @c FIXME: do we have setjmp.h and assoc fns?
55
56 RETURNS
57 If your request for a signal handler cannot be honored, the result is
58 <<SIG_ERR>>; a specific error number is also recorded in <<errno>>.
59
60 Otherwise, the result is the previous handler (a function pointer or
61 one of the predefined macros).
62
63 PORTABILITY
64 ANSI C requires <<signal>>.
65
66 No supporting OS subroutines are required to link with <<signal>>, but
67 it will not have any useful effects, except for software generated signals,
68 without an operating system that can actually raise exceptions.
69 */
70
71 /*
72 * signal.c
73 * Original Author: G. Haley
74 *
75 * signal associates the function pointed to by func with the signal sig. When
76 * a signal occurs, the value of func determines the action taken as follows:
77 * if func is SIG_DFL, the default handling for that signal will occur; if func
78 * is SIG_IGN, the signal will be ignored; otherwise, the default handling for
79 * the signal is restored (SIG_DFL), and the function func is called with sig
80 * as its argument. Returns the value of func for the previous call to signal
81 * for the signal sig, or SIG_ERR if the request fails.
82 */
83
84 #define _DEFAULT_SOURCE
85 #include <errno.h>
86 #include <signal.h>
87 #include <unistd.h>
88 #include <_syslist.h>
89
90 #ifdef _PICOLIBC_ATOMIC_SIGNAL
91
92 #if __SIZEOF_POINTER__ == 2 && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
93 #define _USE_ATOMIC_SIGNAL
94 #endif
95
96 #if __SIZEOF_POINTER__ == 4 && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
97 #define _USE_ATOMIC_SIGNAL
98 #endif
99
100 #if __SIZEOF_POINTER__ == 8 && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
101 #define _USE_ATOMIC_SIGNAL
102 #endif
103
104 #endif
105
106 #ifdef _USE_ATOMIC_SIGNAL
107 #include <stdatomic.h>
108 static _Atomic uintptr_t _sig_func[NSIG];
109 #else
110 static _sig_func_ptr _sig_func[NSIG];
111 #endif
112
113 _sig_func_ptr
signal(int sig,_sig_func_ptr func)114 signal (int sig, _sig_func_ptr func)
115 {
116 if (sig < 0 || sig >= NSIG)
117 {
118 errno = EINVAL;
119 return SIG_ERR;
120 }
121
122 #ifdef _USE_ATOMIC_SIGNAL
123 uintptr_t ifunc = (uintptr_t) func;
124 return (_sig_func_ptr) atomic_exchange(&_sig_func[sig], ifunc);
125 #else
126 _sig_func_ptr old = _sig_func[sig];
127 _sig_func[sig] = func;
128 return old;
129 #endif
130 }
131
132 int
raise(int sig)133 raise (int sig)
134 {
135 if (sig < 0 || sig >= NSIG)
136 {
137 errno = EINVAL;
138 return -1;
139 }
140
141 for (;;) {
142 _sig_func_ptr func;
143 #ifdef _USE_ATOMIC_SIGNAL
144 func = (_sig_func_ptr) atomic_load(&_sig_func[sig]);
145 #else
146 func = _sig_func[sig];
147 #endif
148 if (func == SIG_IGN)
149 return 0;
150 else if (func == SIG_DFL)
151 _exit(128 + sig);
152 #ifdef _USE_ATOMIC_SIGNAL
153 /* make sure it hasn't changed in the meantime */
154 if (!atomic_compare_exchange_strong(&_sig_func[sig],
155 (uintptr_t *) &func,
156 (uintptr_t) SIG_DFL))
157 continue;
158 #else
159 _sig_func[sig] = SIG_DFL;
160 #endif
161 (*func)(sig);
162 return 0;
163 }
164 }
165