[xbuild] Fix bug #674337.
[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(__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
33
34 G_BEGIN_DECLS
35
36 typedef void (*mph_sighandler_t)(int);
37 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
38
39 static int count_handlers (int signum);
40
41 void*
42 Mono_Posix_Stdlib_SIG_DFL (void)
43 {
44         return SIG_DFL;
45 }
46
47 void*
48 Mono_Posix_Stdlib_SIG_ERR (void)
49 {
50         return SIG_ERR;
51 }
52
53 void*
54 Mono_Posix_Stdlib_SIG_IGN (void)
55 {
56         return SIG_IGN;
57 }
58
59 void
60 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
61 {
62         mph_sighandler_t _h = (mph_sighandler_t) handler;
63         _h (signum);
64 }
65
66 int Mono_Posix_SIGRTMIN (void)
67 {
68 #ifdef SIGRTMIN
69         return SIGRTMIN;
70 #else /* def SIGRTMIN */
71         return -1;
72 #endif /* ndef SIGRTMIN */
73 }
74
75 int Mono_Posix_SIGRTMAX (void)
76 {
77 #ifdef SIGRTMAX
78         return SIGRTMAX;
79 #else /* def SIGRTMAX */
80         return -1;
81 #endif /* ndef SIGRTMAX */
82 }
83
84 int Mono_Posix_FromRealTimeSignum (int offset, int *r)
85 {
86         if (NULL == r) {
87                 errno = EINVAL;
88                 return -1;
89         }
90         *r = 0;
91 #if defined (SIGRTMIN) && defined (SIGRTMAX)
92         if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
93                 errno = EINVAL;
94                 return -1;
95         }
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) */
104 }
105
106 #ifndef HOST_WIN32
107
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
126
127 #if HAVE_PSIGNAL
128 int
129 Mono_Posix_Syscall_psignal (int sig, const char* s)
130 {
131         errno = 0;
132         psignal (sig, s);
133         return errno == 0 ? 0 : -1;
134 }
135 #endif  /* def HAVE_PSIGNAL */
136
137 #define NUM_SIGNALS 64
138 static signal_info signals[NUM_SIGNALS];
139
140 static int acquire_mutex (pthread_mutex_t *mutex)
141 {
142         int mr;
143         while ((mr = pthread_mutex_lock (mutex)) == EAGAIN) {
144                 /* try to acquire again */
145         }
146         if ((mr != 0) && (mr != EDEADLK))  {
147                 errno = mr;
148                 return -1;
149         }
150         return 0;
151 }
152
153 static void release_mutex (pthread_mutex_t *mutex)
154 {
155         int mr;
156         while ((mr = pthread_mutex_unlock (mutex)) == EAGAIN) {
157                 /* try to release mutex again */
158         }
159 }
160
161 static inline int
162 keep_trying (int r)
163 {
164         return r == -1 && errno == EINTR;
165 }
166
167 static void
168 default_handler (int signum)
169 {
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 */
186                         }
187                 }
188         }
189 }
190
191 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
192
193 void*
194 Mono_Unix_UnixSignal_install (int sig)
195 {
196         int i;
197         signal_info* h = NULL; 
198         int have_handler = 0;
199         void* handler = NULL;
200
201         if (acquire_mutex (&signals_mutex) == -1)
202                 return NULL;
203
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;
213                 }
214         }
215 #endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/
216
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;
225                         }
226                         else {
227                                 h->have_handler = 1;
228                         }
229                 }
230                 if (!have_handler && signals [i].signum == sig &&
231                                 signals [i].handler != default_handler) {
232                         have_handler = 1;
233                         handler = signals [i].handler;
234                 }
235                 if (h && have_handler)
236                         break;
237         }
238
239         if (h && have_handler) {
240                 h->have_handler = 1;
241                 h->handler      = handler;
242         }
243
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);
248         }
249
250         release_mutex (&signals_mutex);
251
252         return h;
253 }
254
255 static int
256 count_handlers (int signum)
257 {
258         int i;
259         int count = 0;
260         for (i = 0; i < NUM_SIGNALS; ++i) {
261                 if (signals [i].signum == signum)
262                         ++count;
263         }
264         return count;
265 }
266
267 int
268 Mono_Unix_UnixSignal_uninstall (void* info)
269 {
270         signal_info* h;
271         int r = -1;
272
273         if (acquire_mutex (&signals_mutex) == -1)
274                 return -1;
275
276         h = info;
277
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;
288                 }
289                 h->signum = 0;
290         }
291
292         release_mutex (&signals_mutex);
293
294         return r;
295 }
296
297 static int
298 setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
299 {
300         int i;
301         int r = 0;
302         for (i = 0; i < count; ++i) {
303                 signal_info* h;
304                 int filedes[2];
305
306                 h = signals [i];
307
308                 if (mph_int_get (&h->pipecnt) == 0) {
309                         if ((r = pipe (filedes)) != 0) {
310                                 break;
311                         }
312                         h->read_fd  = filedes [0];
313                         h->write_fd = filedes [1];
314                 }
315                 mph_int_inc (&h->pipecnt);
316                 fd_structs[*currfd].fd = h->read_fd;
317                 fd_structs[*currfd].events = POLLIN;
318                 ++(*currfd);
319         }
320         return r;
321 }
322
323 static void
324 teardown_pipes (signal_info** signals, int count)
325 {
326         int i;
327         for (i = 0; i < count; ++i) {
328                 signal_info* h = signals [i];
329
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;
337                 }
338         }
339 }
340
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)
343 {
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;
352                 }
353                 r = poll (fd_structs, count, timeout);
354         } while (keep_trying (r) && !shutting_down ());
355
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;
371                         }
372                 }
373         }
374
375         return idx;
376 }
377
378 /*
379  * returns: -1 on error:
380  *          timeout on timeout
381  *          index into _signals array of signal that was generated on success
382  */
383 int
384 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down)
385 {
386         int r;
387         int currfd = 0;
388         struct pollfd fd_structs[NUM_SIGNALS];
389
390         signal_info** signals = (signal_info**) _signals;
391
392         if (count > NUM_SIGNALS)
393                 return -1;
394
395         if (acquire_mutex (&signals_mutex) == -1)
396                 return -1;
397
398         r = setup_pipes (signals, count, &fd_structs[0], &currfd);
399
400         release_mutex (&signals_mutex);
401
402         if (r == 0) {
403                 r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout, shutting_down);
404         }
405
406         if (acquire_mutex (&signals_mutex) == -1)
407                 return -1;
408
409         teardown_pipes (signals, count);
410
411         release_mutex (&signals_mutex);
412
413         return r;
414 }
415
416 #endif /* ndef HOST_WIN32 */
417
418
419 G_END_DECLS
420
421 /*
422  * vim: noexpandtab
423  */