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) {
74 signal_info* h = &signals [i];
75 if (h->signum != signum)
78 if (h->write_fd > 0) {
80 write (h->write_fd, &c, 1);
85 static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;
88 Mono_Unix_UnixSignal_install (int sig)
91 signal_info* h = NULL;
95 mr = pthread_mutex_lock (&signals_mutex);
101 for (i = 0; i < NUM_SIGNALS; ++i) {
102 if (h == NULL && signals [i].signum == 0) {
104 h->handler = signal (sig, default_handler);
105 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;
130 pthread_mutex_unlock (&signals_mutex);
136 count_handlers (int signum)
140 for (i = 0; i < NUM_SIGNALS; ++i) {
141 if (signals [i].signum == signum)
148 Mono_Unix_UnixSignal_uninstall (void* info)
153 mr = pthread_mutex_lock (&signals_mutex);
161 if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
164 /* last UnixSignal -- we can unregister */
165 if (h->have_handler && count_handlers (h->signum) == 1) {
166 mph_sighandler_t p = signal (h->signum, h->handler);
175 pthread_mutex_unlock (&signals_mutex);
181 setup_pipes (signal_info** signals, int count, fd_set *read_fds, int *max_fd)
184 for (i = 0; i < count; ++i) {
188 if ((r = pipe (filedes)) != 0) {
192 h->read_fd = filedes [0];
193 h->write_fd = filedes [1];
194 if (h->read_fd > *max_fd)
195 *max_fd = h->read_fd;
196 FD_SET (h->read_fd, read_fds);
202 teardown_pipes (signal_info** signals, int count)
205 for (i = 0; i < count; ++i) {
206 signal_info* h = signals [i];
209 if (h->write_fd != 0)
217 wait_for_any (signal_info** signals, int count, int max_fd, fd_set* read_fds, int timeout)
222 struct timeval *ptv = NULL;
224 tv.tv_sec = timeout / 1000;
225 tv.tv_usec = (timeout % 1000)*1000;
229 r = select (max_fd + 1, read_fds, NULL, NULL, ptv);
230 } while (r == -1 && errno == EINTR);
234 for (i = 0; i < count; ++i) {
235 signal_info* h = signals [i];
236 if (FD_ISSET (h->read_fd, read_fds)) {
238 read (h->read_fd, &c, 1); /* ignore error */
247 * returns: -1 on error:
252 Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
258 signal_info** signals = (signal_info**) _signals;
260 mr = pthread_mutex_lock (&signals_mutex);
268 r = setup_pipes (signals, count, &read_fds, &max_fd);
270 r = wait_for_any (signals, count, max_fd, &read_fds, timeout);
272 teardown_pipes (signals, count);
274 pthread_mutex_unlock (&signals_mutex);
279 #endif /* ndef PLATFORM_WIN32 */