• 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 | SA_RESTORER;
81 		ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore;
82 		memcpy(&ksa.mask, &sa->sa_mask, _NSIG/8);
83 	}
84 	int r = __syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8);
85 	if (sig == SIGABRT && sa && sa->sa_handler != SIG_DFL) {
86 		UNLOCK(__abort_lock);
87 		__restore_sigs(&set);
88 	}
89 	if (old && !r) {
90 		old->sa_handler = ksa_old.handler;
91 		old->sa_flags = ksa_old.flags;
92 		memcpy(&old->sa_mask, &ksa_old.mask, _NSIG/8);
93 	}
94 	return __syscall_ret(r);
95 }
96 
__sigaction(int sig,const struct sigaction * restrict sa,struct sigaction * restrict old)97 int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
98 {
99 	if (sig-32U < 3 || sig-1U >= _NSIG-1) {
100 		errno = EINVAL;
101 		return -1;
102 	}
103 	/* sigchain intercepts sigaction */
104 	if (intercept_sigaction(sig, sa, old)) {
105 		return 0;
106 	}
107 
108 	return __libc_sigaction(sig, sa, old);
109 }
110 
111 weak_alias(__sigaction, sigaction);
112