signal.h 12 KB

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