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 _sig_func_ptr _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   return (_sig_func_ptr) atomic_exchange(&_sig_func[sig], func);
124 #else
125   _sig_func_ptr old = _sig_func[sig];
126   _sig_func[sig] = func;
127   return old;
128 #endif
129 }
130 
131 int
raise(int sig)132 raise (int sig)
133 {
134   if (sig < 0 || sig >= _NSIG)
135     {
136       errno = EINVAL;
137       return -1;
138     }
139 
140   for (;;) {
141     _sig_func_ptr func;
142 #ifdef _USE_ATOMIC_SIGNAL
143     func = (_sig_func_ptr) atomic_load(&_sig_func[sig]);
144 #else
145     func = _sig_func[sig];
146 #endif
147     if (func == SIG_IGN)
148       return 0;
149     else if (func == SIG_DFL)
150       _exit(128 + sig);
151 #ifdef _USE_ATOMIC_SIGNAL
152     /* make sure it hasn't changed in the meantime */
153     if (!atomic_compare_exchange_strong(&_sig_func[sig],
154                                         &func,
155                                         SIG_DFL))
156       continue;
157 #else
158     _sig_func[sig] = SIG_DFL;
159 #endif
160     (*func)(sig);
161     return 0;
162   }
163 }
164