• 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_ERROR("%{public}s call usr sigaction for signal: %{public}d sig_action.sa_sigaction=%{public}p", __func__, signo, sig_chains[signo - 1].sig_action.sa_sigaction);
200         sig_chains[signo - 1].sig_action.sa_sigaction(signo, siginfo, ucontext_raw);
201     } else {
202         if (sig_chains[signo - 1].sig_action.sa_handler == SIG_IGN) {
203             SIGCHAIN_PRINT_ERROR("%{public}s SIG_IGN handler for signal: %{public}d", __func__, signo);
204             return;
205         } else if (sig_chains[signo - 1].sig_action.sa_handler == SIG_DFL) {
206             SIGCHAIN_PRINT_ERROR("%{public}s SIG_DFL handler for signal: %{public}d", __func__, signo);
207             remove_all_special_handler(signo);
208             if (__syscall(SYS_rt_tgsigqueueinfo, __syscall(SYS_getpid), __syscall(SYS_gettid), signo, siginfo) != 0) {
209                 SIGCHAIN_PRINT_ERROR("Failed to rethrow sig(%{public}d), errno(%{public}d).", signo, errno);
210             } else {
211                 SIGCHAIN_PRINT_ERROR("pid(%{public}d) rethrow sig(%{public}d) success.", __syscall(SYS_getpid), signo);
212             }
213         } else {
214             SIGCHAIN_PRINT_ERROR("%{public}s call usr sa_handler: %{public}p for signal: %{public}d sig_action.sa_handler=%{public}p", __func__, signo, sig_chains[signo - 1].sig_action.sa_handler);
215             sig_chains[signo - 1].sig_action.sa_handler(signo);
216         }
217     }
218 
219     return;
220 }
221 
222 /**
223   * @brief Register the signal chain with the kernel if needed
224   * @param[in] signo, the value of the signal.
225   * @retval void
226   */
sigchain_register(int signo)227 static void sigchain_register(int signo)
228 {
229     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
230     struct sigaction signal_action = {};
231     sigfillset(&signal_action.sa_mask);
232 
233     signal_action.sa_sigaction = signal_chain_handler;
234     signal_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
235     __libc_sigaction(signo, &signal_action, &sig_chains[signo - 1].sig_action);
236 }
237 
238 /**
239   * @brief Unregister the signal from sigchain, register the signal's user handler with the kernel if needed
240   * @param[in] signo, the value of the signal.
241   * @retval void
242   */
unregister_sigchain(int signo)243 static void unregister_sigchain(int signo)
244 {
245     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
246     __libc_sigaction(signo, &sig_chains[signo - 1].sig_action, NULL);
247     sig_chains[signo - 1].marked = false;
248 }
249 
250 /**
251   * @brief Mark the signal to the sigchain.
252   * @param[in] signo, the value of the signal.
253   * @retval void
254   */
mark_signal_to_sigchain(int signo)255 static void mark_signal_to_sigchain(int signo)
256 {
257     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
258     if (!sig_chains[signo - 1].marked) {
259         sigchain_register(signo);
260         sig_chains[signo - 1].marked = true;
261     }
262 }
263 
264 /**
265   * @brief Set the action of the signal.
266   * @param[in] signo, the value of the signal.
267   * @param[in] new_sa, the new action of the signal.
268   * @retval void
269   */
setaction(int signo,const struct sigaction * restrict new_sa)270 static void setaction(int signo, const struct sigaction *restrict new_sa)
271 {
272     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
273     sig_chains[signo - 1].sig_action = *new_sa;
274 }
275 
276 /**
277   * @brief Get the action of the signal.
278   * @param[in] signo, the value of the signal.
279   * @retval The current action of the signal
280   */
getaction(int signo)281 static struct sigaction getaction(int signo)
282 {
283     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
284     return sig_chains[signo - 1].sig_action;
285 }
286 
287 /**
288   * @brief Add the special handler to the sigchain.
289   * @param[in] signo, the value of the signal.
290   * @param[in] sa, the action with special handler.
291   * @retval void
292   */
add_special_handler(int signo,struct signal_chain_action * sa)293 static void add_special_handler(int signo, struct signal_chain_action* sa)
294 {
295     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
296     for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
297         if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == NULL) {
298             sig_chains[signo - 1].sca_special_actions[i] = *sa;
299             SIGCHAIN_PRINT_INFO("%{public}s signo %{public}d is registered with special handler!", __func__, signo);
300             return;
301         }
302     }
303 
304     SIGCHAIN_PRINT_FATAL("Add too many the special handlers!");
305 }
306 
307 /**
308   * @brief Remove the special handler from the sigchain.
309   * @param[in] signo, the value of the signal.
310   * @param[in] fn, the special handler of the signal.
311   * @retval void
312   */
rm_special_handler(int signo,bool (* fn)(int,siginfo_t *,void *))313 static void rm_special_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
314 {
315     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
316     int len = SIGNAL_CHAIN_SPECIAL_ACTION_MAX;
317     for (int i = 0; i < len; i++) {
318         if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction == fn) {
319             sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
320             int count = 0;
321             for (int k = 0; k < len; k++) {
322                 if (sig_chains[signo - 1].sca_special_actions[k].sca_sigaction == NULL) {
323                     count++;
324                 }
325             }
326             if (count == len) {
327                 unregister_sigchain(signo);
328             }
329             return;
330         }
331     }
332 
333     SIGCHAIN_PRINT_FATAL("%{public}s failed to remove the special handler!. signo: %{public}d",
334             __func__, signo);
335 }
336 
337 /**
338   * @brief This is an external interface,
339   *        Mark the signal to sigchain ,add the special handler to the sigchain.
340   * @param[in] signo, the value of the signal.
341   * @param[in] sa, the action with special handler.
342   * @retval void
343   */
add_special_signal_handler(int signo,struct signal_chain_action * sa)344 void add_special_signal_handler(int signo, struct signal_chain_action* sa)
345 {
346     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
347     if (signo <= 0 || signo >= _NSIG) {
348         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
349         return;
350     }
351 
352     // Add the special hander to the sigchain
353     add_special_handler(signo, sa);
354     mark_signal_to_sigchain(signo);
355 }
356 
357 /**
358   * @brief This is an external interface, remove the special handler from the sigchain.
359   * @param[in] signo, the value of the signal.
360   * @param[in] fn, the special handler of the signal.
361   * @retval void
362   */
remove_special_signal_handler(int signo,bool (* fn)(int,siginfo_t *,void *))363 void remove_special_signal_handler(int signo, bool (*fn)(int, siginfo_t*, void*))
364 {
365     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
366     if (signo <= 0 || signo >= _NSIG) {
367         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
368         return;
369     }
370 
371     if (ismarked(signo)) {
372         // remove the special handler from the sigchain.
373         rm_special_handler(signo, fn);
374     }
375 }
376 
377 /**
378   * @brief This is an external interface, remove all special handler from the sigchain.
379   * @param[in] signo, the value of the signal.
380   * @retval void
381   */
remove_all_special_handler(int signo)382 void remove_all_special_handler(int signo)
383 {
384     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
385     if (signo <= 0 || signo >= _NSIG) {
386         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
387         return;
388     }
389 
390     if (ismarked(signo)) {
391         // remove all special handler from the sigchain.
392         for (int i = 0; i < SIGNAL_CHAIN_SPECIAL_ACTION_MAX; i++) {
393             sig_chains[signo - 1].sca_special_actions[i].sca_sigaction = NULL;
394         }
395         unregister_sigchain(signo);
396     }
397 }
398 
399 /**
400   * @brief This is an external interface, add the special handler at the last of sigchain chains.
401   * @param[in] signo, the value of the signal.
402   * @param[in] sa, the action with special handler.
403   * @retval void
404   */
add_special_handler_at_last(int signo,struct signal_chain_action * sa)405 void add_special_handler_at_last(int signo, struct signal_chain_action* sa)
406 {
407     SIGCHAIN_PRINT_INFO("%{public}s signo: %{public}d", __func__, signo);
408     if (signo <= 0 || signo >= _NSIG) {
409         SIGCHAIN_PRINT_FATAL("%{public}s Invalid signal %{public}d", __func__, signo);
410         return;
411     }
412 
413     if (sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1].sca_sigaction == NULL) {
414         sig_chains[signo - 1].sca_special_actions[SIGNAL_CHAIN_SPECIAL_ACTION_MAX - 1] = *sa;
415         mark_signal_to_sigchain(signo);
416         return;
417     }
418 
419     SIGCHAIN_PRINT_FATAL("Add too many the special handlers at last!");
420 }
421 
422 /**
423   * @brief Intercept the signal and sigaction.
424   * @param[in] signo, the value of the signal.
425   * @param[in] sa, the new action with the signal handler.
426   * @param[out] old, the old action with the signal handler.
427   * @retval true if the signal if intercepted, or false.
428   */
intercept_sigaction(int signo,const struct sigaction * restrict sa,struct sigaction * restrict old)429 bool intercept_sigaction(int signo, const struct sigaction *restrict sa,
430                          struct sigaction *restrict old)
431 {
432     SIGCHAIN_PRINT_DEBUG("%{public}s signo: %{public}d", __func__, signo);
433     if (signo <= 0 || signo >= _NSIG) {
434         SIGCHAIN_PRINT_ERROR("%{public}s Invalid signal %{public}d", __func__, signo);
435         return false;
436     }
437 
438     if (ismarked(signo)) {
439         struct sigaction saved_action = getaction(signo);
440 
441         if (sa != NULL) {
442             setaction(signo, sa);
443         }
444         if (old != NULL) {
445             *old = saved_action;
446         }
447         return true;
448     }
449 
450     return false;
451 }
452 
453 /**
454   * @brief Intercept the pthread_sigmask.
455   * @param[in] how, the value of the mask operation .
456   * @param[out] set, the value of the sigset.
457   * @retval void.
458   */
intercept_pthread_sigmask(int how,sigset_t * restrict set)459 void intercept_pthread_sigmask(int how, sigset_t *restrict set)
460 {
461     SIGCHAIN_PRINT_DEBUG("%{public}s how: %{public}d", __func__, how);
462     // Forward directly to the system mask When this sigchain is handling a signal.
463     if (get_handling_signal()) {
464         return;
465     }
466 
467     sigset_t tmpset;
468     if (set != NULL) {
469         tmpset = *set;
470         if (how == SIG_BLOCK || how == SIG_SETMASK) {
471             for (int i = 1; i < _NSIG; ++i) {
472                 if (ismarked(i) && sigismember(&tmpset, i)) {
473                     sigdelset(&tmpset, i);
474                 }
475             }
476         }
477         *set = tmpset;
478     }
479 
480     return;
481 }
482