1 /* Special use of signals internally.  Linux version.
2    Copyright (C) 2014-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef __INTERNAL_SIGNALS_H
20 # define __INTERNAL_SIGNALS_H
21 
22 #include <internal-sigset.h>
23 #include <limits.h>
24 #include <signal.h>
25 #include <sigsetops.h>
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <sysdep.h>
29 
30 /* The signal used for asynchronous cancelation.  */
31 #define SIGCANCEL       __SIGRTMIN
32 
33 
34 /* Signal needed for the kernel-supported POSIX timer implementation.
35    We can reuse the cancellation signal since we can distinguish
36    cancellation from timer expirations.  */
37 #define SIGTIMER        SIGCANCEL
38 
39 
40 /* Signal used to implement the setuid et.al. functions.  */
41 #define SIGSETXID       (__SIGRTMIN + 1)
42 
43 
44 /* How many signal numbers need to be reserved for libpthread's private uses
45    (SIGCANCEL and SIGSETXID).  */
46 #define RESERVED_SIGRT  2
47 
48 
49 /* Return is sig is used internally.  */
50 static inline bool
is_internal_signal(int sig)51 is_internal_signal (int sig)
52 {
53   return (sig == SIGCANCEL) || (sig == SIGSETXID);
54 }
55 
56 /* Remove internal glibc signal from the mask.  */
57 static inline void
clear_internal_signals(sigset_t * set)58 clear_internal_signals (sigset_t *set)
59 {
60   __sigdelset (set, SIGCANCEL);
61   __sigdelset (set, SIGSETXID);
62 }
63 
64 static const internal_sigset_t sigall_set = {
65    .__val = {[0 ...  __NSIG_WORDS-1 ] =  -1 }
66 };
67 
68 /* Obtain and change blocked signals, including internal glibc ones.  */
69 static inline int
internal_sigprocmask(int how,const internal_sigset_t * set,internal_sigset_t * oldset)70 internal_sigprocmask (int how, const internal_sigset_t *set,
71 		      internal_sigset_t *oldset)
72 {
73   return INTERNAL_SYSCALL_CALL (rt_sigprocmask, how, set, oldset,
74 				__NSIG_BYTES);
75 }
76 
77 /* Block all signals, including internal glibc ones.  */
78 static inline void
internal_signal_block_all(internal_sigset_t * oset)79 internal_signal_block_all (internal_sigset_t *oset)
80 {
81   INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_BLOCK, &sigall_set, oset,
82 			 __NSIG_BYTES);
83 }
84 
85 /* Restore current process signal mask.  */
86 static inline void
internal_signal_restore_set(const internal_sigset_t * set)87 internal_signal_restore_set (const internal_sigset_t *set)
88 {
89   INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_SETMASK, set, NULL,
90 			 __NSIG_BYTES);
91 }
92 
93 
94 /* It is used on timer_create code directly on sigwaitinfo call, so it can not
95    use the internal_sigset_t definitions.  */
96 static const sigset_t sigtimer_set = {
97   .__val = { [0]                      = __sigmask (SIGTIMER),
98              [1 ... _SIGSET_NWORDS-1] = 0
99   }
100 };
101 
102 /* Unblock only SIGTIMER.  */
103 static inline void
signal_unblock_sigtimer(void)104 signal_unblock_sigtimer (void)
105 {
106   INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_UNBLOCK, &sigtimer_set, NULL,
107 			 __NSIG_BYTES);
108 }
109 
110 #endif
111