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