• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "syscall.h"
25 
26 extern int __libc_sigaction(int sig, const struct sigaction *restrict sa,
27                             struct sigaction *restrict old);
28 
29 #define SIG_CHAIN_KEY_VALUE_1 1
30 #define SIGNAL_CHAIN_SPECIAL_ACTION_MAX 3
31 
32 #define SIGCHAIN_LOG_TAG "MUSL-SIGCHAIN"
33 
34 #if (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG))
35 #define SIGCHAIN_PRINT_ERROR(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_ERROR, \
36     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
37 #define SIGCHAIN_PRINT_INFO(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \
38     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
39 #define SIGCHAIN_PRINT_DEBUG(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_DEBUG, \
40     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
41 #define SIGCHAIN_LOG_FATAL(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_FATAL, \
42     MUSL_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
43 #else
44 #define SIGCHAIN_PRINT_ERROR(...)
45 #define SIGCHAIN_PRINT_INFO(...)
46 #define SIGCHAIN_PRINT_DEBUG(...)
47 #define SIGCHAIN_LOG_FATAL(...)
48 #endif
49 
50 #define SIGCHAIN_PRINT_FATAL(...)  do {                    \
51     SIGCHAIN_LOG_FATAL(__VA_ARGS__);                      \
52     abort();                                               \
53 } while(0)
54 
55 struct sc_signal_chain {
56     bool marked;
57     struct sigaction sig_action;
58     struct signal_chain_action sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX];
59 };
60 
61 /* Signal chain set, from 0 to 63. */
62 static struct sc_signal_chain sig_chains[_NSIG - 1];
63 /* static thread Keyword */
64 static pthread_key_t g_sigchain_key;
65 /* This is once flag! */
66 static once_flag g_flag = ONCE_FLAG_INIT;
67 
68 /**
69   * @brief Create the thread key
70   * @retval void
71   */
create_pthread_key(void)72 static void create_pthread_key(void)
73 {
74     SIGCHAIN_PRINT_INFO("%{public}s create the thread key!", __func__);
75     int rc = pthread_key_create(&g_sigchain_key, NULL);
76     if (rc != 0) {
77         SIGCHAIN_PRINT_FATAL("%{public}s failed to create sigchain pthread key, rc:%{public}d",
78                 __func__,  rc);
79     }
80 }
81 
82 /**
83   * @brief Get the key of the signal thread.
84   * @retval int32_t, the value of the sigchain key.
85   */
get_handling_signal_key()86 static pthread_key_t get_handling_signal_key()
87 {
88     call_once(&g_flag, create_pthread_key);
89     return g_sigchain_key;
90 }
91 
92 /**
93   * @brief Get the value of the sigchain key
94   * @retval bool, true if set the value of the key,or false.
95   */
get_handling_signal()96 static bool get_handling_signal()
97 {
98     void *result = pthread_getspecific(get_handling_signal_key());
99     return result == NULL ? false : true;
100 }
101 
102 /**
103   * @brief Set the value of the sigchain key
104   * @param[in] value, the value of the sigchain key
105   * @retval void.
106   */
set_handling_signal(bool value)107 static void set_handling_signal(bool value)
108 {
109     pthread_setspecific(get_handling_signal_key(),
110                         (void *)((uintptr_t)(value)));
111 }
112 
113 /**
114   * @brief Set the mask of the system. Its prototype comes from pthread_sigmask.
115   * @param[in] how, the value of the mask operation .
116   * @param[in] set, the new value of the sigset.
117   * @param[in] old, the old value of the sigset.
118   */
sigchain_sigmask(int how,const sigset_t * restrict set,sigset_t * restrict old)119 static int sigchain_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
120 {
121     int ret;
122     if (set && (unsigned)how - SIG_BLOCK > 2U) return EINVAL;
123     ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8);
124     if (!ret && old) {
125         if (sizeof old->__bits[0] == 8) {
126             old->__bits[0] &= ~0x380000000ULL;
127         } else {
128             old->__bits[0] &= ~0x80000000UL;
129             old->__bits[1] &= ~0x3UL;
130         }
131     }
132     return ret;
133 }
134 
135 /**
136   * @brief Judge whether the signal is marked
137   * @param[in] signo, the value of the signal.
138   * @retval true if the signal is marked, or false.
139   */
ismarked(int signo)140 static bool ismarked(int signo)
141 {
142     return sig_chains[signo - 1].marked;
143 }
144 
145 /**
146   * @brief This is a callback function, which is registered to the kernel
147   * @param[in] signo, the value of the signal.
148   * @param[in] siginfo, the information of the signal.
149   * @param[in] ucontext_raw, the context of the signal.
150   * @retval void
151   */
signal_chain_handler(int signo,siginfo_t * siginfo,void * ucontext_raw)152 static void signal_chain_handler(int signo, siginfo_t* siginfo, void* ucontext_raw)
153 {
154     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
155     /* First call special handler. */
156     /* If a process crashes, the sigchain'll call the corresponding  handler */
157     if (!get_handling_signal()) {
158         for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
159             if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == NULL) {
160                 continue;
161             }
162             /* The special handler might not return. */
163             bool noreturn = (sig_chains[signo - 1].sca_special_actions[i].sca_flags &
164                              SIGCHAIN_ALLOW_NORETURN);
165             sigset_t previous_mask;
166             sigchain_sigmask(SIG_SETMASK, &sig_chains[signo - 1].sca_special_actions[i].sca_mask,
167                             &previous_mask);
168 
169             bool previous_value = get_handling_signal();
170             if (!noreturn) {
171                 set_handling_signal(true);
172             }
173             SIGCHAIN_PRINT_ERROR("%{public}s call %{public}d rd sigchain action for signal: %{public}d", __func__, i, signo);
174             if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction(signo,
175                                                             siginfo, ucontext_raw)) {
176                 set_handling_signal(previous_value);
177                 SIGCHAIN_PRINT_ERROR("%{public}s call %{public}d rd sigchain action for signal: %{public}d directly return", __func__, i, signo);
178                 return;
179             }
180 
181             sigchain_sigmask(SIG_SETMASK, &previous_mask, NULL);
182             set_handling_signal(previous_value);
183         }
184     }
185     /* Then Call the user's signal handler */
186     int sa_flags = sig_chains[signo - 1].sig_action.sa_flags;
187     ucontext_t* ucontext = (ucontext_t*)(ucontext_raw);
188 
189     sigset_t mask;
190     sigorset(&mask, &ucontext->uc_sigmask, &sig_chains[signo - 1].sig_action.sa_mask);
191 
192     if (!(sa_flags & SA_NODEFER)) {
193         sigaddset(&mask, signo);
194     }
195 
196     sigchain_sigmask(SIG_SETMASK, &mask, NULL);
197 
198     if ((sa_flags & SA_SIGINFO)) {
199         SIGCHAIN_PRINT_INFO("%{public}s call usr sigaction for signal:"
200             "%{public}d sig_action.sa_sigaction=%{public}lx",
201             __func__, signo, (unsigned long)sig_chains[signo - 1].sig_action.sa_sigaction);
202         sig_chains[signo - 1].sig_action.sa_sigaction(signo, siginfo, ucontext_raw);
203     } else {
204         if (sig_chains[signo - 1].sig_action.sa_handler == SIG_IGN) {
205             SIGCHAIN_PRINT_ERROR("%{public}s SIG_IGN handler for signal: %{public}d", __func__, signo);
206             return;
207         } else if (sig_chains[signo - 1].sig_action.sa_handler == SIG_DFL) {
208             SIGCHAIN_PRINT_ERROR("%{public}s SIG_DFL handler for signal: %{public}d", __func__, signo);
209             remove_all_special_handler(signo);
210             if (__syscall(SYS_rt_tgsigqueueinfo, __syscall(SYS_getpid), __syscall(SYS_gettid), signo, siginfo) != 0) {
211                 SIGCHAIN_PRINT_ERROR("Failed to rethrow sig(%{public}d), errno(%{public}d).", signo, errno);
212             } else {
213                 SIGCHAIN_PRINT_ERROR("pid(%{public}d) rethrow sig(%{public}d) success.", __syscall(SYS_getpid), signo);
214             }
215         } else {
216             SIGCHAIN_PRINT_INFO("%{public}s call usr sa_handler: %{public}p for signal:"
217                 "%{public}d sig_action.sa_handler=%{public}lx",
218                 __func__, signo, (unsigned long)sig_chains[signo - 1].sig_action.sa_handler);
219             sig_chains[signo - 1].sig_action.sa_handler(signo);
220         }
221     }
222 
223     return;
224 }
225 
226 /**
227   * @brief Register the signal chain with the kernel if needed
228   * @param[in] signo, the value of the signal.
229   * @retval void
230   */
sigchain_register(int signo)231 static void sigchain_register(int signo)
232 {
233     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
234     struct sigaction signal_action = {};
235     sigfillset(&signal_action.sa_mask);
236 
237     signal_action.sa_sigaction = signal_chain_handler;
238     signal_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
239     __libc_sigaction(signo, &signal_action, &sig_chains[signo - 1].sig_action);
240 }
241 
242 /**
243   * @brief Unregister the signal from sigchain, register the signal's user handler with the kernel if needed
244   * @param[in] signo, the value of the signal.
245   * @retval void
246   */
unregister_sigchain(int signo)247 static void unregister_sigchain(int signo)
248 {
249     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
250     __libc_sigaction(signo, &sig_chains[signo - 1].sig_action, NULL);
251     sig_chains[signo - 1].marked = false;
252 }
253 
254 /**
255   * @brief Mark the signal to the sigchain.
256   * @param[in] signo, the value of the signal.
257   * @retval void
258   */
mark_signal_to_sigchain(int signo)259 static void mark_signal_to_sigchain(int signo)
260 {
261     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
262     if (!sig_chains[signo - 1].marked) {
263         sigchain_register(signo);
264         sig_chains[signo - 1].marked = true;
265     }
266 }
267 
268 /**
269   * @brief Set the action of the signal.
270   * @param[in] signo, the value of the signal.
271   * @param[in] new_sa, the new action of the signal.
272   * @retval void
273   */
setaction(int signo,const struct sigaction * restrict new_sa)274 static void setaction(int signo, const struct sigaction *restrict new_sa)
275 {
276     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
277     sig_chains[signo - 1].sig_action = *new_sa;
278 }
279 
280 /**
281   * @brief Get the action of the signal.
282   * @param[in] signo, the value of the signal.
283   * @retval The current action of the signal
284   */
getaction(int signo)285 static struct sigaction getaction(int signo)
286 {
287     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
288     return sig_chains[signo - 1].sig_action;
289 }
290 
291 /**
292   * @brief Add the special handler to the sigchain.
293   * @param[in] signo, the value of the signal.
294   * @param[in] sa, the action with special handler.
295   * @retval void
296   */
add_special_handler(int signo,struct signal_chain_action * sa)297 static void add_special_handler(int signo, struct signal_chain_action* sa)
298 {
299     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
300     for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
301         if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == NULL) {
302             sig_chains[signo - 1].sca_special_actions[i] = *sa;
303             SIGCHAIN_PRINT_INFO("%{public}s signo %{public}d is registered with special handler!", __func__, signo);
304             return;
305         }
306     }
307 
308     SIGCHAIN_PRINT_FATAL("Add too many the special handlers!");
309 }
310 
311 /**
312   * @brief Remove the special handler from the sigchain.
313   * @param[in] signo, the value of the signal.
314   * @param[in] fn, the special handler of the signal.
315   * @retval void
316   */
rm_special_handler(int signo,bool (* fn)(int,siginfo_t *,void *))317 static void rm_special_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
318 {
319     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
320     int len = SIGNAL_CHAIN_SPECIAL_ACTION_MAX;
321     for (int i = 0; i < len; i++) {
322         if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == fn) {
323             sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
324             int count = 0;
325             for (int k = 0; k < len; k++) {
326                 if (sig_chains[signo - 1].sca_special_actions[k].sca_sigaction == NULL) {
327                     count++;
328                 }
329             }
330             if (count == len) {
331                 unregister_sigchain(signo);
332             }
333             return;
334         }
335     }
336 
337     SIGCHAIN_PRINT_FATAL("%{public}s failed to remove the special handler!. signo: %{public}d",
338             __func__, signo);
339 }
340 
341 /**
342   * @brief This is an external interface,
343   *        Mark the signal to sigchain ,add the special handler to the sigchain.
344   * @param[in] signo, the value of the signal.
345   * @param[in] sa, the action with special handler.
346   * @retval void
347   */
add_special_signal_handler(int signo,struct signal_chain_action * sa)348 void add_special_signal_handler(int signo, struct signal_chain_action* sa)
349 {
350     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
351     if (signo <= 0 || signo >= _NSIG) {
352         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
353         return;
354     }
355 
356     // Add the special hander to the sigchain
357     add_special_handler(signo, sa);
358     mark_signal_to_sigchain(signo);
359 }
360 
361 /**
362   * @brief This is an external interface, remove the special handler from the sigchain.
363   * @param[in] signo, the value of the signal.
364   * @param[in] fn, the special handler of the signal.
365   * @retval void
366   */
remove_special_signal_handler(int signo,bool (* fn)(int,siginfo_t *,void *))367 void remove_special_signal_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
368 {
369     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
370     if (signo <= 0 || signo >= _NSIG) {
371         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
372         return;
373     }
374 
375     if (ismarked(signo)) {
376         // remove the special handler from the sigchain.
377         rm_special_handler(signo, fn);
378     }
379 }
380 
381 /**
382   * @brief This is an external interface, remove all special handler from the sigchain.
383   * @param[in] signo, the value of the signal.
384   * @retval void
385   */
remove_all_special_handler(int signo)386 void remove_all_special_handler(int signo)
387 {
388     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
389     if (signo <= 0 || signo >= _NSIG) {
390         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
391         return;
392     }
393 
394     if (ismarked(signo)) {
395         // remove all special handler from the sigchain.
396         for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
397             sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
398         }
399         unregister_sigchain(signo);
400     }
401 }
402 
403 /**
404   * @brief This is an external interface, add the special handler at the last of sigchain chains.
405   * @param[in] signo, the value of the signal.
406   * @param[in] sa, the action with special handler.
407   * @retval void
408   */
add_special_handler_at_last(int signo,struct signal_chain_action * sa)409 void add_special_handler_at_last(int signo, struct signal_chain_action* sa)
410 {
411     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
412     if (signo <= 0 || signo >= _NSIG) {
413         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
414         return;
415     }
416 
417     if (sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1].sca_sigaction == NULL) {
418         sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1] = *sa;
419         mark_signal_to_sigchain(signo);
420         return;
421     }
422 
423     SIGCHAIN_PRINT_FATAL("Add too many the special handlers at last!");
424 }
425 
426 /**
427   * @brief Intercept the signal and sigaction.
428   * @param[in] signo, the value of the signal.
429   * @param[in] sa, the new action with the signal handler.
430   * @param[out] old, the old action with the signal handler.
431   * @retval true if the signal if intercepted, or false.
432   */
intercept_sigaction(int signo,const struct sigaction * restrict sa,struct sigaction * restrict old)433 bool intercept_sigaction(int signo, const struct sigaction *restrict sa,
434                          struct sigaction *restrict old)
435 {
436     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
437     if (signo <= 0 || signo >= _NSIG) {
438         SIGCHAIN_PRINT_ERROR("%{public}s Invalid signal %{public}d", __func__, signo);
439         return false;
440     }
441 
442     if (ismarked(signo)) {
443         struct sigaction saved_action = getaction(signo);
444 
445         if (sa != NULL) {
446             setaction(signo, sa);
447         }
448         if (old != NULL) {
449             *old = saved_action;
450         }
451         return true;
452     }
453 
454     return false;
455 }
456 
457 /**
458   * @brief Intercept the pthread_sigmask.
459   * @param[in] how, the value of the mask operation .
460   * @param[out] set, the value of the sigset.
461   * @retval void.
462   */
intercept_pthread_sigmask(int how,sigset_t * restrict set)463 void intercept_pthread_sigmask(int how, sigset_t *restrict set)
464 {
465     SIGCHAIN_PRINT_DEBUG("%{public}s how: %{public}d", __func__, how);
466     // Forward directly to the system mask When this sigchain is handling a signal.
467     if (get_handling_signal()) {
468         return;
469     }
470 
471     sigset_t tmpset;
472     if (set != NULL) {
473         tmpset = *set;
474         if (how == SIG_BLOCK || how == SIG_SETMASK) {
475             for (int i = 1; i < _NSIG; ++i) {
476                 if (ismarked(i) && sigismember(&tmpset, i)) {
477                     sigdelset(&tmpset, i);
478                 }
479             }
480         }
481         *set = tmpset;
482     }
483 
484     return;
485 }
486