| 1 | #include <signal.h> |
| 2 | #include <errno.h> |
| 3 | #include <string.h> |
| 4 | #include "syscall.h" |
| 5 | #include "pthread_impl.h" |
| 6 | #include "libc.h" |
| 7 | #include "lock.h" |
| 8 | #include "ksigaction.h" |
| 9 | |
| 10 | static int unmask_done; |
| 11 | static unsigned long handler_set[_NSIG/(8*sizeof(long))]; |
| 12 | |
| 13 | void __get_handler_set(sigset_t *set) |
| 14 | { |
| 15 | 	memcpy(set, handler_set, sizeof handler_set); |
| 16 | } |
| 17 | |
| 18 | volatile int __eintr_valid_flag; |
| 19 | |
| 20 | int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) |
| 21 | { |
| 22 | 	struct k_sigaction ksa, ksa_old; |
| 23 | 	if (sa) { |
| 24 | 		if ((uintptr_t)sa->sa_handler > 1UL) { |
| 25 | 			a_or_l(handler_set+(sig-1)/(8*sizeof(long)), |
| 26 | 				1UL<<(sig-1)%(8*sizeof(long))); |
| 27 | |
| 28 | 			/* If pthread_create has not yet been called, |
| 29 | 			 * implementation-internal signals might not |
| 30 | 			 * yet have been unblocked. They must be |
| 31 | 			 * unblocked before any signal handler is |
| 32 | 			 * installed, so that an application cannot |
| 33 | 			 * receive an illegal sigset_t (with them |
| 34 | 			 * blocked) as part of the ucontext_t passed |
| 35 | 			 * to the signal handler. */ |
| 36 | 			if (!libc.threaded && !unmask_done) { |
| 37 | 				__syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, |
| 38 | 					SIGPT_SET, 0, _NSIG/8); |
| 39 | 				unmask_done = 1; |
| 40 | 			} |
| 41 | |
| 42 | 			if (!(sa->sa_flags & SA_RESTART)) { |
| 43 | 				a_store(&__eintr_valid_flag, 1); |
| 44 | 			} |
| 45 | 		} |
| 46 | 		ksa.handler = sa->sa_handler; |
| 47 | 		ksa.flags = sa->sa_flags; |
| 48 | #ifdef SA_RESTORER |
| 49 | 		ksa.flags |= SA_RESTORER; |
| 50 | 		ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore; |
| 51 | #endif |
| 52 | 		memcpy(&ksa.mask, &sa->sa_mask, _NSIG/8); |
| 53 | 	} |
| 54 | 	int r = __syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8); |
| 55 | 	if (old && !r) { |
| 56 | 		old->sa_handler = ksa_old.handler; |
| 57 | 		old->sa_flags = ksa_old.flags; |
| 58 | 		memcpy(&old->sa_mask, &ksa_old.mask, _NSIG/8); |
| 59 | 	} |
| 60 | 	return __syscall_ret(r); |
| 61 | } |
| 62 | |
| 63 | int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) |
| 64 | { |
| 65 | 	unsigned long set[_NSIG/(8*sizeof(long))]; |
| 66 | |
| 67 | 	if (sig-32U < 3 || sig-1U >= _NSIG-1) { |
| 68 | 		errno = EINVAL; |
| 69 | 		return -1; |
| 70 | 	} |
| 71 | |
| 72 | 	/* Doing anything with the disposition of SIGABRT requires a lock, |
| 73 | 	 * so that it cannot be changed while abort is terminating the |
| 74 | 	 * process and so any change made by abort can't be observed. */ |
| 75 | 	if (sig == SIGABRT) { |
| 76 | 		__block_all_sigs(&set); |
| 77 | 		LOCK(__abort_lock); |
| 78 | 	} |
| 79 | 	int r = __libc_sigaction(sig, sa, old); |
| 80 | 	if (sig == SIGABRT) { |
| 81 | 		UNLOCK(__abort_lock); |
| 82 | 		__restore_sigs(&set); |
| 83 | 	} |
| 84 | 	return r; |
| 85 | } |
| 86 | |
| 87 | weak_alias(__sigaction, sigaction); |