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