Welcome to mirror list, hosted at ThFree Co, Russian Federation.

signal.c « support - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6df230b1b7ebba017835d6f96378f9b3fa1978c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * <signal.h> wrapper functions.
 *
 * Authors:
 *   Jonathan Pryor (jonpryor@vt.edu)
 *   Jonathan Pryor (jpryor@novell.com)
 *
 * Copyright (C) 2004-2005 Jonathan Pryor
 * Copyright (C) 2008 Novell, Inc.
 */

#include <signal.h>

#include "map.h"
#include "mph.h"

#ifndef PLATFORM_WIN32
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <mono/io-layer/atomic.h>
#endif

G_BEGIN_DECLS

typedef void (*mph_sighandler_t)(int);
typedef struct Mono_Unix_UnixSignal_SignalInfo signal_info;

static int count_handlers (int signum);

void*
Mono_Posix_Stdlib_SIG_DFL (void)
{
	return SIG_DFL;
}

void*
Mono_Posix_Stdlib_SIG_ERR (void)
{
	return SIG_ERR;
}

void*
Mono_Posix_Stdlib_SIG_IGN (void)
{
	return SIG_IGN;
}

void
Mono_Posix_Stdlib_InvokeSignalHandler (int signum, void *handler)
{
	mph_sighandler_t _h = (mph_sighandler_t) handler;
	_h (signum);
}

int Mono_Posix_SIGRTMIN (void)
{
#ifdef SIGRTMIN
	return SIGRTMIN;
#else /* def SIGRTMIN */
	return -1;
#endif /* ndef SIGRTMIN */
}

int Mono_Posix_SIGRTMAX (void)
{
#ifdef SIGRTMAX
	return SIGRTMAX;
#else /* def SIGRTMAX */
	return -1;
#endif /* ndef SIGRTMAX */
}

int Mono_Posix_FromRealTimeSignum (int offset, int *r)
{
	if (NULL == r) {
		errno = EINVAL;
		return -1;
	}
	*r = 0;
#if defined (SIGRTMIN) && defined (SIGRTMAX)
	if ((offset < 0) || (SIGRTMIN > SIGRTMAX - offset)) {
		errno = EINVAL;
		return -1;
	}
	*r = SIGRTMIN+offset;
	return 0;
#else /* defined (SIGRTMIN) && defined (SIGRTMAX) */
# ifdef ENOSYS
	errno = ENOSYS;
# endif /* ENOSYS */
	return -1;
#endif /* defined (SIGRTMIN) && defined (SIGRTMAX) */
}

#ifndef PLATFORM_WIN32

#ifdef WAPI_ATOMIC_ASM
	#define mph_int_get(p)     InterlockedExchangeAdd ((p), 0)
	#define mph_int_inc(p)     InterlockedIncrement ((p))
	#define mph_int_set(p,o,n) InterlockedExchange ((p), (n))
#elif GLIB_CHECK_VERSION(2,4,0)
	#define mph_int_get(p) g_atomic_int_get ((p))
 	#define mph_int_inc(p) do {g_atomic_int_inc ((p));} while (0)
	#define mph_int_set(p,o,n) do {                                 \
		while (!g_atomic_int_compare_and_exchange ((p), (o), (n))) {} \
	} while (0)
#else
	#define mph_int_get(p) (*(p))
	#define mph_int_inc(p) do { (*(p))++; } while (0)
	#define mph_int_set(p,o,n) do { *(p) = n; } while (0)
#endif

int
Mono_Posix_Syscall_psignal (int sig, const char* s)
{
	errno = 0;
	psignal (sig, s);
	return errno == 0 ? 0 : -1;
}

#define NUM_SIGNALS 64
static signal_info signals[NUM_SIGNALS];

static void
default_handler (int signum)
{
	int i;
	for (i = 0; i < NUM_SIGNALS; ++i) {
		int fd;
		signal_info* h = &signals [i];
		if (mph_int_get (&h->signum) != signum)
			continue;
		mph_int_inc (&h->count);
		fd = mph_int_get (&h->write_fd);
		if (fd > 0) {
			char c = signum;
			write (fd, &c, 1);
		}
	}
}

static pthread_mutex_t signals_mutex = PTHREAD_MUTEX_INITIALIZER;

