signal.h 12 KB

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