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