• 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 <signal.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <stdbool.h>
20 #include "syscall.h"
21 #include "pthread_impl.h"
22 #include "libc.h"
23 #include "lock.h"
24 #include "ksigaction.h"
25 
26 extern bool intercept_sigaction(int signo, const struct sigaction *restrict sa, struct sigaction *restrict old);
27 
28 static volatile int dummy_lock[1] = { 0 };
29 
30 extern hidden volatile int __abort_lock[1];
31 
32 weak_alias(dummy_lock, __abort_lock);
33 
34 static int unmask_done;
35 static unsigned long handler_set[_NSIG / (8 * sizeof(long))];
36 
__get_handler_set(sigset_t * set)37 void __get_handler_set(sigset_t *set)
38 {
39 	memcpy(set, handler_set, sizeof handler_set);
40 }
41 
42 volatile int __eintr_valid_flag;
43 
__libc_sigaction(int sig,const struct sigaction * restrict sa,struct sigaction * restrict old)44 int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
45 {
46 	struct k_sigaction ksa, ksa_old;
47 	unsigned long set[_NSIG / (8 * sizeof(long))];
48 	if (sa) {
49 		if ((uintptr_t)sa->sa_handler > 1UL) {
50 			a_or_l(handler_set + (sig - 1) / (8 * sizeof(long)),
51 				1UL<<(sig - 1) % (8 * sizeof(long)));
52 
53 			/* If pthread_create has not yet been called,
54 			 * implementation-internal signals might not
55 			 * yet have been unblocked. They must be
56 			 * unblocked before any signal handler is
57 			 * installed, so that an application cannot
58 			 * receive an illegal sigset_t (with them
59 			 * blocked) as part of the ucontext_t passed
60 			 * to the signal handler. */
61 			if (!libc.threaded && !unmask_done) {
62 				__syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
63 					SIGPT_SET, 0, _NSIG/8);
64 				unmask_done = 1;
65 			}
66 
67 			if (!(sa->sa_flags & SA_RESTART)) {
68 				a_store(&__eintr_valid_flag, 1);
69 			}
70 		}
71 		/* Changing the disposition of SIGABRT to anything but
72 		 * SIG_DFL requires a lock, so that it cannot be changed
73 		 * while abort is terminating the process after simply
74 		 * calling raise(SIGABRT) failed to do so. */
75 		if (sa->sa_handler != SIG_DFL && sig == SIGABRT) {
76 			__block_all_sigs(&set);
77 			LOCK(__abort_lock);
78 		}
79 		ksa.handler = sa->sa_handler;
80 		ksa.flags = sa->sa_flags;
81 #ifdef SA_RESTORER
82 		ksa.flags |= SA_RESTORER;
83 		ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore;
84 #endif
85 		memcpy(&ksa.mask, &sa->sa_mask, _NSIG/8);
86 	}
87 	int r = __syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8);
88 	if (sig == SIGABRT && sa && sa->sa_handler != SIG_DFL) {
89 		UNLOCK(__abort_lock);
90 		__restore_sigs(&set);
91 	}
92 	if (old && !r) {
93 		old->sa_handler = ksa_old.handler;
94 		old->sa_flags = ksa_old.flags;
95 		memcpy(&old->sa_mask, &ksa_old.mask, _NSIG/8);
96 	}
97 	return __syscall_ret(r);
98 }
99 
__sigaction(int sig,const struct sigaction * restrict sa,struct sigaction * restrict old)100 int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
101 {
102 	if (sig - 32U < 3 || sig - 1U >= _NSIG-1) {
103 		errno = EINVAL;
104 		return -1;
105 	}
106 	/* sigchain intercepts sigaction */
107 	if (intercept_sigaction(sig, sa, old)) {
108 		return 0;
109 	}
110 
111 	return __libc_sigaction(sig, sa, old);
112 }
113 
114 weak_alias(__sigaction, sigaction);
115