2010-06-17 Zoltan Varga <vargaz@gmail.com>
[mono.git] / support / signal.c
blobabd76382f0bc5646fdef31b4e1060f083f26a28d
1 /*
2 * <signal.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
6 * Jonathan Pryor (jpryor@novell.com)
7 * Tim Jenks (tim.jenks@realtimeworlds.com)
9 * Copyright (C) 2004-2005 Jonathan Pryor
10 * Copyright (C) 2008 Novell, Inc.
13 #include <signal.h>
15 #include "map.h"
16 #include "mph.h"
18 #ifndef HOST_WIN32
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #if defined(__APPLE__)
22 #include "fakepoll.h"
23 #else
24 #include <poll.h>
25 #endif
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <mono/io-layer/atomic.h>
31 #include <mono/metadata/appdomain.h>
32 #endif
34 G_BEGIN_DECLS
36 typedef void (*mph_sighandler_t)(int);
37 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
39 static int count_handlers (int signum);
41 void*
42 Mono_Posix_Stdlib_SIG_DFL (void)
44 return SIG_DFL;
47 void*
48 Mono_Posix_Stdlib_SIG_ERR (void)
50 return SIG_ERR;
53 void*
54 Mono_Posix_Stdlib_SIG_IGN (void)
56 return SIG_IGN;
59 void
60 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
62 mph_sighandler_t _h = (mph_sighandler_t) handler;
63 _h (signum);
66 int Mono_Posix_SIGRTMIN (void)
68 #ifdef SIGRTMIN
69 return SIGRTMIN;
70 #else /* def SIGRTMIN */
71 return -1;
72 #endif /* ndef SIGRTMIN */
75 int Mono_Posix_SIGRTMAX (void)
77 #ifdef SIGRTMAX
78 return SIGRTMAX;
79 #else /* def SIGRTMAX */
80 return -1;
81 #endif /* ndef SIGRTMAX */
84 int Mono_Posix_FromRealTimeSignum (int offset, int *r)
86 if (NULL == r) {
87 errno = EINVAL;
88 return -1;
90 *r = 0;
91 #if defined (SIGRTMIN) && defined (SIGRTMAX)
92 if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
93 errno = EINVAL;
94 return -1;
96 *r = SIGRTMIN+offset;
97 return 0;
98 #else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
99 # ifdef ENOSYS
100 errno = ENOSYS;
101 # endif /* ENOSYS */
102 return -1;
103 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
106 #ifndef HOST_WIN32
108 #ifdef WAPI_ATOMIC_ASM
109 #define mph_int_get(p) InterlockedExchangeAdd ((p), 0)
110 #define mph_int_inc(p) InterlockedIncrement ((p))
111 #define mph_int_dec_test(p) (InterlockedDecrement ((p)) == 0)
112 #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
113 #elif GLIB_CHECK_VERSION(2,4,0)
114 #define mph_int_get(p) g_atomic_int_get ((p))
115 #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
116 #define mph_int_dec_test(p) g_atomic_int_dec_and_test ((p))
117 #define mph_int_set(p,o,n) do { \
118 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
119 } while (0)
120 #else
121 #define mph_int_get(p) (*(p))
122 #define mph_int_inc(p) do { (*(p))++; } while (0)
123 #define mph_int_dec_test(p) (--(*(p)) == 0)
124 #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
125 #endif
127 #if HAVE_PSIGNAL
129 Mono_Posix_Syscall_psignal (int sig, const char* s)
131 errno = 0;
132 psignal (sig, s);
133 return errno == 0 ? 0 : -1;
135 #endif /* def HAVE_PSIGNAL */
137 #define NUM_SIGNALS 64
138 static signal_info signals[NUM_SIGNALS];
140 static int acquire_mutex (pthread_mutex_t *mutex)
142 int mr;
143 while ((mr = pthread_mutex_lock (mutex)) == EAGAIN) {
144 /* try to acquire again */
146 if ((mr != 0) && (mr != EDEADLK)) {
147 errno = mr;
148 return -1;
150 return 0;
153 static void release_mutex (pthread_mutex_t *mutex)
155 int mr;
156 while ((mr = pthread_mutex_unlock (mutex)) == EAGAIN) {
157 /* try to release mutex again */
161 static inline int
162 keep_trying (int r)
164 return r == -1 && errno == EINTR;
167 static void
168 default_handler (int signum)
170 int i;
171 for (i = 0; i < NUM_SIGNALS; ++i) {
172 int fd;
173 signal_info* h = &signals [i];
174 if (mph_int_get (&h->signum) != signum)
175 continue;
176 mph_int_inc (&h->count);
177 fd = mph_int_get (&h->write_fd);
178 if (fd > 0) {
179 int j,pipecounter;
180 char c = signum;
181 pipecounter = mph_int_get (&h->pipecnt);
182 for (j = 0; j < pipecounter; ++j) {
183 int r;
184 do { r = write (fd, &c, 1); } while (keep_trying (r));
185 fsync (fd); /* force */
191 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
193 void*
194 Mono_Unix_UnixSignal_install (int sig)
196 int i;
197 signal_info* h = NULL;
198 int have_handler = 0;
199 void* handler = NULL;
201 if (acquire_mutex (&signals_mutex) == -1)
202 return NULL;
204 #if defined (SIGRTMIN) && defined (SIGRTMAX)
205 /*The runtime uses some rt signals for itself so it's important to not override them.*/
206 if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
207 struct sigaction sinfo;
208 sigaction (sig, NULL, &sinfo);
209 if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
210 pthread_mutex_unlock (&signals_mutex);
211 errno = EADDRINUSE;
212 return NULL;
215 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
217 for (i = 0; i < NUM_SIGNALS; ++i) {
218 if (h == NULL && signals [i].signum == 0) {
219 h = &signals [i];
220 h->handler = signal (sig, default_handler);
221 if (h->handler == SIG_ERR) {
222 h->handler = NULL;
223 h = NULL;
224 break;
226 else {
227 h->have_handler = 1;
230 if (!have_handler && signals [i].signum == sig &&
231 signals [i].handler != default_handler) {
232 have_handler = 1;
233 handler = signals [i].handler;
235 if (h && have_handler)
236 break;
239 if (h && have_handler) {
240 h->have_handler = 1;
241 h->handler = handler;
244 if (h) {
245 mph_int_set (&h->count, h->count, 0);
246 mph_int_set (&h->signum, h->signum, sig);
247 mph_int_set (&h->pipecnt, h->pipecnt, 0);
250 release_mutex (&signals_mutex);
252 return h;
255 static int
256 count_handlers (int signum)
258 int i;
259 int count = 0;
260 for (i = 0; i < NUM_SIGNALS; ++i) {
261 if (signals [i].signum == signum)
262 ++count;
264 return count;
268 Mono_Unix_UnixSignal_uninstall (void* info)
270 signal_info* h;
271 int r = -1;
273 if (acquire_mutex (&signals_mutex) == -1)
274 return -1;
276 h = info;
278 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
279 errno = EINVAL;
280 else {
281 /* last UnixSignal -- we can unregister */
282 if (h->have_handler && count_handlers (h->signum) == 1) {
283 mph_sighandler_t p = signal (h->signum, h->handler);
284 if (p != SIG_ERR)
285 r = 0;
286 h->handler = NULL;
287 h->have_handler = 0;
289 h->signum = 0;
292 release_mutex (&signals_mutex);
294 return r;
297 static int
298 setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
300 int i;
301 int r = 0;
302 for (i = 0; i < count; ++i) {
303 signal_info* h;
304 int filedes[2];
306 h = signals [i];
308 if (mph_int_get (&h->pipecnt) == 0) {
309 if ((r = pipe (filedes)) != 0) {
310 break;
312 h->read_fd = filedes [0];
313 h->write_fd = filedes [1];
315 mph_int_inc (&h->pipecnt);
316 fd_structs[*currfd].fd = h->read_fd;
317 fd_structs[*currfd].events = POLLIN;
318 ++(*currfd);
320 return r;
323 static void
324 teardown_pipes (signal_info** signals, int count)
326 int i;
327 for (i = 0; i < count; ++i) {
328 signal_info* h = signals [i];
330 if (mph_int_dec_test (&h->pipecnt)) {
331 if (h->read_fd != 0)
332 close (h->read_fd);
333 if (h->write_fd != 0)
334 close (h->write_fd);
335 h->read_fd = 0;
336 h->write_fd = 0;
341 static int
342 wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_structs, int timeout, Mono_Posix_RuntimeIsShuttingDown shutting_down)
344 int r, idx;
345 do {
346 struct timeval tv;
347 struct timeval *ptv = NULL;
348 if (timeout != -1) {
349 tv.tv_sec = timeout / 1000;
350 tv.tv_usec = (timeout % 1000)*1000;
351 ptv = &tv;
353 r = poll (fd_structs, count, timeout);
354 } while (keep_trying (r) && !shutting_down ());
356 idx = -1;
357 if (r == 0)
358 idx = timeout;
359 else if (r > 0) {
360 int i;
361 for (i = 0; i < count; ++i) {
362 signal_info* h = signals [i];
363 if (fd_structs[i].revents & POLLIN) {
364 int r;
365 char c;
366 do {
367 r = read (h->read_fd, &c, 1);
368 } while (keep_trying (r) && !shutting_down ());
369 if (idx == -1)
370 idx = i;
375 return idx;
379 * returns: -1 on error:
380 * timeout on timeout
381 * index into _signals array of signal that was generated on success
384 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down)
386 int r;
387 int currfd = 0;
388 struct pollfd fd_structs[NUM_SIGNALS];
390 signal_info** signals = (signal_info**) _signals;
392 if (count > NUM_SIGNALS)
393 return -1;
395 if (acquire_mutex (&signals_mutex) == -1)
396 return -1;
398 r = setup_pipes (signals, count, &fd_structs[0], &currfd);
400 release_mutex (&signals_mutex);
402 if (r == 0) {
403 r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout, shutting_down);
406 if (acquire_mutex (&signals_mutex) == -1)
407 return -1;
409 teardown_pipes (signals, count);
411 release_mutex (&signals_mutex);
413 return r;
416 #endif /* ndef HOST_WIN32 */
419 G_END_DECLS
422 * vim: noexpandtab