• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <sigchain.h>
17 #include <locale.h>
18 #include <pthread.h>
19 #include <errno.h>
20 #include <threads.h>
21 #include <musl_log.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <pthread_impl.h>
25 #include "malloc.h"
26 #include "syscall.h"
27 #include "stdatomic_impl.h"
28 #include "dynlink.h"
29 
30 extern int __libc_sigaction(int sig, const struct sigaction *restrict sa,
31                             struct sigaction *restrict old);
32 /* true is custom hook enabled, otherwise false*/
33 extern volatile atomic_bool __custom_hook_flag;
34 
35 
36 #define SIG_CHAIN_KEY_VALUE_1 1
37 #define SIGNAL_CHAIN_SPECIAL_ACTION_MAX 3
38 
39 #define SIGCHAIN_LOG_TAG "MUSL-SIGCHAIN"
40 
41 #if (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG))
42 #define SIGCHAIN_PRINT_ERROR(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_ERROR, \
43     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
44 #define SIGCHAIN_PRINT_WARN(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_WARN, \
45     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
46 #define SIGCHAIN_PRINT_INFO(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \
47     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
48 #define SIGCHAIN_PRINT_DEBUG(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_DEBUG, \
49     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
50 #define SIGCHAIN_LOG_FATAL(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_FATAL, \
51     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
52 #else
53 #define SIGCHAIN_PRINT_ERROR(...)
54 #define SIGCHAIN_PRINT_WARN(...)
55 #define SIGCHAIN_PRINT_INFO(...)
56 #define SIGCHAIN_PRINT_DEBUG(...)
57 #define SIGCHAIN_LOG_FATAL(...)
58 #endif
59 
60 #define FREEZE_SIGNAL_35 (35)
61 #define FREEZE_SIGNAL_38 (38)
62 
63 #define SIGCHAIN_PRINT_FATAL(...)  do {                    \
64     SIGCHAIN_LOG_FATAL(__VA_ARGS__);                      \
65     abort();                                               \
66 } while (0)
67 
68 struct sc_signal_chain {
69     bool marked;
70     struct sigaction sig_action;
71     struct signal_chain_action sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX];
72 };
73 
74 /* Signal chain set, from 0 to 63. */
75 static struct sc_signal_chain sig_chains[_NSIG - 1];
76 /* static thread Keyword */
77 static pthread_key_t g_sigchain_key;
78 /* This is once flag! */
79 static once_flag g_flag = ONCE_FLAG_INIT;
80 
81 /**
82   * @brief Create the thread key
83   * @retval void
84   */
create_pthread_key(void)85 static void create_pthread_key(void)
86 {
87     SIGCHAIN_PRINT_INFO("%{public}s create the thread key!", __func__);
88     int rc = pthread_key_create(&g_sigchain_key, NULL);
89     if (rc != 0) {
90         SIGCHAIN_PRINT_FATAL("%{public}s failed to create sigchain pthread key, rc:%{public}d errno:%{public}d",
91                 __func__,  rc, errno);
92     }
93 }
94 
95 /**
96   * @brief Get the key of the signal thread.
97   * @retval int32_t, the value of the sigchain key.
98   */
get_handling_signal_key()99 static pthread_key_t get_handling_signal_key()
100 {
101     return g_sigchain_key;
102 }
103 
104 /**
105   * @brief Get the value of the sigchain key
106   * @retval bool, true if set the value of the key,or false.
107   */
get_handling_signal()108 static bool get_handling_signal()
109 {
110     pthread_key_t key = get_handling_signal_key();
111     void *result = pthread_getspecific(key);
112     if (result) {
113         SIGCHAIN_PRINT_WARN("###musl get_handling_signal key=%{public}d", key);
114     }
115     return result == NULL ? false : true;
116 }
117 
118 /**
119   * @brief Set the value of the sigchain key
120   * @param[in] value, the value of the sigchain key
121   * @retval void.
122   */
set_handling_signal(bool value)123 static void set_handling_signal(bool value)
124 {
125     pthread_setspecific(get_handling_signal_key(),
126                         (void *)((uintptr_t)(value)));
127 }
128 
129 /**
130   * @brief Set the mask of the system. Its prototype comes from pthread_sigmask.
131   * @param[in] how, the value of the mask operation .
132   * @param[in] set, the new value of the sigset.
133   * @param[in] old, the old value of the sigset.
134   */
sigchain_sigmask(int how,const sigset_t * restrict set,sigset_t * restrict old)135 static int sigchain_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
136 {
137     int ret;
138     if (set && (unsigned)how - SIG_BLOCK > 2U) return EINVAL;
139     ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8);
140     if (!ret && old) {
141         if (sizeof old->__bits[0] == 8) {
142             old->__bits[0] &= ~0x380000000ULL;
143         } else {
144             old->__bits[0] &= ~0x80000000UL;
145             old->__bits[1] &= ~0x3UL;
146         }
147     }
148     return ret;
149 }
150 
151 /**
152   * @brief Judge whether the signal is marked
153   * @param[in] signo, the value of the signal.
154   * @retval true if the signal is marked, or false.
155   */
ismarked(int signo)156 static bool ismarked(int signo)
157 {
158     return sig_chains[signo - 1].marked;
159 }
160 
161 #ifdef USE_JEMALLOC_DFX_INTF
162 /**
163   * @brief This is a print function for malloc_stats_print.
164   * @param[in] fp, file descriptor for print.
165   * @param[in] str, string to print.
166   * @retval void
167   */
binlock_print(void * fp,const char * str)168 static void binlock_print(void *fp, const char *str)
169 {
170     SIGCHAIN_PRINT_WARN("%{public}s", str);
171 }
172 #endif
173 
174 /**
175   * @brief This is a callback function, which is registered to the kernel
176   * @param[in] signo, the value of the signal.
177   * @param[in] siginfo, the information of the signal.
178   * @param[in] ucontext_raw, the context of the signal.
179   * @retval void
180   */
signal_chain_handler(int signo,siginfo_t * siginfo,void * ucontext_raw)181 static void signal_chain_handler(int signo, siginfo_t* siginfo, void* ucontext_raw)
182 {
183     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
184     /* First call special handler. */
185     /* If a process crashes, the sigchain'll call the corresponding  handler */
186     if (!get_handling_signal()) {
187         int idx, validSigaction = 0;
188         for (idx = 0; idx < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; idx++) {
189             if (sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction == NULL) {
190                 continue;
191             }
192             validSigaction = 1;
193             /* The special handler might not return. */
194             bool noreturn = (sig_chains[signo - 1].sca_special_actions[idx].sca_flags &
195                              SIGCHAIN_ALLOW_NORETURN);
196             sigset_t previous_mask;
197             sigchain_sigmask(SIG_SETMASK, &sig_chains[signo - 1].sca_special_actions[idx].sca_mask,
198                             &previous_mask);
199 
200             bool previous_value = get_handling_signal();
201             if (!noreturn) {
202                 set_handling_signal(true);
203             }
204             int thread_list_lock_status = -1;
205             if (signo == FREEZE_SIGNAL_35) {
206 #ifdef a_ll
207                 thread_list_lock_status = a_ll(&__thread_list_lock);
208 #else
209                 thread_list_lock_status = __thread_list_lock;
210 #endif
211 #ifdef USE_JEMALLOC_DFX_INTF
212                 malloc_stats_print(binlock_print, NULL, "o");
213 #endif //USE_JEMALLOC_DFX_INTF
214             } else if (signo == FREEZE_SIGNAL_38) {
215 #ifdef USE_JEMALLOC_DFX_INTF
216                 malloc_stats_print(binlock_print, NULL, "o");
217 #endif
218             }
219               /**
220               * modify style: move `thread_list_lock_status` from `if`
221               * internal branch to outer branch.
222               * void performance degradation
223               */
224             struct call_tl_lock *call_tl_lock_dump;
225             struct call_tl_lock call_tl_lock_stub = { -1 };
226             if (get_tl_lock_caller_count()) {
227                 call_tl_lock_dump = get_tl_lock_caller_count();
228             } else {
229                 call_tl_lock_dump = &call_tl_lock_stub;
230             }
231             SIGCHAIN_PRINT_WARN("%{public}s call %{public}d rd sigchain action for signal: %{public}d"
232                 " sca_sigaction=%{public}llx noreturn=%{public}d "
233                 "FREEZE_signo_%{public}d thread_list_lock_status:%{public}d "
234                 "tl_lock_count=%{public}d tl_lock_waiters=%{public}d "
235                 "tl_lock_tid_fail=%{public}d tl_lock_count_tid=%{public}d "
236                 "tl_lock_count_fail=%{public}d tl_lock_count_tid_sub=%{public}d "
237                 "thread_list_lock_after_lock=%{public}d thread_list_lock_pre_unlock=%{public}d "
238                 "thread_list_lock_pthread_exit=%{public}d thread_list_lock_tid_overlimit=%{public}d "
239                 "tl_lock_unlock_count=%{public}d "
240                 "__pthread_gettid_np_tl_lock=%{public}d "
241                 "__pthread_exit_tl_lock=%{public}d "
242                 "__pthread_create_tl_lock=%{public}d "
243                 "__pthread_key_delete_tl_lock=%{public}d "
244                 "__synccall_tl_lock=%{public}d "
245                 "__membarrier_tl_lock=%{public}d "
246                 "install_new_tls_tl_lock=%{public}d "
247                 "set_syscall_hooks_tl_lock=%{public}d "
248                 "set_syscall_hooks_linux_tl_lock=%{public}d "
249                 "fork_tl_lock=%{public}d "
250                 "register_count=%{public}d "
251                 "__custom_hook_flag=%{public}d"
252                 "g_dlcloseLockStatus=%{public}d"
253                 "g_dlcloseLockLastExitTid=%{public}d",
254                 __func__, idx, signo, (unsigned long long)sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction,
255                 noreturn, signo, thread_list_lock_status,
256                 get_tl_lock_count(), get_tl_lock_waiters(), get_tl_lock_tid_fail(), get_tl_lock_count_tid(),
257                 get_tl_lock_count_fail(), get_tl_lock_count_tid_sub(),
258                 get_thread_list_lock_after_lock(), get_thread_list_lock_pre_unlock(),
259                 get_thread_list_lock_pthread_exit(), get_thread_list_lock_tid_overlimit(),
260                 call_tl_lock_dump->tl_lock_unlock_count,
261                 call_tl_lock_dump->__pthread_gettid_np_tl_lock,
262                 call_tl_lock_dump->__pthread_exit_tl_lock,
263                 call_tl_lock_dump->__pthread_create_tl_lock,
264                 call_tl_lock_dump->__pthread_key_delete_tl_lock,
265                 call_tl_lock_dump->__synccall_tl_lock,
266                 call_tl_lock_dump->__membarrier_tl_lock,
267                 call_tl_lock_dump->install_new_tls_tl_lock,
268                 call_tl_lock_dump->set_syscall_hooks_tl_lock,
269                 call_tl_lock_dump->set_syscall_hooks_linux_tl_lock,
270                 call_tl_lock_dump->fork_tl_lock,
271                 get_register_count(),
272                 __custom_hook_flag,
273                 getDlcloseLockStatus(),
274                 getDlcloseLockLastExitTid());
275             if (sig_chains[signo - 1].sca_special_actions[idx].sca_sigaction(signo,
276                                                             siginfo, ucontext_raw)) {
277                 set_handling_signal(previous_value);
278                 SIGCHAIN_PRINT_WARN("%{public}s call %{public}d rd sigchain action for signal: %{public}d directly return", __func__, idx, signo);
279                 return;
280             }
281 
282             sigchain_sigmask(SIG_SETMASK, &previous_mask, NULL);
283             set_handling_signal(previous_value);
284         }
285         if (idx == SIGNAL_CHAIN_SPECIAL_ACTION_MAX && !validSigaction) {
286             SIGCHAIN_PRINT_WARN("signal_chain_handler sca_sigaction all null for signal: %{public}d", signo);
287         }
288     }
289     /* Then Call the user's signal handler */
290     int sa_flags = sig_chains[signo - 1].sig_action.sa_flags;
291     ucontext_t* ucontext = (ucontext_t*)(ucontext_raw);
292 
293     sigset_t mask;
294     sigorset(&mask, &ucontext->uc_sigmask, &sig_chains[signo - 1].sig_action.sa_mask);
295 
296     if (!(sa_flags & SA_NODEFER)) {
297         sigaddset(&mask, signo);
298     }
299 
300     sigchain_sigmask(SIG_SETMASK, &mask, NULL);
301 
302     if ((sa_flags & SA_SIGINFO)) {
303         SIGCHAIN_PRINT_WARN("%{public}s call usr sigaction for signal: %{public}d sig_action.sa_sigaction=%{public}llx",
304             __func__, signo, (unsigned long long)sig_chains[signo - 1].sig_action.sa_sigaction);
305         sig_chains[signo - 1].sig_action.sa_sigaction(signo, siginfo, ucontext_raw);
306     } else {
307         if (sig_chains[signo - 1].sig_action.sa_handler == SIG_IGN) {
308             SIGCHAIN_PRINT_WARN("%{public}s SIG_IGN handler for signal: %{public}d", __func__, signo);
309             return;
310         } else if (sig_chains[signo - 1].sig_action.sa_handler == SIG_DFL) {
311             SIGCHAIN_PRINT_WARN("%{public}s SIG_DFL handler for signal: %{public}d", __func__, signo);
312             remove_all_special_handler(signo);
313             if (__syscall(SYS_rt_tgsigqueueinfo, __syscall(SYS_getpid), __syscall(SYS_gettid), signo, siginfo) != 0) {
314                 SIGCHAIN_PRINT_WARN("Failed to rethrow sig(%{public}d), errno(%{public}d).", signo, errno);
315             } else {
316                 SIGCHAIN_PRINT_WARN("pid(%{public}ld) rethrow sig(%{public}d) success.", __syscall(SYS_getpid), signo);
317             }
318         } else {
319             SIGCHAIN_PRINT_WARN("%{public}s call usr sa_handler: %{public}llx for signal: %{public}d",
320                 __func__, (unsigned long long)sig_chains[signo - 1].sig_action.sa_handler, signo);
321             sig_chains[signo - 1].sig_action.sa_handler(signo);
322         }
323     }
324 
325     return;
326 }
327 
328 /**
329   * @brief Register the signal chain with the kernel if needed
330   * @param[in] signo, the value of the signal.
331   * @retval void
332   */
sigchain_register(int signo)333 static void sigchain_register(int signo)
334 {
335     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
336     struct sigaction signal_action = {};
337     sigfillset(&signal_action.sa_mask);
338     call_once(&g_flag, create_pthread_key);
339 
340     signal_action.sa_sigaction = signal_chain_handler;
341     signal_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
342     __libc_sigaction(signo, &signal_action, &sig_chains[signo - 1].sig_action);
343 }
344 
345 /**
346   * @brief Unregister the signal from sigchain, register the signal's user handler with the kernel if needed
347   * @param[in] signo, the value of the signal.
348   * @retval void
349   */
unregister_sigchain(int signo)350 static void unregister_sigchain(int signo)
351 {
352     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
353     __libc_sigaction(signo, &sig_chains[signo - 1].sig_action, NULL);
354     sig_chains[signo - 1].marked = false;
355 }
356 
357 /**
358   * @brief Mark the signal to the sigchain.
359   * @param[in] signo, the value of the signal.
360   * @retval void
361   */
mark_signal_to_sigchain(int signo)362 static void mark_signal_to_sigchain(int signo)
363 {
364     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
365     if (!sig_chains[signo - 1].marked) {
366         sigchain_register(signo);
367         sig_chains[signo - 1].marked = true;
368     }
369 }
370 
371 /**
372   * @brief Set the action of the signal.
373   * @param[in] signo, the value of the signal.
374   * @param[in] new_sa, the new action of the signal.
375   * @retval void
376   */
setaction(int signo,const struct sigaction * restrict new_sa)377 static void setaction(int signo, const struct sigaction *restrict new_sa)
378 {
379     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
380     sig_chains[signo - 1].sig_action = *new_sa;
381 }
382 
383 /**
384   * @brief Get the action of the signal.
385   * @param[in] signo, the value of the signal.
386   * @retval The current action of the signal
387   */
getaction(int signo)388 static struct sigaction getaction(int signo)
389 {
390     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
391     return sig_chains[signo - 1].sig_action;
392 }
393 
394 /**
395   * @brief Add the special handler to the sigchain.
396   * @param[in] signo, the value of the signal.
397   * @param[in] sa, the action with special handler.
398   * @retval void
399   */
add_special_handler(int signo,struct signal_chain_action * sa)400 static void add_special_handler(int signo, struct signal_chain_action* sa)
401 {
402     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
403     for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
404         if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == NULL) {
405             sig_chains[signo - 1].sca_special_actions[i] = *sa;
406             SIGCHAIN_PRINT_INFO("%{public}s signo %{public}d is registered with special handler!", __func__, signo);
407             return;
408         }
409     }
410 
411     SIGCHAIN_PRINT_FATAL("Add too many the special handlers!");
412 }
413 
414 /**
415   * @brief Remove the special handler from the sigchain.
416   * @param[in] signo, the value of the signal.
417   * @param[in] fn, the special handler of the signal.
418   * @retval void
419   */
rm_special_handler(int signo,bool (* fn)(int,siginfo_t *,void *))420 static void rm_special_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
421 {
422     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
423     int len = SIGNAL_CHAIN_SPECIAL_ACTION_MAX;
424     for (int i = 0; i < len; i++) {
425         if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == fn) {
426             sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
427             int count = 0;
428             for (int k = 0; k < len; k++) {
429                 if (sig_chains[signo - 1].sca_special_actions[k].sca_sigaction == NULL) {
430                     count++;
431                 }
432             }
433             if (count == len) {
434                 unregister_sigchain(signo);
435             }
436             return;
437         }
438     }
439 
440     SIGCHAIN_PRINT_FATAL("%{public}s failed to remove the special handler!. signo: %{public}d",
441             __func__, signo);
442 }
443 
444 /**
445   * @brief This is an external interface,
446   *        Mark the signal to sigchain ,add the special handler to the sigchain.
447   * @param[in] signo, the value of the signal.
448   * @param[in] sa, the action with special handler.
449   * @retval void
450   */
add_special_signal_handler(int signo,struct signal_chain_action * sa)451 void add_special_signal_handler(int signo, struct signal_chain_action* sa)
452 {
453     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
454     if (signo <= 0 || signo >= _NSIG) {
455         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
456         return;
457     }
458 
459     // Add the special hander to the sigchain
460     add_special_handler(signo, sa);
461     mark_signal_to_sigchain(signo);
462 }
463 
464 /**
465   * @brief This is an external interface, remove the special handler from the sigchain.
466   * @param[in] signo, the value of the signal.
467   * @param[in] fn, the special handler of the signal.
468   * @retval void
469   */
remove_special_signal_handler(int signo,bool (* fn)(int,siginfo_t *,void *))470 void remove_special_signal_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
471 {
472     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
473     if (signo <= 0 || signo >= _NSIG) {
474         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
475         return;
476     }
477 
478     if (ismarked(signo)) {
479         // remove the special handler from the sigchain.
480         rm_special_handler(signo, fn);
481     }
482 }
483 
484 /**
485   * @brief This is an external interface, remove all special handler from the sigchain.
486   * @param[in] signo, the value of the signal.
487   * @retval void
488   */
remove_all_special_handler(int signo)489 void remove_all_special_handler(int signo)
490 {
491     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
492     if (signo <= 0 || signo >= _NSIG) {
493         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
494         return;
495     }
496 
497     if (ismarked(signo)) {
498         // remove all special handler from the sigchain.
499         for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
500             sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
501         }
502         unregister_sigchain(signo);
503     }
504 }
505 
506 /**
507   * @brief This is an external interface, add the special handler at the last of sigchain chains.
508   * @param[in] signo, the value of the signal.
509   * @param[in] sa, the action with special handler.
510   * @retval void
511   */
add_special_handler_at_last(int signo,struct signal_chain_action * sa)512 void add_special_handler_at_last(int signo, struct signal_chain_action* sa)
513 {
514     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
515     if (signo <= 0 || signo >= _NSIG) {
516         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
517         return;
518     }
519 
520     if (sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1].sca_sigaction == NULL) {
521         sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1] = *sa;
522         mark_signal_to_sigchain(signo);
523         return;
524     }
525 
526     SIGCHAIN_PRINT_FATAL("Add too many the special handlers at last!");
527 }
528 
529 /**
530   * @brief Intercept the signal and sigaction.
531   * @param[in] signo, the value of the signal.
532   * @param[in] sa, the new action with the signal handler.
533   * @param[out] old, the old action with the signal handler.
534   * @retval true if the signal if intercepted, or false.
535   */
intercept_sigaction(int signo,const struct sigaction * restrict sa,struct sigaction * restrict old)536 bool intercept_sigaction(int signo, const struct sigaction *restrict sa,
537                          struct sigaction *restrict old)
538 {
539     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
540     if (signo <= 0 || signo >= _NSIG) {
541         SIGCHAIN_PRINT_ERROR("%{public}s Invalid signal %{public}d", __func__, signo);
542         return false;
543     }
544 
545     if (ismarked(signo)) {
546         struct sigaction saved_action = getaction(signo);
547 
548         if (sa != NULL) {
549             setaction(signo, sa);
550         }
551         if (old != NULL) {
552             *old = saved_action;
553         }
554         return true;
555     }
556 
557     return false;
558 }
559 
560 /**
561   * @brief Intercept the pthread_sigmask.
562   * @param[in] how, the value of the mask operation .
563   * @param[out] set, the value of the sigset.
564   * @retval void.
565   */
intercept_pthread_sigmask(int how,sigset_t * restrict set)566 void intercept_pthread_sigmask(int how, sigset_t *restrict set)
567 {
568     SIGCHAIN_PRINT_DEBUG("%{public}s how: %{public}d", __func__, how);
569     // Forward directly to the system mask When this sigchain is handling a signal.
570     if (get_handling_signal()) {
571         return;
572     }
573 
574     sigset_t tmpset;
575     if (set != NULL) {
576         tmpset = *set;
577         if (how == SIG_BLOCK || how == SIG_SETMASK) {
578             for (int i = 1; i < _NSIG; ++i) {
579                 if (ismarked(i) && sigismember(&tmpset, i)) {
580                     sigdelset(&tmpset, i);
581                 }
582             }
583         }
584         *set = tmpset;
585     }
586 
587     return;
588 }
589