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