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