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>
28 typedef void (*mph_sighandler_t)(int);
29 typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;
32 Mono_Posix_Stdlib_SIG_DFL (void)
38 Mono_Posix_Stdlib_SIG_ERR (void)
44 Mono_Posix_Stdlib_SIG_IGN (void)
50 Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
52 mph_sighandler_t _h = (mph_sighandler_t) handler;
56 #ifndef PLATFORM_WIN32
59 Mono_Posix_Syscall_psignal (int sig, const char* s)
63 return errno == 0 ? 0 : -1;
66 #define NUM_SIGNALS 64
67 static signal_info signals[NUM_SIGNALS];
70 default_handler (int signum)
73 for (i = 0; i < NUM_SIGNALS; ++i) {
75 signal_info* h = &signals [i];
76 if (g_atomic_int_get (&h->signum) != signum)
78 g_atomic_int_inc (&h->count);
79 fd = g_atomic_int_get (&h->write_fd);
87 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
90 Mono_Unix_UnixSignal_install (int sig)
93 signal_info* h = NULL;
97 mr = pthread_mutex_lock (&signals_mutex);
103 for (i = 0; i < NUM_SIGNALS; ++i) {
104 if (h == NULL && signals [i].signum == 0) {
106 h->handler = signal (sig, default_handler);
107 if (h->handler == SIG_ERR) {
116 if (!have_handler && signals [i].signum == sig &&
117 signals [i].handler != default_handler) {
119 handler = signals [i].handler;
121 if (h && have_handler)
125 if (h && have_handler) {
127 h->handler = handler;
131 g_atomic_int_set (&h->count, 0);
132 g_atomic_int_set (&h->signum, sig);
135 pthread_mutex_unlock (&signals_mutex);
141 count_handlers (int signum)
145 for (i = 0; i < NUM_SIGNALS; ++i) {
146 if (signals [i].signum == signum)
153 Mono_Unix_UnixSignal_uninstall (void* info)
158 mr = pthread_mutex_lock (&signals_mutex);
166 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
169 /* last UnixSignal -- we can unregister */
170 if (h->have_handler && count_handlers (h->signum) == 1) {
171 mph_sighandler_t p = signal (h->signum, h->handler);
180 pthread_mutex_unlock (&signals_mutex);
186 setup_pipes (signal_info** signals, int count, fd_set *read_fds, int *max_fd)
189 for (i = 0; i < count; ++i) {
193 if ((r = pipe (filedes)) != 0) {
197 h->read_fd = filedes [0];
198 h->write_fd = filedes [1];
199 if (h->read_fd > *max_fd)
200 *max_fd = h->read_fd;
201 FD_SET (h->read_fd, read_fds);
207 teardown_pipes (signal_info** signals, int count)
210 for (i = 0; i < count; ++i) {
211 signal_info* h = signals [i];
214 if (h->write_fd != 0)
222 wait_for_any (signal_info** signals, int count, int max_fd, fd_set* read_fds, int timeout)
227 struct timeval *ptv = NULL;
229 tv.tv_sec = timeout / 1000;
230 tv.tv_usec = (timeout % 1000)*1000;
234 r = select (max_fd + 1, read_fds, NULL, NULL, ptv);
235 } while (r == -1 && errno == EINTR);
239 for (i = 0; i < count; ++i) {
240 signal_info* h = signals [i];
241 if (FD_ISSET (h->read_fd, read_fds)) {
243 read (h->read_fd, &c, 1); /* ignore error */
252 * returns: -1 on error:
257 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
263 signal_info** signals = (signal_info**) _signals;
265 mr = pthread_mutex_lock (&signals_mutex);
273 r = setup_pipes (signals, count, &read_fds, &max_fd);
275 r = wait_for_any (signals, count, max_fd, &read_fds, timeout);
277 teardown_pipes (signals, count);
279 pthread_mutex_unlock (&signals_mutex);
284 #endif /* ndef PLATFORM_WIN32 */