signal.h 13 KB

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