signal.h 14 KB

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