| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | namespace factor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 03:55:18 -04:00
										 |  |  | THREADHANDLE start_thread(void *(*start_routine)(void *),void *args) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	pthread_attr_t attr; | 
					
						
							|  |  |  | 	pthread_t thread; | 
					
						
							|  |  |  | 	if (pthread_attr_init (&attr) != 0) | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 		fatal_error("pthread_attr_init() failed",0); | 
					
						
							| 
									
										
										
										
											2009-08-25 13:08:45 -04:00
										 |  |  | 	if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE) != 0) | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 		fatal_error("pthread_attr_setdetachstate() failed",0); | 
					
						
							| 
									
										
										
										
											2009-08-25 01:35:54 -04:00
										 |  |  | 	if (pthread_create (&thread, &attr, start_routine, args) != 0) | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 		fatal_error("pthread_create() failed",0); | 
					
						
							| 
									
										
										
										
											2010-03-28 11:37:28 -04:00
										 |  |  | 	pthread_attr_destroy(&attr); | 
					
						
							| 
									
										
										
										
											2009-08-25 03:55:18 -04:00
										 |  |  | 	return thread; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void *null_dll; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 05:49:29 -05:00
										 |  |  | void sleep_nanos(u64 nsec) | 
					
						
							| 
									
										
										
										
											2009-11-18 05:20:05 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-19 05:49:29 -05:00
										 |  |  | 	timespec ts; | 
					
						
							| 
									
										
										
										
											2009-11-18 16:58:48 -05:00
										 |  |  | 	timespec ts_rem; | 
					
						
							|  |  |  | 	int ret; | 
					
						
							| 
									
										
										
										
											2009-11-19 05:49:29 -05:00
										 |  |  | 	ts.tv_sec = nsec / 1000000000; | 
					
						
							|  |  |  | 	ts.tv_nsec = nsec % 1000000000; | 
					
						
							| 
									
										
										
										
											2009-11-18 16:58:48 -05:00
										 |  |  | 	ret = nanosleep(&ts,&ts_rem); | 
					
						
							|  |  |  | 	while(ret == -1 && errno == EINTR) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		memcpy(&ts, &ts_rem, sizeof(ts)); | 
					
						
							|  |  |  | 		ret = nanosleep(&ts, &ts_rem); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(ret == -1) | 
					
						
							|  |  |  | 		fatal_error("nanosleep failed", 0); | 
					
						
							| 
									
										
										
										
											2009-11-18 05:20:05 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::init_ffi() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-02 00:03:26 -04:00
										 |  |  | 	null_dll = dlopen(NULL,RTLD_LAZY); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::ffi_dlopen(dll *dll) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-16 09:43:22 -05:00
										 |  |  | 	dll->handle = dlopen(alien_offset(dll->path), RTLD_LAZY); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-20 18:11:50 -04:00
										 |  |  | void *factor_vm::ffi_dlsym_raw(dll *dll, symbol_char *symbol) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return dlsym(dll ? dll->handle : null_dll, symbol); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-20 18:11:50 -04:00
										 |  |  | 	return FUNCTION_CODE_POINTER(ffi_dlsym_raw(dll, symbol)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef FACTOR_PPC
 | 
					
						
							|  |  |  | void *factor_vm::ffi_dlsym_toc(dll *dll, symbol_char *symbol) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return FUNCTION_TOC_POINTER(ffi_dlsym_raw(dll, symbol)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-05-20 18:11:50 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::ffi_dlclose(dll *dll) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-16 09:43:22 -05:00
										 |  |  | 	if(dlclose(dll->handle)) | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 		general_error(ERROR_FFI,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2010-01-16 09:43:22 -05:00
										 |  |  | 	dll->handle = NULL; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-27 14:42:18 -04:00
										 |  |  | void factor_vm::primitive_existsp() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct stat sb; | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | 	char *path = (char *)(untag_check<byte_array>(ctx->pop()) + 1); | 
					
						
							|  |  |  | 	ctx->push(tag_boolean(stat(path,&sb) >= 0)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-03 16:12:13 -05:00
										 |  |  | void factor_vm::move_file(const vm_char *path1, const vm_char *path2) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int ret = 0; | 
					
						
							| 
									
										
										
										
											2010-03-28 12:52:16 -04:00
										 |  |  | 	do | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-02-03 16:12:13 -05:00
										 |  |  | 		ret = rename((path1),(path2)); | 
					
						
							| 
									
										
										
										
											2010-03-28 12:52:16 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	while(ret < 0 && errno == EINTR); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-03 16:12:13 -05:00
										 |  |  | 	if(ret < 0) | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 		general_error(ERROR_IO,tag_fixnum(errno),false_object); | 
					
						
							| 
									
										
										
										
											2010-02-03 16:12:13 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 14:49:34 -04:00
										 |  |  | segment::segment(cell size_, bool executable_p) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-25 22:17:20 -04:00
										 |  |  | 	size = size_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 14:49:34 -04:00
										 |  |  | 	int pagesize = getpagesize(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-16 05:37:27 -04:00
										 |  |  | 	int prot; | 
					
						
							|  |  |  | 	if(executable_p) | 
					
						
							|  |  |  | 		prot = (PROT_READ | PROT_WRITE | PROT_EXEC); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		prot = (PROT_READ | PROT_WRITE); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 14:49:34 -04:00
										 |  |  | 	char *array = (char *)mmap(NULL,pagesize + size + pagesize,prot,MAP_ANON | MAP_PRIVATE,-1,0); | 
					
						
							| 
									
										
										
										
											2009-10-07 09:33:54 -04:00
										 |  |  | 	if(array == (char*)-1) out_of_memory(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 14:49:34 -04:00
										 |  |  | 	if(mprotect(array,pagesize,PROT_NONE) == -1) | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 		fatal_error("Cannot protect low guard page",(cell)array); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 14:49:34 -04:00
										 |  |  | 	if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1) | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 		fatal_error("Cannot protect high guard page",(cell)array); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 14:49:34 -04:00
										 |  |  | 	start = (cell)(array + pagesize); | 
					
						
							| 
									
										
										
										
											2009-09-25 22:17:20 -04:00
										 |  |  | 	end = start + size; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-25 22:17:20 -04:00
										 |  |  | segment::~segment() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	int pagesize = getpagesize(); | 
					
						
							| 
									
										
										
										
											2009-09-25 22:17:20 -04:00
										 |  |  | 	int retval = munmap((void*)(start - pagesize),pagesize + size + pagesize); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	if(retval) | 
					
						
							| 
									
										
										
										
											2009-09-25 22:17:20 -04:00
										 |  |  | 		fatal_error("Segment deallocation failed",0); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-17 16:46:25 -04:00
										 |  |  | void code_heap::guard_safepoint() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(mprotect(safepoint_page,getpagesize(),PROT_NONE) == -1) | 
					
						
							|  |  |  | 		fatal_error("Cannot protect safepoint guard page",(cell)safepoint_page); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void code_heap::unguard_safepoint() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(mprotect(safepoint_page,getpagesize(),PROT_WRITE) == -1) | 
					
						
							|  |  |  | 		fatal_error("Cannot unprotect safepoint guard page",(cell)safepoint_page); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | void factor_vm::dispatch_signal(void *uap, void (handler)()) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-21 00:30:40 -04:00
										 |  |  | 	dispatch_signal_handler( | 
					
						
							| 
									
										
										
										
											2011-10-26 19:22:42 -04:00
										 |  |  | 		(cell*)&UAP_STACK_POINTER(uap), | 
					
						
							|  |  |  | 		(cell*)&UAP_PROGRAM_COUNTER(uap), | 
					
						
							|  |  |  | 		(cell)FUNCTION_CODE_POINTER(handler) | 
					
						
							| 
									
										
										
										
											2011-10-21 00:30:40 -04:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2011-05-20 18:11:50 -04:00
										 |  |  | 	UAP_SET_TOC_POINTER(uap, (cell)FUNCTION_TOC_POINTER(handler)); | 
					
						
							| 
									
										
										
										
											2009-09-04 14:23:20 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:33 -04:00
										 |  |  | void factor_vm::start_sampling_profiler_timer() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct itimerval timer; | 
					
						
							|  |  |  | 	memset((void*)&timer, 0, sizeof(struct itimerval)); | 
					
						
							| 
									
										
										
										
											2011-11-01 00:31:41 -04:00
										 |  |  | 	timer.it_value.tv_usec = 1000000/samples_per_second; | 
					
						
							|  |  |  | 	timer.it_interval.tv_usec = 1000000/samples_per_second; | 
					
						
							| 
									
										
										
										
											2011-10-28 18:42:33 -04:00
										 |  |  | 	setitimer(ITIMER_REAL, &timer, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void factor_vm::end_sampling_profiler_timer() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct itimerval timer; | 
					
						
							|  |  |  | 	memset((void*)&timer, 0, sizeof(struct itimerval)); | 
					
						
							|  |  |  | 	setitimer(ITIMER_REAL, &timer, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-27 03:44:40 -04:00
										 |  |  | 	factor_vm *vm = current_vm(); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	vm->verify_memory_protection_error((cell)siginfo->si_addr); | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | 	vm->signal_fault_addr = (cell)siginfo->si_addr; | 
					
						
							| 
									
										
										
										
											2011-11-17 20:13:59 -05:00
										 |  |  | 	vm->signal_fault_pc = (cell)UAP_PROGRAM_COUNTER(uap); | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | 	vm->dispatch_signal(uap,factor::memory_signal_handler_impl); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | void synchronous_signal_handler(int signal, siginfo_t *siginfo, void *uap) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	if (factor_vm::fatal_erroring_p) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-20 01:47:14 -04:00
										 |  |  | 	factor_vm *vm = current_vm_p(); | 
					
						
							|  |  |  | 	if (vm) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		vm->signal_number = signal; | 
					
						
							|  |  |  | 		vm->dispatch_signal(uap,factor::synchronous_signal_handler_impl); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 		fatal_error("Foreign thread received signal", signal); | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 01:07:22 -05:00
										 |  |  | void safe_write_nonblock(int fd, void *data, ssize_t size); | 
					
						
							| 
									
										
										
										
											2011-11-07 21:53:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void enqueue_signal(factor_vm *vm, int signal) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (vm->signal_pipe_output != 0) | 
					
						
							| 
									
										
										
										
											2011-11-09 01:07:22 -05:00
										 |  |  | 		safe_write_nonblock(vm->signal_pipe_output, &signal, sizeof(int)); | 
					
						
							| 
									
										
										
										
											2011-11-07 21:53:07 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | void enqueue_signal_handler(int signal, siginfo_t *siginfo, void *uap) | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	if (factor_vm::fatal_erroring_p) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | 	factor_vm *vm = current_vm_p(); | 
					
						
							|  |  |  | 	if (vm) | 
					
						
							| 
									
										
										
										
											2011-11-07 21:53:07 -05:00
										 |  |  | 		enqueue_signal(vm, signal); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | void fep_signal_handler(int signal, siginfo_t *siginfo, void *uap) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	if (factor_vm::fatal_erroring_p) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	factor_vm *vm = current_vm_p(); | 
					
						
							|  |  |  | 	if (vm) | 
					
						
							| 
									
										
										
										
											2011-11-07 21:53:07 -05:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-11-08 14:06:00 -05:00
										 |  |  | 		vm->safepoint.enqueue_fep(vm); | 
					
						
							| 
									
										
										
										
											2011-11-07 21:53:07 -05:00
										 |  |  | 		enqueue_signal(vm, signal); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2011-10-27 23:24:51 -04:00
										 |  |  | 		fatal_error("Foreign thread received signal", signal); | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void sample_signal_handler(int signal, siginfo_t *siginfo, void *uap) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	factor_vm *vm = current_vm_p(); | 
					
						
							| 
									
										
										
										
											2011-11-09 02:37:22 -05:00
										 |  |  | 	bool foreign_thread = false; | 
					
						
							|  |  |  | 	if (vm == NULL) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		foreign_thread = true; | 
					
						
							|  |  |  | 		vm = thread_vms.begin()->second; | 
					
						
							| 
									
										
										
										
											2011-10-31 03:10:30 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-11-09 02:37:22 -05:00
										 |  |  | 	if (atomic::load(&vm->sampling_profiler_p)) | 
					
						
							|  |  |  | 		vm->safepoint.enqueue_samples(vm, 1, (cell)UAP_PROGRAM_COUNTER(uap), foreign_thread); | 
					
						
							|  |  |  | 	else if (!foreign_thread) | 
					
						
							|  |  |  | 		enqueue_signal(vm, signal); | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-03 01:11:45 -04:00
										 |  |  | void ignore_signal_handler(int signal, siginfo_t *siginfo, void *uap) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap) | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-27 03:44:40 -04:00
										 |  |  | 	factor_vm *vm = current_vm(); | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | 	vm->signal_number = signal; | 
					
						
							|  |  |  | 	vm->signal_fpu_status = fpu_status(uap_fpu_status(uap)); | 
					
						
							| 
									
										
										
										
											2009-09-13 16:50:20 -04:00
										 |  |  | 	uap_clear_fpu_status(uap); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | 	vm->dispatch_signal(uap, | 
					
						
							|  |  |  | 		(siginfo->si_code == FPE_INTDIV || siginfo->si_code == FPE_INTOVF) | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | 		? factor::synchronous_signal_handler_impl | 
					
						
							| 
									
										
										
										
											2009-10-22 06:22:59 -04:00
										 |  |  | 		: factor::fp_signal_handler_impl); | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | static void sigaction_safe(int signum, const struct sigaction *act, struct sigaction *oldact) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int ret; | 
					
						
							|  |  |  | 	do | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ret = sigaction(signum, act, oldact); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	while(ret == -1 && errno == EINTR); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(ret == -1) | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 		fatal_error("sigaction failed", errno); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | static void init_sigaction_with_handler(struct sigaction *act, | 
					
						
							|  |  |  | 	void (*handler)(int, siginfo_t*, void*)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	memset(act, 0, sizeof(struct sigaction)); | 
					
						
							|  |  |  | 	sigemptyset(&act->sa_mask); | 
					
						
							|  |  |  | 	act->sa_sigaction = handler; | 
					
						
							|  |  |  | 	act->sa_flags = SA_SIGINFO | SA_ONSTACK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-07 15:51:49 -05:00
										 |  |  | static void safe_pipe(int *in, int *out) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int filedes[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(pipe(filedes) < 0) | 
					
						
							|  |  |  | 		fatal_error("Error opening pipe",errno); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*in = filedes[0]; | 
					
						
							|  |  |  | 	*out = filedes[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(fcntl(*in,F_SETFD,FD_CLOEXEC) < 0) | 
					
						
							|  |  |  | 		fatal_error("Error with fcntl",errno); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(fcntl(*out,F_SETFD,FD_CLOEXEC) < 0) | 
					
						
							|  |  |  | 		fatal_error("Error with fcntl",errno); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void init_signal_pipe(factor_vm *vm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	safe_pipe(&vm->signal_pipe_input, &vm->signal_pipe_output); | 
					
						
							| 
									
										
										
										
											2011-11-09 01:07:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(fcntl(vm->signal_pipe_output,F_SETFL,O_NONBLOCK) < 0) | 
					
						
							|  |  |  | 		fatal_error("Error with fcntl",errno); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-07 21:53:07 -05:00
										 |  |  | 	vm->special_objects[OBJ_SIGNAL_PIPE] = tag_fixnum(vm->signal_pipe_input); | 
					
						
							| 
									
										
										
										
											2011-11-07 15:51:49 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-28 11:37:28 -04:00
										 |  |  | void factor_vm::unix_init_signals() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-07 15:51:49 -05:00
										 |  |  | 	init_signal_pipe(this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-03 14:50:23 -04:00
										 |  |  | 	signal_callstack_seg = new segment(callstack_size,false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	stack_t signal_callstack; | 
					
						
							|  |  |  | 	signal_callstack.ss_sp = (char *)signal_callstack_seg->start; | 
					
						
							|  |  |  | 	signal_callstack.ss_size = signal_callstack_seg->size; | 
					
						
							|  |  |  | 	signal_callstack.ss_flags = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(sigaltstack(&signal_callstack,(stack_t *)NULL) < 0) | 
					
						
							|  |  |  | 		fatal_error("sigaltstack() failed",0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	struct sigaction memory_sigaction; | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | 	struct sigaction synchronous_sigaction; | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	struct sigaction enqueue_sigaction; | 
					
						
							|  |  |  | 	struct sigaction sample_sigaction; | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | 	struct sigaction fpe_sigaction; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	struct sigaction ignore_sigaction; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	init_sigaction_with_handler(&memory_sigaction, memory_signal_handler); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	sigaction_safe(SIGBUS,&memory_sigaction,NULL); | 
					
						
							|  |  |  | 	sigaction_safe(SIGSEGV,&memory_sigaction,NULL); | 
					
						
							| 
									
										
										
										
											2011-05-20 18:11:50 -04:00
										 |  |  | 	sigaction_safe(SIGTRAP,&memory_sigaction,NULL); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	init_sigaction_with_handler(&fpe_sigaction, fpe_signal_handler); | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | 	sigaction_safe(SIGFPE,&fpe_sigaction,NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	init_sigaction_with_handler(&synchronous_sigaction, synchronous_signal_handler); | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | 	sigaction_safe(SIGILL,&synchronous_sigaction,NULL); | 
					
						
							|  |  |  | 	sigaction_safe(SIGABRT,&synchronous_sigaction,NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	init_sigaction_with_handler(&enqueue_sigaction, enqueue_signal_handler); | 
					
						
							|  |  |  | 	sigaction_safe(SIGWINCH,&enqueue_sigaction,NULL); | 
					
						
							| 
									
										
										
										
											2011-11-08 13:15:44 -05:00
										 |  |  | 	sigaction_safe(SIGUSR1,&enqueue_sigaction,NULL); | 
					
						
							|  |  |  | 	sigaction_safe(SIGCONT,&enqueue_sigaction,NULL); | 
					
						
							|  |  |  | 	sigaction_safe(SIGURG,&enqueue_sigaction,NULL); | 
					
						
							|  |  |  | 	sigaction_safe(SIGIO,&enqueue_sigaction,NULL); | 
					
						
							|  |  |  | 	sigaction_safe(SIGPROF,&enqueue_sigaction,NULL); | 
					
						
							|  |  |  | 	sigaction_safe(SIGVTALRM,&enqueue_sigaction,NULL); | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | #ifdef SIGINFO
 | 
					
						
							|  |  |  | 	sigaction_safe(SIGINFO,&enqueue_sigaction,NULL); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-15 21:50:42 -05:00
										 |  |  | 	handle_ctrl_c(); | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	init_sigaction_with_handler(&sample_sigaction, sample_signal_handler); | 
					
						
							|  |  |  | 	sigaction_safe(SIGALRM,&sample_sigaction,NULL); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-03 01:11:45 -04:00
										 |  |  | 	/* We don't use SA_IGN here because then the ignore action is inherited
 | 
					
						
							|  |  |  | 	by subprocesses, which we don't want. There is a unit test in | 
					
						
							|  |  |  | 	io.launcher.unix for this. */ | 
					
						
							| 
									
										
										
										
											2011-10-21 00:00:25 -04:00
										 |  |  | 	init_sigaction_with_handler(&ignore_sigaction, ignore_signal_handler); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	sigaction_safe(SIGPIPE,&ignore_sigaction,NULL); | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	/* We send SIGUSR2 to the stdin_loop thread to interrupt it on FEP */ | 
					
						
							|  |  |  | 	sigaction_safe(SIGUSR2,&ignore_sigaction,NULL); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* On Unix, shared fds such as stdin cannot be set to non-blocking mode
 | 
					
						
							|  |  |  | (http://homepages.tesco.net/J.deBoynePollard/FGA/dont-set-shared-file-descriptors-to-non-blocking-mode.html)
 | 
					
						
							|  |  |  | so we kludge around this by spawning a thread, which waits on a control pipe | 
					
						
							|  |  |  | for a signal, upon receiving this signal it reads one block of data from stdin | 
					
						
							|  |  |  | and writes it to a data pipe. Upon completion, it writes a 4-byte integer to | 
					
						
							|  |  |  | the size pipe, indicating how much data was written to the data pipe. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The read end of the size pipe can be set to non-blocking. */ | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | 	int stdin_read; | 
					
						
							|  |  |  | 	int stdin_write; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int control_read; | 
					
						
							|  |  |  | 	int control_write; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int size_read; | 
					
						
							|  |  |  | 	int size_write; | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-12 17:47:32 -05:00
										 |  |  | 	bool stdin_thread_initialized_p = false; | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	THREADHANDLE stdin_thread; | 
					
						
							|  |  |  | 	pthread_mutex_t stdin_mutex; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void safe_close(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(close(fd) < 0) | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 		fatal_error("error closing fd",errno); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool check_write(int fd, void *data, ssize_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(write(fd,data,size) == size) | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if(errno == EINTR) | 
					
						
							|  |  |  | 			return check_write(fd,data,size); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void safe_write(int fd, void *data, ssize_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(!check_write(fd,data,size)) | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 		fatal_error("error writing fd",errno); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 01:07:22 -05:00
										 |  |  | void safe_write_nonblock(int fd, void *data, ssize_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(!check_write(fd,data,size) && errno != EAGAIN) | 
					
						
							|  |  |  | 		fatal_error("error writing fd",errno); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | bool safe_read(int fd, void *data, ssize_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ssize_t bytes = read(fd,data,size); | 
					
						
							|  |  |  | 	if(bytes < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if(errno == EINTR) | 
					
						
							|  |  |  | 			return safe_read(fd,data,size); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 			fatal_error("error reading fd",errno); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return (bytes == size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *stdin_loop(void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned char buf[4096]; | 
					
						
							|  |  |  | 	bool loop_running = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 21:55:31 -04:00
										 |  |  | 	sigset_t mask; | 
					
						
							|  |  |  | 	sigfillset(&mask); | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	sigdelset(&mask, SIGUSR2); | 
					
						
							| 
									
										
										
										
											2011-11-08 01:19:18 -05:00
										 |  |  | 	sigdelset(&mask, SIGTTIN); | 
					
						
							| 
									
										
										
										
											2011-11-10 16:46:44 -05:00
										 |  |  | 	sigdelset(&mask, SIGTERM); | 
					
						
							|  |  |  | 	sigdelset(&mask, SIGQUIT); | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	pthread_sigmask(SIG_SETMASK, &mask, NULL); | 
					
						
							| 
									
										
										
										
											2011-10-19 21:55:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-13 01:39:28 -05:00
										 |  |  | 	int unused; | 
					
						
							|  |  |  | 	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &unused); | 
					
						
							|  |  |  | 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &unused); | 
					
						
							| 
									
										
										
										
											2011-11-12 17:49:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	while(loop_running) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-11-08 01:19:18 -05:00
										 |  |  | 		if(!safe_read(control_read,buf,1)) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(buf[0] != 'X') | 
					
						
							| 
									
										
										
										
											2009-08-25 03:20:58 -04:00
										 |  |  | 			fatal_error("stdin_loop: bad data on control fd",buf[0]); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for(;;) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-11-13 01:39:28 -05:00
										 |  |  | 			/* If we fep, the parent thread will grab stdin_mutex and send us
 | 
					
						
							|  |  |  | 			SIGUSR2 to interrupt the read() call. */ | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 			pthread_mutex_lock(&stdin_mutex); | 
					
						
							|  |  |  | 			pthread_mutex_unlock(&stdin_mutex); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 			ssize_t bytes = read(0,buf,sizeof(buf)); | 
					
						
							|  |  |  | 			if(bytes < 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if(errno == EINTR) | 
					
						
							|  |  |  | 					continue; | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					loop_running = false; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else if(bytes >= 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				safe_write(size_write,&bytes,sizeof(bytes)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(!check_write(stdin_write,buf,bytes)) | 
					
						
							|  |  |  | 					loop_running = false; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	safe_close(stdin_write); | 
					
						
							|  |  |  | 	safe_close(control_read); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | void factor_vm::open_console() | 
					
						
							| 
									
										
										
										
											2010-09-16 01:20:24 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | 	FACTOR_ASSERT(!stdin_thread_initialized_p); | 
					
						
							| 
									
										
										
										
											2010-09-16 01:20:24 -04:00
										 |  |  | 	safe_pipe(&control_read,&control_write); | 
					
						
							|  |  |  | 	safe_pipe(&size_read,&size_write); | 
					
						
							|  |  |  | 	safe_pipe(&stdin_read,&stdin_write); | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	stdin_thread = start_thread(stdin_loop,NULL); | 
					
						
							| 
									
										
										
										
											2011-11-12 17:47:32 -05:00
										 |  |  | 	stdin_thread_initialized_p = true; | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	pthread_mutex_init(&stdin_mutex, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-13 01:39:28 -05:00
										 |  |  | /* This method is used to kill the stdin_loop before exiting from factor.
 | 
					
						
							|  |  |  | A Nvidia driver bug on Linux is the reason this has to be done, see: | 
					
						
							|  |  |  | http://www.nvnews.net/vbulletin/showthread.php?t=164619 */
 | 
					
						
							| 
									
										
										
										
											2011-11-12 16:06:47 -05:00
										 |  |  | void factor_vm::close_console() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-12 17:47:32 -05:00
										 |  |  | 	if (stdin_thread_initialized_p) | 
					
						
							| 
									
										
										
										
											2011-11-12 17:49:20 -05:00
										 |  |  | 		pthread_cancel(stdin_thread); | 
					
						
							| 
									
										
										
										
											2011-11-12 16:06:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | void factor_vm::lock_console() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | 	FACTOR_ASSERT(stdin_thread_initialized_p); | 
					
						
							| 
									
										
										
										
											2011-11-13 01:39:28 -05:00
										 |  |  | 	/* Lock the stdin_mutex and send the stdin_loop thread a signal to interrupt
 | 
					
						
							|  |  |  | 	any read() it has in progress. When the stdin loop iterates again, it will | 
					
						
							|  |  |  | 	try to lock the same mutex and wait until unlock_console() is called. */ | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	pthread_mutex_lock(&stdin_mutex); | 
					
						
							|  |  |  | 	pthread_kill(stdin_thread, SIGUSR2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void factor_vm::unlock_console() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | 	FACTOR_ASSERT(stdin_thread_initialized_p); | 
					
						
							| 
									
										
										
										
											2011-11-08 00:12:11 -05:00
										 |  |  | 	pthread_mutex_unlock(&stdin_mutex); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-15 21:50:42 -05:00
										 |  |  | void factor_vm::ignore_ctrl_c() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	sig_t ret; | 
					
						
							|  |  |  | 	do | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ret = signal(SIGINT, SIG_DFL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	while(ret == SIG_ERR && errno == EINTR); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void factor_vm::handle_ctrl_c() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sigaction fep_sigaction; | 
					
						
							|  |  |  | 	init_sigaction_with_handler(&fep_sigaction, fep_signal_handler); | 
					
						
							|  |  |  | 	sigaction_safe(SIGINT,&fep_sigaction,NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | void abort() | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | { | 
					
						
							|  |  |  | 	sig_t ret; | 
					
						
							|  |  |  | 	do | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ret = signal(SIGABRT, SIG_DFL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	while(ret == SIG_ERR && errno == EINTR); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | 	factor_vm::close_console(); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	::abort(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |