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