Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/signal.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson |
| 7 | * |
| 8 | * 2003-06-02 Jim Houston - Concurrent Computer Corp. |
| 9 | * Changes to use preallocated sigqueue structures |
| 10 | * to allow signals to be sent reliably. |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/tty.h> |
| 19 | #include <linux/binfmts.h> |
| 20 | #include <linux/security.h> |
| 21 | #include <linux/syscalls.h> |
| 22 | #include <linux/ptrace.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 23 | #include <linux/signal.h> |
Davide Libenzi | fba2afa | 2007-05-10 22:23:13 -0700 | [diff] [blame] | 24 | #include <linux/signalfd.h> |
Roland McGrath | 35de254 | 2008-07-25 19:45:51 -0700 | [diff] [blame] | 25 | #include <linux/tracehook.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 26 | #include <linux/capability.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 27 | #include <linux/freezer.h> |
Sukadev Bhattiprolu | 84d7378 | 2006-12-08 02:38:01 -0800 | [diff] [blame] | 28 | #include <linux/pid_namespace.h> |
| 29 | #include <linux/nsproxy.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 30 | #include <trace/events/sched.h> |
Sukadev Bhattiprolu | 84d7378 | 2006-12-08 02:38:01 -0800 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/param.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <asm/unistd.h> |
| 35 | #include <asm/siginfo.h> |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 36 | #include "audit.h" /* audit_signal_info() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * SLAB caches for signal bits. |
| 40 | */ |
| 41 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 42 | static struct kmem_cache *sigqueue_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Roland McGrath | 35de254 | 2008-07-25 19:45:51 -0700 | [diff] [blame] | 44 | static void __user *sig_handler(struct task_struct *t, int sig) |
Pavel Emelyanov | 93585ee | 2008-04-30 00:52:39 -0700 | [diff] [blame] | 45 | { |
Roland McGrath | 35de254 | 2008-07-25 19:45:51 -0700 | [diff] [blame] | 46 | return t->sighand->action[sig - 1].sa.sa_handler; |
| 47 | } |
Pavel Emelyanov | 93585ee | 2008-04-30 00:52:39 -0700 | [diff] [blame] | 48 | |
Roland McGrath | 35de254 | 2008-07-25 19:45:51 -0700 | [diff] [blame] | 49 | static int sig_handler_ignored(void __user *handler, int sig) |
| 50 | { |
Pavel Emelyanov | 93585ee | 2008-04-30 00:52:39 -0700 | [diff] [blame] | 51 | /* Is it explicitly or implicitly ignored? */ |
Pavel Emelyanov | 93585ee | 2008-04-30 00:52:39 -0700 | [diff] [blame] | 52 | return handler == SIG_IGN || |
| 53 | (handler == SIG_DFL && sig_kernel_ignore(sig)); |
| 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 56 | static int sig_task_ignored(struct task_struct *t, int sig, |
| 57 | int from_ancestor_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Roland McGrath | 35de254 | 2008-07-25 19:45:51 -0700 | [diff] [blame] | 59 | void __user *handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Oleg Nesterov | f008faf | 2009-04-02 16:58:02 -0700 | [diff] [blame] | 61 | handler = sig_handler(t, sig); |
| 62 | |
| 63 | if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) && |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 64 | handler == SIG_DFL && !from_ancestor_ns) |
Oleg Nesterov | f008faf | 2009-04-02 16:58:02 -0700 | [diff] [blame] | 65 | return 1; |
| 66 | |
| 67 | return sig_handler_ignored(handler, sig); |
| 68 | } |
| 69 | |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 70 | static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns) |
Oleg Nesterov | f008faf | 2009-04-02 16:58:02 -0700 | [diff] [blame] | 71 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Blocked signals are never ignored, since the |
| 74 | * signal handler may change by the time it is |
| 75 | * unblocked. |
| 76 | */ |
Roland McGrath | 325d22d | 2007-11-12 15:41:55 -0800 | [diff] [blame] | 77 | if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return 0; |
| 79 | |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 80 | if (!sig_task_ignored(t, sig, from_ancestor_ns)) |
Roland McGrath | 35de254 | 2008-07-25 19:45:51 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | |
| 83 | /* |
| 84 | * Tracers may want to know about even ignored signals. |
| 85 | */ |
Oleg Nesterov | 43918f2 | 2009-04-02 16:58:00 -0700 | [diff] [blame] | 86 | return !tracehook_consider_ignored_signal(t, sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Re-calculate pending state from the set of locally pending |
| 91 | * signals, globally pending signals, and blocked signals. |
| 92 | */ |
| 93 | static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked) |
| 94 | { |
| 95 | unsigned long ready; |
| 96 | long i; |
| 97 | |
| 98 | switch (_NSIG_WORDS) { |
| 99 | default: |
| 100 | for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;) |
| 101 | ready |= signal->sig[i] &~ blocked->sig[i]; |
| 102 | break; |
| 103 | |
| 104 | case 4: ready = signal->sig[3] &~ blocked->sig[3]; |
| 105 | ready |= signal->sig[2] &~ blocked->sig[2]; |
| 106 | ready |= signal->sig[1] &~ blocked->sig[1]; |
| 107 | ready |= signal->sig[0] &~ blocked->sig[0]; |
| 108 | break; |
| 109 | |
| 110 | case 2: ready = signal->sig[1] &~ blocked->sig[1]; |
| 111 | ready |= signal->sig[0] &~ blocked->sig[0]; |
| 112 | break; |
| 113 | |
| 114 | case 1: ready = signal->sig[0] &~ blocked->sig[0]; |
| 115 | } |
| 116 | return ready != 0; |
| 117 | } |
| 118 | |
| 119 | #define PENDING(p,b) has_pending_signals(&(p)->signal, (b)) |
| 120 | |
Roland McGrath | 7bb44ad | 2007-05-23 13:57:44 -0700 | [diff] [blame] | 121 | static int recalc_sigpending_tsk(struct task_struct *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | if (t->signal->group_stop_count > 0 || |
| 124 | PENDING(&t->pending, &t->blocked) || |
Roland McGrath | 7bb44ad | 2007-05-23 13:57:44 -0700 | [diff] [blame] | 125 | PENDING(&t->signal->shared_pending, &t->blocked)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | set_tsk_thread_flag(t, TIF_SIGPENDING); |
Roland McGrath | 7bb44ad | 2007-05-23 13:57:44 -0700 | [diff] [blame] | 127 | return 1; |
| 128 | } |
Roland McGrath | b74d0de | 2007-06-06 03:59:00 -0700 | [diff] [blame] | 129 | /* |
| 130 | * We must never clear the flag in another thread, or in current |
| 131 | * when it's possible the current syscall is returning -ERESTART*. |
| 132 | * So we don't clear it here, and only callers who know they should do. |
| 133 | */ |
Roland McGrath | 7bb44ad | 2007-05-23 13:57:44 -0700 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up. |
| 139 | * This is superfluous when called on current, the wakeup is a harmless no-op. |
| 140 | */ |
| 141 | void recalc_sigpending_and_wake(struct task_struct *t) |
| 142 | { |
| 143 | if (recalc_sigpending_tsk(t)) |
| 144 | signal_wake_up(t, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void recalc_sigpending(void) |
| 148 | { |
Roland McGrath | b787f7b | 2008-07-25 19:45:55 -0700 | [diff] [blame] | 149 | if (unlikely(tracehook_force_sigpending())) |
| 150 | set_thread_flag(TIF_SIGPENDING); |
| 151 | else if (!recalc_sigpending_tsk(current) && !freezing(current)) |
Roland McGrath | b74d0de | 2007-06-06 03:59:00 -0700 | [diff] [blame] | 152 | clear_thread_flag(TIF_SIGPENDING); |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* Given the mask, find the first available signal that should be serviced. */ |
| 157 | |
Davide Libenzi | fba2afa | 2007-05-10 22:23:13 -0700 | [diff] [blame] | 158 | int next_signal(struct sigpending *pending, sigset_t *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | unsigned long i, *s, *m, x; |
| 161 | int sig = 0; |
| 162 | |
| 163 | s = pending->signal.sig; |
| 164 | m = mask->sig; |
| 165 | switch (_NSIG_WORDS) { |
| 166 | default: |
| 167 | for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m) |
| 168 | if ((x = *s &~ *m) != 0) { |
| 169 | sig = ffz(~x) + i*_NSIG_BPW + 1; |
| 170 | break; |
| 171 | } |
| 172 | break; |
| 173 | |
| 174 | case 2: if ((x = s[0] &~ m[0]) != 0) |
| 175 | sig = 1; |
| 176 | else if ((x = s[1] &~ m[1]) != 0) |
| 177 | sig = _NSIG_BPW + 1; |
| 178 | else |
| 179 | break; |
| 180 | sig += ffz(~x); |
| 181 | break; |
| 182 | |
| 183 | case 1: if ((x = *s &~ *m) != 0) |
| 184 | sig = ffz(~x) + 1; |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | return sig; |
| 189 | } |
| 190 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 191 | /* |
| 192 | * allocate a new signal queue record |
| 193 | * - this may be called without locks if and only if t == current, otherwise an |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 194 | * appopriate lock must be held to stop the target task from exiting |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 195 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 196 | static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | int override_rlimit) |
| 198 | { |
| 199 | struct sigqueue *q = NULL; |
Linus Torvalds | 10b1fbd | 2006-11-04 13:03:00 -0800 | [diff] [blame] | 200 | struct user_struct *user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Linus Torvalds | 10b1fbd | 2006-11-04 13:03:00 -0800 | [diff] [blame] | 202 | /* |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 203 | * We won't get problems with the target's UID changing under us |
| 204 | * because changing it requires RCU be used, and if t != current, the |
| 205 | * caller must be holding the RCU readlock (by way of a spinlock) and |
| 206 | * we use RCU protection here |
Linus Torvalds | 10b1fbd | 2006-11-04 13:03:00 -0800 | [diff] [blame] | 207 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 208 | user = get_uid(__task_cred(t)->user); |
Linus Torvalds | 10b1fbd | 2006-11-04 13:03:00 -0800 | [diff] [blame] | 209 | atomic_inc(&user->sigpending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (override_rlimit || |
Linus Torvalds | 10b1fbd | 2006-11-04 13:03:00 -0800 | [diff] [blame] | 211 | atomic_read(&user->sigpending) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur) |
| 213 | q = kmem_cache_alloc(sigqueue_cachep, flags); |
| 214 | if (unlikely(q == NULL)) { |
Linus Torvalds | 10b1fbd | 2006-11-04 13:03:00 -0800 | [diff] [blame] | 215 | atomic_dec(&user->sigpending); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 216 | free_uid(user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } else { |
| 218 | INIT_LIST_HEAD(&q->list); |
| 219 | q->flags = 0; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 220 | q->user = user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 222 | |
| 223 | return q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Andrew Morton | 514a01b | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 226 | static void __sigqueue_free(struct sigqueue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
| 228 | if (q->flags & SIGQUEUE_PREALLOC) |
| 229 | return; |
| 230 | atomic_dec(&q->user->sigpending); |
| 231 | free_uid(q->user); |
| 232 | kmem_cache_free(sigqueue_cachep, q); |
| 233 | } |
| 234 | |
Oleg Nesterov | 6a14c5c | 2006-03-28 16:11:18 -0800 | [diff] [blame] | 235 | void flush_sigqueue(struct sigpending *queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 237 | struct sigqueue *q; |
| 238 | |
| 239 | sigemptyset(&queue->signal); |
| 240 | while (!list_empty(&queue->list)) { |
| 241 | q = list_entry(queue->list.next, struct sigqueue , list); |
| 242 | list_del_init(&q->list); |
| 243 | __sigqueue_free(q); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Flush all pending signals for a task. |
| 249 | */ |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 250 | void __flush_signals(struct task_struct *t) |
| 251 | { |
| 252 | clear_tsk_thread_flag(t, TIF_SIGPENDING); |
| 253 | flush_sigqueue(&t->pending); |
| 254 | flush_sigqueue(&t->signal->shared_pending); |
| 255 | } |
| 256 | |
Oleg Nesterov | c81addc | 2006-03-28 16:11:17 -0800 | [diff] [blame] | 257 | void flush_signals(struct task_struct *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | unsigned long flags; |
| 260 | |
| 261 | spin_lock_irqsave(&t->sighand->siglock, flags); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 262 | __flush_signals(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | spin_unlock_irqrestore(&t->sighand->siglock, flags); |
| 264 | } |
| 265 | |
Oleg Nesterov | cbaffba | 2008-05-26 20:55:42 +0400 | [diff] [blame] | 266 | static void __flush_itimer_signals(struct sigpending *pending) |
| 267 | { |
| 268 | sigset_t signal, retain; |
| 269 | struct sigqueue *q, *n; |
| 270 | |
| 271 | signal = pending->signal; |
| 272 | sigemptyset(&retain); |
| 273 | |
| 274 | list_for_each_entry_safe(q, n, &pending->list, list) { |
| 275 | int sig = q->info.si_signo; |
| 276 | |
| 277 | if (likely(q->info.si_code != SI_TIMER)) { |
| 278 | sigaddset(&retain, sig); |
| 279 | } else { |
| 280 | sigdelset(&signal, sig); |
| 281 | list_del_init(&q->list); |
| 282 | __sigqueue_free(q); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | sigorsets(&pending->signal, &signal, &retain); |
| 287 | } |
| 288 | |
| 289 | void flush_itimer_signals(void) |
| 290 | { |
| 291 | struct task_struct *tsk = current; |
| 292 | unsigned long flags; |
| 293 | |
| 294 | spin_lock_irqsave(&tsk->sighand->siglock, flags); |
| 295 | __flush_itimer_signals(&tsk->pending); |
| 296 | __flush_itimer_signals(&tsk->signal->shared_pending); |
| 297 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
| 298 | } |
| 299 | |
Oleg Nesterov | 10ab825 | 2007-05-09 02:34:37 -0700 | [diff] [blame] | 300 | void ignore_signals(struct task_struct *t) |
| 301 | { |
| 302 | int i; |
| 303 | |
| 304 | for (i = 0; i < _NSIG; ++i) |
| 305 | t->sighand->action[i].sa.sa_handler = SIG_IGN; |
| 306 | |
| 307 | flush_signals(t); |
| 308 | } |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | * Flush all handlers for a task. |
| 312 | */ |
| 313 | |
| 314 | void |
| 315 | flush_signal_handlers(struct task_struct *t, int force_default) |
| 316 | { |
| 317 | int i; |
| 318 | struct k_sigaction *ka = &t->sighand->action[0]; |
| 319 | for (i = _NSIG ; i != 0 ; i--) { |
| 320 | if (force_default || ka->sa.sa_handler != SIG_IGN) |
| 321 | ka->sa.sa_handler = SIG_DFL; |
| 322 | ka->sa.sa_flags = 0; |
| 323 | sigemptyset(&ka->sa.sa_mask); |
| 324 | ka++; |
| 325 | } |
| 326 | } |
| 327 | |
Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 328 | int unhandled_signal(struct task_struct *tsk, int sig) |
| 329 | { |
Roland McGrath | 445a91d | 2008-07-25 19:45:52 -0700 | [diff] [blame] | 330 | void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler; |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 331 | if (is_global_init(tsk)) |
Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 332 | return 1; |
Roland McGrath | 445a91d | 2008-07-25 19:45:52 -0700 | [diff] [blame] | 333 | if (handler != SIG_IGN && handler != SIG_DFL) |
Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 334 | return 0; |
Oleg Nesterov | 43918f2 | 2009-04-02 16:58:00 -0700 | [diff] [blame] | 335 | return !tracehook_consider_fatal_signal(tsk, sig); |
Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | /* Notify the system that a driver wants to block all signals for this |
| 340 | * process, and wants to be notified if any signals at all were to be |
| 341 | * sent/acted upon. If the notifier routine returns non-zero, then the |
| 342 | * signal will be acted upon after all. If the notifier routine returns 0, |
| 343 | * then then signal will be blocked. Only one block per process is |
| 344 | * allowed. priv is a pointer to private data that the notifier routine |
| 345 | * can use to determine if the signal should be blocked or not. */ |
| 346 | |
| 347 | void |
| 348 | block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask) |
| 349 | { |
| 350 | unsigned long flags; |
| 351 | |
| 352 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 353 | current->notifier_mask = mask; |
| 354 | current->notifier_data = priv; |
| 355 | current->notifier = notifier; |
| 356 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 357 | } |
| 358 | |
| 359 | /* Notify the system that blocking has ended. */ |
| 360 | |
| 361 | void |
| 362 | unblock_all_signals(void) |
| 363 | { |
| 364 | unsigned long flags; |
| 365 | |
| 366 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 367 | current->notifier = NULL; |
| 368 | current->notifier_data = NULL; |
| 369 | recalc_sigpending(); |
| 370 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 371 | } |
| 372 | |
Oleg Nesterov | 100360f | 2008-07-25 01:47:29 -0700 | [diff] [blame] | 373 | static void collect_signal(int sig, struct sigpending *list, siginfo_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | struct sigqueue *q, *first = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* |
| 378 | * Collect the siginfo appropriate to this signal. Check if |
| 379 | * there is another siginfo for the same signal. |
| 380 | */ |
| 381 | list_for_each_entry(q, &list->list, list) { |
| 382 | if (q->info.si_signo == sig) { |
Oleg Nesterov | d443420 | 2008-07-25 01:47:28 -0700 | [diff] [blame] | 383 | if (first) |
| 384 | goto still_pending; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | first = q; |
| 386 | } |
| 387 | } |
Oleg Nesterov | d443420 | 2008-07-25 01:47:28 -0700 | [diff] [blame] | 388 | |
| 389 | sigdelset(&list->signal, sig); |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (first) { |
Oleg Nesterov | d443420 | 2008-07-25 01:47:28 -0700 | [diff] [blame] | 392 | still_pending: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | list_del_init(&first->list); |
| 394 | copy_siginfo(info, &first->info); |
| 395 | __sigqueue_free(first); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | /* Ok, it wasn't in the queue. This must be |
| 398 | a fast-pathed signal or we must have been |
| 399 | out of queue space. So zero out the info. |
| 400 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | info->si_signo = sig; |
| 402 | info->si_errno = 0; |
| 403 | info->si_code = 0; |
| 404 | info->si_pid = 0; |
| 405 | info->si_uid = 0; |
| 406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int __dequeue_signal(struct sigpending *pending, sigset_t *mask, |
| 410 | siginfo_t *info) |
| 411 | { |
Roland McGrath | 27d91e0 | 2006-09-29 02:00:31 -0700 | [diff] [blame] | 412 | int sig = next_signal(pending, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (sig) { |
| 415 | if (current->notifier) { |
| 416 | if (sigismember(current->notifier_mask, sig)) { |
| 417 | if (!(current->notifier)(current->notifier_data)) { |
| 418 | clear_thread_flag(TIF_SIGPENDING); |
| 419 | return 0; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
Oleg Nesterov | 100360f | 2008-07-25 01:47:29 -0700 | [diff] [blame] | 424 | collect_signal(sig, pending, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | return sig; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Dequeue a signal and return the element to the caller, which is |
| 432 | * expected to free it. |
| 433 | * |
| 434 | * All callers have to hold the siglock. |
| 435 | */ |
| 436 | int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info) |
| 437 | { |
Pavel Emelyanov | c5363d0 | 2008-04-30 00:52:40 -0700 | [diff] [blame] | 438 | int signr; |
Benjamin Herrenschmidt | caec4e8 | 2007-06-12 08:16:18 +1000 | [diff] [blame] | 439 | |
| 440 | /* We only dequeue private signals from ourselves, we don't let |
| 441 | * signalfd steal them |
| 442 | */ |
Davide Libenzi | b8fceee | 2007-09-20 12:40:16 -0700 | [diff] [blame] | 443 | signr = __dequeue_signal(&tsk->pending, mask, info); |
Thomas Gleixner | 8bfd9a7 | 2007-02-16 01:28:12 -0800 | [diff] [blame] | 444 | if (!signr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | signr = __dequeue_signal(&tsk->signal->shared_pending, |
| 446 | mask, info); |
Thomas Gleixner | 8bfd9a7 | 2007-02-16 01:28:12 -0800 | [diff] [blame] | 447 | /* |
| 448 | * itimer signal ? |
| 449 | * |
| 450 | * itimers are process shared and we restart periodic |
| 451 | * itimers in the signal delivery path to prevent DoS |
| 452 | * attacks in the high resolution timer case. This is |
| 453 | * compliant with the old way of self restarting |
| 454 | * itimers, as the SIGALRM is a legacy signal and only |
| 455 | * queued once. Changing the restart behaviour to |
| 456 | * restart the timer in the signal dequeue path is |
| 457 | * reducing the timer noise on heavy loaded !highres |
| 458 | * systems too. |
| 459 | */ |
| 460 | if (unlikely(signr == SIGALRM)) { |
| 461 | struct hrtimer *tmr = &tsk->signal->real_timer; |
| 462 | |
| 463 | if (!hrtimer_is_queued(tmr) && |
| 464 | tsk->signal->it_real_incr.tv64 != 0) { |
| 465 | hrtimer_forward(tmr, tmr->base->get_time(), |
| 466 | tsk->signal->it_real_incr); |
| 467 | hrtimer_restart(tmr); |
| 468 | } |
| 469 | } |
| 470 | } |
Pavel Emelyanov | c5363d0 | 2008-04-30 00:52:40 -0700 | [diff] [blame] | 471 | |
Davide Libenzi | b8fceee | 2007-09-20 12:40:16 -0700 | [diff] [blame] | 472 | recalc_sigpending(); |
Pavel Emelyanov | c5363d0 | 2008-04-30 00:52:40 -0700 | [diff] [blame] | 473 | if (!signr) |
| 474 | return 0; |
| 475 | |
| 476 | if (unlikely(sig_kernel_stop(signr))) { |
Thomas Gleixner | 8bfd9a7 | 2007-02-16 01:28:12 -0800 | [diff] [blame] | 477 | /* |
| 478 | * Set a marker that we have dequeued a stop signal. Our |
| 479 | * caller might release the siglock and then the pending |
| 480 | * stop signal it is about to process is no longer in the |
| 481 | * pending bitmasks, but must still be cleared by a SIGCONT |
| 482 | * (and overruled by a SIGKILL). So those cases clear this |
| 483 | * shared flag after we've set it. Note that this flag may |
| 484 | * remain set after the signal we return is ignored or |
| 485 | * handled. That doesn't matter because its only purpose |
| 486 | * is to alert stop-signal processing code when another |
| 487 | * processor has come along and cleared the flag. |
| 488 | */ |
Oleg Nesterov | 92413d7 | 2008-07-25 01:47:30 -0700 | [diff] [blame] | 489 | tsk->signal->flags |= SIGNAL_STOP_DEQUEUED; |
Thomas Gleixner | 8bfd9a7 | 2007-02-16 01:28:12 -0800 | [diff] [blame] | 490 | } |
Pavel Emelyanov | c5363d0 | 2008-04-30 00:52:40 -0700 | [diff] [blame] | 491 | if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* |
| 493 | * Release the siglock to ensure proper locking order |
| 494 | * of timer locks outside of siglocks. Note, we leave |
| 495 | * irqs disabled here, since the posix-timers code is |
| 496 | * about to disable them again anyway. |
| 497 | */ |
| 498 | spin_unlock(&tsk->sighand->siglock); |
| 499 | do_schedule_next_timer(info); |
| 500 | spin_lock(&tsk->sighand->siglock); |
| 501 | } |
| 502 | return signr; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Tell a process that it has a new active signal.. |
| 507 | * |
| 508 | * NOTE! we rely on the previous spin_lock to |
| 509 | * lock interrupts for us! We can only be called with |
| 510 | * "siglock" held, and the local interrupt must |
| 511 | * have been disabled when that got acquired! |
| 512 | * |
| 513 | * No need to set need_resched since signal event passing |
| 514 | * goes through ->blocked |
| 515 | */ |
| 516 | void signal_wake_up(struct task_struct *t, int resume) |
| 517 | { |
| 518 | unsigned int mask; |
| 519 | |
| 520 | set_tsk_thread_flag(t, TIF_SIGPENDING); |
| 521 | |
| 522 | /* |
Matthew Wilcox | f021a3c | 2007-12-06 11:13:16 -0500 | [diff] [blame] | 523 | * For SIGKILL, we want to wake it up in the stopped/traced/killable |
| 524 | * case. We don't check t->state here because there is a race with it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | * executing another processor and just now entering stopped state. |
| 526 | * By using wake_up_state, we ensure the process will wake up and |
| 527 | * handle its death signal. |
| 528 | */ |
| 529 | mask = TASK_INTERRUPTIBLE; |
| 530 | if (resume) |
Matthew Wilcox | f021a3c | 2007-12-06 11:13:16 -0500 | [diff] [blame] | 531 | mask |= TASK_WAKEKILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (!wake_up_state(t, mask)) |
| 533 | kick_process(t); |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Remove signals in mask from the pending set and queue. |
| 538 | * Returns 1 if any signals were found. |
| 539 | * |
| 540 | * All callers must be holding the siglock. |
George Anzinger | 71fabd5 | 2006-01-08 01:02:48 -0800 | [diff] [blame] | 541 | * |
| 542 | * This version takes a sigset mask and looks at all signals, |
| 543 | * not just those in the first mask word. |
| 544 | */ |
| 545 | static int rm_from_queue_full(sigset_t *mask, struct sigpending *s) |
| 546 | { |
| 547 | struct sigqueue *q, *n; |
| 548 | sigset_t m; |
| 549 | |
| 550 | sigandsets(&m, mask, &s->signal); |
| 551 | if (sigisemptyset(&m)) |
| 552 | return 0; |
| 553 | |
| 554 | signandsets(&s->signal, &s->signal, mask); |
| 555 | list_for_each_entry_safe(q, n, &s->list, list) { |
| 556 | if (sigismember(mask, q->info.si_signo)) { |
| 557 | list_del_init(&q->list); |
| 558 | __sigqueue_free(q); |
| 559 | } |
| 560 | } |
| 561 | return 1; |
| 562 | } |
| 563 | /* |
| 564 | * Remove signals in mask from the pending set and queue. |
| 565 | * Returns 1 if any signals were found. |
| 566 | * |
| 567 | * All callers must be holding the siglock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | */ |
| 569 | static int rm_from_queue(unsigned long mask, struct sigpending *s) |
| 570 | { |
| 571 | struct sigqueue *q, *n; |
| 572 | |
| 573 | if (!sigtestsetmask(&s->signal, mask)) |
| 574 | return 0; |
| 575 | |
| 576 | sigdelsetmask(&s->signal, mask); |
| 577 | list_for_each_entry_safe(q, n, &s->list, list) { |
| 578 | if (q->info.si_signo < SIGRTMIN && |
| 579 | (mask & sigmask(q->info.si_signo))) { |
| 580 | list_del_init(&q->list); |
| 581 | __sigqueue_free(q); |
| 582 | } |
| 583 | } |
| 584 | return 1; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Bad permissions for sending the signal |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 589 | * - the caller must hold at least the RCU read lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | */ |
| 591 | static int check_kill_permission(int sig, struct siginfo *info, |
| 592 | struct task_struct *t) |
| 593 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 594 | const struct cred *cred = current_cred(), *tcred; |
Oleg Nesterov | 2e2ba22 | 2008-04-30 00:53:01 -0700 | [diff] [blame] | 595 | struct pid *sid; |
Oleg Nesterov | 3b5e9e5 | 2008-04-30 00:52:42 -0700 | [diff] [blame] | 596 | int error; |
| 597 | |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 598 | if (!valid_signal(sig)) |
Oleg Nesterov | 3b5e9e5 | 2008-04-30 00:52:42 -0700 | [diff] [blame] | 599 | return -EINVAL; |
| 600 | |
| 601 | if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) |
| 602 | return 0; |
| 603 | |
| 604 | error = audit_signal_info(sig, t); /* Let audit system see the signal */ |
| 605 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | return error; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 607 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 608 | tcred = __task_cred(t); |
| 609 | if ((cred->euid ^ tcred->suid) && |
| 610 | (cred->euid ^ tcred->uid) && |
| 611 | (cred->uid ^ tcred->suid) && |
| 612 | (cred->uid ^ tcred->uid) && |
Oleg Nesterov | 2e2ba22 | 2008-04-30 00:53:01 -0700 | [diff] [blame] | 613 | !capable(CAP_KILL)) { |
| 614 | switch (sig) { |
| 615 | case SIGCONT: |
Oleg Nesterov | 2e2ba22 | 2008-04-30 00:53:01 -0700 | [diff] [blame] | 616 | sid = task_session(t); |
Oleg Nesterov | 2e2ba22 | 2008-04-30 00:53:01 -0700 | [diff] [blame] | 617 | /* |
| 618 | * We don't return the error if sid == NULL. The |
| 619 | * task was unhashed, the caller must notice this. |
| 620 | */ |
| 621 | if (!sid || sid == task_session(current)) |
| 622 | break; |
| 623 | default: |
| 624 | return -EPERM; |
| 625 | } |
| 626 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 627 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 628 | return security_task_kill(t, info, sig, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | /* |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 632 | * Handle magic process-wide effects of stop/continue signals. Unlike |
| 633 | * the signal actions, these happen immediately at signal-generation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | * time regardless of blocking, ignoring, or handling. This does the |
| 635 | * actual continuing for SIGCONT, but not the actual stopping for stop |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 636 | * signals. The process stop is done as a signal action for SIG_DFL. |
| 637 | * |
| 638 | * Returns true if the signal should be actually delivered, otherwise |
| 639 | * it should be dropped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | */ |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 641 | static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 643 | struct signal_struct *signal = p->signal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | struct task_struct *t; |
| 645 | |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 646 | if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | /* |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 648 | * The process is in the middle of dying, nothing to do. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | */ |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 650 | } else if (sig_kernel_stop(sig)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* |
| 652 | * This is a stop signal. Remove SIGCONT from all queues. |
| 653 | */ |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 654 | rm_from_queue(sigmask(SIGCONT), &signal->shared_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | t = p; |
| 656 | do { |
| 657 | rm_from_queue(sigmask(SIGCONT), &t->pending); |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 658 | } while_each_thread(p, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } else if (sig == SIGCONT) { |
Oleg Nesterov | fc321d2 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 660 | unsigned int why; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | /* |
| 662 | * Remove all stop signals from all queues, |
| 663 | * and wake all threads. |
| 664 | */ |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 665 | rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | t = p; |
| 667 | do { |
| 668 | unsigned int state; |
| 669 | rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | /* |
| 671 | * If there is a handler for SIGCONT, we must make |
| 672 | * sure that no thread returns to user mode before |
| 673 | * we post the signal, in case it was the only |
| 674 | * thread eligible to run the signal handler--then |
| 675 | * it must not do anything between resuming and |
| 676 | * running the handler. With the TIF_SIGPENDING |
| 677 | * flag set, the thread will pause and acquire the |
| 678 | * siglock that we hold now and until we've queued |
Oleg Nesterov | fc321d2 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 679 | * the pending signal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | * |
| 681 | * Wake up the stopped thread _after_ setting |
| 682 | * TIF_SIGPENDING |
| 683 | */ |
Matthew Wilcox | f021a3c | 2007-12-06 11:13:16 -0500 | [diff] [blame] | 684 | state = __TASK_STOPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) { |
| 686 | set_tsk_thread_flag(t, TIF_SIGPENDING); |
| 687 | state |= TASK_INTERRUPTIBLE; |
| 688 | } |
| 689 | wake_up_state(t, state); |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 690 | } while_each_thread(p, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
Oleg Nesterov | fc321d2 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 692 | /* |
| 693 | * Notify the parent with CLD_CONTINUED if we were stopped. |
| 694 | * |
| 695 | * If we were in the middle of a group stop, we pretend it |
| 696 | * was already finished, and then continued. Since SIGCHLD |
| 697 | * doesn't queue we report only CLD_STOPPED, as if the next |
| 698 | * CLD_CONTINUED was dropped. |
| 699 | */ |
| 700 | why = 0; |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 701 | if (signal->flags & SIGNAL_STOP_STOPPED) |
Oleg Nesterov | fc321d2 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 702 | why |= SIGNAL_CLD_CONTINUED; |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 703 | else if (signal->group_stop_count) |
Oleg Nesterov | fc321d2 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 704 | why |= SIGNAL_CLD_STOPPED; |
| 705 | |
| 706 | if (why) { |
Oleg Nesterov | 021e1ae | 2008-04-30 00:53:00 -0700 | [diff] [blame] | 707 | /* |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 708 | * The first thread which returns from do_signal_stop() |
Oleg Nesterov | 021e1ae | 2008-04-30 00:53:00 -0700 | [diff] [blame] | 709 | * will take ->siglock, notice SIGNAL_CLD_MASK, and |
| 710 | * notify its parent. See get_signal_to_deliver(). |
| 711 | */ |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 712 | signal->flags = why | SIGNAL_STOP_CONTINUED; |
| 713 | signal->group_stop_count = 0; |
| 714 | signal->group_exit_code = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } else { |
| 716 | /* |
| 717 | * We are not stopped, but there could be a stop |
| 718 | * signal in the middle of being processed after |
| 719 | * being removed from the queue. Clear that too. |
| 720 | */ |
Oleg Nesterov | ad16a460 | 2008-04-30 00:52:46 -0700 | [diff] [blame] | 721 | signal->flags &= ~SIGNAL_STOP_DEQUEUED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 724 | |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 725 | return !sig_ignored(p, sig, from_ancestor_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Oleg Nesterov | 71f11dc | 2008-04-30 00:52:53 -0700 | [diff] [blame] | 728 | /* |
| 729 | * Test if P wants to take SIG. After we've checked all threads with this, |
| 730 | * it's equivalent to finding no threads not blocking SIG. Any threads not |
| 731 | * blocking SIG were ruled out because they are not running and already |
| 732 | * have pending signals. Such threads will dequeue from the shared queue |
| 733 | * as soon as they're available, so putting the signal on the shared queue |
| 734 | * will be equivalent to sending it to one such thread. |
| 735 | */ |
| 736 | static inline int wants_signal(int sig, struct task_struct *p) |
| 737 | { |
| 738 | if (sigismember(&p->blocked, sig)) |
| 739 | return 0; |
| 740 | if (p->flags & PF_EXITING) |
| 741 | return 0; |
| 742 | if (sig == SIGKILL) |
| 743 | return 1; |
| 744 | if (task_is_stopped_or_traced(p)) |
| 745 | return 0; |
| 746 | return task_curr(p) || !signal_pending(p); |
| 747 | } |
| 748 | |
Oleg Nesterov | 5fcd835 | 2008-04-30 00:52:55 -0700 | [diff] [blame] | 749 | static void complete_signal(int sig, struct task_struct *p, int group) |
Oleg Nesterov | 71f11dc | 2008-04-30 00:52:53 -0700 | [diff] [blame] | 750 | { |
| 751 | struct signal_struct *signal = p->signal; |
| 752 | struct task_struct *t; |
| 753 | |
| 754 | /* |
| 755 | * Now find a thread we can wake up to take the signal off the queue. |
| 756 | * |
| 757 | * If the main thread wants the signal, it gets first crack. |
| 758 | * Probably the least surprising to the average bear. |
| 759 | */ |
| 760 | if (wants_signal(sig, p)) |
| 761 | t = p; |
Oleg Nesterov | 5fcd835 | 2008-04-30 00:52:55 -0700 | [diff] [blame] | 762 | else if (!group || thread_group_empty(p)) |
Oleg Nesterov | 71f11dc | 2008-04-30 00:52:53 -0700 | [diff] [blame] | 763 | /* |
| 764 | * There is just one thread and it does not need to be woken. |
| 765 | * It will dequeue unblocked signals before it runs again. |
| 766 | */ |
| 767 | return; |
| 768 | else { |
| 769 | /* |
| 770 | * Otherwise try to find a suitable thread. |
| 771 | */ |
| 772 | t = signal->curr_target; |
| 773 | while (!wants_signal(sig, t)) { |
| 774 | t = next_thread(t); |
| 775 | if (t == signal->curr_target) |
| 776 | /* |
| 777 | * No thread needs to be woken. |
| 778 | * Any eligible threads will see |
| 779 | * the signal in the queue soon. |
| 780 | */ |
| 781 | return; |
| 782 | } |
| 783 | signal->curr_target = t; |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | * Found a killable thread. If the signal will be fatal, |
| 788 | * then start taking the whole group down immediately. |
| 789 | */ |
Oleg Nesterov | fae5fa4 | 2008-04-30 00:53:03 -0700 | [diff] [blame] | 790 | if (sig_fatal(p, sig) && |
| 791 | !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) && |
Oleg Nesterov | 71f11dc | 2008-04-30 00:52:53 -0700 | [diff] [blame] | 792 | !sigismember(&t->real_blocked, sig) && |
Roland McGrath | 445a91d | 2008-07-25 19:45:52 -0700 | [diff] [blame] | 793 | (sig == SIGKILL || |
Oleg Nesterov | 43918f2 | 2009-04-02 16:58:00 -0700 | [diff] [blame] | 794 | !tracehook_consider_fatal_signal(t, sig))) { |
Oleg Nesterov | 71f11dc | 2008-04-30 00:52:53 -0700 | [diff] [blame] | 795 | /* |
| 796 | * This signal will be fatal to the whole group. |
| 797 | */ |
| 798 | if (!sig_kernel_coredump(sig)) { |
| 799 | /* |
| 800 | * Start a group exit and wake everybody up. |
| 801 | * This way we don't have other threads |
| 802 | * running and doing things after a slower |
| 803 | * thread has the fatal signal pending. |
| 804 | */ |
| 805 | signal->flags = SIGNAL_GROUP_EXIT; |
| 806 | signal->group_exit_code = sig; |
| 807 | signal->group_stop_count = 0; |
| 808 | t = p; |
| 809 | do { |
| 810 | sigaddset(&t->pending.signal, SIGKILL); |
| 811 | signal_wake_up(t, 1); |
| 812 | } while_each_thread(p, t); |
| 813 | return; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * The signal is already in the shared-pending queue. |
| 819 | * Tell the chosen thread to wake up and dequeue it. |
| 820 | */ |
| 821 | signal_wake_up(t, sig == SIGKILL); |
| 822 | return; |
| 823 | } |
| 824 | |
Pavel Emelyanov | af7fff9 | 2008-04-30 00:52:34 -0700 | [diff] [blame] | 825 | static inline int legacy_queue(struct sigpending *signals, int sig) |
| 826 | { |
| 827 | return (sig < SIGRTMIN) && sigismember(&signals->signal, sig); |
| 828 | } |
| 829 | |
Sukadev Bhattiprolu | 7978b56 | 2009-04-02 16:58:04 -0700 | [diff] [blame] | 830 | static int __send_signal(int sig, struct siginfo *info, struct task_struct *t, |
| 831 | int group, int from_ancestor_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
Oleg Nesterov | 2ca3515 | 2008-04-30 00:52:54 -0700 | [diff] [blame] | 833 | struct sigpending *pending; |
Oleg Nesterov | 6e65acb | 2008-04-30 00:52:50 -0700 | [diff] [blame] | 834 | struct sigqueue *q; |
Vegard Nossum | 7a0aeb1 | 2009-05-16 11:28:33 +0200 | [diff] [blame] | 835 | int override_rlimit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Mathieu Desnoyers | 0a16b60 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 837 | trace_sched_signal_send(sig, t); |
| 838 | |
Oleg Nesterov | 6e65acb | 2008-04-30 00:52:50 -0700 | [diff] [blame] | 839 | assert_spin_locked(&t->sighand->siglock); |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 840 | |
| 841 | if (!prepare_signal(sig, t, from_ancestor_ns)) |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 842 | return 0; |
Oleg Nesterov | 2ca3515 | 2008-04-30 00:52:54 -0700 | [diff] [blame] | 843 | |
| 844 | pending = group ? &t->signal->shared_pending : &t->pending; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | /* |
Pavel Emelyanov | 2acb024 | 2008-04-30 00:52:35 -0700 | [diff] [blame] | 846 | * Short-circuit ignored signals and support queuing |
| 847 | * exactly one non-rt signal, so that we can get more |
| 848 | * detailed information about the cause of the signal. |
| 849 | */ |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 850 | if (legacy_queue(pending, sig)) |
Pavel Emelyanov | 2acb024 | 2008-04-30 00:52:35 -0700 | [diff] [blame] | 851 | return 0; |
Davide Libenzi | fba2afa | 2007-05-10 22:23:13 -0700 | [diff] [blame] | 852 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | * fast-pathed signals for kernel-internal things like SIGSTOP |
| 854 | * or SIGKILL. |
| 855 | */ |
Oleg Nesterov | b67a1b9 | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 856 | if (info == SEND_SIG_FORCED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | goto out_set; |
| 858 | |
| 859 | /* Real-time signals must be queued if sent by sigqueue, or |
| 860 | some other real-time mechanism. It is implementation |
| 861 | defined whether kill() does so. We attempt to do so, on |
| 862 | the principle of least surprise, but since kill is not |
| 863 | allowed to fail with EAGAIN when low on memory we just |
| 864 | make sure at least one signal gets delivered and don't |
| 865 | pass on the info struct. */ |
| 866 | |
Vegard Nossum | 7a0aeb1 | 2009-05-16 11:28:33 +0200 | [diff] [blame] | 867 | if (sig < SIGRTMIN) |
| 868 | override_rlimit = (is_si_special(info) || info->si_code >= 0); |
| 869 | else |
| 870 | override_rlimit = 0; |
| 871 | |
| 872 | q = __sigqueue_alloc(t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE, |
| 873 | override_rlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | if (q) { |
Oleg Nesterov | 2ca3515 | 2008-04-30 00:52:54 -0700 | [diff] [blame] | 875 | list_add_tail(&q->list, &pending->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | switch ((unsigned long) info) { |
Oleg Nesterov | b67a1b9 | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 877 | case (unsigned long) SEND_SIG_NOINFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | q->info.si_signo = sig; |
| 879 | q->info.si_errno = 0; |
| 880 | q->info.si_code = SI_USER; |
Sukadev Bhattiprolu | 9cd4fd1 | 2009-01-06 14:42:46 -0800 | [diff] [blame] | 881 | q->info.si_pid = task_tgid_nr_ns(current, |
Sukadev Bhattiprolu | 09bca05 | 2009-01-06 14:42:45 -0800 | [diff] [blame] | 882 | task_active_pid_ns(t)); |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 883 | q->info.si_uid = current_uid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | break; |
Oleg Nesterov | b67a1b9 | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 885 | case (unsigned long) SEND_SIG_PRIV: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | q->info.si_signo = sig; |
| 887 | q->info.si_errno = 0; |
| 888 | q->info.si_code = SI_KERNEL; |
| 889 | q->info.si_pid = 0; |
| 890 | q->info.si_uid = 0; |
| 891 | break; |
| 892 | default: |
| 893 | copy_siginfo(&q->info, info); |
Sukadev Bhattiprolu | 6588c1e | 2009-04-02 16:58:09 -0700 | [diff] [blame] | 894 | if (from_ancestor_ns) |
| 895 | q->info.si_pid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | break; |
| 897 | } |
Oleg Nesterov | 621d312 | 2005-10-30 15:03:45 -0800 | [diff] [blame] | 898 | } else if (!is_si_special(info)) { |
| 899 | if (sig >= SIGRTMIN && info->si_code != SI_USER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | /* |
| 901 | * Queue overflow, abort. We may abort if the signal was rt |
| 902 | * and sent by user using something other than kill(). |
| 903 | */ |
| 904 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | out_set: |
Oleg Nesterov | 53c3033 | 2008-04-30 00:53:00 -0700 | [diff] [blame] | 908 | signalfd_notify(t, sig); |
Oleg Nesterov | 2ca3515 | 2008-04-30 00:52:54 -0700 | [diff] [blame] | 909 | sigaddset(&pending->signal, sig); |
Pavel Emelyanov | 4cd4b6d | 2008-04-30 00:52:55 -0700 | [diff] [blame] | 910 | complete_signal(sig, t, group); |
| 911 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Sukadev Bhattiprolu | 7978b56 | 2009-04-02 16:58:04 -0700 | [diff] [blame] | 914 | static int send_signal(int sig, struct siginfo *info, struct task_struct *t, |
| 915 | int group) |
| 916 | { |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 917 | int from_ancestor_ns = 0; |
| 918 | |
| 919 | #ifdef CONFIG_PID_NS |
| 920 | if (!is_si_special(info) && SI_FROMUSER(info) && |
| 921 | task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0) |
| 922 | from_ancestor_ns = 1; |
| 923 | #endif |
| 924 | |
| 925 | return __send_signal(sig, info, t, group, from_ancestor_ns); |
Sukadev Bhattiprolu | 7978b56 | 2009-04-02 16:58:04 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Ingo Molnar | 45807a1 | 2007-07-15 23:40:10 -0700 | [diff] [blame] | 928 | int print_fatal_signals; |
| 929 | |
| 930 | static void print_fatal_signal(struct pt_regs *regs, int signr) |
| 931 | { |
| 932 | printk("%s/%d: potentially unexpected fatal signal %d.\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 933 | current->comm, task_pid_nr(current), signr); |
Ingo Molnar | 45807a1 | 2007-07-15 23:40:10 -0700 | [diff] [blame] | 934 | |
Al Viro | ca5cd87 | 2007-10-29 04:31:16 +0000 | [diff] [blame] | 935 | #if defined(__i386__) && !defined(__arch_um__) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 936 | printk("code at %08lx: ", regs->ip); |
Ingo Molnar | 45807a1 | 2007-07-15 23:40:10 -0700 | [diff] [blame] | 937 | { |
| 938 | int i; |
| 939 | for (i = 0; i < 16; i++) { |
| 940 | unsigned char insn; |
| 941 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 942 | __get_user(insn, (unsigned char *)(regs->ip + i)); |
Ingo Molnar | 45807a1 | 2007-07-15 23:40:10 -0700 | [diff] [blame] | 943 | printk("%02x ", insn); |
| 944 | } |
| 945 | } |
| 946 | #endif |
| 947 | printk("\n"); |
Ed Swierk | 3a9f84d | 2009-01-26 15:33:31 -0800 | [diff] [blame] | 948 | preempt_disable(); |
Ingo Molnar | 45807a1 | 2007-07-15 23:40:10 -0700 | [diff] [blame] | 949 | show_regs(regs); |
Ed Swierk | 3a9f84d | 2009-01-26 15:33:31 -0800 | [diff] [blame] | 950 | preempt_enable(); |
Ingo Molnar | 45807a1 | 2007-07-15 23:40:10 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static int __init setup_print_fatal_signals(char *str) |
| 954 | { |
| 955 | get_option (&str, &print_fatal_signals); |
| 956 | |
| 957 | return 1; |
| 958 | } |
| 959 | |
| 960 | __setup("print-fatal-signals=", setup_print_fatal_signals); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Pavel Emelyanov | 4cd4b6d | 2008-04-30 00:52:55 -0700 | [diff] [blame] | 962 | int |
| 963 | __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p) |
| 964 | { |
| 965 | return send_signal(sig, info, p, 1); |
| 966 | } |
| 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | static int |
| 969 | specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t) |
| 970 | { |
Pavel Emelyanov | 4cd4b6d | 2008-04-30 00:52:55 -0700 | [diff] [blame] | 971 | return send_signal(sig, info, t, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Oleg Nesterov | 4a30deb | 2009-09-23 15:57:00 -0700 | [diff] [blame^] | 974 | int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p, |
| 975 | bool group) |
| 976 | { |
| 977 | unsigned long flags; |
| 978 | int ret = -ESRCH; |
| 979 | |
| 980 | if (lock_task_sighand(p, &flags)) { |
| 981 | ret = send_signal(sig, info, p, group); |
| 982 | unlock_task_sighand(p, &flags); |
| 983 | } |
| 984 | |
| 985 | return ret; |
| 986 | } |
| 987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | /* |
| 989 | * Force a signal that the process can't ignore: if necessary |
| 990 | * we unblock the signal and change any SIG_IGN to SIG_DFL. |
Linus Torvalds | ae74c3b | 2006-08-02 20:17:49 -0700 | [diff] [blame] | 991 | * |
| 992 | * Note: If we unblock the signal, we always reset it to SIG_DFL, |
| 993 | * since we do not want to have a signal handler that was blocked |
| 994 | * be invoked when user space had explicitly blocked it. |
| 995 | * |
Oleg Nesterov | 80fe728 | 2008-04-30 00:53:05 -0700 | [diff] [blame] | 996 | * We don't want to have recursive SIGSEGV's etc, for example, |
| 997 | * that is why we also clear SIGNAL_UNKILLABLE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | int |
| 1000 | force_sig_info(int sig, struct siginfo *info, struct task_struct *t) |
| 1001 | { |
| 1002 | unsigned long int flags; |
Linus Torvalds | ae74c3b | 2006-08-02 20:17:49 -0700 | [diff] [blame] | 1003 | int ret, blocked, ignored; |
| 1004 | struct k_sigaction *action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
| 1006 | spin_lock_irqsave(&t->sighand->siglock, flags); |
Linus Torvalds | ae74c3b | 2006-08-02 20:17:49 -0700 | [diff] [blame] | 1007 | action = &t->sighand->action[sig-1]; |
| 1008 | ignored = action->sa.sa_handler == SIG_IGN; |
| 1009 | blocked = sigismember(&t->blocked, sig); |
| 1010 | if (blocked || ignored) { |
| 1011 | action->sa.sa_handler = SIG_DFL; |
| 1012 | if (blocked) { |
| 1013 | sigdelset(&t->blocked, sig); |
Roland McGrath | 7bb44ad | 2007-05-23 13:57:44 -0700 | [diff] [blame] | 1014 | recalc_sigpending_and_wake(t); |
Linus Torvalds | ae74c3b | 2006-08-02 20:17:49 -0700 | [diff] [blame] | 1015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
Oleg Nesterov | 80fe728 | 2008-04-30 00:53:05 -0700 | [diff] [blame] | 1017 | if (action->sa.sa_handler == SIG_DFL) |
| 1018 | t->signal->flags &= ~SIGNAL_UNKILLABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | ret = specific_send_sig_info(sig, info, t); |
| 1020 | spin_unlock_irqrestore(&t->sighand->siglock, flags); |
| 1021 | |
| 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | void |
| 1026 | force_sig_specific(int sig, struct task_struct *t) |
| 1027 | { |
Paul E. McKenney | b0423a0 | 2005-10-30 15:03:46 -0800 | [diff] [blame] | 1028 | force_sig_info(sig, SEND_SIG_FORCED, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | /* |
| 1032 | * Nuke all other threads in the group. |
| 1033 | */ |
| 1034 | void zap_other_threads(struct task_struct *p) |
| 1035 | { |
| 1036 | struct task_struct *t; |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | p->signal->group_stop_count = 0; |
| 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | for (t = next_thread(p); t != p; t = next_thread(t)) { |
| 1041 | /* |
| 1042 | * Don't bother with already dead threads |
| 1043 | */ |
| 1044 | if (t->exit_state) |
| 1045 | continue; |
| 1046 | |
Andrea Arcangeli | 30e0fca6 | 2005-10-30 15:02:38 -0800 | [diff] [blame] | 1047 | /* SIGKILL will be handled before any pending SIGSTOP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | sigaddset(&t->pending.signal, SIGKILL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | signal_wake_up(t, 1); |
| 1050 | } |
| 1051 | } |
| 1052 | |
Harvey Harrison | b5606c2 | 2008-02-13 15:03:16 -0800 | [diff] [blame] | 1053 | int __fatal_signal_pending(struct task_struct *tsk) |
Matthew Wilcox | f776d12 | 2007-12-06 11:15:50 -0500 | [diff] [blame] | 1054 | { |
| 1055 | return sigismember(&tsk->pending.signal, SIGKILL); |
| 1056 | } |
Trond Myklebust | 13f09b9 | 2008-01-31 20:40:29 -0500 | [diff] [blame] | 1057 | EXPORT_SYMBOL(__fatal_signal_pending); |
Matthew Wilcox | f776d12 | 2007-12-06 11:15:50 -0500 | [diff] [blame] | 1058 | |
Oleg Nesterov | f63ee72 | 2006-03-28 16:11:13 -0800 | [diff] [blame] | 1059 | struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags) |
| 1060 | { |
| 1061 | struct sighand_struct *sighand; |
| 1062 | |
Oleg Nesterov | 1406f2d | 2008-04-30 00:52:37 -0700 | [diff] [blame] | 1063 | rcu_read_lock(); |
Oleg Nesterov | f63ee72 | 2006-03-28 16:11:13 -0800 | [diff] [blame] | 1064 | for (;;) { |
| 1065 | sighand = rcu_dereference(tsk->sighand); |
| 1066 | if (unlikely(sighand == NULL)) |
| 1067 | break; |
| 1068 | |
| 1069 | spin_lock_irqsave(&sighand->siglock, *flags); |
| 1070 | if (likely(sighand == tsk->sighand)) |
| 1071 | break; |
| 1072 | spin_unlock_irqrestore(&sighand->siglock, *flags); |
| 1073 | } |
Oleg Nesterov | 1406f2d | 2008-04-30 00:52:37 -0700 | [diff] [blame] | 1074 | rcu_read_unlock(); |
Oleg Nesterov | f63ee72 | 2006-03-28 16:11:13 -0800 | [diff] [blame] | 1075 | |
| 1076 | return sighand; |
| 1077 | } |
| 1078 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1079 | /* |
| 1080 | * send signal info to all the members of a group |
| 1081 | * - the caller must hold the RCU read lock at least |
| 1082 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p) |
| 1084 | { |
Oleg Nesterov | 4a30deb | 2009-09-23 15:57:00 -0700 | [diff] [blame^] | 1085 | int ret = check_kill_permission(sig, info, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
Oleg Nesterov | 4a30deb | 2009-09-23 15:57:00 -0700 | [diff] [blame^] | 1087 | if (!ret && sig) |
| 1088 | ret = do_send_sig_info(sig, info, p, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
Pavel Emelyanov | 146a505 | 2008-02-08 04:19:22 -0800 | [diff] [blame] | 1094 | * __kill_pgrp_info() sends a signal to a process group: this is what the tty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | * control characters do (^C, ^Z etc) |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1096 | * - the caller must hold at least a readlock on tasklist_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | */ |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1098 | int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | { |
| 1100 | struct task_struct *p = NULL; |
| 1101 | int retval, success; |
| 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | success = 0; |
| 1104 | retval = -ESRCH; |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1105 | do_each_pid_task(pgrp, PIDTYPE_PGID, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | int err = group_send_sig_info(sig, info, p); |
| 1107 | success |= !err; |
| 1108 | retval = err; |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1109 | } while_each_pid_task(pgrp, PIDTYPE_PGID, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return success ? 0 : retval; |
| 1111 | } |
| 1112 | |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1113 | int kill_pid_info(int sig, struct siginfo *info, struct pid *pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | { |
Oleg Nesterov | d36174b | 2008-02-08 04:19:18 -0800 | [diff] [blame] | 1115 | int error = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | struct task_struct *p; |
| 1117 | |
Ingo Molnar | e56d090 | 2006-01-08 01:01:37 -0800 | [diff] [blame] | 1118 | rcu_read_lock(); |
Oleg Nesterov | d36174b | 2008-02-08 04:19:18 -0800 | [diff] [blame] | 1119 | retry: |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1120 | p = pid_task(pid, PIDTYPE_PID); |
Oleg Nesterov | d36174b | 2008-02-08 04:19:18 -0800 | [diff] [blame] | 1121 | if (p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | error = group_send_sig_info(sig, info, p); |
Oleg Nesterov | d36174b | 2008-02-08 04:19:18 -0800 | [diff] [blame] | 1123 | if (unlikely(error == -ESRCH)) |
| 1124 | /* |
| 1125 | * The task was unhashed in between, try again. |
| 1126 | * If it is dead, pid_task() will return NULL, |
| 1127 | * if we race with de_thread() it will find the |
| 1128 | * new leader. |
| 1129 | */ |
| 1130 | goto retry; |
| 1131 | } |
Ingo Molnar | e56d090 | 2006-01-08 01:01:37 -0800 | [diff] [blame] | 1132 | rcu_read_unlock(); |
Oleg Nesterov | 6ca25b5 | 2008-04-30 00:52:45 -0700 | [diff] [blame] | 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | return error; |
| 1135 | } |
| 1136 | |
Matthew Wilcox | c3de4b3 | 2007-02-09 08:11:47 -0700 | [diff] [blame] | 1137 | int |
| 1138 | kill_proc_info(int sig, struct siginfo *info, pid_t pid) |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1139 | { |
| 1140 | int error; |
| 1141 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1142 | error = kill_pid_info(sig, info, find_vpid(pid)); |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1143 | rcu_read_unlock(); |
| 1144 | return error; |
| 1145 | } |
| 1146 | |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 1147 | /* like kill_pid_info(), but doesn't use uid/euid of "current" */ |
| 1148 | int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid, |
David Quigley | 8f95dc5 | 2006-06-30 01:55:47 -0700 | [diff] [blame] | 1149 | uid_t uid, uid_t euid, u32 secid) |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 1150 | { |
| 1151 | int ret = -EINVAL; |
| 1152 | struct task_struct *p; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1153 | const struct cred *pcred; |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 1154 | |
| 1155 | if (!valid_signal(sig)) |
| 1156 | return ret; |
| 1157 | |
| 1158 | read_lock(&tasklist_lock); |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 1159 | p = pid_task(pid, PIDTYPE_PID); |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 1160 | if (!p) { |
| 1161 | ret = -ESRCH; |
| 1162 | goto out_unlock; |
| 1163 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1164 | pcred = __task_cred(p); |
| 1165 | if ((info == SEND_SIG_NOINFO || |
| 1166 | (!is_si_special(info) && SI_FROMUSER(info))) && |
| 1167 | euid != pcred->suid && euid != pcred->uid && |
| 1168 | uid != pcred->suid && uid != pcred->uid) { |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 1169 | ret = -EPERM; |
| 1170 | goto out_unlock; |
| 1171 | } |
David Quigley | 8f95dc5 | 2006-06-30 01:55:47 -0700 | [diff] [blame] | 1172 | ret = security_task_kill(p, info, sig, secid); |
| 1173 | if (ret) |
| 1174 | goto out_unlock; |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 1175 | if (sig && p->sighand) { |
| 1176 | unsigned long flags; |
| 1177 | spin_lock_irqsave(&p->sighand->siglock, flags); |
Sukadev Bhattiprolu | 7978b56 | 2009-04-02 16:58:04 -0700 | [diff] [blame] | 1178 | ret = __send_signal(sig, info, p, 1, 0); |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 1179 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
| 1180 | } |
| 1181 | out_unlock: |
| 1182 | read_unlock(&tasklist_lock); |
| 1183 | return ret; |
| 1184 | } |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 1185 | EXPORT_SYMBOL_GPL(kill_pid_info_as_uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
| 1187 | /* |
| 1188 | * kill_something_info() interprets pid in interesting ways just like kill(2). |
| 1189 | * |
| 1190 | * POSIX specifies that kill(-1,sig) is unspecified, but what we have |
| 1191 | * is probably wrong. Should make it like BSD or SYSV. |
| 1192 | */ |
| 1193 | |
Gustavo Fernando Padovan | bc64efd | 2008-07-25 01:47:33 -0700 | [diff] [blame] | 1194 | static int kill_something_info(int sig, struct siginfo *info, pid_t pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
Eric W. Biederman | 8d42db18 | 2007-02-12 00:52:55 -0800 | [diff] [blame] | 1196 | int ret; |
Pavel Emelyanov | d5df763 | 2008-02-08 04:19:22 -0800 | [diff] [blame] | 1197 | |
| 1198 | if (pid > 0) { |
| 1199 | rcu_read_lock(); |
| 1200 | ret = kill_pid_info(sig, info, find_vpid(pid)); |
| 1201 | rcu_read_unlock(); |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | read_lock(&tasklist_lock); |
| 1206 | if (pid != -1) { |
| 1207 | ret = __kill_pgrp_info(sig, info, |
| 1208 | pid ? find_vpid(-pid) : task_pgrp(current)); |
| 1209 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | int retval = 0, count = 0; |
| 1211 | struct task_struct * p; |
| 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | for_each_process(p) { |
Sukadev Bhattiprolu | d25141a | 2008-10-29 14:01:11 -0700 | [diff] [blame] | 1214 | if (task_pid_vnr(p) > 1 && |
| 1215 | !same_thread_group(p, current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | int err = group_send_sig_info(sig, info, p); |
| 1217 | ++count; |
| 1218 | if (err != -EPERM) |
| 1219 | retval = err; |
| 1220 | } |
| 1221 | } |
Eric W. Biederman | 8d42db18 | 2007-02-12 00:52:55 -0800 | [diff] [blame] | 1222 | ret = count ? retval : -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
Pavel Emelyanov | d5df763 | 2008-02-08 04:19:22 -0800 | [diff] [blame] | 1224 | read_unlock(&tasklist_lock); |
| 1225 | |
Eric W. Biederman | 8d42db18 | 2007-02-12 00:52:55 -0800 | [diff] [blame] | 1226 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * These are for backward compatibility with the rest of the kernel source. |
| 1231 | */ |
| 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | int |
| 1234 | send_sig_info(int sig, struct siginfo *info, struct task_struct *p) |
| 1235 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | /* |
| 1237 | * Make sure legacy kernel users don't send in bad values |
| 1238 | * (normal paths check this in check_kill_permission). |
| 1239 | */ |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 1240 | if (!valid_signal(sig)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return -EINVAL; |
| 1242 | |
Oleg Nesterov | 4a30deb | 2009-09-23 15:57:00 -0700 | [diff] [blame^] | 1243 | return do_send_sig_info(sig, info, p, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Oleg Nesterov | b67a1b9 | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 1246 | #define __si_special(priv) \ |
| 1247 | ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO) |
| 1248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | int |
| 1250 | send_sig(int sig, struct task_struct *p, int priv) |
| 1251 | { |
Oleg Nesterov | b67a1b9 | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 1252 | return send_sig_info(sig, __si_special(priv), p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | void |
| 1256 | force_sig(int sig, struct task_struct *p) |
| 1257 | { |
Oleg Nesterov | b67a1b9 | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 1258 | force_sig_info(sig, SEND_SIG_PRIV, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * When things go south during signal handling, we |
| 1263 | * will force a SIGSEGV. And if the signal that caused |
| 1264 | * the problem was already a SIGSEGV, we'll want to |
| 1265 | * make sure we don't even try to deliver the signal.. |
| 1266 | */ |
| 1267 | int |
| 1268 | force_sigsegv(int sig, struct task_struct *p) |
| 1269 | { |
| 1270 | if (sig == SIGSEGV) { |
| 1271 | unsigned long flags; |
| 1272 | spin_lock_irqsave(&p->sighand->siglock, flags); |
| 1273 | p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL; |
| 1274 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
| 1275 | } |
| 1276 | force_sig(SIGSEGV, p); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1280 | int kill_pgrp(struct pid *pid, int sig, int priv) |
| 1281 | { |
Pavel Emelyanov | 146a505 | 2008-02-08 04:19:22 -0800 | [diff] [blame] | 1282 | int ret; |
| 1283 | |
| 1284 | read_lock(&tasklist_lock); |
| 1285 | ret = __kill_pgrp_info(sig, __si_special(priv), pid); |
| 1286 | read_unlock(&tasklist_lock); |
| 1287 | |
| 1288 | return ret; |
Eric W. Biederman | c4b92fc | 2006-10-02 02:17:10 -0700 | [diff] [blame] | 1289 | } |
| 1290 | EXPORT_SYMBOL(kill_pgrp); |
| 1291 | |
| 1292 | int kill_pid(struct pid *pid, int sig, int priv) |
| 1293 | { |
| 1294 | return kill_pid_info(sig, __si_special(priv), pid); |
| 1295 | } |
| 1296 | EXPORT_SYMBOL(kill_pid); |
| 1297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | /* |
| 1299 | * These functions support sending signals using preallocated sigqueue |
| 1300 | * structures. This is needed "because realtime applications cannot |
| 1301 | * afford to lose notifications of asynchronous events, like timer |
| 1302 | * expirations or I/O completions". In the case of Posix Timers |
| 1303 | * we allocate the sigqueue structure from the timer_create. If this |
| 1304 | * allocation fails we are able to report the failure to the application |
| 1305 | * with an EAGAIN error. |
| 1306 | */ |
| 1307 | |
| 1308 | struct sigqueue *sigqueue_alloc(void) |
| 1309 | { |
| 1310 | struct sigqueue *q; |
| 1311 | |
| 1312 | if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0))) |
| 1313 | q->flags |= SIGQUEUE_PREALLOC; |
| 1314 | return(q); |
| 1315 | } |
| 1316 | |
| 1317 | void sigqueue_free(struct sigqueue *q) |
| 1318 | { |
| 1319 | unsigned long flags; |
Oleg Nesterov | 60187d2 | 2007-08-30 23:56:35 -0700 | [diff] [blame] | 1320 | spinlock_t *lock = ¤t->sighand->siglock; |
| 1321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); |
| 1323 | /* |
Oleg Nesterov | c8e85b4f | 2008-05-26 20:55:42 +0400 | [diff] [blame] | 1324 | * We must hold ->siglock while testing q->list |
| 1325 | * to serialize with collect_signal() or with |
Oleg Nesterov | da7978b | 2008-05-23 13:04:41 -0700 | [diff] [blame] | 1326 | * __exit_signal()->flush_sigqueue(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | */ |
Oleg Nesterov | 60187d2 | 2007-08-30 23:56:35 -0700 | [diff] [blame] | 1328 | spin_lock_irqsave(lock, flags); |
Oleg Nesterov | c8e85b4f | 2008-05-26 20:55:42 +0400 | [diff] [blame] | 1329 | q->flags &= ~SIGQUEUE_PREALLOC; |
| 1330 | /* |
| 1331 | * If it is queued it will be freed when dequeued, |
| 1332 | * like the "regular" sigqueue. |
| 1333 | */ |
Oleg Nesterov | 60187d2 | 2007-08-30 23:56:35 -0700 | [diff] [blame] | 1334 | if (!list_empty(&q->list)) |
Oleg Nesterov | c8e85b4f | 2008-05-26 20:55:42 +0400 | [diff] [blame] | 1335 | q = NULL; |
Oleg Nesterov | 60187d2 | 2007-08-30 23:56:35 -0700 | [diff] [blame] | 1336 | spin_unlock_irqrestore(lock, flags); |
| 1337 | |
Oleg Nesterov | c8e85b4f | 2008-05-26 20:55:42 +0400 | [diff] [blame] | 1338 | if (q) |
| 1339 | __sigqueue_free(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Oleg Nesterov | ac5c215 | 2008-04-30 00:52:57 -0700 | [diff] [blame] | 1342 | int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group) |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1343 | { |
Oleg Nesterov | e62e665 | 2008-04-30 00:52:56 -0700 | [diff] [blame] | 1344 | int sig = q->info.si_signo; |
Oleg Nesterov | 2ca3515 | 2008-04-30 00:52:54 -0700 | [diff] [blame] | 1345 | struct sigpending *pending; |
Oleg Nesterov | e62e665 | 2008-04-30 00:52:56 -0700 | [diff] [blame] | 1346 | unsigned long flags; |
| 1347 | int ret; |
Oleg Nesterov | 2ca3515 | 2008-04-30 00:52:54 -0700 | [diff] [blame] | 1348 | |
Pavel Emelyanov | 4cd4b6d | 2008-04-30 00:52:55 -0700 | [diff] [blame] | 1349 | BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); |
Oleg Nesterov | e62e665 | 2008-04-30 00:52:56 -0700 | [diff] [blame] | 1350 | |
| 1351 | ret = -1; |
| 1352 | if (!likely(lock_task_sighand(t, &flags))) |
| 1353 | goto ret; |
| 1354 | |
Oleg Nesterov | 7e695a5 | 2008-04-30 00:52:59 -0700 | [diff] [blame] | 1355 | ret = 1; /* the signal is ignored */ |
Sukadev Bhattiprolu | 921cf9f | 2009-04-02 16:58:05 -0700 | [diff] [blame] | 1356 | if (!prepare_signal(sig, t, 0)) |
Oleg Nesterov | e62e665 | 2008-04-30 00:52:56 -0700 | [diff] [blame] | 1357 | goto out; |
| 1358 | |
| 1359 | ret = 0; |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1360 | if (unlikely(!list_empty(&q->list))) { |
| 1361 | /* |
| 1362 | * If an SI_TIMER entry is already queue just increment |
| 1363 | * the overrun count. |
| 1364 | */ |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1365 | BUG_ON(q->info.si_code != SI_TIMER); |
| 1366 | q->info.si_overrun++; |
Oleg Nesterov | e62e665 | 2008-04-30 00:52:56 -0700 | [diff] [blame] | 1367 | goto out; |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1368 | } |
Oleg Nesterov | ba66129 | 2008-07-23 20:52:05 +0400 | [diff] [blame] | 1369 | q->info.si_overrun = 0; |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1370 | |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1371 | signalfd_notify(t, sig); |
Oleg Nesterov | 2ca3515 | 2008-04-30 00:52:54 -0700 | [diff] [blame] | 1372 | pending = group ? &t->signal->shared_pending : &t->pending; |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1373 | list_add_tail(&q->list, &pending->list); |
| 1374 | sigaddset(&pending->signal, sig); |
Pavel Emelyanov | 4cd4b6d | 2008-04-30 00:52:55 -0700 | [diff] [blame] | 1375 | complete_signal(sig, t, group); |
Oleg Nesterov | e62e665 | 2008-04-30 00:52:56 -0700 | [diff] [blame] | 1376 | out: |
| 1377 | unlock_task_sighand(t, &flags); |
| 1378 | ret: |
| 1379 | return ret; |
Pavel Emelyanov | 9e3bd6c | 2008-04-30 00:52:41 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | * Let a parent know about the death of a child. |
| 1384 | * For a stopped/continued status change, use do_notify_parent_cldstop instead. |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 1385 | * |
| 1386 | * Returns -1 if our parent ignored us and so we've switched to |
| 1387 | * self-reaping, or else @sig. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | */ |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 1389 | int do_notify_parent(struct task_struct *tsk, int sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | { |
| 1391 | struct siginfo info; |
| 1392 | unsigned long flags; |
| 1393 | struct sighand_struct *psig; |
Roland McGrath | 1b04624 | 2008-08-19 20:37:07 -0700 | [diff] [blame] | 1394 | int ret = sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
| 1396 | BUG_ON(sig == -1); |
| 1397 | |
| 1398 | /* do_notify_parent_cldstop should have been called instead. */ |
Matthew Wilcox | e1abb39 | 2007-12-06 11:07:35 -0500 | [diff] [blame] | 1399 | BUG_ON(task_is_stopped_or_traced(tsk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
Oleg Nesterov | 5cb1144 | 2009-06-17 16:27:30 -0700 | [diff] [blame] | 1401 | BUG_ON(!task_ptrace(tsk) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | (tsk->group_leader != tsk || !thread_group_empty(tsk))); |
| 1403 | |
| 1404 | info.si_signo = sig; |
| 1405 | info.si_errno = 0; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | * we are under tasklist_lock here so our parent is tied to |
| 1408 | * us and cannot exit and release its namespace. |
| 1409 | * |
| 1410 | * the only it can is to switch its nsproxy with sys_unshare, |
| 1411 | * bu uncharing pid namespaces is not allowed, so we'll always |
| 1412 | * see relevant namespace |
| 1413 | * |
| 1414 | * write_lock() currently calls preempt_disable() which is the |
| 1415 | * same as rcu_read_lock(), but according to Oleg, this is not |
| 1416 | * correct to rely on this |
| 1417 | */ |
| 1418 | rcu_read_lock(); |
| 1419 | info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1420 | info.si_uid = __task_cred(tsk)->uid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1421 | rcu_read_unlock(); |
| 1422 | |
Peter Zijlstra | 32bd671 | 2009-02-05 12:24:15 +0100 | [diff] [blame] | 1423 | info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime, |
| 1424 | tsk->signal->utime)); |
| 1425 | info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime, |
| 1426 | tsk->signal->stime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | |
| 1428 | info.si_status = tsk->exit_code & 0x7f; |
| 1429 | if (tsk->exit_code & 0x80) |
| 1430 | info.si_code = CLD_DUMPED; |
| 1431 | else if (tsk->exit_code & 0x7f) |
| 1432 | info.si_code = CLD_KILLED; |
| 1433 | else { |
| 1434 | info.si_code = CLD_EXITED; |
| 1435 | info.si_status = tsk->exit_code >> 8; |
| 1436 | } |
| 1437 | |
| 1438 | psig = tsk->parent->sighand; |
| 1439 | spin_lock_irqsave(&psig->siglock, flags); |
Oleg Nesterov | 5cb1144 | 2009-06-17 16:27:30 -0700 | [diff] [blame] | 1440 | if (!task_ptrace(tsk) && sig == SIGCHLD && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN || |
| 1442 | (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) { |
| 1443 | /* |
| 1444 | * We are exiting and our parent doesn't care. POSIX.1 |
| 1445 | * defines special semantics for setting SIGCHLD to SIG_IGN |
| 1446 | * or setting the SA_NOCLDWAIT flag: we should be reaped |
| 1447 | * automatically and not left for our parent's wait4 call. |
| 1448 | * Rather than having the parent do it as a magic kind of |
| 1449 | * signal handler, we just set this to tell do_exit that we |
| 1450 | * can be cleaned up without becoming a zombie. Note that |
| 1451 | * we still call __wake_up_parent in this case, because a |
| 1452 | * blocked sys_wait4 might now return -ECHILD. |
| 1453 | * |
| 1454 | * Whether we send SIGCHLD or not for SA_NOCLDWAIT |
| 1455 | * is implementation-defined: we do (if you don't want |
| 1456 | * it, just use SIG_IGN instead). |
| 1457 | */ |
Roland McGrath | 1b04624 | 2008-08-19 20:37:07 -0700 | [diff] [blame] | 1458 | ret = tsk->exit_signal = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 1460 | sig = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | } |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 1462 | if (valid_signal(sig) && sig > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | __group_send_sig_info(sig, &info, tsk->parent); |
| 1464 | __wake_up_parent(tsk, tsk->parent); |
| 1465 | spin_unlock_irqrestore(&psig->siglock, flags); |
Roland McGrath | 2b2a1ff | 2008-07-25 19:45:54 -0700 | [diff] [blame] | 1466 | |
Roland McGrath | 1b04624 | 2008-08-19 20:37:07 -0700 | [diff] [blame] | 1467 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Oleg Nesterov | a1d5e21 | 2006-03-28 16:11:29 -0800 | [diff] [blame] | 1470 | static void do_notify_parent_cldstop(struct task_struct *tsk, int why) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | { |
| 1472 | struct siginfo info; |
| 1473 | unsigned long flags; |
Oleg Nesterov | bc505a4 | 2005-09-06 15:17:32 -0700 | [diff] [blame] | 1474 | struct task_struct *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | struct sighand_struct *sighand; |
| 1476 | |
Oleg Nesterov | 5cb1144 | 2009-06-17 16:27:30 -0700 | [diff] [blame] | 1477 | if (task_ptrace(tsk)) |
Oleg Nesterov | bc505a4 | 2005-09-06 15:17:32 -0700 | [diff] [blame] | 1478 | parent = tsk->parent; |
| 1479 | else { |
| 1480 | tsk = tsk->group_leader; |
| 1481 | parent = tsk->real_parent; |
| 1482 | } |
| 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | info.si_signo = SIGCHLD; |
| 1485 | info.si_errno = 0; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1486 | /* |
| 1487 | * see comment in do_notify_parent() abot the following 3 lines |
| 1488 | */ |
| 1489 | rcu_read_lock(); |
Oleg Nesterov | d926566 | 2009-06-17 16:27:35 -0700 | [diff] [blame] | 1490 | info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1491 | info.si_uid = __task_cred(tsk)->uid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1492 | rcu_read_unlock(); |
| 1493 | |
Michael Kerrisk | d8878ba | 2008-07-25 01:47:32 -0700 | [diff] [blame] | 1494 | info.si_utime = cputime_to_clock_t(tsk->utime); |
| 1495 | info.si_stime = cputime_to_clock_t(tsk->stime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | info.si_code = why; |
| 1498 | switch (why) { |
| 1499 | case CLD_CONTINUED: |
| 1500 | info.si_status = SIGCONT; |
| 1501 | break; |
| 1502 | case CLD_STOPPED: |
| 1503 | info.si_status = tsk->signal->group_exit_code & 0x7f; |
| 1504 | break; |
| 1505 | case CLD_TRAPPED: |
| 1506 | info.si_status = tsk->exit_code & 0x7f; |
| 1507 | break; |
| 1508 | default: |
| 1509 | BUG(); |
| 1510 | } |
| 1511 | |
| 1512 | sighand = parent->sighand; |
| 1513 | spin_lock_irqsave(&sighand->siglock, flags); |
| 1514 | if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN && |
| 1515 | !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP)) |
| 1516 | __group_send_sig_info(SIGCHLD, &info, parent); |
| 1517 | /* |
| 1518 | * Even if SIGCHLD is not generated, we must wake up wait4 calls. |
| 1519 | */ |
| 1520 | __wake_up_parent(tsk, parent); |
| 1521 | spin_unlock_irqrestore(&sighand->siglock, flags); |
| 1522 | } |
| 1523 | |
Oleg Nesterov | d5f70c0 | 2006-06-26 00:26:07 -0700 | [diff] [blame] | 1524 | static inline int may_ptrace_stop(void) |
| 1525 | { |
Oleg Nesterov | 5cb1144 | 2009-06-17 16:27:30 -0700 | [diff] [blame] | 1526 | if (!likely(task_ptrace(current))) |
Oleg Nesterov | d5f70c0 | 2006-06-26 00:26:07 -0700 | [diff] [blame] | 1527 | return 0; |
Oleg Nesterov | d5f70c0 | 2006-06-26 00:26:07 -0700 | [diff] [blame] | 1528 | /* |
| 1529 | * Are we in the middle of do_coredump? |
| 1530 | * If so and our tracer is also part of the coredump stopping |
| 1531 | * is a deadlock situation, and pointless because our tracer |
| 1532 | * is dead so don't allow us to stop. |
| 1533 | * If SIGKILL was already sent before the caller unlocked |
Oleg Nesterov | 999d9fc | 2008-07-25 01:47:41 -0700 | [diff] [blame] | 1534 | * ->siglock we must see ->core_state != NULL. Otherwise it |
Oleg Nesterov | d5f70c0 | 2006-06-26 00:26:07 -0700 | [diff] [blame] | 1535 | * is safe to enter schedule(). |
| 1536 | */ |
Oleg Nesterov | 999d9fc | 2008-07-25 01:47:41 -0700 | [diff] [blame] | 1537 | if (unlikely(current->mm->core_state) && |
Oleg Nesterov | d5f70c0 | 2006-06-26 00:26:07 -0700 | [diff] [blame] | 1538 | unlikely(current->mm == current->parent->mm)) |
| 1539 | return 0; |
| 1540 | |
| 1541 | return 1; |
| 1542 | } |
| 1543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | /* |
Roland McGrath | 1a669c2 | 2008-02-06 01:37:37 -0800 | [diff] [blame] | 1545 | * Return nonzero if there is a SIGKILL that should be waking us up. |
| 1546 | * Called with the siglock held. |
| 1547 | */ |
| 1548 | static int sigkill_pending(struct task_struct *tsk) |
| 1549 | { |
Oleg Nesterov | 3d749b9 | 2008-07-25 01:47:37 -0700 | [diff] [blame] | 1550 | return sigismember(&tsk->pending.signal, SIGKILL) || |
| 1551 | sigismember(&tsk->signal->shared_pending.signal, SIGKILL); |
Roland McGrath | 1a669c2 | 2008-02-06 01:37:37 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | * This must be called with current->sighand->siglock held. |
| 1556 | * |
| 1557 | * This should be the path for all ptrace stops. |
| 1558 | * We always set current->last_siginfo while stopped here. |
| 1559 | * That makes it a way to test a stopped process for |
| 1560 | * being ptrace-stopped vs being job-control-stopped. |
| 1561 | * |
Oleg Nesterov | 20686a3 | 2008-02-08 04:19:03 -0800 | [diff] [blame] | 1562 | * If we actually decide not to stop at all because the tracer |
| 1563 | * is gone, we keep current->exit_code unless clear_code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | */ |
Oleg Nesterov | 20686a3 | 2008-02-08 04:19:03 -0800 | [diff] [blame] | 1565 | static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | { |
Roland McGrath | 1a669c2 | 2008-02-06 01:37:37 -0800 | [diff] [blame] | 1567 | if (arch_ptrace_stop_needed(exit_code, info)) { |
| 1568 | /* |
| 1569 | * The arch code has something special to do before a |
| 1570 | * ptrace stop. This is allowed to block, e.g. for faults |
| 1571 | * on user stack pages. We can't keep the siglock while |
| 1572 | * calling arch_ptrace_stop, so we must release it now. |
| 1573 | * To preserve proper semantics, we must do this before |
| 1574 | * any signal bookkeeping like checking group_stop_count. |
| 1575 | * Meanwhile, a SIGKILL could come in before we retake the |
| 1576 | * siglock. That must prevent us from sleeping in TASK_TRACED. |
| 1577 | * So after regaining the lock, we must check for SIGKILL. |
| 1578 | */ |
| 1579 | spin_unlock_irq(¤t->sighand->siglock); |
| 1580 | arch_ptrace_stop(exit_code, info); |
| 1581 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 3d749b9 | 2008-07-25 01:47:37 -0700 | [diff] [blame] | 1582 | if (sigkill_pending(current)) |
| 1583 | return; |
Roland McGrath | 1a669c2 | 2008-02-06 01:37:37 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | /* |
| 1587 | * If there is a group stop in progress, |
| 1588 | * we must participate in the bookkeeping. |
| 1589 | */ |
| 1590 | if (current->signal->group_stop_count > 0) |
| 1591 | --current->signal->group_stop_count; |
| 1592 | |
| 1593 | current->last_siginfo = info; |
| 1594 | current->exit_code = exit_code; |
| 1595 | |
| 1596 | /* Let the debugger run. */ |
Oleg Nesterov | d9ae90a | 2008-02-06 01:36:13 -0800 | [diff] [blame] | 1597 | __set_current_state(TASK_TRACED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | spin_unlock_irq(¤t->sighand->siglock); |
| 1599 | read_lock(&tasklist_lock); |
Oleg Nesterov | 3d749b9 | 2008-07-25 01:47:37 -0700 | [diff] [blame] | 1600 | if (may_ptrace_stop()) { |
Oleg Nesterov | a1d5e21 | 2006-03-28 16:11:29 -0800 | [diff] [blame] | 1601 | do_notify_parent_cldstop(current, CLD_TRAPPED); |
Miklos Szeredi | 53da1d9 | 2009-03-23 16:07:24 +0100 | [diff] [blame] | 1602 | /* |
| 1603 | * Don't want to allow preemption here, because |
| 1604 | * sys_ptrace() needs this task to be inactive. |
| 1605 | * |
| 1606 | * XXX: implement read_unlock_no_resched(). |
| 1607 | */ |
| 1608 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | read_unlock(&tasklist_lock); |
Miklos Szeredi | 53da1d9 | 2009-03-23 16:07:24 +0100 | [diff] [blame] | 1610 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | schedule(); |
| 1612 | } else { |
| 1613 | /* |
| 1614 | * By the time we got the lock, our tracer went away. |
Oleg Nesterov | 6405f7f | 2008-02-08 04:19:00 -0800 | [diff] [blame] | 1615 | * Don't drop the lock yet, another tracer may come. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | */ |
Oleg Nesterov | 6405f7f | 2008-02-08 04:19:00 -0800 | [diff] [blame] | 1617 | __set_current_state(TASK_RUNNING); |
Oleg Nesterov | 20686a3 | 2008-02-08 04:19:03 -0800 | [diff] [blame] | 1618 | if (clear_code) |
| 1619 | current->exit_code = 0; |
Oleg Nesterov | 6405f7f | 2008-02-08 04:19:00 -0800 | [diff] [blame] | 1620 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
Roland McGrath | 13b1c3d | 2008-03-03 20:22:05 -0800 | [diff] [blame] | 1624 | * While in TASK_TRACED, we were considered "frozen enough". |
| 1625 | * Now that we woke up, it's crucial if we're supposed to be |
| 1626 | * frozen that we freeze now before running anything substantial. |
| 1627 | */ |
| 1628 | try_to_freeze(); |
| 1629 | |
| 1630 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | * We are back. Now reacquire the siglock before touching |
| 1632 | * last_siginfo, so that we are sure to have synchronized with |
| 1633 | * any signal-sending on another CPU that wants to examine it. |
| 1634 | */ |
| 1635 | spin_lock_irq(¤t->sighand->siglock); |
| 1636 | current->last_siginfo = NULL; |
| 1637 | |
| 1638 | /* |
| 1639 | * Queued signals ignored us while we were stopped for tracing. |
| 1640 | * So check for any that we should take before resuming user mode. |
Roland McGrath | b74d0de | 2007-06-06 03:59:00 -0700 | [diff] [blame] | 1641 | * This sets TIF_SIGPENDING, but never clears it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | */ |
Roland McGrath | b74d0de | 2007-06-06 03:59:00 -0700 | [diff] [blame] | 1643 | recalc_sigpending_tsk(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | void ptrace_notify(int exit_code) |
| 1647 | { |
| 1648 | siginfo_t info; |
| 1649 | |
| 1650 | BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP); |
| 1651 | |
| 1652 | memset(&info, 0, sizeof info); |
| 1653 | info.si_signo = SIGTRAP; |
| 1654 | info.si_code = exit_code; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1655 | info.si_pid = task_pid_vnr(current); |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 1656 | info.si_uid = current_uid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | |
| 1658 | /* Let the debugger run. */ |
| 1659 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 20686a3 | 2008-02-08 04:19:03 -0800 | [diff] [blame] | 1660 | ptrace_stop(exit_code, 1, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | spin_unlock_irq(¤t->sighand->siglock); |
| 1662 | } |
| 1663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | /* |
| 1665 | * This performs the stopping for SIGSTOP and other stop signals. |
| 1666 | * We have to stop all threads in the thread group. |
| 1667 | * Returns nonzero if we've actually stopped and released the siglock. |
| 1668 | * Returns zero if we didn't stop and still hold the siglock. |
| 1669 | */ |
Oleg Nesterov | a122b34 | 2006-03-28 16:11:22 -0800 | [diff] [blame] | 1670 | static int do_signal_stop(int signr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | { |
| 1672 | struct signal_struct *sig = current->signal; |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1673 | int notify; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1675 | if (!sig->group_stop_count) { |
Oleg Nesterov | f558b7e | 2008-02-04 22:27:24 -0800 | [diff] [blame] | 1676 | struct task_struct *t; |
| 1677 | |
Oleg Nesterov | 2b201a9 | 2008-07-25 01:47:31 -0700 | [diff] [blame] | 1678 | if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) || |
Oleg Nesterov | 573cf9a | 2008-04-30 00:52:36 -0700 | [diff] [blame] | 1679 | unlikely(signal_group_exit(sig))) |
Oleg Nesterov | f558b7e | 2008-02-04 22:27:24 -0800 | [diff] [blame] | 1680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | * There is no group stop already in progress. |
Oleg Nesterov | a122b34 | 2006-03-28 16:11:22 -0800 | [diff] [blame] | 1683 | * We must initiate one now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | */ |
Oleg Nesterov | a122b34 | 2006-03-28 16:11:22 -0800 | [diff] [blame] | 1685 | sig->group_exit_code = signr; |
| 1686 | |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1687 | sig->group_stop_count = 1; |
Oleg Nesterov | a122b34 | 2006-03-28 16:11:22 -0800 | [diff] [blame] | 1688 | for (t = next_thread(current); t != current; t = next_thread(t)) |
| 1689 | /* |
| 1690 | * Setting state to TASK_STOPPED for a group |
| 1691 | * stop is always done with the siglock held, |
| 1692 | * so this check has no races. |
| 1693 | */ |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1694 | if (!(t->flags & PF_EXITING) && |
Matthew Wilcox | e1abb39 | 2007-12-06 11:07:35 -0500 | [diff] [blame] | 1695 | !task_is_stopped_or_traced(t)) { |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1696 | sig->group_stop_count++; |
Oleg Nesterov | a122b34 | 2006-03-28 16:11:22 -0800 | [diff] [blame] | 1697 | signal_wake_up(t, 0); |
| 1698 | } |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1699 | } |
| 1700 | /* |
| 1701 | * If there are no other threads in the group, or if there is |
| 1702 | * a group stop in progress and we are the last to stop, report |
| 1703 | * to the parent. When ptraced, every thread reports itself. |
| 1704 | */ |
| 1705 | notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0; |
| 1706 | notify = tracehook_notify_jctl(notify, CLD_STOPPED); |
| 1707 | /* |
| 1708 | * tracehook_notify_jctl() can drop and reacquire siglock, so |
| 1709 | * we keep ->group_stop_count != 0 before the call. If SIGCONT |
| 1710 | * or SIGKILL comes in between ->group_stop_count == 0. |
| 1711 | */ |
| 1712 | if (sig->group_stop_count) { |
| 1713 | if (!--sig->group_stop_count) |
| 1714 | sig->flags = SIGNAL_STOP_STOPPED; |
| 1715 | current->exit_code = sig->group_exit_code; |
| 1716 | __set_current_state(TASK_STOPPED); |
| 1717 | } |
| 1718 | spin_unlock_irq(¤t->sighand->siglock); |
| 1719 | |
| 1720 | if (notify) { |
| 1721 | read_lock(&tasklist_lock); |
| 1722 | do_notify_parent_cldstop(current, notify); |
| 1723 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1726 | /* Now we don't run again until woken by SIGCONT or SIGKILL */ |
| 1727 | do { |
| 1728 | schedule(); |
| 1729 | } while (try_to_freeze()); |
Oleg Nesterov | dac27f4 | 2006-03-28 16:11:28 -0800 | [diff] [blame] | 1730 | |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1731 | tracehook_finish_jctl(); |
| 1732 | current->exit_code = 0; |
| 1733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | return 1; |
| 1735 | } |
| 1736 | |
Roland McGrath | 18c98b6 | 2008-04-17 18:44:38 -0700 | [diff] [blame] | 1737 | static int ptrace_signal(int signr, siginfo_t *info, |
| 1738 | struct pt_regs *regs, void *cookie) |
| 1739 | { |
Oleg Nesterov | 5cb1144 | 2009-06-17 16:27:30 -0700 | [diff] [blame] | 1740 | if (!task_ptrace(current)) |
Roland McGrath | 18c98b6 | 2008-04-17 18:44:38 -0700 | [diff] [blame] | 1741 | return signr; |
| 1742 | |
| 1743 | ptrace_signal_deliver(regs, cookie); |
| 1744 | |
| 1745 | /* Let the debugger run. */ |
| 1746 | ptrace_stop(signr, 0, info); |
| 1747 | |
| 1748 | /* We're back. Did the debugger cancel the sig? */ |
| 1749 | signr = current->exit_code; |
| 1750 | if (signr == 0) |
| 1751 | return signr; |
| 1752 | |
| 1753 | current->exit_code = 0; |
| 1754 | |
| 1755 | /* Update the siginfo structure if the signal has |
| 1756 | changed. If the debugger wanted something |
| 1757 | specific in the siginfo structure then it should |
| 1758 | have updated *info via PTRACE_SETSIGINFO. */ |
| 1759 | if (signr != info->si_signo) { |
| 1760 | info->si_signo = signr; |
| 1761 | info->si_errno = 0; |
| 1762 | info->si_code = SI_USER; |
| 1763 | info->si_pid = task_pid_vnr(current->parent); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1764 | info->si_uid = task_uid(current->parent); |
Roland McGrath | 18c98b6 | 2008-04-17 18:44:38 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | /* If the (new) signal is now blocked, requeue it. */ |
| 1768 | if (sigismember(¤t->blocked, signr)) { |
| 1769 | specific_send_sig_info(signr, info, current); |
| 1770 | signr = 0; |
| 1771 | } |
| 1772 | |
| 1773 | return signr; |
| 1774 | } |
| 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka, |
| 1777 | struct pt_regs *regs, void *cookie) |
| 1778 | { |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1779 | struct sighand_struct *sighand = current->sighand; |
| 1780 | struct signal_struct *signal = current->signal; |
| 1781 | int signr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
Roland McGrath | 13b1c3d | 2008-03-03 20:22:05 -0800 | [diff] [blame] | 1783 | relock: |
| 1784 | /* |
| 1785 | * We'll jump back here after any time we were stopped in TASK_STOPPED. |
| 1786 | * While in TASK_STOPPED, we were considered "frozen enough". |
| 1787 | * Now that we woke up, it's crucial if we're supposed to be |
| 1788 | * frozen that we freeze now before running anything substantial. |
| 1789 | */ |
Rafael J. Wysocki | fc558a7 | 2006-03-23 03:00:05 -0800 | [diff] [blame] | 1790 | try_to_freeze(); |
| 1791 | |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1792 | spin_lock_irq(&sighand->siglock); |
Oleg Nesterov | 021e1ae | 2008-04-30 00:53:00 -0700 | [diff] [blame] | 1793 | /* |
| 1794 | * Every stopped thread goes here after wakeup. Check to see if |
| 1795 | * we should notify the parent, prepare_signal(SIGCONT) encodes |
| 1796 | * the CLD_ si_code into SIGNAL_CLD_MASK bits. |
| 1797 | */ |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1798 | if (unlikely(signal->flags & SIGNAL_CLD_MASK)) { |
| 1799 | int why = (signal->flags & SIGNAL_STOP_CONTINUED) |
Oleg Nesterov | e442055 | 2008-04-30 00:52:44 -0700 | [diff] [blame] | 1800 | ? CLD_CONTINUED : CLD_STOPPED; |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1801 | signal->flags &= ~SIGNAL_CLD_MASK; |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1802 | |
| 1803 | why = tracehook_notify_jctl(why, CLD_CONTINUED); |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1804 | spin_unlock_irq(&sighand->siglock); |
Oleg Nesterov | e442055 | 2008-04-30 00:52:44 -0700 | [diff] [blame] | 1805 | |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1806 | if (why) { |
| 1807 | read_lock(&tasklist_lock); |
| 1808 | do_notify_parent_cldstop(current->group_leader, why); |
| 1809 | read_unlock(&tasklist_lock); |
| 1810 | } |
Oleg Nesterov | e442055 | 2008-04-30 00:52:44 -0700 | [diff] [blame] | 1811 | goto relock; |
| 1812 | } |
| 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | for (;;) { |
| 1815 | struct k_sigaction *ka; |
| 1816 | |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1817 | if (unlikely(signal->group_stop_count > 0) && |
Oleg Nesterov | f558b7e | 2008-02-04 22:27:24 -0800 | [diff] [blame] | 1818 | do_signal_stop(0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | goto relock; |
| 1820 | |
Roland McGrath | 7bcf6a2 | 2008-07-25 19:45:53 -0700 | [diff] [blame] | 1821 | /* |
| 1822 | * Tracing can induce an artifical signal and choose sigaction. |
| 1823 | * The return value in @signr determines the default action, |
| 1824 | * but @info->si_signo is the signal number we will report. |
| 1825 | */ |
| 1826 | signr = tracehook_get_signal(current, regs, info, return_ka); |
| 1827 | if (unlikely(signr < 0)) |
| 1828 | goto relock; |
| 1829 | if (unlikely(signr != 0)) |
| 1830 | ka = return_ka; |
| 1831 | else { |
| 1832 | signr = dequeue_signal(current, ¤t->blocked, |
| 1833 | info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | |
Roland McGrath | 18c98b6 | 2008-04-17 18:44:38 -0700 | [diff] [blame] | 1835 | if (!signr) |
Roland McGrath | 7bcf6a2 | 2008-07-25 19:45:53 -0700 | [diff] [blame] | 1836 | break; /* will return 0 */ |
| 1837 | |
| 1838 | if (signr != SIGKILL) { |
| 1839 | signr = ptrace_signal(signr, info, |
| 1840 | regs, cookie); |
| 1841 | if (!signr) |
| 1842 | continue; |
| 1843 | } |
| 1844 | |
| 1845 | ka = &sighand->action[signr-1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */ |
| 1849 | continue; |
| 1850 | if (ka->sa.sa_handler != SIG_DFL) { |
| 1851 | /* Run the handler. */ |
| 1852 | *return_ka = *ka; |
| 1853 | |
| 1854 | if (ka->sa.sa_flags & SA_ONESHOT) |
| 1855 | ka->sa.sa_handler = SIG_DFL; |
| 1856 | |
| 1857 | break; /* will return non-zero "signr" value */ |
| 1858 | } |
| 1859 | |
| 1860 | /* |
| 1861 | * Now we are doing the default action for this signal. |
| 1862 | */ |
| 1863 | if (sig_kernel_ignore(signr)) /* Default is nothing. */ |
| 1864 | continue; |
| 1865 | |
Sukadev Bhattiprolu | 84d7378 | 2006-12-08 02:38:01 -0800 | [diff] [blame] | 1866 | /* |
Sukadev Bhattiprolu | 0fbc26a | 2007-10-18 23:40:13 -0700 | [diff] [blame] | 1867 | * Global init gets no signals it doesn't want. |
Sukadev Bhattiprolu | b3bfa0c | 2009-04-02 16:58:08 -0700 | [diff] [blame] | 1868 | * Container-init gets no signals it doesn't want from same |
| 1869 | * container. |
| 1870 | * |
| 1871 | * Note that if global/container-init sees a sig_kernel_only() |
| 1872 | * signal here, the signal must have been generated internally |
| 1873 | * or must have come from an ancestor namespace. In either |
| 1874 | * case, the signal cannot be dropped. |
Sukadev Bhattiprolu | 84d7378 | 2006-12-08 02:38:01 -0800 | [diff] [blame] | 1875 | */ |
Oleg Nesterov | fae5fa4 | 2008-04-30 00:53:03 -0700 | [diff] [blame] | 1876 | if (unlikely(signal->flags & SIGNAL_UNKILLABLE) && |
Sukadev Bhattiprolu | b3bfa0c | 2009-04-02 16:58:08 -0700 | [diff] [blame] | 1877 | !sig_kernel_only(signr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | continue; |
| 1879 | |
| 1880 | if (sig_kernel_stop(signr)) { |
| 1881 | /* |
| 1882 | * The default action is to stop all threads in |
| 1883 | * the thread group. The job control signals |
| 1884 | * do nothing in an orphaned pgrp, but SIGSTOP |
| 1885 | * always works. Note that siglock needs to be |
| 1886 | * dropped during the call to is_orphaned_pgrp() |
| 1887 | * because of lock ordering with tasklist_lock. |
| 1888 | * This allows an intervening SIGCONT to be posted. |
| 1889 | * We need to check for that and bail out if necessary. |
| 1890 | */ |
| 1891 | if (signr != SIGSTOP) { |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1892 | spin_unlock_irq(&sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
| 1894 | /* signals can be posted during this window */ |
| 1895 | |
Eric W. Biederman | 3e7cd6c | 2007-02-12 00:52:58 -0800 | [diff] [blame] | 1896 | if (is_current_pgrp_orphaned()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | goto relock; |
| 1898 | |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1899 | spin_lock_irq(&sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Roland McGrath | 7bcf6a2 | 2008-07-25 19:45:53 -0700 | [diff] [blame] | 1902 | if (likely(do_signal_stop(info->si_signo))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | /* It released the siglock. */ |
| 1904 | goto relock; |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * We didn't actually stop, due to a race |
| 1909 | * with SIGCONT or something like that. |
| 1910 | */ |
| 1911 | continue; |
| 1912 | } |
| 1913 | |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1914 | spin_unlock_irq(&sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
| 1916 | /* |
| 1917 | * Anything else is fatal, maybe with a core dump. |
| 1918 | */ |
| 1919 | current->flags |= PF_SIGNALED; |
Oleg Nesterov | 2dce81b | 2008-04-30 00:52:58 -0700 | [diff] [blame] | 1920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | if (sig_kernel_coredump(signr)) { |
Oleg Nesterov | 2dce81b | 2008-04-30 00:52:58 -0700 | [diff] [blame] | 1922 | if (print_fatal_signals) |
Roland McGrath | 7bcf6a2 | 2008-07-25 19:45:53 -0700 | [diff] [blame] | 1923 | print_fatal_signal(regs, info->si_signo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | /* |
| 1925 | * If it was able to dump core, this kills all |
| 1926 | * other threads in the group and synchronizes with |
| 1927 | * their demise. If we lost the race with another |
| 1928 | * thread getting here, it set group_exit_code |
| 1929 | * first and our do_group_exit call below will use |
| 1930 | * that value and ignore the one we pass it. |
| 1931 | */ |
Roland McGrath | 7bcf6a2 | 2008-07-25 19:45:53 -0700 | [diff] [blame] | 1932 | do_coredump(info->si_signo, info->si_signo, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
| 1936 | * Death signals, no core dump. |
| 1937 | */ |
Roland McGrath | 7bcf6a2 | 2008-07-25 19:45:53 -0700 | [diff] [blame] | 1938 | do_group_exit(info->si_signo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | /* NOTREACHED */ |
| 1940 | } |
Oleg Nesterov | f6b76d4 | 2008-04-30 00:52:47 -0700 | [diff] [blame] | 1941 | spin_unlock_irq(&sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | return signr; |
| 1943 | } |
| 1944 | |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1945 | void exit_signals(struct task_struct *tsk) |
| 1946 | { |
| 1947 | int group_stop = 0; |
Oleg Nesterov | 5dee170 | 2008-02-08 04:19:13 -0800 | [diff] [blame] | 1948 | struct task_struct *t; |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1949 | |
Oleg Nesterov | 5dee170 | 2008-02-08 04:19:13 -0800 | [diff] [blame] | 1950 | if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) { |
| 1951 | tsk->flags |= PF_EXITING; |
| 1952 | return; |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
Oleg Nesterov | 5dee170 | 2008-02-08 04:19:13 -0800 | [diff] [blame] | 1955 | spin_lock_irq(&tsk->sighand->siglock); |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1956 | /* |
| 1957 | * From now this task is not visible for group-wide signals, |
| 1958 | * see wants_signal(), do_signal_stop(). |
| 1959 | */ |
| 1960 | tsk->flags |= PF_EXITING; |
Oleg Nesterov | 5dee170 | 2008-02-08 04:19:13 -0800 | [diff] [blame] | 1961 | if (!signal_pending(tsk)) |
| 1962 | goto out; |
| 1963 | |
| 1964 | /* It could be that __group_complete_signal() choose us to |
| 1965 | * notify about group-wide signal. Another thread should be |
| 1966 | * woken now to take the signal since we will not. |
| 1967 | */ |
| 1968 | for (t = tsk; (t = next_thread(t)) != tsk; ) |
| 1969 | if (!signal_pending(t) && !(t->flags & PF_EXITING)) |
| 1970 | recalc_sigpending_and_wake(t); |
| 1971 | |
| 1972 | if (unlikely(tsk->signal->group_stop_count) && |
| 1973 | !--tsk->signal->group_stop_count) { |
| 1974 | tsk->signal->flags = SIGNAL_STOP_STOPPED; |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1975 | group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED); |
Oleg Nesterov | 5dee170 | 2008-02-08 04:19:13 -0800 | [diff] [blame] | 1976 | } |
| 1977 | out: |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1978 | spin_unlock_irq(&tsk->sighand->siglock); |
| 1979 | |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1980 | if (unlikely(group_stop)) { |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1981 | read_lock(&tasklist_lock); |
Roland McGrath | ae6d2ed | 2009-09-23 15:56:53 -0700 | [diff] [blame] | 1982 | do_notify_parent_cldstop(tsk, group_stop); |
Oleg Nesterov | d12619b | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1983 | read_unlock(&tasklist_lock); |
| 1984 | } |
| 1985 | } |
| 1986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | EXPORT_SYMBOL(recalc_sigpending); |
| 1988 | EXPORT_SYMBOL_GPL(dequeue_signal); |
| 1989 | EXPORT_SYMBOL(flush_signals); |
| 1990 | EXPORT_SYMBOL(force_sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | EXPORT_SYMBOL(send_sig); |
| 1992 | EXPORT_SYMBOL(send_sig_info); |
| 1993 | EXPORT_SYMBOL(sigprocmask); |
| 1994 | EXPORT_SYMBOL(block_all_signals); |
| 1995 | EXPORT_SYMBOL(unblock_all_signals); |
| 1996 | |
| 1997 | |
| 1998 | /* |
| 1999 | * System call entry points. |
| 2000 | */ |
| 2001 | |
Heiko Carstens | 754fe8d | 2009-01-14 14:14:09 +0100 | [diff] [blame] | 2002 | SYSCALL_DEFINE0(restart_syscall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | { |
| 2004 | struct restart_block *restart = ¤t_thread_info()->restart_block; |
| 2005 | return restart->fn(restart); |
| 2006 | } |
| 2007 | |
| 2008 | long do_no_restart_syscall(struct restart_block *param) |
| 2009 | { |
| 2010 | return -EINTR; |
| 2011 | } |
| 2012 | |
| 2013 | /* |
| 2014 | * We don't need to get the kernel lock - this is all local to this |
| 2015 | * particular thread.. (and that's good, because this is _heavily_ |
| 2016 | * used by various programs) |
| 2017 | */ |
| 2018 | |
| 2019 | /* |
| 2020 | * This is also useful for kernel threads that want to temporarily |
| 2021 | * (or permanently) block certain signals. |
| 2022 | * |
| 2023 | * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel |
| 2024 | * interface happily blocks "unblockable" signals like SIGKILL |
| 2025 | * and friends. |
| 2026 | */ |
| 2027 | int sigprocmask(int how, sigset_t *set, sigset_t *oldset) |
| 2028 | { |
| 2029 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | |
| 2031 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | a26fd33 | 2006-03-23 03:00:49 -0800 | [diff] [blame] | 2032 | if (oldset) |
| 2033 | *oldset = current->blocked; |
| 2034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | error = 0; |
| 2036 | switch (how) { |
| 2037 | case SIG_BLOCK: |
| 2038 | sigorsets(¤t->blocked, ¤t->blocked, set); |
| 2039 | break; |
| 2040 | case SIG_UNBLOCK: |
| 2041 | signandsets(¤t->blocked, ¤t->blocked, set); |
| 2042 | break; |
| 2043 | case SIG_SETMASK: |
| 2044 | current->blocked = *set; |
| 2045 | break; |
| 2046 | default: |
| 2047 | error = -EINVAL; |
| 2048 | } |
| 2049 | recalc_sigpending(); |
| 2050 | spin_unlock_irq(¤t->sighand->siglock); |
Oleg Nesterov | a26fd33 | 2006-03-23 03:00:49 -0800 | [diff] [blame] | 2051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | return error; |
| 2053 | } |
| 2054 | |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 2055 | SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set, |
| 2056 | sigset_t __user *, oset, size_t, sigsetsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | { |
| 2058 | int error = -EINVAL; |
| 2059 | sigset_t old_set, new_set; |
| 2060 | |
| 2061 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 2062 | if (sigsetsize != sizeof(sigset_t)) |
| 2063 | goto out; |
| 2064 | |
| 2065 | if (set) { |
| 2066 | error = -EFAULT; |
| 2067 | if (copy_from_user(&new_set, set, sizeof(*set))) |
| 2068 | goto out; |
| 2069 | sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 2070 | |
| 2071 | error = sigprocmask(how, &new_set, &old_set); |
| 2072 | if (error) |
| 2073 | goto out; |
| 2074 | if (oset) |
| 2075 | goto set_old; |
| 2076 | } else if (oset) { |
| 2077 | spin_lock_irq(¤t->sighand->siglock); |
| 2078 | old_set = current->blocked; |
| 2079 | spin_unlock_irq(¤t->sighand->siglock); |
| 2080 | |
| 2081 | set_old: |
| 2082 | error = -EFAULT; |
| 2083 | if (copy_to_user(oset, &old_set, sizeof(*oset))) |
| 2084 | goto out; |
| 2085 | } |
| 2086 | error = 0; |
| 2087 | out: |
| 2088 | return error; |
| 2089 | } |
| 2090 | |
| 2091 | long do_sigpending(void __user *set, unsigned long sigsetsize) |
| 2092 | { |
| 2093 | long error = -EINVAL; |
| 2094 | sigset_t pending; |
| 2095 | |
| 2096 | if (sigsetsize > sizeof(sigset_t)) |
| 2097 | goto out; |
| 2098 | |
| 2099 | spin_lock_irq(¤t->sighand->siglock); |
| 2100 | sigorsets(&pending, ¤t->pending.signal, |
| 2101 | ¤t->signal->shared_pending.signal); |
| 2102 | spin_unlock_irq(¤t->sighand->siglock); |
| 2103 | |
| 2104 | /* Outside the lock because only this thread touches it. */ |
| 2105 | sigandsets(&pending, ¤t->blocked, &pending); |
| 2106 | |
| 2107 | error = -EFAULT; |
| 2108 | if (!copy_to_user(set, &pending, sigsetsize)) |
| 2109 | error = 0; |
| 2110 | |
| 2111 | out: |
| 2112 | return error; |
| 2113 | } |
| 2114 | |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 2115 | SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | { |
| 2117 | return do_sigpending(set, sigsetsize); |
| 2118 | } |
| 2119 | |
| 2120 | #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER |
| 2121 | |
| 2122 | int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from) |
| 2123 | { |
| 2124 | int err; |
| 2125 | |
| 2126 | if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t))) |
| 2127 | return -EFAULT; |
| 2128 | if (from->si_code < 0) |
| 2129 | return __copy_to_user(to, from, sizeof(siginfo_t)) |
| 2130 | ? -EFAULT : 0; |
| 2131 | /* |
| 2132 | * If you change siginfo_t structure, please be sure |
| 2133 | * this code is fixed accordingly. |
Davide Libenzi | fba2afa | 2007-05-10 22:23:13 -0700 | [diff] [blame] | 2134 | * Please remember to update the signalfd_copyinfo() function |
| 2135 | * inside fs/signalfd.c too, in case siginfo_t changes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | * It should never copy any pad contained in the structure |
| 2137 | * to avoid security leaks, but must copy the generic |
| 2138 | * 3 ints plus the relevant union member. |
| 2139 | */ |
| 2140 | err = __put_user(from->si_signo, &to->si_signo); |
| 2141 | err |= __put_user(from->si_errno, &to->si_errno); |
| 2142 | err |= __put_user((short)from->si_code, &to->si_code); |
| 2143 | switch (from->si_code & __SI_MASK) { |
| 2144 | case __SI_KILL: |
| 2145 | err |= __put_user(from->si_pid, &to->si_pid); |
| 2146 | err |= __put_user(from->si_uid, &to->si_uid); |
| 2147 | break; |
| 2148 | case __SI_TIMER: |
| 2149 | err |= __put_user(from->si_tid, &to->si_tid); |
| 2150 | err |= __put_user(from->si_overrun, &to->si_overrun); |
| 2151 | err |= __put_user(from->si_ptr, &to->si_ptr); |
| 2152 | break; |
| 2153 | case __SI_POLL: |
| 2154 | err |= __put_user(from->si_band, &to->si_band); |
| 2155 | err |= __put_user(from->si_fd, &to->si_fd); |
| 2156 | break; |
| 2157 | case __SI_FAULT: |
| 2158 | err |= __put_user(from->si_addr, &to->si_addr); |
| 2159 | #ifdef __ARCH_SI_TRAPNO |
| 2160 | err |= __put_user(from->si_trapno, &to->si_trapno); |
| 2161 | #endif |
| 2162 | break; |
| 2163 | case __SI_CHLD: |
| 2164 | err |= __put_user(from->si_pid, &to->si_pid); |
| 2165 | err |= __put_user(from->si_uid, &to->si_uid); |
| 2166 | err |= __put_user(from->si_status, &to->si_status); |
| 2167 | err |= __put_user(from->si_utime, &to->si_utime); |
| 2168 | err |= __put_user(from->si_stime, &to->si_stime); |
| 2169 | break; |
| 2170 | case __SI_RT: /* This is not generated by the kernel as of now. */ |
| 2171 | case __SI_MESGQ: /* But this is */ |
| 2172 | err |= __put_user(from->si_pid, &to->si_pid); |
| 2173 | err |= __put_user(from->si_uid, &to->si_uid); |
| 2174 | err |= __put_user(from->si_ptr, &to->si_ptr); |
| 2175 | break; |
| 2176 | default: /* this is just in case for now ... */ |
| 2177 | err |= __put_user(from->si_pid, &to->si_pid); |
| 2178 | err |= __put_user(from->si_uid, &to->si_uid); |
| 2179 | break; |
| 2180 | } |
| 2181 | return err; |
| 2182 | } |
| 2183 | |
| 2184 | #endif |
| 2185 | |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 2186 | SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese, |
| 2187 | siginfo_t __user *, uinfo, const struct timespec __user *, uts, |
| 2188 | size_t, sigsetsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | { |
| 2190 | int ret, sig; |
| 2191 | sigset_t these; |
| 2192 | struct timespec ts; |
| 2193 | siginfo_t info; |
| 2194 | long timeout = 0; |
| 2195 | |
| 2196 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 2197 | if (sigsetsize != sizeof(sigset_t)) |
| 2198 | return -EINVAL; |
| 2199 | |
| 2200 | if (copy_from_user(&these, uthese, sizeof(these))) |
| 2201 | return -EFAULT; |
| 2202 | |
| 2203 | /* |
| 2204 | * Invert the set of allowed signals to get those we |
| 2205 | * want to block. |
| 2206 | */ |
| 2207 | sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 2208 | signotset(&these); |
| 2209 | |
| 2210 | if (uts) { |
| 2211 | if (copy_from_user(&ts, uts, sizeof(ts))) |
| 2212 | return -EFAULT; |
| 2213 | if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0 |
| 2214 | || ts.tv_sec < 0) |
| 2215 | return -EINVAL; |
| 2216 | } |
| 2217 | |
| 2218 | spin_lock_irq(¤t->sighand->siglock); |
| 2219 | sig = dequeue_signal(current, &these, &info); |
| 2220 | if (!sig) { |
| 2221 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 2222 | if (uts) |
| 2223 | timeout = (timespec_to_jiffies(&ts) |
| 2224 | + (ts.tv_sec || ts.tv_nsec)); |
| 2225 | |
| 2226 | if (timeout) { |
| 2227 | /* None ready -- temporarily unblock those we're |
| 2228 | * interested while we are sleeping in so that we'll |
| 2229 | * be awakened when they arrive. */ |
| 2230 | current->real_blocked = current->blocked; |
| 2231 | sigandsets(¤t->blocked, ¤t->blocked, &these); |
| 2232 | recalc_sigpending(); |
| 2233 | spin_unlock_irq(¤t->sighand->siglock); |
| 2234 | |
Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 2235 | timeout = schedule_timeout_interruptible(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | spin_lock_irq(¤t->sighand->siglock); |
| 2238 | sig = dequeue_signal(current, &these, &info); |
| 2239 | current->blocked = current->real_blocked; |
| 2240 | siginitset(¤t->real_blocked, 0); |
| 2241 | recalc_sigpending(); |
| 2242 | } |
| 2243 | } |
| 2244 | spin_unlock_irq(¤t->sighand->siglock); |
| 2245 | |
| 2246 | if (sig) { |
| 2247 | ret = sig; |
| 2248 | if (uinfo) { |
| 2249 | if (copy_siginfo_to_user(uinfo, &info)) |
| 2250 | ret = -EFAULT; |
| 2251 | } |
| 2252 | } else { |
| 2253 | ret = -EAGAIN; |
| 2254 | if (timeout) |
| 2255 | ret = -EINTR; |
| 2256 | } |
| 2257 | |
| 2258 | return ret; |
| 2259 | } |
| 2260 | |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 2261 | SYSCALL_DEFINE2(kill, pid_t, pid, int, sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | { |
| 2263 | struct siginfo info; |
| 2264 | |
| 2265 | info.si_signo = sig; |
| 2266 | info.si_errno = 0; |
| 2267 | info.si_code = SI_USER; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2268 | info.si_pid = task_tgid_vnr(current); |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 2269 | info.si_uid = current_uid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | |
| 2271 | return kill_something_info(sig, &info, pid); |
| 2272 | } |
| 2273 | |
Thomas Gleixner | 30b4ae8 | 2009-04-04 21:01:01 +0000 | [diff] [blame] | 2274 | static int |
| 2275 | do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info) |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2276 | { |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2277 | struct task_struct *p; |
Thomas Gleixner | 30b4ae8 | 2009-04-04 21:01:01 +0000 | [diff] [blame] | 2278 | int error = -ESRCH; |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2279 | |
Oleg Nesterov | 3547ff3 | 2008-04-30 00:52:51 -0700 | [diff] [blame] | 2280 | rcu_read_lock(); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 2281 | p = find_task_by_vpid(pid); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2282 | if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) { |
Thomas Gleixner | 30b4ae8 | 2009-04-04 21:01:01 +0000 | [diff] [blame] | 2283 | error = check_kill_permission(sig, info, p); |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2284 | /* |
| 2285 | * The null signal is a permissions and process existence |
| 2286 | * probe. No signal is actually delivered. |
| 2287 | */ |
Oleg Nesterov | 4a30deb | 2009-09-23 15:57:00 -0700 | [diff] [blame^] | 2288 | if (!error && sig) { |
| 2289 | error = do_send_sig_info(sig, info, p, false); |
| 2290 | /* |
| 2291 | * If lock_task_sighand() failed we pretend the task |
| 2292 | * dies after receiving the signal. The window is tiny, |
| 2293 | * and the signal is private anyway. |
| 2294 | */ |
| 2295 | if (unlikely(error == -ESRCH)) |
| 2296 | error = 0; |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2297 | } |
| 2298 | } |
Oleg Nesterov | 3547ff3 | 2008-04-30 00:52:51 -0700 | [diff] [blame] | 2299 | rcu_read_unlock(); |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2300 | |
| 2301 | return error; |
| 2302 | } |
| 2303 | |
Thomas Gleixner | 30b4ae8 | 2009-04-04 21:01:01 +0000 | [diff] [blame] | 2304 | static int do_tkill(pid_t tgid, pid_t pid, int sig) |
| 2305 | { |
| 2306 | struct siginfo info; |
| 2307 | |
| 2308 | info.si_signo = sig; |
| 2309 | info.si_errno = 0; |
| 2310 | info.si_code = SI_TKILL; |
| 2311 | info.si_pid = task_tgid_vnr(current); |
| 2312 | info.si_uid = current_uid(); |
| 2313 | |
| 2314 | return do_send_specific(tgid, pid, sig, &info); |
| 2315 | } |
| 2316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | /** |
| 2318 | * sys_tgkill - send signal to one specific thread |
| 2319 | * @tgid: the thread group ID of the thread |
| 2320 | * @pid: the PID of the thread |
| 2321 | * @sig: signal to be sent |
| 2322 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2323 | * This syscall also checks the @tgid and returns -ESRCH even if the PID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | * exists but it's not belonging to the target process anymore. This |
| 2325 | * method solves the problem of threads exiting and PIDs getting reused. |
| 2326 | */ |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 2327 | SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | /* This is only valid for single tasks */ |
| 2330 | if (pid <= 0 || tgid <= 0) |
| 2331 | return -EINVAL; |
| 2332 | |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2333 | return do_tkill(tgid, pid, sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | * Send a signal to only one task, even if it's a CLONE_THREAD task. |
| 2338 | */ |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 2339 | SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | /* This is only valid for single tasks */ |
| 2342 | if (pid <= 0) |
| 2343 | return -EINVAL; |
| 2344 | |
Vadim Lobanov | 6dd69f1 | 2005-10-30 15:02:18 -0800 | [diff] [blame] | 2345 | return do_tkill(0, pid, sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 2348 | SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig, |
| 2349 | siginfo_t __user *, uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | { |
| 2351 | siginfo_t info; |
| 2352 | |
| 2353 | if (copy_from_user(&info, uinfo, sizeof(siginfo_t))) |
| 2354 | return -EFAULT; |
| 2355 | |
| 2356 | /* Not even root can pretend to send signals from the kernel. |
| 2357 | Nor can they impersonate a kill(), which adds source info. */ |
| 2358 | if (info.si_code >= 0) |
| 2359 | return -EPERM; |
| 2360 | info.si_signo = sig; |
| 2361 | |
| 2362 | /* POSIX.1b doesn't mention process groups. */ |
| 2363 | return kill_proc_info(sig, &info, pid); |
| 2364 | } |
| 2365 | |
Thomas Gleixner | 62ab450 | 2009-04-04 21:01:06 +0000 | [diff] [blame] | 2366 | long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info) |
| 2367 | { |
| 2368 | /* This is only valid for single tasks */ |
| 2369 | if (pid <= 0 || tgid <= 0) |
| 2370 | return -EINVAL; |
| 2371 | |
| 2372 | /* Not even root can pretend to send signals from the kernel. |
| 2373 | Nor can they impersonate a kill(), which adds source info. */ |
| 2374 | if (info->si_code >= 0) |
| 2375 | return -EPERM; |
| 2376 | info->si_signo = sig; |
| 2377 | |
| 2378 | return do_send_specific(tgid, pid, sig, info); |
| 2379 | } |
| 2380 | |
| 2381 | SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig, |
| 2382 | siginfo_t __user *, uinfo) |
| 2383 | { |
| 2384 | siginfo_t info; |
| 2385 | |
| 2386 | if (copy_from_user(&info, uinfo, sizeof(siginfo_t))) |
| 2387 | return -EFAULT; |
| 2388 | |
| 2389 | return do_rt_tgsigqueueinfo(tgid, pid, sig, &info); |
| 2390 | } |
| 2391 | |
Oleg Nesterov | 88531f7 | 2006-03-28 16:11:24 -0800 | [diff] [blame] | 2392 | int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | { |
Pavel Emelyanov | 93585ee | 2008-04-30 00:52:39 -0700 | [diff] [blame] | 2394 | struct task_struct *t = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | struct k_sigaction *k; |
George Anzinger | 71fabd5 | 2006-01-08 01:02:48 -0800 | [diff] [blame] | 2396 | sigset_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 2398 | if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | return -EINVAL; |
| 2400 | |
Pavel Emelyanov | 93585ee | 2008-04-30 00:52:39 -0700 | [diff] [blame] | 2401 | k = &t->sighand->action[sig-1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | |
| 2403 | spin_lock_irq(¤t->sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if (oact) |
| 2405 | *oact = *k; |
| 2406 | |
| 2407 | if (act) { |
Oleg Nesterov | 9ac95f2 | 2006-02-09 22:41:50 +0300 | [diff] [blame] | 2408 | sigdelsetmask(&act->sa.sa_mask, |
| 2409 | sigmask(SIGKILL) | sigmask(SIGSTOP)); |
Oleg Nesterov | 88531f7 | 2006-03-28 16:11:24 -0800 | [diff] [blame] | 2410 | *k = *act; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | /* |
| 2412 | * POSIX 3.3.1.3: |
| 2413 | * "Setting a signal action to SIG_IGN for a signal that is |
| 2414 | * pending shall cause the pending signal to be discarded, |
| 2415 | * whether or not it is blocked." |
| 2416 | * |
| 2417 | * "Setting a signal action to SIG_DFL for a signal that is |
| 2418 | * pending and whose default action is to ignore the signal |
| 2419 | * (for example, SIGCHLD), shall cause the pending signal to |
| 2420 | * be discarded, whether or not it is blocked" |
| 2421 | */ |
Roland McGrath | 35de254 | 2008-07-25 19:45:51 -0700 | [diff] [blame] | 2422 | if (sig_handler_ignored(sig_handler(t, sig), sig)) { |
George Anzinger | 71fabd5 | 2006-01-08 01:02:48 -0800 | [diff] [blame] | 2423 | sigemptyset(&mask); |
| 2424 | sigaddset(&mask, sig); |
| 2425 | rm_from_queue_full(&mask, &t->signal->shared_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | do { |
George Anzinger | 71fabd5 | 2006-01-08 01:02:48 -0800 | [diff] [blame] | 2427 | rm_from_queue_full(&mask, &t->pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | t = next_thread(t); |
| 2429 | } while (t != current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | spin_unlock_irq(¤t->sighand->siglock); |
| 2434 | return 0; |
| 2435 | } |
| 2436 | |
| 2437 | int |
| 2438 | do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp) |
| 2439 | { |
| 2440 | stack_t oss; |
| 2441 | int error; |
| 2442 | |
Linus Torvalds | 0083fc2 | 2009-08-01 10:34:56 -0700 | [diff] [blame] | 2443 | oss.ss_sp = (void __user *) current->sas_ss_sp; |
| 2444 | oss.ss_size = current->sas_ss_size; |
| 2445 | oss.ss_flags = sas_ss_flags(sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | |
| 2447 | if (uss) { |
| 2448 | void __user *ss_sp; |
| 2449 | size_t ss_size; |
| 2450 | int ss_flags; |
| 2451 | |
| 2452 | error = -EFAULT; |
Linus Torvalds | 0dd8486 | 2009-08-01 11:18:56 -0700 | [diff] [blame] | 2453 | if (!access_ok(VERIFY_READ, uss, sizeof(*uss))) |
| 2454 | goto out; |
| 2455 | error = __get_user(ss_sp, &uss->ss_sp) | |
| 2456 | __get_user(ss_flags, &uss->ss_flags) | |
| 2457 | __get_user(ss_size, &uss->ss_size); |
| 2458 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | goto out; |
| 2460 | |
| 2461 | error = -EPERM; |
| 2462 | if (on_sig_stack(sp)) |
| 2463 | goto out; |
| 2464 | |
| 2465 | error = -EINVAL; |
| 2466 | /* |
| 2467 | * |
| 2468 | * Note - this code used to test ss_flags incorrectly |
| 2469 | * old code may have been written using ss_flags==0 |
| 2470 | * to mean ss_flags==SS_ONSTACK (as this was the only |
| 2471 | * way that worked) - this fix preserves that older |
| 2472 | * mechanism |
| 2473 | */ |
| 2474 | if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0) |
| 2475 | goto out; |
| 2476 | |
| 2477 | if (ss_flags == SS_DISABLE) { |
| 2478 | ss_size = 0; |
| 2479 | ss_sp = NULL; |
| 2480 | } else { |
| 2481 | error = -ENOMEM; |
| 2482 | if (ss_size < MINSIGSTKSZ) |
| 2483 | goto out; |
| 2484 | } |
| 2485 | |
| 2486 | current->sas_ss_sp = (unsigned long) ss_sp; |
| 2487 | current->sas_ss_size = ss_size; |
| 2488 | } |
| 2489 | |
Linus Torvalds | 0083fc2 | 2009-08-01 10:34:56 -0700 | [diff] [blame] | 2490 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | if (uoss) { |
| 2492 | error = -EFAULT; |
Linus Torvalds | 0083fc2 | 2009-08-01 10:34:56 -0700 | [diff] [blame] | 2493 | if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | goto out; |
Linus Torvalds | 0083fc2 | 2009-08-01 10:34:56 -0700 | [diff] [blame] | 2495 | error = __put_user(oss.ss_sp, &uoss->ss_sp) | |
| 2496 | __put_user(oss.ss_size, &uoss->ss_size) | |
| 2497 | __put_user(oss.ss_flags, &uoss->ss_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | out: |
| 2501 | return error; |
| 2502 | } |
| 2503 | |
| 2504 | #ifdef __ARCH_WANT_SYS_SIGPENDING |
| 2505 | |
Heiko Carstens | b290ebe | 2009-01-14 14:14:06 +0100 | [diff] [blame] | 2506 | SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | { |
| 2508 | return do_sigpending(set, sizeof(*set)); |
| 2509 | } |
| 2510 | |
| 2511 | #endif |
| 2512 | |
| 2513 | #ifdef __ARCH_WANT_SYS_SIGPROCMASK |
| 2514 | /* Some platforms have their own version with special arguments others |
| 2515 | support only sys_rt_sigprocmask. */ |
| 2516 | |
Heiko Carstens | b290ebe | 2009-01-14 14:14:06 +0100 | [diff] [blame] | 2517 | SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set, |
| 2518 | old_sigset_t __user *, oset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | { |
| 2520 | int error; |
| 2521 | old_sigset_t old_set, new_set; |
| 2522 | |
| 2523 | if (set) { |
| 2524 | error = -EFAULT; |
| 2525 | if (copy_from_user(&new_set, set, sizeof(*set))) |
| 2526 | goto out; |
| 2527 | new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP)); |
| 2528 | |
| 2529 | spin_lock_irq(¤t->sighand->siglock); |
| 2530 | old_set = current->blocked.sig[0]; |
| 2531 | |
| 2532 | error = 0; |
| 2533 | switch (how) { |
| 2534 | default: |
| 2535 | error = -EINVAL; |
| 2536 | break; |
| 2537 | case SIG_BLOCK: |
| 2538 | sigaddsetmask(¤t->blocked, new_set); |
| 2539 | break; |
| 2540 | case SIG_UNBLOCK: |
| 2541 | sigdelsetmask(¤t->blocked, new_set); |
| 2542 | break; |
| 2543 | case SIG_SETMASK: |
| 2544 | current->blocked.sig[0] = new_set; |
| 2545 | break; |
| 2546 | } |
| 2547 | |
| 2548 | recalc_sigpending(); |
| 2549 | spin_unlock_irq(¤t->sighand->siglock); |
| 2550 | if (error) |
| 2551 | goto out; |
| 2552 | if (oset) |
| 2553 | goto set_old; |
| 2554 | } else if (oset) { |
| 2555 | old_set = current->blocked.sig[0]; |
| 2556 | set_old: |
| 2557 | error = -EFAULT; |
| 2558 | if (copy_to_user(oset, &old_set, sizeof(*oset))) |
| 2559 | goto out; |
| 2560 | } |
| 2561 | error = 0; |
| 2562 | out: |
| 2563 | return error; |
| 2564 | } |
| 2565 | #endif /* __ARCH_WANT_SYS_SIGPROCMASK */ |
| 2566 | |
| 2567 | #ifdef __ARCH_WANT_SYS_RT_SIGACTION |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 2568 | SYSCALL_DEFINE4(rt_sigaction, int, sig, |
| 2569 | const struct sigaction __user *, act, |
| 2570 | struct sigaction __user *, oact, |
| 2571 | size_t, sigsetsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | { |
| 2573 | struct k_sigaction new_sa, old_sa; |
| 2574 | int ret = -EINVAL; |
| 2575 | |
| 2576 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 2577 | if (sigsetsize != sizeof(sigset_t)) |
| 2578 | goto out; |
| 2579 | |
| 2580 | if (act) { |
| 2581 | if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa))) |
| 2582 | return -EFAULT; |
| 2583 | } |
| 2584 | |
| 2585 | ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL); |
| 2586 | |
| 2587 | if (!ret && oact) { |
| 2588 | if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa))) |
| 2589 | return -EFAULT; |
| 2590 | } |
| 2591 | out: |
| 2592 | return ret; |
| 2593 | } |
| 2594 | #endif /* __ARCH_WANT_SYS_RT_SIGACTION */ |
| 2595 | |
| 2596 | #ifdef __ARCH_WANT_SYS_SGETMASK |
| 2597 | |
| 2598 | /* |
| 2599 | * For backwards compatibility. Functionality superseded by sigprocmask. |
| 2600 | */ |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 2601 | SYSCALL_DEFINE0(sgetmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | { |
| 2603 | /* SMP safe */ |
| 2604 | return current->blocked.sig[0]; |
| 2605 | } |
| 2606 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 2607 | SYSCALL_DEFINE1(ssetmask, int, newmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | { |
| 2609 | int old; |
| 2610 | |
| 2611 | spin_lock_irq(¤t->sighand->siglock); |
| 2612 | old = current->blocked.sig[0]; |
| 2613 | |
| 2614 | siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)| |
| 2615 | sigmask(SIGSTOP))); |
| 2616 | recalc_sigpending(); |
| 2617 | spin_unlock_irq(¤t->sighand->siglock); |
| 2618 | |
| 2619 | return old; |
| 2620 | } |
| 2621 | #endif /* __ARCH_WANT_SGETMASK */ |
| 2622 | |
| 2623 | #ifdef __ARCH_WANT_SYS_SIGNAL |
| 2624 | /* |
| 2625 | * For backwards compatibility. Functionality superseded by sigaction. |
| 2626 | */ |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 2627 | SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | { |
| 2629 | struct k_sigaction new_sa, old_sa; |
| 2630 | int ret; |
| 2631 | |
| 2632 | new_sa.sa.sa_handler = handler; |
| 2633 | new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK; |
Oleg Nesterov | c70d3d70 | 2006-02-09 22:41:41 +0300 | [diff] [blame] | 2634 | sigemptyset(&new_sa.sa.sa_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | |
| 2636 | ret = do_sigaction(sig, &new_sa, &old_sa); |
| 2637 | |
| 2638 | return ret ? ret : (unsigned long)old_sa.sa.sa_handler; |
| 2639 | } |
| 2640 | #endif /* __ARCH_WANT_SYS_SIGNAL */ |
| 2641 | |
| 2642 | #ifdef __ARCH_WANT_SYS_PAUSE |
| 2643 | |
Heiko Carstens | a5f8fa9 | 2009-01-14 14:14:11 +0100 | [diff] [blame] | 2644 | SYSCALL_DEFINE0(pause) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | { |
| 2646 | current->state = TASK_INTERRUPTIBLE; |
| 2647 | schedule(); |
| 2648 | return -ERESTARTNOHAND; |
| 2649 | } |
| 2650 | |
| 2651 | #endif |
| 2652 | |
David Woodhouse | 150256d | 2006-01-18 17:43:57 -0800 | [diff] [blame] | 2653 | #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 2654 | SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize) |
David Woodhouse | 150256d | 2006-01-18 17:43:57 -0800 | [diff] [blame] | 2655 | { |
| 2656 | sigset_t newset; |
| 2657 | |
| 2658 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 2659 | if (sigsetsize != sizeof(sigset_t)) |
| 2660 | return -EINVAL; |
| 2661 | |
| 2662 | if (copy_from_user(&newset, unewset, sizeof(newset))) |
| 2663 | return -EFAULT; |
| 2664 | sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 2665 | |
| 2666 | spin_lock_irq(¤t->sighand->siglock); |
| 2667 | current->saved_sigmask = current->blocked; |
| 2668 | current->blocked = newset; |
| 2669 | recalc_sigpending(); |
| 2670 | spin_unlock_irq(¤t->sighand->siglock); |
| 2671 | |
| 2672 | current->state = TASK_INTERRUPTIBLE; |
| 2673 | schedule(); |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 2674 | set_restore_sigmask(); |
David Woodhouse | 150256d | 2006-01-18 17:43:57 -0800 | [diff] [blame] | 2675 | return -ERESTARTNOHAND; |
| 2676 | } |
| 2677 | #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */ |
| 2678 | |
David Howells | f269fdd | 2006-09-27 01:50:23 -0700 | [diff] [blame] | 2679 | __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma) |
| 2680 | { |
| 2681 | return NULL; |
| 2682 | } |
| 2683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | void __init signals_init(void) |
| 2685 | { |
Christoph Lameter | 0a31bd5 | 2007-05-06 14:49:57 -0700 | [diff] [blame] | 2686 | sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | } |