Merge pull request #1840 from ludovic-henry/iolayer-thread-interrupt
[mono.git] / support / signal.c
1 /*
2  * <signal.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *   Jonathan Pryor (jpryor@novell.com)
7  *   Tim Jenks (tim.jenks@realtimeworlds.com)
8  *
9  * Copyright (C) 2004-2005 Jonathan Pryor
10  * Copyright (C) 2008 Novell, Inc.
11  */
12
13 #include <signal.h>
14
15 #include "map.h"
16 #include "mph.h"
17
18 #ifndef HOST_WIN32
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #if defined(HAVE_POLL_H)
22 #include <poll.h>
23 #elif defined(HAVE_SYS_POLL_H)
24 #include <sys/poll.h>
25 #endif
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <mono/utils/atomic.h>
31 #include <mono/metadata/appdomain.h>
32 #endif
33
34 G_BEGIN_DECLS
35
36 typedef void (*mph_sighandler_t)(int);
37 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
38
39 #ifndef HOST_WIN32
40 static int count_handlers (int signum);
41 #endif
42
43 void*
44 Mono_Posix_Stdlib_SIG_DFL (void)
45 {
46         return SIG_DFL;
47 }
48
49 void*
50 Mono_Posix_Stdlib_SIG_ERR (void)
51 {
52         return SIG_ERR;
53 }
54
55 void*
56 Mono_Posix_Stdlib_SIG_IGN (void)
57 {
58         return SIG_IGN;
59 }
60
61 void
62 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
63 {
64         mph_sighandler_t _h = (mph_sighandler_t) handler;
65         _h (signum);
66 }
67
68 int Mono_Posix_SIGRTMIN (void)
69 {
70 #ifdef SIGRTMIN
71         return SIGRTMIN;
72 #else /* def SIGRTMIN */
73         return -1;
74 #endif /* ndef SIGRTMIN */
75 }
76
77 int Mono_Posix_SIGRTMAX (void)
78 {
79 #ifdef SIGRTMAX
80         return SIGRTMAX;
81 #else /* def SIGRTMAX */
82         return -1;
83 #endif /* ndef SIGRTMAX */
84 }
85
86 int Mono_Posix_FromRealTimeSignum (int offset, int *r)
87 {
88         if (NULL == r) {
89                 errno = EINVAL;
90                 return -1;
91         }
92         *r = 0;
93 #if defined (SIGRTMIN) && defined (SIGRTMAX)
94         if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
95                 errno = EINVAL;
96                 return -1;
97         }
98         *r = SIGRTMIN+offset;
99         return 0;
100 #else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
101 # ifdef ENOSYS
102         errno = ENOSYS;
103 # endif /* ENOSYS */
104         return -1;
105 #endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
106 }
107
108 #ifndef HOST_WIN32
109
110 #ifndef WAPI_NO_ATOMIC_ASM
111         #define mph_int_get(p)     InterlockedExchangeAdd ((p), 0)
112         #define mph_int_inc(p)     InterlockedIncrement ((p))
113         #define mph_int_dec_test(p)     (InterlockedDecrement ((p)) == 0)
114         #define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
115 #elif GLIB_CHECK_VERSION(2,4,0)
116         #define mph_int_get(p) g_atomic_int_get ((p))
117         #define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
118         #define mph_int_dec_test(p) g_atomic_int_dec_and_test ((p))
119         #define mph_int_set(p,o,n) do {                                 \
120                 while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
121         } while (0)
122 #else
123         #define mph_int_get(p) (*(p))
124         #define mph_int_inc(p) do { (*(p))++; } while (0)
125         #define mph_int_dec_test(p) (--(*(p)) == 0)
126         #define mph_int_set(p,o,n) do { *(p) = n; } while (0)
127 #endif
128
129 #if HAVE_PSIGNAL
130 int
131 Mono_Posix_Syscall_psignal (int sig, const char* s)
132 {
133         errno = 0;
134         psignal (sig, s);
135         return errno == 0 ? 0 : -1;
136 }
137 #endif  /* def HAVE_PSIGNAL */
138
139 #define NUM_SIGNALS 64
140 static signal_info signals[NUM_SIGNALS];
141
142 static int acquire_mutex (pthread_mutex_t *mutex)
143 {
144         int mr;
145         while ((mr = pthread_mutex_lock (mutex)) == EAGAIN) {
146                 /* try to acquire again */
147         }
148         if ((mr != 0) && (mr != EDEADLK))  {
149                 errno = mr;
150                 return -1;
151         }
152         return 0;
153 }
154
155 static void release_mutex (pthread_mutex_t *mutex)
156 {
157         int mr;
158         while ((mr = pthread_mutex_unlock (mutex)) == EAGAIN) {
159                 /* try to release mutex again */
160         }
161 }
162
163 static inline int
164 keep_trying (int r)
165 {
166         return r == -1 && errno == EINTR;
167 }
168
169 static void
170 default_handler (int signum)
171 {
172         int i;
173         for (i = 0; i < NUM_SIGNALS; ++i) {
174                 int fd;
175                 signal_info* h = &signals [i];
176                 if (mph_int_get (&h->signum) != signum)
177                         continue;
178                 mph_int_inc (&h->count);
179                 fd = mph_int_get (&h->write_fd);
180                 if (fd > 0) {
181                         int j,pipecounter;
182                         char c = signum;
183                         pipecounter = mph_int_get (&h->pipecnt);
184                         for (j = 0; j < pipecounter; ++j) {
185                                 int r;
186                                 do { r = write (fd, &c, 1); } while (keep_trying (r));
187                                 fsync (fd); /* force */
188                         }
189                 }
190         }
191 }
192
193 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
194
195 void*
196 Mono_Unix_UnixSignal_install (int sig)
197 {
198 #if defined(HAVE_SIGNAL)
199         int i;
200         signal_info* h = NULL; 
201         int have_handler = 0;
202         void* handler = NULL;
203
204         if (acquire_mutex (&signals_mutex) == -1)
205                 return NULL;
206
207 #if defined (SIGRTMIN) && defined (SIGRTMAX)
208         /*The runtime uses some rt signals for itself so it's important to not override them.*/
209         if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
210                 struct sigaction sinfo;
211                 sigaction (sig, NULL, &sinfo);
212                 if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
213                         pthread_mutex_unlock (&signals_mutex);
214                         errno = EADDRINUSE;
215                         return NULL;
216                 }
217         }
218 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
219
220         for (i = 0; i < NUM_SIGNALS; ++i) {
221                 if (h == NULL && signals [i].signum == 0) {
222                         h = &signals [i];
223                         h->handler = signal (sig, default_handler);
224                         if (h->handler == SIG_ERR) {
225                                 h->handler = NULL;
226                                 h = NULL;
227                                 break;
228                         }
229                         else {
230                                 h->have_handler = 1;
231                         }
232                 }
233                 if (!have_handler && signals [i].signum == sig &&
234                                 signals [i].handler != default_handler) {
235                         have_handler = 1;
236                         handler = signals [i].handler;
237                 }
238                 if (h && have_handler)
239                         break;
240         }
241
242         if (h && have_handler) {
243                 h->have_handler = 1;
244                 h->handler      = handler;
245         }
246
247         if (h) {
248                 mph_int_set (&h->count, h->count, 0);
249                 mph_int_set (&h->signum, h->signum, sig);
250                 mph_int_set (&h->pipecnt, h->pipecnt, 0);
251         }
252
253         release_mutex (&signals_mutex);
254
255         return h;
256 #else
257         g_error ("signal() is not supported by this platform");
258         return 0;
259 #endif
260 }
261
262 static int
263 count_handlers (int signum)
264 {
265         int i;
266         int count = 0;
267         for (i = 0; i < NUM_SIGNALS; ++i) {
268                 if (signals [i].signum == signum)
269                         ++count;
270         }
271         return count;
272 }
273
274 int
275 Mono_Unix_UnixSignal_uninstall (void* info)
276 {
277 #if defined(HAVE_SIGNAL)        
278         signal_info* h;
279         int r = -1;
280
281         if (acquire_mutex (&signals_mutex) == -1)
282                 return -1;
283
284         h = info;
285
286         if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
287                 errno = EINVAL;
288         else {
289                 /* last UnixSignal -- we can unregister */
290                 if (h->have_handler && count_handlers (h->signum) == 1) {
291                         mph_sighandler_t p = signal (h->signum, h->handler);
292                         if (p != SIG_ERR)
293                                 r = 0;
294                         h->handler      = NULL;
295                         h->have_handler = 0;
296                 }
297                 h->signum = 0;
298         }
299
300         release_mutex (&signals_mutex);
301
302         return r;
303 #else
304         g_error ("signal() is not supported by this platform");
305         return 0;
306 #endif
307 }
308
309 static int
310 setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
311 {
312         int i;
313         int r = 0;
314         for (i = 0; i < count; ++i) {
315                 signal_info* h;
316                 int filedes[2];
317
318                 h = signals [i];
319
320                 if (mph_int_get (&h->pipecnt) == 0) {
321                         if ((r = pipe (filedes)) != 0) {
322                                 break;
323                         }
324                         h->read_fd  = filedes [0];
325                         h->write_fd = filedes [1];
326                 }
327                 mph_int_inc (&h->pipecnt);
328                 fd_structs[*currfd].fd = h->read_fd;
329                 fd_structs[*currfd].events = POLLIN;
330                 ++(*currfd);
331         }
332         return r;
333 }
334
335 static void
336 teardown_pipes (signal_info** signals, int count)
337 {
338         int i;
339         for (i = 0; i < count; ++i) {
340                 signal_info* h = signals [i];
341
342                 if (mph_int_dec_test (&h->pipecnt)) {
343                         if (h->read_fd != 0)
344                                 close (h->read_fd);
345                         if (h->write_fd != 0)
346                                 close (h->write_fd);
347                         h->read_fd  = 0;
348                         h->write_fd = 0;
349                 }
350         }
351 }
352
353 static int
354 wait_for_any (signal_info** signals, int count, int *currfd, struct pollfd* fd_structs, int timeout, Mono_Posix_RuntimeIsShuttingDown shutting_down)
355 {
356         int r, idx;
357         do {
358                 struct timeval tv;
359                 struct timeval *ptv = NULL;
360                 if (timeout != -1) {
361                         tv.tv_sec  = timeout / 1000;
362                         tv.tv_usec = (timeout % 1000)*1000;
363                         ptv = &tv;
364                 }
365                 r = poll (fd_structs, count, timeout);
366         } while (keep_trying (r) && !shutting_down ());
367
368         idx = -1;
369         if (r == 0)
370                 idx = timeout;
371         else if (r > 0) {
372                 int i;
373                 for (i = 0; i < count; ++i) {
374                         signal_info* h = signals [i];
375                         if (fd_structs[i].revents & POLLIN) {
376                                 int r;
377                                 char c;
378                                 do {
379                                         r = read (h->read_fd, &c, 1);
380                                 } while (keep_trying (r) && !shutting_down ());
381                                 if (idx == -1)
382                                         idx = i;
383                         }
384                 }
385         }
386
387         return idx;
388 }
389
390 /*
391  * returns: -1 on error:
392  *          timeout on timeout
393  *          index into _signals array of signal that was generated on success
394  */
395 int
396 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down)
397 {
398         int r;
399         int currfd = 0;
400         struct pollfd fd_structs[NUM_SIGNALS];
401
402         signal_info** signals = (signal_info**) _signals;
403
404         if (count > NUM_SIGNALS)
405                 return -1;
406
407         if (acquire_mutex (&signals_mutex) == -1)
408                 return -1;
409
410         r = setup_pipes (signals, count, &fd_structs[0], &currfd);
411
412         release_mutex (&signals_mutex);
413
414         if (r == 0) {
415                 r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout, shutting_down);
416         }
417
418         if (acquire_mutex (&signals_mutex) == -1)
419                 return -1;
420
421         teardown_pipes (signals, count);
422
423         release_mutex (&signals_mutex);
424
425         return r;
426 }
427
428 #endif /* ndef HOST_WIN32 */
429
430
431 G_END_DECLS
432
433 /*
434  * vim: noexpandtab
435  */