Example #1
0
File: signal.c Project: medo64/mono
// Set up a signal_info to begin waiting for signal
static int
setup_pipes (signal_info** signals, int count, struct pollfd *fd_structs, int *currfd)
{
	int i;
	int r = 0;
	for (i = 0; i < count; ++i) {
		signal_info* h;
		int filedes[2];

		h = signals [i];

		if (mph_int_get (&h->pipecnt) == 0) { // First listener for this signal_info
			if ((r = pipe (filedes)) != 0) {
				break;
			}
			mph_int_set (&h->read_fd,  filedes [0]);
			mph_int_set (&h->write_fd, filedes [1]);
		}
		mph_int_inc (&h->pipecnt);
		fd_structs[*currfd].fd = mph_int_get (&h->read_fd);
		fd_structs[*currfd].events = POLLIN;
		++(*currfd); // count is verified less than NUM_SIGNALS by caller
	}
	return r;
}
Example #2
0
File: signal.c Project: medo64/mono
// A UnixSignal object is being Disposed
int
Mono_Unix_UnixSignal_uninstall (void* info)
{
#if defined(HAVE_SIGNAL)
	signal_info* h;
	int r = -1;

	if (acquire_mutex (&signals_mutex) == -1)
		return -1;

	h = info;

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

	release_mutex (&signals_mutex);

	return r;
#else
	g_error ("signal() is not supported by this platform");
	return 0;
#endif
}
Example #3
0
File: signal.c Project: medo64/mono
// Cleanup a signal_info after waiting for signal
static void
teardown_pipes (signal_info** signals, int count)
{
	int i;
	for (i = 0; i < count; ++i) {
		signal_info* h = signals [i];

		if (mph_int_dec_test (&h->pipecnt)) { // Final listener for this signal_info
			acquire_pipelock_teardown (&h->pipelock);
			int read_fd = mph_int_get (&h->read_fd);
			int write_fd = mph_int_get (&h->write_fd);
			if (read_fd != 0)
				close (read_fd);
			if (write_fd != 0)
				close (write_fd);
			mph_int_set (&h->read_fd, 0);
			mph_int_set (&h->write_fd, 0);
			release_pipelock_teardown (&h->pipelock);
		}
	}
}
Example #4
0
File: signal.c Project: ANahr/mono
void*
Mono_Unix_UnixSignal_install (int sig)
{
	int i;
	signal_info* h = NULL; 
	int have_handler = 0;
	void* handler = NULL;

	if (acquire_mutex (&signals_mutex) == -1)
		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);
		mph_int_set (&h->pipecnt, h->pipecnt, 0);
	}

	release_mutex (&signals_mutex);

	return h;
}
Example #5
0
File: signal.c Project: medo64/mono
// A UnixSignal object is being constructed
void*
Mono_Unix_UnixSignal_install (int sig)
{
#if defined(HAVE_SIGNAL)
	int i;
	signal_info* h = NULL;        // signals[] slot to install to
	int have_handler = 0;         // Candidates for signal_info handler fields
	void* handler = NULL;

	if (acquire_mutex (&signals_mutex) == -1)
		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; // This is an rt signal with an existing handler. Bail out.
		}
	}
#endif /*defined (SIGRTMIN) && defined (SIGRTMAX)*/

	// Scan through signals list looking for (1) an unused spot (2) a usable value for handler
	for (i = 0; i < NUM_SIGNALS; ++i) {
		int just_installed = 0;
		// We're still looking for a signal_info spot, and this one is available:
		if (h == NULL && mph_int_get (&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 {
				just_installed = 1;
			}
		}
		// Check if this slot has a "usable" (not installed by this file) handler-to-restore-later:
		// (On the first signal to be installed, signals [i] will be == h when this happens.)
		if (!have_handler && (just_installed || mph_int_get (&signals [i].signum) == sig) &&
				signals [i].handler != default_handler) {
			have_handler = 1;
			handler = signals [i].handler;
		}
		if (h && have_handler) // We have everything we need
			break;
	}

	if (h) {
		// If we reached here without have_handler, this means that default_handler
		// was set as the signal handler before the first UnixSignal object was installed.
		g_assert (have_handler);

		// Overwrite the tenative handler we set a moment ago with a known-usable one
		h->handler = handler;
		h->have_handler = 1;

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

	release_mutex (&signals_mutex);

	return h;
#else
	g_error ("signal() is not supported by this platform");
	return 0;
#endif
}