1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_SIGNAL_H 3 #define _LINUX_SIGNAL_H 4 5 #include <linux/bug.h> 6 #include <linux/signal_types.h> 7 #include <linux/string.h> 8 9 struct task_struct; 10 11 /* for sysctl */ 12 extern int print_fatal_signals; 13 14 static inline void copy_siginfo(kernel_siginfo_t *to, 15 const kernel_siginfo_t *from) 16 { 17 memcpy(to, from, sizeof(*to)); 18 } 19 20 static inline void clear_siginfo(kernel_siginfo_t *info) 21 { 22 memset(info, 0, sizeof(*info)); 23 } 24 25 #define SI_EXPANSION_SIZE (sizeof(struct siginfo) - sizeof(struct kernel_siginfo)) 26 27 static inline void copy_siginfo_to_external(siginfo_t *to, 28 const kernel_siginfo_t *from) 29 { 30 memcpy(to, from, sizeof(*from)); 31 memset(((char *)to) + sizeof(struct kernel_siginfo), 0, 32 SI_EXPANSION_SIZE); 33 } 34 35 int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from); 36 int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from); 37 38 enum siginfo_layout { 39 SIL_KILL, 40 SIL_TIMER, 41 SIL_POLL, 42 SIL_FAULT, 43 SIL_FAULT_TRAPNO, 44 SIL_FAULT_MCEERR, 45 SIL_FAULT_BNDERR, 46 SIL_FAULT_PKUERR, 47 SIL_FAULT_PERF_EVENT, 48 SIL_CHLD, 49 SIL_RT, 50 SIL_SYS, 51 }; 52 53 enum siginfo_layout siginfo_layout(unsigned sig, int si_code); 54 55 /* 56 * Define some primitives to manipulate sigset_t. 57 */ 58 59 #ifndef __HAVE_ARCH_SIG_BITOPS 60 #include <linux/bitops.h> 61 62 /* We don't use <linux/bitops.h> for these because there is no need to 63 be atomic. */ 64 static inline void sigaddset(sigset_t *set, int _sig) 65 { 66 unsigned long sig = _sig - 1; 67 if (_NSIG_WORDS == 1) 68 set->sig[0] |= 1UL << sig; 69 else 70 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW); 71 } 72 73 static inline void sigdelset(sigset_t *set, int _sig) 74 { 75 unsigned long sig = _sig - 1; 76 if (_NSIG_WORDS == 1) 77 set->sig[0] &= ~(1UL << sig); 78 else 79 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW)); 80 } 81 82 static inline int sigismember(sigset_t *set, int _sig) 83 { 84 unsigned long sig = _sig - 1; 85 if (_NSIG_WORDS == 1) 86 return 1 & (set->sig[0] >> sig); 87 else 88 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW)); 89 } 90 91 #endif /* __HAVE_ARCH_SIG_BITOPS */ 92 93 static inline int sigisemptyset(sigset_t *set) 94 { 95 switch (_NSIG_WORDS) { 96 case 4: 97 return (set->sig[3] | set->sig[2] | 98 set->sig[1] | set->sig[0]) == 0; 99 case 2: 100 return (set->sig[1] | set->sig[0]) == 0; 101 case 1: 102 return set->sig[0] == 0; 103 default: 104 BUILD_BUG(); 105 return 0; 106 } 107 } 108 109 static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2) 110 { 111 switch (_NSIG_WORDS) { 112 case 4: 113 return (set1->sig[3] == set2->sig[3]) && 114 (set1->sig[2] == set2->sig[2]) && 115 (set1->sig[1] == set2->sig[1]) && 116 (set1->sig[0] == set2->sig[0]); 117 case 2: 118 return (set1->sig[1] == set2->sig[1]) && 119 (set1->sig[0] == set2->sig[0]); 120 case 1: 121 return set1->sig[0] == set2->sig[0]; 122 } 123 return 0; 124 } 125 126 #define sigmask(sig) (1UL << ((sig) - 1)) 127 128 #ifndef __HAVE_ARCH_SIG_SETOPS 129 130 #define _SIG_SET_BINOP(name, op) \ 131 static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \ 132 { \ 133 unsigned long a0, a1, a2, a3, b0, b1, b2, b3; \ 134 \ 135 switch (_NSIG_WORDS) { \ 136 case 4: \ 137 a3 = a->sig[3]; a2 = a->sig[2]; \ 138 b3 = b->sig[3]; b2 = b->sig[2]; \ 139 r->sig[3] = op(a3, b3); \ 140 r->sig[2] = op(a2, b2); \ 141 fallthrough; \ 142 case 2: \ 143 a1 = a->sig[1]; b1 = b->sig[1]; \ 144 r->sig[1] = op(a1, b1); \ 145 fallthrough; \ 146 case 1: \ 147 a0 = a->sig[0]; b0 = b->sig[0]; \ 148 r->sig[0] = op(a0, b0); \ 149 break; \ 150 default: \ 151 BUILD_BUG(); \ 152 } \ 153 } 154 155 #define _sig_or(x,y) ((x) | (y)) 156 _SIG_SET_BINOP(sigorsets, _sig_or) 157 158 #define _sig_and(x,y) ((x) & (y)) 159 _SIG_SET_BINOP(sigandsets, _sig_and) 160 161 #define _sig_andn(x,y) ((x) & ~(y)) 162 _SIG_SET_BINOP(sigandnsets, _sig_andn) 163 164 #undef _SIG_SET_BINOP 165 #undef _sig_or 166 #undef _sig_and 167 #undef _sig_andn 168 169 #define _SIG_SET_OP(name, op) \ 170 static inline void name(sigset_t *set) \ 171 { \ 172 switch (_NSIG_WORDS) { \ 173 case 4: set->sig[3] = op(set->sig[3]); \ 174 set->sig[2] = op(set->sig[2]); \ 175 fallthrough; \ 176 case 2: set->sig[1] = op(set->sig[1]); \ 177 fallthrough; \ 178 case 1: set->sig[0] = op(set->sig[0]); \ 179 break; \ 180 default: \ 181 BUILD_BUG(); \ 182 } \ 183 } 184 185 #define _sig_not(x) (~(x)) 186 _SIG_SET_OP(signotset, _sig_not) 187 188 #undef _SIG_SET_OP 189 #undef _sig_not 190 191 static inline void sigemptyset(sigset_t *set) 192 { 193 switch (_NSIG_WORDS) { 194 default: 195 memset(set, 0, sizeof(sigset_t)); 196 break; 197 case 2: set->sig[1] = 0; 198 fallthrough; 199 case 1: set->sig[0] = 0; 200 break; 201 } 202 } 203 204 static inline void sigfillset(sigset_t *set) 205 { 206 switch (_NSIG_WORDS) { 207 default: 208 memset(set, -1, sizeof(sigset_t)); 209 break; 210 case 2: set->sig[1] = -1; 211 fallthrough; 212 case 1: set->sig[0] = -1; 213 break; 214 } 215 } 216 217 /* Some extensions for manipulating the low 32 signals in particular. */ 218 219 static inline void sigaddsetmask(sigset_t *set, unsigned long mask) 220 { 221 set->sig[0] |= mask; 222 } 223 224 static inline void sigdelsetmask(sigset_t *set, unsigned long mask) 225 { 226 set->sig[0] &= ~mask; 227 } 228 229 static inline int sigtestsetmask(sigset_t *set, unsigned long mask) 230 { 231 return (set->sig[0] & mask) != 0; 232 } 233 234 static inline void siginitset(sigset_t *set, unsigned long mask) 235 { 236 set->sig[0] = mask; 237 switch (_NSIG_WORDS) { 238 default: 239 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1)); 240 break; 241 case 2: set->sig[1] = 0; 242 break; 243 case 1: ; 244 } 245 } 246 247 static inline void siginitsetinv(sigset_t *set, unsigned long mask) 248 { 249 set->sig[0] = ~mask; 250 switch (_NSIG_WORDS) { 251 default: 252 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1)); 253 break; 254 case 2: set->sig[1] = -1; 255 break; 256 case 1: ; 257 } 258 } 259 260 #endif /* __HAVE_ARCH_SIG_SETOPS */ 261 262 static inline void init_sigpending(struct sigpending *sig) 263 { 264 sigemptyset(&sig->signal); 265 INIT_LIST_HEAD(&sig->list); 266 } 267 268 extern void flush_sigqueue(struct sigpending *queue); 269 270 /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */ 271 static inline int valid_signal(unsigned long sig) 272 { 273 return sig <= _NSIG ? 1 : 0; 274 } 275 276 struct timespec; 277 struct pt_regs; 278 enum pid_type; 279 280 extern int next_signal(struct sigpending *pending, sigset_t *mask); 281 extern int do_send_sig_info(int sig, struct kernel_siginfo *info, 282 struct task_struct *p, enum pid_type type); 283 extern int group_send_sig_info(int sig, struct kernel_siginfo *info, 284 struct task_struct *p, enum pid_type type); 285 extern int __group_send_sig_info(int, struct kernel_siginfo *, struct task_struct *); 286 extern int sigprocmask(int, sigset_t *, sigset_t *); 287 extern void set_current_blocked(sigset_t *); 288 extern void __set_current_blocked(const sigset_t *); 289 extern int show_unhandled_signals; 290 291 extern bool get_signal(struct ksignal *ksig); 292 extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping); 293 extern void exit_signals(struct task_struct *tsk); 294 extern void kernel_sigaction(int, __sighandler_t); 295 296 #define SIG_KTHREAD ((__force __sighandler_t)2) 297 #define SIG_KTHREAD_KERNEL ((__force __sighandler_t)3) 298 299 static inline void allow_signal(int sig) 300 { 301 /* 302 * Kernel threads handle their own signals. Let the signal code 303 * know it'll be handled, so that they don't get converted to 304 * SIGKILL or just silently dropped. 305 */ 306 kernel_sigaction(sig, SIG_KTHREAD); 307 } 308 309 static inline void allow_kernel_signal(int sig) 310 { 311 /* 312 * Kernel threads handle their own signals. Let the signal code 313 * know signals sent by the kernel will be handled, so that they 314 * don't get silently dropped. 315 */ 316 kernel_sigaction(sig, SIG_KTHREAD_KERNEL); 317 } 318 319 static inline void disallow_signal(int sig) 320 { 321 kernel_sigaction(sig, SIG_IGN); 322 } 323 324 extern struct kmem_cache *sighand_cachep; 325 326 extern bool unhandled_signal(struct task_struct *tsk, int sig); 327 328 /* 329 * In POSIX a signal is sent either to a specific thread (Linux task) 330 * or to the process as a whole (Linux thread group). How the signal 331 * is sent determines whether it's to one thread or the whole group, 332 * which determines which signal mask(s) are involved in blocking it 333 * from being delivered until later. When the signal is delivered, 334 * either it's caught or ignored by a user handler or it has a default 335 * effect that applies to the whole thread group (POSIX process). 336 * 337 * The possible effects an unblocked signal set to SIG_DFL can have are: 338 * ignore - Nothing Happens 339 * terminate - kill the process, i.e. all threads in the group, 340 * similar to exit_group. The group leader (only) reports 341 * WIFSIGNALED status to its parent. 342 * coredump - write a core dump file describing all threads using 343 * the same mm and then kill all those threads 344 * stop - stop all the threads in the group, i.e. TASK_STOPPED state 345 * 346 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. 347 * Other signals when not blocked and set to SIG_DFL behaves as follows. 348 * The job control signals also have other special effects. 349 * 350 * +--------------------+------------------+ 351 * | POSIX signal | default action | 352 * +--------------------+------------------+ 353 * | SIGHUP | terminate | 354 * | SIGINT | terminate | 355 * | SIGQUIT | coredump | 356 * | SIGILL | coredump | 357 * | SIGTRAP | coredump | 358 * | SIGABRT/SIGIOT | coredump | 359 * | SIGBUS | coredump | 360 * | SIGFPE | coredump | 361 * | SIGKILL | terminate(+) | 362 * | SIGUSR1 | terminate | 363 * | SIGSEGV | coredump | 364 * | SIGUSR2 | terminate | 365 * | SIGPIPE | terminate | 366 * | SIGALRM | terminate | 367 * | SIGTERM | terminate | 368 * | SIGCHLD | ignore | 369 * | SIGCONT | ignore(*) | 370 * | SIGSTOP | stop(*)(+) | 371 * | SIGTSTP | stop(*) | 372 * | SIGTTIN | stop(*) | 373 * | SIGTTOU | stop(*) | 374 * | SIGURG | ignore | 375 * | SIGXCPU | coredump | 376 * | SIGXFSZ | coredump | 377 * | SIGVTALRM | terminate | 378 * | SIGPROF | terminate | 379 * | SIGPOLL/SIGIO | terminate | 380 * | SIGSYS/SIGUNUSED | coredump | 381 * | SIGSTKFLT | terminate | 382 * | SIGWINCH | ignore | 383 * | SIGPWR | terminate | 384 * | SIGRTMIN-SIGRTMAX | terminate | 385 * +--------------------+------------------+ 386 * | non-POSIX signal | default action | 387 * +--------------------+------------------+ 388 * | SIGEMT | coredump | 389 * +--------------------+------------------+ 390 * 391 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default". 392 * (*) Special job control effects: 393 * When SIGCONT is sent, it resumes the process (all threads in the group) 394 * from TASK_STOPPED state and also clears any pending/queued stop signals 395 * (any of those marked with "stop(*)"). This happens regardless of blocking, 396 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears 397 * any pending/queued SIGCONT signals; this happens regardless of blocking, 398 * catching, or ignored the stop signal, though (except for SIGSTOP) the 399 * default action of stopping the process may happen later or never. 400 */ 401 402 #ifdef SIGEMT 403 #define SIGEMT_MASK rt_sigmask(SIGEMT) 404 #else 405 #define SIGEMT_MASK 0 406 #endif 407 408 #if SIGRTMIN > BITS_PER_LONG 409 #define rt_sigmask(sig) (1ULL << ((sig)-1)) 410 #else 411 #define rt_sigmask(sig) sigmask(sig) 412 #endif 413 414 #define siginmask(sig, mask) \ 415 ((sig) > 0 && (sig) < SIGRTMIN && (rt_sigmask(sig) & (mask))) 416 417 #define SIG_KERNEL_ONLY_MASK (\ 418 rt_sigmask(SIGKILL) | rt_sigmask(SIGSTOP)) 419 420 #define SIG_KERNEL_STOP_MASK (\ 421 rt_sigmask(SIGSTOP) | rt_sigmask(SIGTSTP) | \ 422 rt_sigmask(SIGTTIN) | rt_sigmask(SIGTTOU) ) 423 424 #define SIG_KERNEL_COREDUMP_MASK (\ 425 rt_sigmask(SIGQUIT) | rt_sigmask(SIGILL) | \ 426 rt_sigmask(SIGTRAP) | rt_sigmask(SIGABRT) | \ 427 rt_sigmask(SIGFPE) | rt_sigmask(SIGSEGV) | \ 428 rt_sigmask(SIGBUS) | rt_sigmask(SIGSYS) | \ 429 rt_sigmask(SIGXCPU) | rt_sigmask(SIGXFSZ) | \ 430 SIGEMT_MASK ) 431 432 #define SIG_KERNEL_IGNORE_MASK (\ 433 rt_sigmask(SIGCONT) | rt_sigmask(SIGCHLD) | \ 434 rt_sigmask(SIGWINCH) | rt_sigmask(SIGURG) ) 435 436 #define SIG_SPECIFIC_SICODES_MASK (\ 437 rt_sigmask(SIGILL) | rt_sigmask(SIGFPE) | \ 438 rt_sigmask(SIGSEGV) | rt_sigmask(SIGBUS) | \ 439 rt_sigmask(SIGTRAP) | rt_sigmask(SIGCHLD) | \ 440 rt_sigmask(SIGPOLL) | rt_sigmask(SIGSYS) | \ 441 SIGEMT_MASK ) 442 443 #define sig_kernel_only(sig) siginmask(sig, SIG_KERNEL_ONLY_MASK) 444 #define sig_kernel_coredump(sig) siginmask(sig, SIG_KERNEL_COREDUMP_MASK) 445 #define sig_kernel_ignore(sig) siginmask(sig, SIG_KERNEL_IGNORE_MASK) 446 #define sig_kernel_stop(sig) siginmask(sig, SIG_KERNEL_STOP_MASK) 447 #define sig_specific_sicodes(sig) siginmask(sig, SIG_SPECIFIC_SICODES_MASK) 448 449 #define sig_fatal(t, signr) \ 450 (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \ 451 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL) 452 453 void signals_init(void); 454 455 int restore_altstack(const stack_t __user *); 456 int __save_altstack(stack_t __user *, unsigned long); 457 458 #define unsafe_save_altstack(uss, sp, label) do { \ 459 stack_t __user *__uss = uss; \ 460 struct task_struct *t = current; \ 461 unsafe_put_user((void __user *)t->sas_ss_sp, &__uss->ss_sp, label); \ 462 unsafe_put_user(t->sas_ss_flags, &__uss->ss_flags, label); \ 463 unsafe_put_user(t->sas_ss_size, &__uss->ss_size, label); \ 464 } while (0); 465 466 #ifdef CONFIG_DYNAMIC_SIGFRAME 467 bool sigaltstack_size_valid(size_t ss_size); 468 #else 469 static inline bool sigaltstack_size_valid(size_t size) { return true; } 470 #endif /* !CONFIG_DYNAMIC_SIGFRAME */ 471 472 #ifdef CONFIG_PROC_FS 473 struct seq_file; 474 extern void render_sigset_t(struct seq_file *, const char *, sigset_t *); 475 #endif 476 477 #ifndef arch_untagged_si_addr 478 /* 479 * Given a fault address and a signal and si_code which correspond to the 480 * _sigfault union member, returns the address that must appear in si_addr if 481 * the signal handler does not have SA_EXPOSE_TAGBITS enabled in sa_flags. 482 */ 483 static inline void __user *arch_untagged_si_addr(void __user *addr, 484 unsigned long sig, 485 unsigned long si_code) 486 { 487 return addr; 488 } 489 #endif 490 491 #endif /* _LINUX_SIGNAL_H */ 492