2 * <signal.h> wrapper functions.
5 * Jonathan Pryor (jonpryor@vt.edu)
6 * Jonathan Pryor (jpryor@novell.com)
8 * Copyright (C) 2004-2005 Jonathan Pryor
9 * Copyright (C) 2008 Novell, Inc.
17 #ifndef PLATFORM_WIN32
19 #include <sys/types.h>
24 #include <mono/io-layer/atomic.h>
29 typedef void (*mph_sighandler_t)(int);
30 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
32 static int count_handlers (int signum);
35 Mono_Posix_Stdlib_SIG_DFL (void)
41 Mono_Posix_Stdlib_SIG_ERR (void)
47 Mono_Posix_Stdlib_SIG_IGN (void)
53 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
55 mph_sighandler_t _h = (mph_sighandler_t) handler;
59 int Mono_Posix_SIGRTMIN (void)
63 #else /* def SIGRTMIN */
65 #endif /* ndef SIGRTMIN */
68 int Mono_Posix_SIGRTMAX (void)
72 #else /* def SIGRTMAX */
74 #endif /* ndef SIGRTMAX */
77 int Mono_Posix_FromRealTimeSignum (int offset, int *r)
84 #if defined (SIGRTMIN) && defined (SIGRTMAX)
85 if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
91 #else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
96 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
99 #ifndef PLATFORM_WIN32
101 #ifdef WAPI_ATOMIC_ASM
102 #define mph_int_get(p) InterlockedExchangeAdd ((p), 0)
103 #define mph_int_inc(p) InterlockedIncrement ((p))
104 #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
105 #elif GLIB_CHECK_VERSION(2,4,0)
106 #define mph_int_get(p) g_atomic_int_get ((p))
107 #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
108 #define mph_int_set(p,o,n) do { \
109 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
112 #define mph_int_get(p) (*(p))
113 #define mph_int_inc(p) do { (*(p))++; } while (0)
114 #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
118 Mono_Posix_Syscall_psignal (int sig, const char* s)
122 return errno == 0 ? 0 : -1;
125 #define NUM_SIGNALS 64
126 static signal_info signals[NUM_SIGNALS];
129 default_handler (int signum)
132 for (i = 0; i < NUM_SIGNALS; ++i) {
134 signal_info* h = &signals [i];
135 if (mph_int_get (&h->signum) != signum)
137 mph_int_inc (&h->count);
138 fd = mph_int_get (&h->write_fd);
146 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
149 Mono_Unix_UnixSignal_install (int sig)
152 signal_info* h = NULL;
153 int have_handler = 0;
154 void* handler = NULL;
156 mr = pthread_mutex_lock (&signals_mutex);
162 #if defined (SIGRTMIN) && defined (SIGRTMAX)
163 /*The runtime uses some rt signals for itself so it's important to not override them.*/
164 if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
165 struct sigaction sinfo;
166 sigaction (sig, NULL, &sinfo);
167 if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
168 pthread_mutex_unlock (&signals_mutex);
173 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
175 for (i = 0; i < NUM_SIGNALS; ++i) {
176 if (h == NULL && signals [i].signum == 0) {
178 h->handler = signal (sig, default_handler);
179 if (h->handler == SIG_ERR) {
188 if (!have_handler && signals [i].signum == sig &&
189 signals [i].handler != default_handler) {
191 handler = signals [i].handler;
193 if (h && have_handler)
197 if (h && have_handler) {
199 h->handler = handler;
203 mph_int_set (&h->count, h->count, 0);
204 mph_int_set (&h->signum, h->signum, sig);
207 pthread_mutex_unlock (&signals_mutex);
213 count_handlers (int signum)
217 for (i = 0; i < NUM_SIGNALS; ++i) {
218 if (signals [i].signum == signum)
225 Mono_Unix_UnixSignal_uninstall (void* info)
230 mr = pthread_mutex_lock (&signals_mutex);
238 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
241 /* last UnixSignal -- we can unregister */
242 if (h->have_handler && count_handlers (h->signum) == 1) {
243 mph_sighandler_t p = signal (h->signum, h->handler);
252 pthread_mutex_unlock (&signals_mutex);
258 setup_pipes (signal_info** signals, int count, fd_set *read_fds, int *max_fd)
261 for (i = 0; i < count; ++i) {
265 if ((r = pipe (filedes)) != 0) {
269 h->read_fd = filedes [0];
270 h->write_fd = filedes [1];
271 if (h->read_fd > *max_fd)
272 *max_fd = h->read_fd;
273 FD_SET (h->read_fd, read_fds);
279 teardown_pipes (signal_info** signals, int count)
282 for (i = 0; i < count; ++i) {
283 signal_info* h = signals [i];
286 if (h->write_fd != 0)
294 wait_for_any (signal_info** signals, int count, int max_fd, fd_set* read_fds, int timeout)
299 struct timeval *ptv = NULL;
301 tv.tv_sec = timeout / 1000;
302 tv.tv_usec = (timeout % 1000)*1000;
306 r = select (max_fd + 1, read_fds, NULL, NULL, ptv);
307 } while (r == -1 && errno == EINTR);
314 for (i = 0; i < count; ++i) {
315 signal_info* h = signals [i];
316 if (FD_ISSET (h->read_fd, read_fds)) {
318 read (h->read_fd, &c, 1); /* ignore any error */
329 * returns: -1 on error:
331 * index into _signals array of signal that was generated on success
334 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
340 signal_info** signals = (signal_info**) _signals;
342 mr = pthread_mutex_lock (&signals_mutex);
350 r = setup_pipes (signals, count, &read_fds, &max_fd);
352 r = wait_for_any (signals, count, max_fd, &read_fds, timeout);
354 teardown_pipes (signals, count);
356 pthread_mutex_unlock (&signals_mutex);
361 #endif /* ndef PLATFORM_WIN32 */