1 #include <signal.h>
2 #include <errno.h>
3 #ifndef __LITEOS__
4 #include <sigchain.h>
5 #include <stddef.h>
6 #include <hilog_adapter.h>
7 #include <string.h>
8 #endif
9 #include "syscall.h"
10
11 #ifndef __LITEOS__
12 #ifdef OHOS_ENABLE_PARAMETER
13 #include "sys_param.h"
14 #endif
15
16 extern void intercept_pthread_sigmask(int how, sigset_t *restrict set);
17 static const char *param_name = "musl.sigchain.procmask";
18
19 /**
20 * @brief Get whether sigchain mask is enabled
21 * @retval True if the sigchain mask is enable, or false.
22 */
get_sigchain_mask_enable()23 bool get_sigchain_mask_enable()
24 {
25 #ifdef OHOS_ENABLE_PARAMETER
26 static CachedHandle sigchain_procmask_handle = NULL;
27 if (sigchain_procmask_handle == NULL) {
28 sigchain_procmask_handle = CachedParameterCreate(param_name, "false");
29 }
30 char *param_value = CachedParameterGet(sigchain_procmask_handle);
31 if (param_value != NULL) {
32 if (strcmp(param_value, "true") == 0) {
33 return true;
34 }
35 }
36 #endif
37 return false;
38 }
39 #endif
40
pthread_sigmask(int how,const sigset_t * restrict set,sigset_t * restrict old)41 int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
42 {
43 int ret;
44 if (set && (unsigned)how - SIG_BLOCK > 2U) return EINVAL;
45 #ifndef __LITEOS__
46 /* sigchain intercepts pthread_sigmask */
47 if (set && get_sigchain_mask_enable()) {
48 sigset_t tmpset = *set;
49 intercept_pthread_sigmask(how, &tmpset);
50 const sigset_t *new_set_ptr = &tmpset;
51 ret = -__syscall(SYS_rt_sigprocmask, how, new_set_ptr, old, _NSIG / 8);
52 } else {
53 ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG / 8);
54 }
55 #else
56 ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8);
57 #endif
58 if (!ret && old) {
59 if (sizeof old->__bits[0] == 8) {
60 old->__bits[0] &= ~0x380000000ULL;
61 } else {
62 old->__bits[0] &= ~0x80000000UL;
63 old->__bits[1] &= ~0x3UL;
64 }
65 }
66 return ret;
67 }
68