void*
Mono_Unix_UnixSignal_install (int sig)
{
	int i, mr;
	signal_info* h = NULL; 
	int have_handler = 0;
	void* handler = NULL;

	mr = pthread_mutex_lock (&signals_mutex);
	if (mr != 0) {
		errno = mr;
		return NULL;
	}

#if defined (SIGRTMIN) && defined (SIGRTMAX)
	/*The runtime uses some rt signals for itself so it's important to not override them.*/
	if (sig >= SIGRTMIN && sig <= SIGRTMAX && count_handlers (sig) == 0) {
		struct sigaction sinfo;
		sigaction (sig, NULL, &sinfo);
		if (sinfo.sa_handler != SIG_DFL || (void*)sinfo.sa_sigaction != (void*)SIG_DFL) {
			pthread_mutex_unlock (&signals_mutex);
			errno = EADDRINUSE;
			return NULL;
		}
	}
#endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/

	for (i = 0; i < NUM_SIGNALS; ++i) {
		if (h == NULL && signals [i].signum == 0) {
			h = &signals [i];
			h->handler = signal (sig, default_handler);
			if (h->handler == SIG_ERR) {
				h->handler = NULL;
				h = NULL;
				break;
			}
			else {
				h->have_handler = 1;
			}
		}
		if (!have_handler && signals [i].signum == sig &&
				signals [i].handler != default_handler) {
			have_handler = 1;
			handler = signals [i].handler;
		}
		if (h && have_handler)
			break;
	}

	if (h && have_handler) {
		h->have_handler = 1;
		h->handler      = handler;
	}

	if (h) {
		mph_int_set (&h->count, h->count, 0);
		mph_int_set (&h->signum, h->signum, sig);
	}

	pthread_mutex_unlock (&signals_mutex);

	return h;
}

static int
count_handlers (int signum)
{
	int i;
	int count = 0;
	for (i = 0; i < NUM_SIGNALS; ++i) {
		if (signals [i].signum == signum)
			++count;
	}
	return count;
}

int
Mono_Unix_UnixSignal_uninstall (void* info)
{
	signal_info* h;
	int mr, r = -1;

	mr = pthread_mutex_lock (&signals_mutex);
	if (mr != 0) {
		errno = mr;
		return -1;
	}

	h = info;

	if (h == NULL || h < signals || h > &signals [NUM_SIGNALS])
		errno = EINVAL;
	else {
		/* last UnixSignal -- we can unregister */
		if (h->have_handler && count_handlers (h->signum) == 1) {
			mph_sighandler_t p = signal (h->signum, h->handler);
			if (p != SIG_ERR)
				r = 0;
			h->handler      = NULL;
			h->have_handler = 0;
		}
		h->signum = 0;
	}

	pthread_mutex_unlock (&signals_mutex);

	return r;
}

static int
setup_pipes (signal_info** signals, int count, fd_set *read_fds, int *max_fd)
{
	int i, r;
	for (i = 0; i < count; ++i) {
		signal_info* h;
		int filedes[2];

		if ((r = pipe (filedes)) != 0) {
			break;
		}
		h = signals [i];
		h->read_fd  = filedes [0];
		h->write_fd = filedes [1];
		if (h->read_fd > *max_fd)
			*max_fd = h->read_fd;
		FD_SET (h->read_fd, read_fds);
	}
	return r;
}

static void
teardown_pipes (signal_info** signals, int count)
{
	int i;
	for (i = 0; i < count; ++i) {
		signal_info* h = signals [i];
		if (h->read_fd != 0)
			close (h->read_fd);
		if (h->write_fd != 0)
			close (h->write_fd);
		h->read_fd  = 0;
		h->write_fd = 0;
	}
}

static int
wait_for_any (signal_info** signals, int count, int max_fd, fd_set* read_fds, int timeout)
{
	int r, idx;
	do {
		struct timeval tv;
		struct timeval *ptv = NULL;
		if (timeout != -1) {
			tv.tv_sec  = timeout / 1000;
			tv.tv_usec = (timeout % 1000)*1000;
			ptv = &tv;
		}

		r = select (max_fd + 1, read_fds, NULL, NULL, ptv);
	} while (r == -1 && errno == EINTR);

	idx = -1;
	if (r == 0)
		idx = timeout;
	else if (r > 0) {
		int i;
		for (i = 0; i < count; ++i) {
			signal_info* h = signals [i];
			if (FD_ISSET (h->read_fd, read_fds)) {
				char c;
				read (h->read_fd, &c, 1); /* ignore any error */
				if (idx == -1)
					idx = i;
			}
		}
	}

	return idx;
}

/*
 * returns: -1 on error:
 *          timeout on timeout
 *          index into _signals array of signal that was generated on success
 */
int
Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */)
{
	fd_set read_fds;
	int mr, r;
	int max_fd = 0;

	signal_info** signals = (signal_info**) _signals;

	mr = pthread_mutex_lock (&signals_mutex);
	if (mr != 0) {
		errno = mr;
		return -1;
	}

	FD_ZERO (&read_fds);

	r = setup_pipes (signals, count, &read_fds, &max_fd);
	if (r == 0) {
		r = wait_for_any (signals, count, max_fd, &read_fds, timeout);
	}
	teardown_pipes (signals, count);

	pthread_mutex_unlock (&signals_mutex);

	return r;
}

#endif /* ndef PLATFORM_WIN32 */


G_END_DECLS

/*
 * vim: noexpandtab
 */