signal.h 14 KB

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