File: /usr/src/linux/kernel/signal.c

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     
9     #include <linux/config.h>
10     #include <linux/slab.h>
11     #include <linux/module.h>
12     #include <linux/unistd.h>
13     #include <linux/smp_lock.h>
14     #include <linux/init.h>
15     #include <linux/sched.h>
16     
17     #include <asm/uaccess.h>
18     
19     /*
20      * SLAB caches for signal bits.
21      */
22     
23     #define DEBUG_SIG 0
24     
25     #if DEBUG_SIG
26     #define SIG_SLAB_DEBUG	(SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
27     #else
28     #define SIG_SLAB_DEBUG	0
29     #endif
30     
31     static kmem_cache_t *sigqueue_cachep;
32     
33     atomic_t nr_queued_signals;
34     int max_queued_signals = 1024;
35     
36     void __init signals_init(void)
37     {
38     	sigqueue_cachep =
39     		kmem_cache_create("sigqueue",
40     				  sizeof(struct sigqueue),
41     				  __alignof__(struct sigqueue),
42     				  SIG_SLAB_DEBUG, NULL, NULL);
43     	if (!sigqueue_cachep)
44     		panic("signals_init(): cannot create sigqueue SLAB cache");
45     }
46     
47     
48     /* Given the mask, find the first available signal that should be serviced. */
49     
50     static int
51     next_signal(struct task_struct *tsk, sigset_t *mask)
52     {
53     	unsigned long i, *s, *m, x;
54     	int sig = 0;
55     	
56     	s = tsk->pending.signal.sig;
57     	m = mask->sig;
58     	switch (_NSIG_WORDS) {
59     	default:
60     		for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
61     			if ((x = *s &~ *m) != 0) {
62     				sig = ffz(~x) + i*_NSIG_BPW + 1;
63     				break;
64     			}
65     		break;
66     
67     	case 2: if ((x = s[0] &~ m[0]) != 0)
68     			sig = 1;
69     		else if ((x = s[1] &~ m[1]) != 0)
70     			sig = _NSIG_BPW + 1;
71     		else
72     			break;
73     		sig += ffz(~x);
74     		break;
75     
76     	case 1: if ((x = *s &~ *m) != 0)
77     			sig = ffz(~x) + 1;
78     		break;
79     	}
80     	
81     	return sig;
82     }
83     
84     static void flush_sigqueue(struct sigpending *queue)
85     {
86     	struct sigqueue *q, *n;
87     
88     	sigemptyset(&queue->signal);
89     	q = queue->head;
90     	queue->head = NULL;
91     	queue->tail = &queue->head;
92     
93     	while (q) {
94     		n = q->next;
95     		kmem_cache_free(sigqueue_cachep, q);
96     		atomic_dec(&nr_queued_signals);
97     		q = n;
98     	}
99     }
100     
101     /*
102      * Flush all pending signals for a task.
103      */
104     
105     void
106     flush_signals(struct task_struct *t)
107     {
108     	t->sigpending = 0;
109     	flush_sigqueue(&t->pending);
110     }
111     
112     void exit_sighand(struct task_struct *tsk)
113     {
114     	struct signal_struct * sig = tsk->sig;
115     
116     	spin_lock_irq(&tsk->sigmask_lock);
117     	if (sig) {
118     		tsk->sig = NULL;
119     		if (atomic_dec_and_test(&sig->count))
120     			kmem_cache_free(sigact_cachep, sig);
121     	}
122     	tsk->sigpending = 0;
123     	flush_sigqueue(&tsk->pending);
124     	spin_unlock_irq(&tsk->sigmask_lock);
125     }
126     
127     /*
128      * Flush all handlers for a task.
129      */
130     
131     void
132     flush_signal_handlers(struct task_struct *t)
133     {
134     	int i;
135     	struct k_sigaction *ka = &t->sig->action[0];
136     	for (i = _NSIG ; i != 0 ; i--) {
137     		if (ka->sa.sa_handler != SIG_IGN)
138     			ka->sa.sa_handler = SIG_DFL;
139     		ka->sa.sa_flags = 0;
140     		sigemptyset(&ka->sa.sa_mask);
141     		ka++;
142     	}
143     }
144     
145     /* Notify the system that a driver wants to block all signals for this
146      * process, and wants to be notified if any signals at all were to be
147      * sent/acted upon.  If the notifier routine returns non-zero, then the
148      * signal will be acted upon after all.  If the notifier routine returns 0,
149      * then then signal will be blocked.  Only one block per process is
150      * allowed.  priv is a pointer to private data that the notifier routine
151      * can use to determine if the signal should be blocked or not.  */
152     
153     void
154     block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
155     {
156     	unsigned long flags;
157     
158     	spin_lock_irqsave(&current->sigmask_lock, flags);
159     	current->notifier_mask = mask;
160     	current->notifier_data = priv;
161     	current->notifier = notifier;
162     	spin_unlock_irqrestore(&current->sigmask_lock, flags);
163     }
164     
165     /* Notify the system that blocking has ended. */
166     
167     void
168     unblock_all_signals(void)
169     {
170     	unsigned long flags;
171     
172     	spin_lock_irqsave(&current->sigmask_lock, flags);
173     	current->notifier = NULL;
174     	current->notifier_data = NULL;
175     	recalc_sigpending(current);
176     	spin_unlock_irqrestore(&current->sigmask_lock, flags);
177     }
178     
179     static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
180     {
181     	if (sigismember(&list->signal, sig)) {
182     		/* Collect the siginfo appropriate to this signal.  */
183     		struct sigqueue *q, **pp;
184     		pp = &list->head;
185     		while ((q = *pp) != NULL) {
186     			if (q->info.si_signo == sig)
187     				goto found_it;
188     			pp = &q->next;
189     		}
190     
191     		/* Ok, it wasn't in the queue.  We must have
192     		   been out of queue space.  So zero out the
193     		   info.  */
194     		sigdelset(&list->signal, sig);
195     		info->si_signo = sig;
196     		info->si_errno = 0;
197     		info->si_code = 0;
198     		info->si_pid = 0;
199     		info->si_uid = 0;
200     		return 1;
201     
202     found_it:
203     		if ((*pp = q->next) == NULL)
204     			list->tail = pp;
205     
206     		/* Copy the sigqueue information and free the queue entry */
207     		copy_siginfo(info, &q->info);
208     		kmem_cache_free(sigqueue_cachep,q);
209     		atomic_dec(&nr_queued_signals);
210     
211     		/* Non-RT signals can exist multiple times.. */
212     		if (sig >= SIGRTMIN) {
213     			while ((q = *pp) != NULL) {
214     				if (q->info.si_signo == sig)
215     					goto found_another;
216     				pp = &q->next;
217     			}
218     		}
219     
220     		sigdelset(&list->signal, sig);
221     found_another:
222     		return 1;
223     	}
224     	return 0;
225     }
226     
227     /*
228      * Dequeue a signal and return the element to the caller, which is 
229      * expected to free it.
230      *
231      * All callers must be holding current->sigmask_lock.
232      */
233     
234     int
235     dequeue_signal(sigset_t *mask, siginfo_t *info)
236     {
237     	int sig = 0;
238     
239     #if DEBUG_SIG
240     printk("SIG dequeue (%s:%d): %d ", current->comm, current->pid,
241     	signal_pending(current));
242     #endif
243     
244     	sig = next_signal(current, mask);
245     	if (sig) {
246     		if (current->notifier) {
247     			if (sigismember(current->notifier_mask, sig)) {
248     				if (!(current->notifier)(current->notifier_data)) {
249     					current->sigpending = 0;
250     					return 0;
251     				}
252     			}
253     		}
254     
255     		if (!collect_signal(sig, &current->pending, info))
256     			sig = 0;
257     				
258     		/* XXX: Once POSIX.1b timers are in, if si_code == SI_TIMER,
259     		   we need to xchg out the timer overrun values.  */
260     	}
261     	recalc_sigpending(current);
262     
263     #if DEBUG_SIG
264     printk(" %d -> %d\n", signal_pending(current), sig);
265     #endif
266     
267     	return sig;
268     }
269     
270     static int rm_from_queue(int sig, struct sigpending *s)
271     {
272     	struct sigqueue *q, **pp;
273     
274     	if (!sigismember(&s->signal, sig))
275     		return 0;
276     
277     	sigdelset(&s->signal, sig);
278     
279     	pp = &s->head;
280     
281     	while ((q = *pp) != NULL) {
282     		if (q->info.si_signo == sig) {
283     			if ((*pp = q->next) == NULL)
284     				s->tail = pp;
285     			kmem_cache_free(sigqueue_cachep,q);
286     			atomic_dec(&nr_queued_signals);
287     			continue;
288     		}
289     		pp = &q->next;
290     	}
291     	return 1;
292     }
293     
294     /*
295      * Remove signal sig from t->pending.
296      * Returns 1 if sig was found.
297      *
298      * All callers must be holding t->sigmask_lock.
299      */
300     static int rm_sig_from_queue(int sig, struct task_struct *t)
301     {
302     	return rm_from_queue(sig, &t->pending);
303     }
304     
305     /*
306      * Bad permissions for sending the signal
307      */
308     int bad_signal(int sig, struct siginfo *info, struct task_struct *t)
309     {
310     	return (!info || ((unsigned long)info != 1 && SI_FROMUSER(info)))
311     	    && ((sig != SIGCONT) || (current->session != t->session))
312     	    && (current->euid ^ t->suid) && (current->euid ^ t->uid)
313     	    && (current->uid ^ t->suid) && (current->uid ^ t->uid)
314     	    && !capable(CAP_KILL);
315     }
316     
317     /*
318      * Signal type:
319      *    < 0 : global action (kill - spread to all non-blocked threads)
320      *    = 0 : ignored
321      *    > 0 : wake up.
322      */
323     static int signal_type(int sig, struct signal_struct *signals)
324     {
325     	unsigned long handler;
326     
327     	if (!signals)
328     		return 0;
329     	
330     	handler = (unsigned long) signals->action[sig-1].sa.sa_handler;
331     	if (handler > 1)
332     		return 1;
333     
334     	/* "Ignore" handler.. Illogical, but that has an implicit handler for SIGCHLD */
335     	if (handler == 1)
336     		return sig == SIGCHLD;
337     
338     	/* Default handler. Normally lethal, but.. */
339     	switch (sig) {
340     
341     	/* Ignored */
342     	case SIGCONT: case SIGWINCH:
343     	case SIGCHLD: case SIGURG:
344     		return 0;
345     
346     	/* Implicit behaviour */
347     	case SIGTSTP: case SIGTTIN: case SIGTTOU:
348     		return 1;
349     
350     	/* Implicit actions (kill or do special stuff) */
351     	default:
352     		return -1;
353     	}
354     }
355     		
356     
357     /*
358      * Determine whether a signal should be posted or not.
359      *
360      * Signals with SIG_IGN can be ignored, except for the
361      * special case of a SIGCHLD. 
362      *
363      * Some signals with SIG_DFL default to a non-action.
364      */
365     static int ignored_signal(int sig, struct task_struct *t)
366     {
367     	/* Don't ignore traced or blocked signals */
368     	if ((t->ptrace & PT_PTRACED) || sigismember(&t->blocked, sig))
369     		return 0;
370     
371     	return signal_type(sig, t->sig) == 0;
372     }
373     
374     /*
375      * Handle TASK_STOPPED cases etc implicit behaviour
376      * of certain magical signals.
377      *
378      * SIGKILL gets spread out to every thread. 
379      */
380     static void handle_stop_signal(int sig, struct task_struct *t)
381     {
382     	switch (sig) {
383     	case SIGKILL: case SIGCONT:
384     		/* Wake up the process if stopped.  */
385     		if (t->state == TASK_STOPPED)
386     			wake_up_process(t);
387     		t->exit_code = 0;
388     		rm_sig_from_queue(SIGSTOP, t);
389     		rm_sig_from_queue(SIGTSTP, t);
390     		rm_sig_from_queue(SIGTTOU, t);
391     		rm_sig_from_queue(SIGTTIN, t);
392     		break;
393     
394     	case SIGSTOP: case SIGTSTP:
395     	case SIGTTIN: case SIGTTOU:
396     		/* If we're stopping again, cancel SIGCONT */
397     		rm_sig_from_queue(SIGCONT, t);
398     		break;
399     	}
400     }
401     
402     static int send_signal(int sig, struct siginfo *info, struct sigpending *signals)
403     {
404     	struct sigqueue * q = NULL;
405     
406     	/* Real-time signals must be queued if sent by sigqueue, or
407     	   some other real-time mechanism.  It is implementation
408     	   defined whether kill() does so.  We attempt to do so, on
409     	   the principle of least surprise, but since kill is not
410     	   allowed to fail with EAGAIN when low on memory we just
411     	   make sure at least one signal gets delivered and don't
412     	   pass on the info struct.  */
413     
414     	if (atomic_read(&nr_queued_signals) < max_queued_signals) {
415     		q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
416     	}
417     
418     	if (q) {
419     		atomic_inc(&nr_queued_signals);
420     		q->next = NULL;
421     		*signals->tail = q;
422     		signals->tail = &q->next;
423     		switch ((unsigned long) info) {
424     			case 0:
425     				q->info.si_signo = sig;
426     				q->info.si_errno = 0;
427     				q->info.si_code = SI_USER;
428     				q->info.si_pid = current->pid;
429     				q->info.si_uid = current->uid;
430     				break;
431     			case 1:
432     				q->info.si_signo = sig;
433     				q->info.si_errno = 0;
434     				q->info.si_code = SI_KERNEL;
435     				q->info.si_pid = 0;
436     				q->info.si_uid = 0;
437     				break;
438     			default:
439     				copy_siginfo(&q->info, info);
440     				break;
441     		}
442     	} else if (sig >= SIGRTMIN && info && (unsigned long)info != 1
443     		   && info->si_code != SI_USER) {
444     		/*
445     		 * Queue overflow, abort.  We may abort if the signal was rt
446     		 * and sent by user using something other than kill().
447     		 */
448     		return -EAGAIN;
449     	}
450     
451     	sigaddset(&signals->signal, sig);
452     	return 0;
453     }
454     
455     /*
456      * Tell a process that it has a new active signal..
457      *
458      * NOTE! we rely on the previous spin_lock to
459      * lock interrupts for us! We can only be called with
460      * "sigmask_lock" held, and the local interrupt must
461      * have been disabled when that got acquired!
462      *
463      * No need to set need_resched since signal event passing
464      * goes through ->blocked
465      */
466     static inline void signal_wake_up(struct task_struct *t)
467     {
468     	t->sigpending = 1;
469     
470     #ifdef CONFIG_SMP
471     	/*
472     	 * If the task is running on a different CPU 
473     	 * force a reschedule on the other CPU to make
474     	 * it notice the new signal quickly.
475     	 *
476     	 * The code below is a tad loose and might occasionally
477     	 * kick the wrong CPU if we catch the process in the
478     	 * process of changing - but no harm is done by that
479     	 * other than doing an extra (lightweight) IPI interrupt.
480     	 */
481     	spin_lock(&runqueue_lock);
482     	if (t->has_cpu && t->processor != smp_processor_id())
483     		smp_send_reschedule(t->processor);
484     	spin_unlock(&runqueue_lock);
485     #endif /* CONFIG_SMP */
486     
487     	if (t->state & TASK_INTERRUPTIBLE) {
488     		wake_up_process(t);
489     		return;
490     	}
491     }
492     
493     static int deliver_signal(int sig, struct siginfo *info, struct task_struct *t)
494     {
495     	int retval = send_signal(sig, info, &t->pending);
496     
497     	if (!retval && !sigismember(&t->blocked, sig))
498     		signal_wake_up(t);
499     
500     	return retval;
501     }
502     
503     int
504     send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
505     {
506     	unsigned long flags;
507     	int ret;
508     
509     
510     #if DEBUG_SIG
511     printk("SIG queue (%s:%d): %d ", t->comm, t->pid, sig);
512     #endif
513     
514     	ret = -EINVAL;
515     	if (sig < 0 || sig > _NSIG)
516     		goto out_nolock;
517     	/* The somewhat baroque permissions check... */
518     	ret = -EPERM;
519     	if (bad_signal(sig, info, t))
520     		goto out_nolock;
521     
522     	/* The null signal is a permissions and process existance probe.
523     	   No signal is actually delivered.  Same goes for zombies. */
524     	ret = 0;
525     	if (!sig || !t->sig)
526     		goto out_nolock;
527     
528     	spin_lock_irqsave(&t->sigmask_lock, flags);
529     	handle_stop_signal(sig, t);
530     
531     	/* Optimize away the signal, if it's a signal that can be
532     	   handled immediately (ie non-blocked and untraced) and
533     	   that is ignored (either explicitly or by default).  */
534     
535     	if (ignored_signal(sig, t))
536     		goto out;
537     
538     	/* Support queueing exactly one non-rt signal, so that we
539     	   can get more detailed information about the cause of
540     	   the signal. */
541     	if (sig < SIGRTMIN && sigismember(&t->pending.signal, sig))
542     		goto out;
543     
544     	ret = deliver_signal(sig, info, t);
545     out:
546     	spin_unlock_irqrestore(&t->sigmask_lock, flags);
547     out_nolock:
548     #if DEBUG_SIG
549     printk(" %d -> %d\n", signal_pending(t), ret);
550     #endif
551     
552     	return ret;
553     }
554     
555     /*
556      * Force a signal that the process can't ignore: if necessary
557      * we unblock the signal and change any SIG_IGN to SIG_DFL.
558      */
559     
560     int
561     force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
562     {
563     	unsigned long int flags;
564     
565     	spin_lock_irqsave(&t->sigmask_lock, flags);
566     	if (t->sig == NULL) {
567     		spin_unlock_irqrestore(&t->sigmask_lock, flags);
568     		return -ESRCH;
569     	}
570     
571     	if (t->sig->action[sig-1].sa.sa_handler == SIG_IGN)
572     		t->sig->action[sig-1].sa.sa_handler = SIG_DFL;
573     	sigdelset(&t->blocked, sig);
574     	recalc_sigpending(t);
575     	spin_unlock_irqrestore(&t->sigmask_lock, flags);
576     
577     	return send_sig_info(sig, info, t);
578     }
579     
580     /*
581      * kill_pg_info() sends a signal to a process group: this is what the tty
582      * control characters do (^C, ^Z etc)
583      */
584     
585     int
586     kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
587     {
588     	int retval = -EINVAL;
589     	if (pgrp > 0) {
590     		struct task_struct *p;
591     
592     		retval = -ESRCH;
593     		read_lock(&tasklist_lock);
594     		for_each_task(p) {
595     			if (p->pgrp == pgrp) {
596     				int err = send_sig_info(sig, info, p);
597     				if (retval)
598     					retval = err;
599     			}
600     		}
601     		read_unlock(&tasklist_lock);
602     	}
603     	return retval;
604     }
605     
606     /*
607      * kill_sl_info() sends a signal to the session leader: this is used
608      * to send SIGHUP to the controlling process of a terminal when
609      * the connection is lost.
610      */
611     
612     int
613     kill_sl_info(int sig, struct siginfo *info, pid_t sess)
614     {
615     	int retval = -EINVAL;
616     	if (sess > 0) {
617     		struct task_struct *p;
618     
619     		retval = -ESRCH;
620     		read_lock(&tasklist_lock);
621     		for_each_task(p) {
622     			if (p->leader && p->session == sess) {
623     				int err = send_sig_info(sig, info, p);
624     				if (retval)
625     					retval = err;
626     			}
627     		}
628     		read_unlock(&tasklist_lock);
629     	}
630     	return retval;
631     }
632     
633     inline int
634     kill_proc_info(int sig, struct siginfo *info, pid_t pid)
635     {
636     	int error;
637     	struct task_struct *p;
638     
639     	read_lock(&tasklist_lock);
640     	p = find_task_by_pid(pid);
641     	error = -ESRCH;
642     	if (p)
643     		error = send_sig_info(sig, info, p);
644     	read_unlock(&tasklist_lock);
645     	return error;
646     }
647     
648     
649     /*
650      * kill_something_info() interprets pid in interesting ways just like kill(2).
651      *
652      * POSIX specifies that kill(-1,sig) is unspecified, but what we have
653      * is probably wrong.  Should make it like BSD or SYSV.
654      */
655     
656     static int kill_something_info(int sig, struct siginfo *info, int pid)
657     {
658     	if (!pid) {
659     		return kill_pg_info(sig, info, current->pgrp);
660     	} else if (pid == -1) {
661     		int retval = 0, count = 0;
662     		struct task_struct * p;
663     
664     		read_lock(&tasklist_lock);
665     		for_each_task(p) {
666     			if (p->pid > 1 && p != current) {
667     				int err = send_sig_info(sig, info, p);
668     				++count;
669     				if (err != -EPERM)
670     					retval = err;
671     			}
672     		}
673     		read_unlock(&tasklist_lock);
674     		return count ? retval : -ESRCH;
675     	} else if (pid < 0) {
676     		return kill_pg_info(sig, info, -pid);
677     	} else {
678     		return kill_proc_info(sig, info, pid);
679     	}
680     }
681     
682     /*
683      * These are for backward compatibility with the rest of the kernel source.
684      */
685     
686     int
687     send_sig(int sig, struct task_struct *p, int priv)
688     {
689     	return send_sig_info(sig, (void*)(long)(priv != 0), p);
690     }
691     
692     void
693     force_sig(int sig, struct task_struct *p)
694     {
695     	force_sig_info(sig, (void*)1L, p);
696     }
697     
698     int
699     kill_pg(pid_t pgrp, int sig, int priv)
700     {
701     	return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
702     }
703     
704     int
705     kill_sl(pid_t sess, int sig, int priv)
706     {
707     	return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
708     }
709     
710     int
711     kill_proc(pid_t pid, int sig, int priv)
712     {
713     	return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
714     }
715     
716     /*
717      * Joy. Or not. Pthread wants us to wake up every thread
718      * in our parent group.
719      */
720     static void wake_up_parent(struct task_struct *parent)
721     {
722     	struct task_struct *tsk = parent;
723     
724     	do {
725     		wake_up_interruptible(&tsk->wait_chldexit);
726     		tsk = next_thread(tsk);
727     	} while (tsk != parent);
728     }
729     
730     /*
731      * Let a parent know about a status change of a child.
732      */
733     
734     void do_notify_parent(struct task_struct *tsk, int sig)
735     {
736     	struct siginfo info;
737     	int why, status;
738     
739     	info.si_signo = sig;
740     	info.si_errno = 0;
741     	info.si_pid = tsk->pid;
742     	info.si_uid = tsk->uid;
743     
744     	/* FIXME: find out whether or not this is supposed to be c*time. */
745     	info.si_utime = tsk->times.tms_utime;
746     	info.si_stime = tsk->times.tms_stime;
747     
748     	status = tsk->exit_code & 0x7f;
749     	why = SI_KERNEL;	/* shouldn't happen */
750     	switch (tsk->state) {
751     	case TASK_STOPPED:
752     		/* FIXME -- can we deduce CLD_TRAPPED or CLD_CONTINUED? */
753     		if (tsk->ptrace & PT_PTRACED)
754     			why = CLD_TRAPPED;
755     		else
756     			why = CLD_STOPPED;
757     		break;
758     
759     	default:
760     		if (tsk->exit_code & 0x80)
761     			why = CLD_DUMPED;
762     		else if (tsk->exit_code & 0x7f)
763     			why = CLD_KILLED;
764     		else {
765     			why = CLD_EXITED;
766     			status = tsk->exit_code >> 8;
767     		}
768     		break;
769     	}
770     	info.si_code = why;
771     	info.si_status = status;
772     
773     	send_sig_info(sig, &info, tsk->p_pptr);
774     	wake_up_parent(tsk->p_pptr);
775     }
776     
777     
778     /*
779      * We need the tasklist lock because it's the only
780      * thing that protects out "parent" pointer.
781      *
782      * exit.c calls "do_notify_parent()" directly, because
783      * it already has the tasklist lock.
784      */
785     void
786     notify_parent(struct task_struct *tsk, int sig)
787     {
788     	read_lock(&tasklist_lock);
789     	do_notify_parent(tsk, sig);
790     	read_unlock(&tasklist_lock);
791     }
792     
793     EXPORT_SYMBOL(dequeue_signal);
794     EXPORT_SYMBOL(flush_signals);
795     EXPORT_SYMBOL(force_sig);
796     EXPORT_SYMBOL(force_sig_info);
797     EXPORT_SYMBOL(kill_pg);
798     EXPORT_SYMBOL(kill_pg_info);
799     EXPORT_SYMBOL(kill_proc);
800     EXPORT_SYMBOL(kill_proc_info);
801     EXPORT_SYMBOL(kill_sl);
802     EXPORT_SYMBOL(kill_sl_info);
803     EXPORT_SYMBOL(notify_parent);
804     EXPORT_SYMBOL(recalc_sigpending);
805     EXPORT_SYMBOL(send_sig);
806     EXPORT_SYMBOL(send_sig_info);
807     EXPORT_SYMBOL(block_all_signals);
808     EXPORT_SYMBOL(unblock_all_signals);
809     
810     
811     /*
812      * System call entry points.
813      */
814     
815     /*
816      * We don't need to get the kernel lock - this is all local to this
817      * particular thread.. (and that's good, because this is _heavily_
818      * used by various programs)
819      */
820     
821     asmlinkage long
822     sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset, size_t sigsetsize)
823     {
824     	int error = -EINVAL;
825     	sigset_t old_set, new_set;
826     
827     	/* XXX: Don't preclude handling different sized sigset_t's.  */
828     	if (sigsetsize != sizeof(sigset_t))
829     		goto out;
830     
831     	if (set) {
832     		error = -EFAULT;
833     		if (copy_from_user(&new_set, set, sizeof(*set)))
834     			goto out;
835     		sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
836     
837     		spin_lock_irq(&current->sigmask_lock);
838     		old_set = current->blocked;
839     
840     		error = 0;
841     		switch (how) {
842     		default:
843     			error = -EINVAL;
844     			break;
845     		case SIG_BLOCK:
846     			sigorsets(&new_set, &old_set, &new_set);
847     			break;
848     		case SIG_UNBLOCK:
849     			signandsets(&new_set, &old_set, &new_set);
850     			break;
851     		case SIG_SETMASK:
852     			break;
853     		}
854     
855     		current->blocked = new_set;
856     		recalc_sigpending(current);
857     		spin_unlock_irq(&current->sigmask_lock);
858     		if (error)
859     			goto out;
860     		if (oset)
861     			goto set_old;
862     	} else if (oset) {
863     		spin_lock_irq(&current->sigmask_lock);
864     		old_set = current->blocked;
865     		spin_unlock_irq(&current->sigmask_lock);
866     
867     	set_old:
868     		error = -EFAULT;
869     		if (copy_to_user(oset, &old_set, sizeof(*oset)))
870     			goto out;
871     	}
872     	error = 0;
873     out:
874     	return error;
875     }
876     
877     long do_sigpending(void *set, unsigned long sigsetsize)
878     {
879     	long error = -EINVAL;
880     	sigset_t pending;
881     
882     	if (sigsetsize > sizeof(sigset_t))
883     		goto out;
884     
885     	spin_lock_irq(&current->sigmask_lock);
886     	sigandsets(&pending, &current->blocked, &current->pending.signal);
887     	spin_unlock_irq(&current->sigmask_lock);
888     
889     	error = -EFAULT;
890     	if (!copy_to_user(set, &pending, sigsetsize))
891     		error = 0;
892     out:
893     	return error;
894     }	
895     
896     asmlinkage long
897     sys_rt_sigpending(sigset_t *set, size_t sigsetsize)
898     {
899     	return do_sigpending(set, sigsetsize);
900     }
901     
902     asmlinkage long
903     sys_rt_sigtimedwait(const sigset_t *uthese, siginfo_t *uinfo,
904     		    const struct timespec *uts, size_t sigsetsize)
905     {
906     	int ret, sig;
907     	sigset_t these;
908     	struct timespec ts;
909     	siginfo_t info;
910     	long timeout = 0;
911     
912     	/* XXX: Don't preclude handling different sized sigset_t's.  */
913     	if (sigsetsize != sizeof(sigset_t))
914     		return -EINVAL;
915     
916     	if (copy_from_user(&these, uthese, sizeof(these)))
917     		return -EFAULT;
918     		
919     	/*
920     	 * Invert the set of allowed signals to get those we
921     	 * want to block.
922     	 */
923     	sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
924     	signotset(&these);
925     
926     	if (uts) {
927     		if (copy_from_user(&ts, uts, sizeof(ts)))
928     			return -EFAULT;
929     		if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
930     		    || ts.tv_sec < 0)
931     			return -EINVAL;
932     	}
933     
934     	spin_lock_irq(&current->sigmask_lock);
935     	sig = dequeue_signal(&these, &info);
936     	if (!sig) {
937     		timeout = MAX_SCHEDULE_TIMEOUT;
938     		if (uts)
939     			timeout = (timespec_to_jiffies(&ts)
940     				   + (ts.tv_sec || ts.tv_nsec));
941     
942     		if (timeout) {
943     			/* None ready -- temporarily unblock those we're
944     			 * interested while we are sleeping in so that we'll
945     			 * be awakened when they arrive.  */
946     			sigset_t oldblocked = current->blocked;
947     			sigandsets(&current->blocked, &current->blocked, &these);
948     			recalc_sigpending(current);
949     			spin_unlock_irq(&current->sigmask_lock);
950     
951     			current->state = TASK_INTERRUPTIBLE;
952     			timeout = schedule_timeout(timeout);
953     
954     			spin_lock_irq(&current->sigmask_lock);
955     			sig = dequeue_signal(&these, &info);
956     			current->blocked = oldblocked;
957     			recalc_sigpending(current);
958     		}
959     	}
960     	spin_unlock_irq(&current->sigmask_lock);
961     
962     	if (sig) {
963     		ret = sig;
964     		if (uinfo) {
965     			if (copy_siginfo_to_user(uinfo, &info))
966     				ret = -EFAULT;
967     		}
968     	} else {
969     		ret = -EAGAIN;
970     		if (timeout)
971     			ret = -EINTR;
972     	}
973     
974     	return ret;
975     }
976     
977     asmlinkage long
978     sys_kill(int pid, int sig)
979     {
980     	struct siginfo info;
981     
982     	info.si_signo = sig;
983     	info.si_errno = 0;
984     	info.si_code = SI_USER;
985     	info.si_pid = current->pid;
986     	info.si_uid = current->uid;
987     
988     	return kill_something_info(sig, &info, pid);
989     }
990     
991     asmlinkage long
992     sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo)
993     {
994     	siginfo_t info;
995     
996     	if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
997     		return -EFAULT;
998     
999     	/* Not even root can pretend to send signals from the kernel.
1000     	   Nor can they impersonate a kill(), which adds source info.  */
1001     	if (info.si_code >= 0)
1002     		return -EPERM;
1003     	info.si_signo = sig;
1004     
1005     	/* POSIX.1b doesn't mention process groups.  */
1006     	return kill_proc_info(sig, &info, pid);
1007     }
1008     
1009     int
1010     do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
1011     {
1012     	struct k_sigaction *k;
1013     
1014     	if (sig < 1 || sig > _NSIG ||
1015     	    (act && (sig == SIGKILL || sig == SIGSTOP)))
1016     		return -EINVAL;
1017     
1018     	k = &current->sig->action[sig-1];
1019     
1020     	spin_lock(&current->sig->siglock);
1021     
1022     	if (oact)
1023     		*oact = *k;
1024     
1025     	if (act) {
1026     		*k = *act;
1027     		sigdelsetmask(&k->sa.sa_mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1028     
1029     		/*
1030     		 * POSIX 3.3.1.3:
1031     		 *  "Setting a signal action to SIG_IGN for a signal that is
1032     		 *   pending shall cause the pending signal to be discarded,
1033     		 *   whether or not it is blocked."
1034     		 *
1035     		 *  "Setting a signal action to SIG_DFL for a signal that is
1036     		 *   pending and whose default action is to ignore the signal
1037     		 *   (for example, SIGCHLD), shall cause the pending signal to
1038     		 *   be discarded, whether or not it is blocked"
1039     		 *
1040     		 * Note the silly behaviour of SIGCHLD: SIG_IGN means that the
1041     		 * signal isn't actually ignored, but does automatic child
1042     		 * reaping, while SIG_DFL is explicitly said by POSIX to force
1043     		 * the signal to be ignored.
1044     		 */
1045     
1046     		if (k->sa.sa_handler == SIG_IGN
1047     		    || (k->sa.sa_handler == SIG_DFL
1048     			&& (sig == SIGCONT ||
1049     			    sig == SIGCHLD ||
1050     			    sig == SIGWINCH))) {
1051     			spin_lock_irq(&current->sigmask_lock);
1052     			if (rm_sig_from_queue(sig, current))
1053     				recalc_sigpending(current);
1054     			spin_unlock_irq(&current->sigmask_lock);
1055     		}
1056     	}
1057     
1058     	spin_unlock(&current->sig->siglock);
1059     	return 0;
1060     }
1061     
1062     int 
1063     do_sigaltstack (const stack_t *uss, stack_t *uoss, unsigned long sp)
1064     {
1065     	stack_t oss;
1066     	int error;
1067     
1068     	if (uoss) {
1069     		oss.ss_sp = (void *) current->sas_ss_sp;
1070     		oss.ss_size = current->sas_ss_size;
1071     		oss.ss_flags = sas_ss_flags(sp);
1072     	}
1073     
1074     	if (uss) {
1075     		void *ss_sp;
1076     		size_t ss_size;
1077     		int ss_flags;
1078     
1079     		error = -EFAULT;
1080     		if (verify_area(VERIFY_READ, uss, sizeof(*uss))
1081     		    || __get_user(ss_sp, &uss->ss_sp)
1082     		    || __get_user(ss_flags, &uss->ss_flags)
1083     		    || __get_user(ss_size, &uss->ss_size))
1084     			goto out;
1085     
1086     		error = -EPERM;
1087     		if (on_sig_stack (sp))
1088     			goto out;
1089     
1090     		error = -EINVAL;
1091     		/*
1092     		 *
1093     		 * Note - this code used to test ss_flags incorrectly
1094     		 *  	  old code may have been written using ss_flags==0
1095     		 *	  to mean ss_flags==SS_ONSTACK (as this was the only
1096     		 *	  way that worked) - this fix preserves that older
1097     		 *	  mechanism
1098     		 */
1099     		if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
1100     			goto out;
1101     
1102     		if (ss_flags == SS_DISABLE) {
1103     			ss_size = 0;
1104     			ss_sp = NULL;
1105     		} else {
1106     			error = -ENOMEM;
1107     			if (ss_size < MINSIGSTKSZ)
1108     				goto out;
1109     		}
1110     
1111     		current->sas_ss_sp = (unsigned long) ss_sp;
1112     		current->sas_ss_size = ss_size;
1113     	}
1114     
1115     	if (uoss) {
1116     		error = -EFAULT;
1117     		if (copy_to_user(uoss, &oss, sizeof(oss)))
1118     			goto out;
1119     	}
1120     
1121     	error = 0;
1122     out:
1123     	return error;
1124     }
1125     
1126     asmlinkage long
1127     sys_sigpending(old_sigset_t *set)
1128     {
1129     	return do_sigpending(set, sizeof(*set));
1130     }
1131     
1132     #if !defined(__alpha__)
1133     /* Alpha has its own versions with special arguments.  */
1134     
1135     asmlinkage long
1136     sys_sigprocmask(int how, old_sigset_t *set, old_sigset_t *oset)
1137     {
1138     	int error;
1139     	old_sigset_t old_set, new_set;
1140     
1141     	if (set) {
1142     		error = -EFAULT;
1143     		if (copy_from_user(&new_set, set, sizeof(*set)))
1144     			goto out;
1145     		new_set &= ~(sigmask(SIGKILL)|sigmask(SIGSTOP));
1146     
1147     		spin_lock_irq(&current->sigmask_lock);
1148     		old_set = current->blocked.sig[0];
1149     
1150     		error = 0;
1151     		switch (how) {
1152     		default:
1153     			error = -EINVAL;
1154     			break;
1155     		case SIG_BLOCK:
1156     			sigaddsetmask(&current->blocked, new_set);
1157     			break;
1158     		case SIG_UNBLOCK:
1159     			sigdelsetmask(&current->blocked, new_set);
1160     			break;
1161     		case SIG_SETMASK:
1162     			current->blocked.sig[0] = new_set;
1163     			break;
1164     		}
1165     
1166     		recalc_sigpending(current);
1167     		spin_unlock_irq(&current->sigmask_lock);
1168     		if (error)
1169     			goto out;
1170     		if (oset)
1171     			goto set_old;
1172     	} else if (oset) {
1173     		old_set = current->blocked.sig[0];
1174     	set_old:
1175     		error = -EFAULT;
1176     		if (copy_to_user(oset, &old_set, sizeof(*oset)))
1177     			goto out;
1178     	}
1179     	error = 0;
1180     out:
1181     	return error;
1182     }
1183     
1184     #ifndef __sparc__
1185     asmlinkage long
1186     sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
1187     		 size_t sigsetsize)
1188     {
1189     	struct k_sigaction new_sa, old_sa;
1190     	int ret = -EINVAL;
1191     
1192     	/* XXX: Don't preclude handling different sized sigset_t's.  */
1193     	if (sigsetsize != sizeof(sigset_t))
1194     		goto out;
1195     
1196     	if (act) {
1197     		if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
1198     			return -EFAULT;
1199     	}
1200     
1201     	ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
1202     
1203     	if (!ret && oact) {
1204     		if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
1205     			return -EFAULT;
1206     	}
1207     out:
1208     	return ret;
1209     }
1210     #endif /* __sparc__ */
1211     #endif
1212     
1213     #if !defined(__alpha__) && !defined(__ia64__)
1214     /*
1215      * For backwards compatibility.  Functionality superseded by sigprocmask.
1216      */
1217     asmlinkage long
1218     sys_sgetmask(void)
1219     {
1220     	/* SMP safe */
1221     	return current->blocked.sig[0];
1222     }
1223     
1224     asmlinkage long
1225     sys_ssetmask(int newmask)
1226     {
1227     	int old;
1228     
1229     	spin_lock_irq(&current->sigmask_lock);
1230     	old = current->blocked.sig[0];
1231     
1232     	siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
1233     						  sigmask(SIGSTOP)));
1234     	recalc_sigpending(current);
1235     	spin_unlock_irq(&current->sigmask_lock);
1236     
1237     	return old;
1238     }
1239     #endif /* !defined(__alpha__) */
1240     
1241     #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__)
1242     /*
1243      * For backwards compatibility.  Functionality superseded by sigaction.
1244      */
1245     asmlinkage unsigned long
1246     sys_signal(int sig, __sighandler_t handler)
1247     {
1248     	struct k_sigaction new_sa, old_sa;
1249     	int ret;
1250     
1251     	new_sa.sa.sa_handler = handler;
1252     	new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
1253     
1254     	ret = do_sigaction(sig, &new_sa, &old_sa);
1255     
1256     	return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
1257     }
1258     #endif /* !alpha && !__ia64__ && !defined(__mips__) */
1259