Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_SIGNAL_H
0003 #define _LINUX_SIGNAL_H
0004 
0005 #include <linux/bug.h>
0006 #include <linux/signal_types.h>
0007 #include <linux/string.h>
0008 
0009 struct task_struct;
0010 
0011 /* for sysctl */
0012 extern int print_fatal_signals;
0013 
0014 static inline void copy_siginfo(kernel_siginfo_t *to,
0015                 const kernel_siginfo_t *from)
0016 {
0017     memcpy(to, from, sizeof(*to));
0018 }
0019 
0020 static inline void clear_siginfo(kernel_siginfo_t *info)
0021 {
0022     memset(info, 0, sizeof(*info));
0023 }
0024 
0025 #define SI_EXPANSION_SIZE (sizeof(struct siginfo) - sizeof(struct kernel_siginfo))
0026 
0027 static inline void copy_siginfo_to_external(siginfo_t *to,
0028                         const kernel_siginfo_t *from)
0029 {
0030     memcpy(to, from, sizeof(*from));
0031     memset(((char *)to) + sizeof(struct kernel_siginfo), 0,
0032         SI_EXPANSION_SIZE);
0033 }
0034 
0035 int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from);
0036 int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from);
0037 
0038 enum siginfo_layout {
0039     SIL_KILL,
0040     SIL_TIMER,
0041     SIL_POLL,
0042     SIL_FAULT,
0043     SIL_FAULT_TRAPNO,
0044     SIL_FAULT_MCEERR,
0045     SIL_FAULT_BNDERR,
0046     SIL_FAULT_PKUERR,
0047     SIL_FAULT_PERF_EVENT,
0048     SIL_CHLD,
0049     SIL_RT,
0050     SIL_SYS,
0051 };
0052 
0053 enum siginfo_layout siginfo_layout(unsigned sig, int si_code);
0054 
0055 /*
0056  * Define some primitives to manipulate sigset_t.
0057  */
0058 
0059 #ifndef __HAVE_ARCH_SIG_BITOPS
0060 #include <linux/bitops.h>
0061 
0062 /* We don't use <linux/bitops.h> for these because there is no need to
0063    be atomic.  */
0064 static inline void sigaddset(sigset_t *set, int _sig)
0065 {
0066     unsigned long sig = _sig - 1;
0067     if (_NSIG_WORDS == 1)
0068         set->sig[0] |= 1UL << sig;
0069     else
0070         set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
0071 }
0072 
0073 static inline void sigdelset(sigset_t *set, int _sig)
0074 {
0075     unsigned long sig = _sig - 1;
0076     if (_NSIG_WORDS == 1)
0077         set->sig[0] &= ~(1UL << sig);
0078     else
0079         set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
0080 }
0081 
0082 static inline int sigismember(sigset_t *set, int _sig)
0083 {
0084     unsigned long sig = _sig - 1;
0085     if (_NSIG_WORDS == 1)
0086         return 1 & (set->sig[0] >> sig);
0087     else
0088         return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
0089 }
0090 
0091 #endif /* __HAVE_ARCH_SIG_BITOPS */
0092 
0093 static inline int sigisemptyset(sigset_t *set)
0094 {
0095     switch (_NSIG_WORDS) {
0096     case 4:
0097         return (set->sig[3] | set->sig[2] |
0098             set->sig[1] | set->sig[0]) == 0;
0099     case 2:
0100         return (set->sig[1] | set->sig[0]) == 0;
0101     case 1:
0102         return set->sig[0] == 0;
0103     default:
0104         BUILD_BUG();
0105         return 0;
0106     }
0107 }
0108 
0109 static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
0110 {
0111     switch (_NSIG_WORDS) {
0112     case 4:
0113         return  (set1->sig[3] == set2->sig[3]) &&
0114             (set1->sig[2] == set2->sig[2]) &&
0115             (set1->sig[1] == set2->sig[1]) &&
0116             (set1->sig[0] == set2->sig[0]);
0117     case 2:
0118         return  (set1->sig[1] == set2->sig[1]) &&
0119             (set1->sig[0] == set2->sig[0]);
0120     case 1:
0121         return  set1->sig[0] == set2->sig[0];
0122     }
0123     return 0;
0124 }
0125 
0126 #define sigmask(sig)    (1UL << ((sig) - 1))
0127 
0128 #ifndef __HAVE_ARCH_SIG_SETOPS
0129 
0130 #define _SIG_SET_BINOP(name, op)                    \
0131 static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
0132 {                                   \
0133     unsigned long a0, a1, a2, a3, b0, b1, b2, b3;           \
0134                                     \
0135     switch (_NSIG_WORDS) {                      \
0136     case 4:                             \
0137         a3 = a->sig[3]; a2 = a->sig[2];             \
0138         b3 = b->sig[3]; b2 = b->sig[2];             \
0139         r->sig[3] = op(a3, b3);                 \
0140         r->sig[2] = op(a2, b2);                 \
0141         fallthrough;                        \
0142     case 2:                             \
0143         a1 = a->sig[1]; b1 = b->sig[1];             \
0144         r->sig[1] = op(a1, b1);                 \
0145         fallthrough;                        \
0146     case 1:                             \
0147         a0 = a->sig[0]; b0 = b->sig[0];             \
0148         r->sig[0] = op(a0, b0);                 \
0149         break;                          \
0150     default:                            \
0151         BUILD_BUG();                        \
0152     }                               \
0153 }
0154 
0155 #define _sig_or(x,y)    ((x) | (y))
0156 _SIG_SET_BINOP(sigorsets, _sig_or)
0157 
0158 #define _sig_and(x,y)   ((x) & (y))
0159 _SIG_SET_BINOP(sigandsets, _sig_and)
0160 
0161 #define _sig_andn(x,y)  ((x) & ~(y))
0162 _SIG_SET_BINOP(sigandnsets, _sig_andn)
0163 
0164 #undef _SIG_SET_BINOP
0165 #undef _sig_or
0166 #undef _sig_and
0167 #undef _sig_andn
0168 
0169 #define _SIG_SET_OP(name, op)                       \
0170 static inline void name(sigset_t *set)                  \
0171 {                                   \
0172     switch (_NSIG_WORDS) {                      \
0173     case 4: set->sig[3] = op(set->sig[3]);              \
0174         set->sig[2] = op(set->sig[2]);              \
0175         fallthrough;                        \
0176     case 2: set->sig[1] = op(set->sig[1]);              \
0177         fallthrough;                        \
0178     case 1: set->sig[0] = op(set->sig[0]);              \
0179             break;                      \
0180     default:                            \
0181         BUILD_BUG();                        \
0182     }                               \
0183 }
0184 
0185 #define _sig_not(x) (~(x))
0186 _SIG_SET_OP(signotset, _sig_not)
0187 
0188 #undef _SIG_SET_OP
0189 #undef _sig_not
0190 
0191 static inline void sigemptyset(sigset_t *set)
0192 {
0193     switch (_NSIG_WORDS) {
0194     default:
0195         memset(set, 0, sizeof(sigset_t));
0196         break;
0197     case 2: set->sig[1] = 0;
0198         fallthrough;
0199     case 1: set->sig[0] = 0;
0200         break;
0201     }
0202 }
0203 
0204 static inline void sigfillset(sigset_t *set)
0205 {
0206     switch (_NSIG_WORDS) {
0207     default:
0208         memset(set, -1, sizeof(sigset_t));
0209         break;
0210     case 2: set->sig[1] = -1;
0211         fallthrough;
0212     case 1: set->sig[0] = -1;
0213         break;
0214     }
0215 }
0216 
0217 /* Some extensions for manipulating the low 32 signals in particular.  */
0218 
0219 static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
0220 {
0221     set->sig[0] |= mask;
0222 }
0223 
0224 static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
0225 {
0226     set->sig[0] &= ~mask;
0227 }
0228 
0229 static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
0230 {
0231     return (set->sig[0] & mask) != 0;
0232 }
0233 
0234 static inline void siginitset(sigset_t *set, unsigned long mask)
0235 {
0236     set->sig[0] = mask;
0237     switch (_NSIG_WORDS) {
0238     default:
0239         memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
0240         break;
0241     case 2: set->sig[1] = 0;
0242         break;
0243     case 1: ;
0244     }
0245 }
0246 
0247 static inline void siginitsetinv(sigset_t *set, unsigned long mask)
0248 {
0249     set->sig[0] = ~mask;
0250     switch (_NSIG_WORDS) {
0251     default:
0252         memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
0253         break;
0254     case 2: set->sig[1] = -1;
0255         break;
0256     case 1: ;
0257     }
0258 }
0259 
0260 #endif /* __HAVE_ARCH_SIG_SETOPS */
0261 
0262 static inline void init_sigpending(struct sigpending *sig)
0263 {
0264     sigemptyset(&sig->signal);
0265     INIT_LIST_HEAD(&sig->list);
0266 }
0267 
0268 extern void flush_sigqueue(struct sigpending *queue);
0269 
0270 /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
0271 static inline int valid_signal(unsigned long sig)
0272 {
0273     return sig <= _NSIG ? 1 : 0;
0274 }
0275 
0276 struct timespec;
0277 struct pt_regs;
0278 enum pid_type;
0279 
0280 extern int next_signal(struct sigpending *pending, sigset_t *mask);
0281 extern int do_send_sig_info(int sig, struct kernel_siginfo *info,
0282                 struct task_struct *p, enum pid_type type);
0283 extern int group_send_sig_info(int sig, struct kernel_siginfo *info,
0284                    struct task_struct *p, enum pid_type type);
0285 extern int send_signal_locked(int sig, struct kernel_siginfo *info,
0286                   struct task_struct *p, enum pid_type type);
0287 extern int sigprocmask(int, sigset_t *, sigset_t *);
0288 extern void set_current_blocked(sigset_t *);
0289 extern void __set_current_blocked(const sigset_t *);
0290 extern int show_unhandled_signals;
0291 
0292 extern bool get_signal(struct ksignal *ksig);
0293 extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
0294 extern void exit_signals(struct task_struct *tsk);
0295 extern void kernel_sigaction(int, __sighandler_t);
0296 
0297 #define SIG_KTHREAD ((__force __sighandler_t)2)
0298 #define SIG_KTHREAD_KERNEL ((__force __sighandler_t)3)
0299 
0300 static inline void allow_signal(int sig)
0301 {
0302     /*
0303      * Kernel threads handle their own signals. Let the signal code
0304      * know it'll be handled, so that they don't get converted to
0305      * SIGKILL or just silently dropped.
0306      */
0307     kernel_sigaction(sig, SIG_KTHREAD);
0308 }
0309 
0310 static inline void allow_kernel_signal(int sig)
0311 {
0312     /*
0313      * Kernel threads handle their own signals. Let the signal code
0314      * know signals sent by the kernel will be handled, so that they
0315      * don't get silently dropped.
0316      */
0317     kernel_sigaction(sig, SIG_KTHREAD_KERNEL);
0318 }
0319 
0320 static inline void disallow_signal(int sig)
0321 {
0322     kernel_sigaction(sig, SIG_IGN);
0323 }
0324 
0325 extern struct kmem_cache *sighand_cachep;
0326 
0327 extern bool unhandled_signal(struct task_struct *tsk, int sig);
0328 
0329 /*
0330  * In POSIX a signal is sent either to a specific thread (Linux task)
0331  * or to the process as a whole (Linux thread group).  How the signal
0332  * is sent determines whether it's to one thread or the whole group,
0333  * which determines which signal mask(s) are involved in blocking it
0334  * from being delivered until later.  When the signal is delivered,
0335  * either it's caught or ignored by a user handler or it has a default
0336  * effect that applies to the whole thread group (POSIX process).
0337  *
0338  * The possible effects an unblocked signal set to SIG_DFL can have are:
0339  *   ignore - Nothing Happens
0340  *   terminate  - kill the process, i.e. all threads in the group,
0341  *        similar to exit_group.  The group leader (only) reports
0342  *        WIFSIGNALED status to its parent.
0343  *   coredump   - write a core dump file describing all threads using
0344  *        the same mm and then kill all those threads
0345  *   stop   - stop all the threads in the group, i.e. TASK_STOPPED state
0346  *
0347  * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
0348  * Other signals when not blocked and set to SIG_DFL behaves as follows.
0349  * The job control signals also have other special effects.
0350  *
0351  *  +--------------------+------------------+
0352  *  |  POSIX signal      |  default action  |
0353  *  +--------------------+------------------+
0354  *  |  SIGHUP            |  terminate   |
0355  *  |  SIGINT            |  terminate   |
0356  *  |  SIGQUIT           |  coredump    |
0357  *  |  SIGILL            |  coredump    |
0358  *  |  SIGTRAP           |  coredump    |
0359  *  |  SIGABRT/SIGIOT    |  coredump    |
0360  *  |  SIGBUS            |  coredump    |
0361  *  |  SIGFPE            |  coredump    |
0362  *  |  SIGKILL           |  terminate(+)    |
0363  *  |  SIGUSR1           |  terminate   |
0364  *  |  SIGSEGV           |  coredump    |
0365  *  |  SIGUSR2           |  terminate   |
0366  *  |  SIGPIPE           |  terminate   |
0367  *  |  SIGALRM           |  terminate   |
0368  *  |  SIGTERM           |  terminate   |
0369  *  |  SIGCHLD           |  ignore      |
0370  *  |  SIGCONT           |  ignore(*)   |
0371  *  |  SIGSTOP           |  stop(*)(+)      |
0372  *  |  SIGTSTP           |  stop(*)     |
0373  *  |  SIGTTIN           |  stop(*)     |
0374  *  |  SIGTTOU           |  stop(*)     |
0375  *  |  SIGURG            |  ignore      |
0376  *  |  SIGXCPU           |  coredump    |
0377  *  |  SIGXFSZ           |  coredump    |
0378  *  |  SIGVTALRM         |  terminate   |
0379  *  |  SIGPROF           |  terminate   |
0380  *  |  SIGPOLL/SIGIO     |  terminate   |
0381  *  |  SIGSYS/SIGUNUSED  |  coredump    |
0382  *  |  SIGSTKFLT         |  terminate   |
0383  *  |  SIGWINCH          |  ignore      |
0384  *  |  SIGPWR            |  terminate   |
0385  *  |  SIGRTMIN-SIGRTMAX |  terminate       |
0386  *  +--------------------+------------------+
0387  *  |  non-POSIX signal  |  default action  |
0388  *  +--------------------+------------------+
0389  *  |  SIGEMT            |  coredump    |
0390  *  +--------------------+------------------+
0391  *
0392  * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
0393  * (*) Special job control effects:
0394  * When SIGCONT is sent, it resumes the process (all threads in the group)
0395  * from TASK_STOPPED state and also clears any pending/queued stop signals
0396  * (any of those marked with "stop(*)").  This happens regardless of blocking,
0397  * catching, or ignoring SIGCONT.  When any stop signal is sent, it clears
0398  * any pending/queued SIGCONT signals; this happens regardless of blocking,
0399  * catching, or ignored the stop signal, though (except for SIGSTOP) the
0400  * default action of stopping the process may happen later or never.
0401  */
0402 
0403 #ifdef SIGEMT
0404 #define SIGEMT_MASK rt_sigmask(SIGEMT)
0405 #else
0406 #define SIGEMT_MASK 0
0407 #endif
0408 
0409 #if SIGRTMIN > BITS_PER_LONG
0410 #define rt_sigmask(sig) (1ULL << ((sig)-1))
0411 #else
0412 #define rt_sigmask(sig) sigmask(sig)
0413 #endif
0414 
0415 #define siginmask(sig, mask) \
0416     ((sig) > 0 && (sig) < SIGRTMIN && (rt_sigmask(sig) & (mask)))
0417 
0418 #define SIG_KERNEL_ONLY_MASK (\
0419     rt_sigmask(SIGKILL)   |  rt_sigmask(SIGSTOP))
0420 
0421 #define SIG_KERNEL_STOP_MASK (\
0422     rt_sigmask(SIGSTOP)   |  rt_sigmask(SIGTSTP)   | \
0423     rt_sigmask(SIGTTIN)   |  rt_sigmask(SIGTTOU)   )
0424 
0425 #define SIG_KERNEL_COREDUMP_MASK (\
0426         rt_sigmask(SIGQUIT)   |  rt_sigmask(SIGILL)    | \
0427     rt_sigmask(SIGTRAP)   |  rt_sigmask(SIGABRT)   | \
0428         rt_sigmask(SIGFPE)    |  rt_sigmask(SIGSEGV)   | \
0429     rt_sigmask(SIGBUS)    |  rt_sigmask(SIGSYS)    | \
0430         rt_sigmask(SIGXCPU)   |  rt_sigmask(SIGXFSZ)   | \
0431     SIGEMT_MASK                    )
0432 
0433 #define SIG_KERNEL_IGNORE_MASK (\
0434         rt_sigmask(SIGCONT)   |  rt_sigmask(SIGCHLD)   | \
0435     rt_sigmask(SIGWINCH)  |  rt_sigmask(SIGURG)    )
0436 
0437 #define SIG_SPECIFIC_SICODES_MASK (\
0438     rt_sigmask(SIGILL)    |  rt_sigmask(SIGFPE)    | \
0439     rt_sigmask(SIGSEGV)   |  rt_sigmask(SIGBUS)    | \
0440     rt_sigmask(SIGTRAP)   |  rt_sigmask(SIGCHLD)   | \
0441     rt_sigmask(SIGPOLL)   |  rt_sigmask(SIGSYS)    | \
0442     SIGEMT_MASK                                    )
0443 
0444 #define sig_kernel_only(sig)        siginmask(sig, SIG_KERNEL_ONLY_MASK)
0445 #define sig_kernel_coredump(sig)    siginmask(sig, SIG_KERNEL_COREDUMP_MASK)
0446 #define sig_kernel_ignore(sig)      siginmask(sig, SIG_KERNEL_IGNORE_MASK)
0447 #define sig_kernel_stop(sig)        siginmask(sig, SIG_KERNEL_STOP_MASK)
0448 #define sig_specific_sicodes(sig)   siginmask(sig, SIG_SPECIFIC_SICODES_MASK)
0449 
0450 #define sig_fatal(t, signr) \
0451     (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
0452      (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
0453 
0454 void signals_init(void);
0455 
0456 int restore_altstack(const stack_t __user *);
0457 int __save_altstack(stack_t __user *, unsigned long);
0458 
0459 #define unsafe_save_altstack(uss, sp, label) do { \
0460     stack_t __user *__uss = uss; \
0461     struct task_struct *t = current; \
0462     unsafe_put_user((void __user *)t->sas_ss_sp, &__uss->ss_sp, label); \
0463     unsafe_put_user(t->sas_ss_flags, &__uss->ss_flags, label); \
0464     unsafe_put_user(t->sas_ss_size, &__uss->ss_size, label); \
0465 } while (0);
0466 
0467 #ifdef CONFIG_DYNAMIC_SIGFRAME
0468 bool sigaltstack_size_valid(size_t ss_size);
0469 #else
0470 static inline bool sigaltstack_size_valid(size_t size) { return true; }
0471 #endif /* !CONFIG_DYNAMIC_SIGFRAME */
0472 
0473 #ifdef CONFIG_PROC_FS
0474 struct seq_file;
0475 extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
0476 #endif
0477 
0478 #ifndef arch_untagged_si_addr
0479 /*
0480  * Given a fault address and a signal and si_code which correspond to the
0481  * _sigfault union member, returns the address that must appear in si_addr if
0482  * the signal handler does not have SA_EXPOSE_TAGBITS enabled in sa_flags.
0483  */
0484 static inline void __user *arch_untagged_si_addr(void __user *addr,
0485                          unsigned long sig,
0486                          unsigned long si_code)
0487 {
0488     return addr;
0489 }
0490 #endif
0491 
0492 #endif /* _LINUX_SIGNAL_H */