This commit was manufactured by cvs2svn to create branch
[linux-2.6.git] / kernel / exit.c
1 /*
2  *  linux/kernel/exit.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/config.h>
8 #include <linux/mm.h>
9 #include <linux/slab.h>
10 #include <linux/interrupt.h>
11 #include <linux/smp_lock.h>
12 #include <linux/module.h>
13 #include <linux/completion.h>
14 #include <linux/personality.h>
15 #include <linux/tty.h>
16 #include <linux/namespace.h>
17 #include <linux/security.h>
18 #include <linux/acct.h>
19 #include <linux/file.h>
20 #include <linux/binfmts.h>
21 #include <linux/ptrace.h>
22 #include <linux/profile.h>
23 #include <linux/mount.h>
24 #include <linux/proc_fs.h>
25 #include <linux/mempolicy.h>
26 #include <linux/ckrm.h>
27 #include <linux/ckrm_tsk.h>
28 #include <linux/vs_limit.h>
29 #include <linux/ckrm_mem.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/unistd.h>
33 #include <asm/pgtable.h>
34 #include <asm/mmu_context.h>
35
36 extern void sem_exit (void);
37 extern struct task_struct *child_reaper;
38
39 int getrusage(struct task_struct *, int, struct rusage __user *);
40
41 static void __unhash_process(struct task_struct *p)
42 {
43         nr_threads--;
44         /* tasklist_lock is held, is this sufficient? */
45         if (p->vx_info) {
46                 atomic_dec(&p->vx_info->cacct.nr_threads);
47                 atomic_dec(&p->vx_info->limit.rcur[RLIMIT_NPROC]);
48         }
49         detach_pid(p, PIDTYPE_PID);
50         detach_pid(p, PIDTYPE_TGID);
51         if (thread_group_leader(p)) {
52                 detach_pid(p, PIDTYPE_PGID);
53                 detach_pid(p, PIDTYPE_SID);
54                 if (p->pid)
55                         __get_cpu_var(process_counts)--;
56         }
57
58         REMOVE_LINKS(p);
59 }
60
61 void release_task(struct task_struct * p)
62 {
63         int zap_leader;
64         task_t *leader;
65         struct dentry *proc_dentry;
66
67 repeat: 
68         BUG_ON(p->state < TASK_ZOMBIE);
69  
70         atomic_dec(&p->user->processes);
71         spin_lock(&p->proc_lock);
72         proc_dentry = proc_pid_unhash(p);
73         write_lock_irq(&tasklist_lock);
74         if (unlikely(p->ptrace))
75                 __ptrace_unlink(p);
76         BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
77         __exit_signal(p);
78         __exit_sighand(p);
79         __unhash_process(p);
80
81         /*
82          * If we are the last non-leader member of the thread
83          * group, and the leader is zombie, then notify the
84          * group leader's parent process. (if it wants notification.)
85          */
86         zap_leader = 0;
87         leader = p->group_leader;
88         if (leader != p && thread_group_empty(leader) && leader->state == TASK_ZOMBIE) {
89                 BUG_ON(leader->exit_signal == -1);
90                 do_notify_parent(leader, leader->exit_signal);
91                 /*
92                  * If we were the last child thread and the leader has
93                  * exited already, and the leader's parent ignores SIGCHLD,
94                  * then we are the one who should release the leader.
95                  *
96                  * do_notify_parent() will have marked it self-reaping in
97                  * that case.
98                  */
99                 zap_leader = (leader->exit_signal == -1);
100         }
101
102         p->parent->cutime += p->utime + p->cutime;
103         p->parent->cstime += p->stime + p->cstime;
104         p->parent->cmin_flt += p->min_flt + p->cmin_flt;
105         p->parent->cmaj_flt += p->maj_flt + p->cmaj_flt;
106         p->parent->cnvcsw += p->nvcsw + p->cnvcsw;
107         p->parent->cnivcsw += p->nivcsw + p->cnivcsw;
108         sched_exit(p);
109         write_unlock_irq(&tasklist_lock);
110         spin_unlock(&p->proc_lock);
111         dput(proc_dentry);
112         release_thread(p);
113         put_task_struct(p);
114
115         p = leader;
116         if (unlikely(zap_leader))
117                 goto repeat;
118 }
119
120 /* we are using it only for SMP init */
121
122 void unhash_process(struct task_struct *p)
123 {
124         struct dentry *proc_dentry;
125
126         spin_lock(&p->proc_lock);
127         proc_dentry = proc_pid_unhash(p);
128         write_lock_irq(&tasklist_lock);
129         __unhash_process(p);
130         write_unlock_irq(&tasklist_lock);
131         spin_unlock(&p->proc_lock);
132         proc_pid_flush(proc_dentry);
133 }
134
135 /*
136  * This checks not only the pgrp, but falls back on the pid if no
137  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
138  * without this...
139  */
140 int session_of_pgrp(int pgrp)
141 {
142         struct task_struct *p;
143         struct list_head *l;
144         struct pid *pid;
145         int sid = -1;
146
147         read_lock(&tasklist_lock);
148         for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid)
149                 if (p->signal->session > 0) {
150                         sid = p->signal->session;
151                         goto out;
152                 }
153         p = find_task_by_pid(pgrp);
154         if (p)
155                 sid = p->signal->session;
156 out:
157         read_unlock(&tasklist_lock);
158         
159         return sid;
160 }
161
162 /*
163  * Determine if a process group is "orphaned", according to the POSIX
164  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
165  * by terminal-generated stop signals.  Newly orphaned process groups are
166  * to receive a SIGHUP and a SIGCONT.
167  *
168  * "I ask you, have you ever known what it is to be an orphan?"
169  */
170 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
171 {
172         struct task_struct *p;
173         struct list_head *l;
174         struct pid *pid;
175         int ret = 1;
176
177         for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
178                 if (p == ignored_task
179                                 || p->state >= TASK_ZOMBIE 
180                                 || p->real_parent->pid == 1)
181                         continue;
182                 if (process_group(p->real_parent) != pgrp
183                             && p->real_parent->signal->session == p->signal->session) {
184                         ret = 0;
185                         break;
186                 }
187         }
188         return ret;     /* (sighing) "Often!" */
189 }
190
191 int is_orphaned_pgrp(int pgrp)
192 {
193         int retval;
194
195         read_lock(&tasklist_lock);
196         retval = will_become_orphaned_pgrp(pgrp, NULL);
197         read_unlock(&tasklist_lock);
198
199         return retval;
200 }
201
202 static inline int has_stopped_jobs(int pgrp)
203 {
204         int retval = 0;
205         struct task_struct *p;
206         struct list_head *l;
207         struct pid *pid;
208
209         for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
210                 if (p->state != TASK_STOPPED)
211                         continue;
212
213                 /* If p is stopped by a debugger on a signal that won't
214                    stop it, then don't count p as stopped.  This isn't
215                    perfect but it's a good approximation.  */
216                 if (unlikely (p->ptrace)
217                     && p->exit_code != SIGSTOP
218                     && p->exit_code != SIGTSTP
219                     && p->exit_code != SIGTTOU
220                     && p->exit_code != SIGTTIN)
221                         continue;
222
223                 retval = 1;
224                 break;
225         }
226         return retval;
227 }
228
229 /**
230  * reparent_to_init() - Reparent the calling kernel thread to the init task.
231  *
232  * If a kernel thread is launched as a result of a system call, or if
233  * it ever exits, it should generally reparent itself to init so that
234  * it is correctly cleaned up on exit.
235  *
236  * The various task state such as scheduling policy and priority may have
237  * been inherited from a user process, so we reset them to sane values here.
238  *
239  * NOTE that reparent_to_init() gives the caller full capabilities.
240  */
241 void reparent_to_init(void)
242 {
243         write_lock_irq(&tasklist_lock);
244
245         ptrace_unlink(current);
246         /* Reparent to init */
247         REMOVE_LINKS(current);
248         /* FIXME handle vchild_reaper/initpid */
249         current->parent = child_reaper;
250         current->real_parent = child_reaper;
251         SET_LINKS(current);
252
253         /* Set the exit signal to SIGCHLD so we signal init on exit */
254         current->exit_signal = SIGCHLD;
255
256         if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
257                 set_user_nice(current, 0);
258         /* cpus_allowed? */
259         /* rt_priority? */
260         /* signals? */
261         security_task_reparent_to_init(current);
262         memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
263         atomic_inc(&(INIT_USER->__count));
264         switch_uid(INIT_USER);
265
266         write_unlock_irq(&tasklist_lock);
267 }
268
269 void __set_special_pids(pid_t session, pid_t pgrp)
270 {
271         struct task_struct *curr = current;
272
273         if (curr->signal->session != session) {
274                 detach_pid(curr, PIDTYPE_SID);
275                 curr->signal->session = session;
276                 attach_pid(curr, PIDTYPE_SID, session);
277         }
278         if (process_group(curr) != pgrp) {
279                 detach_pid(curr, PIDTYPE_PGID);
280                 curr->signal->pgrp = pgrp;
281                 attach_pid(curr, PIDTYPE_PGID, pgrp);
282         }
283 }
284
285 void set_special_pids(pid_t session, pid_t pgrp)
286 {
287         write_lock_irq(&tasklist_lock);
288         __set_special_pids(session, pgrp);
289         write_unlock_irq(&tasklist_lock);
290 }
291
292 /*
293  * Let kernel threads use this to say that they
294  * allow a certain signal (since daemonize() will
295  * have disabled all of them by default).
296  */
297 int allow_signal(int sig)
298 {
299         if (sig < 1 || sig > _NSIG)
300                 return -EINVAL;
301
302         spin_lock_irq(&current->sighand->siglock);
303         sigdelset(&current->blocked, sig);
304         if (!current->mm) {
305                 /* Kernel threads handle their own signals.
306                    Let the signal code know it'll be handled, so
307                    that they don't get converted to SIGKILL or
308                    just silently dropped */
309                 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
310         }
311         recalc_sigpending();
312         spin_unlock_irq(&current->sighand->siglock);
313         return 0;
314 }
315
316 EXPORT_SYMBOL(allow_signal);
317
318 int disallow_signal(int sig)
319 {
320         if (sig < 1 || sig > _NSIG)
321                 return -EINVAL;
322
323         spin_lock_irq(&current->sighand->siglock);
324         sigaddset(&current->blocked, sig);
325         recalc_sigpending();
326         spin_unlock_irq(&current->sighand->siglock);
327         return 0;
328 }
329
330 EXPORT_SYMBOL(disallow_signal);
331
332 /*
333  *      Put all the gunge required to become a kernel thread without
334  *      attached user resources in one place where it belongs.
335  */
336
337 void daemonize(const char *name, ...)
338 {
339         va_list args;
340         struct fs_struct *fs;
341         sigset_t blocked;
342
343         va_start(args, name);
344         vsnprintf(current->comm, sizeof(current->comm), name, args);
345         va_end(args);
346
347         /*
348          * If we were started as result of loading a module, close all of the
349          * user space pages.  We don't need them, and if we didn't close them
350          * they would be locked into memory.
351          */
352         exit_mm(current);
353
354         set_special_pids(1, 1);
355         current->signal->tty = NULL;
356
357         /* Block and flush all signals */
358         sigfillset(&blocked);
359         sigprocmask(SIG_BLOCK, &blocked, NULL);
360         flush_signals(current);
361
362         /* Become as one with the init task */
363
364         exit_fs(current);       /* current->fs->count--; */
365         fs = init_task.fs;
366         current->fs = fs;
367         atomic_inc(&fs->count);
368         exit_files(current);
369         current->files = init_task.files;
370         atomic_inc(&current->files->count);
371
372         reparent_to_init();
373 }
374
375 EXPORT_SYMBOL(daemonize);
376
377 static inline void close_files(struct files_struct * files)
378 {
379         int i, j;
380
381         j = 0;
382         for (;;) {
383                 unsigned long set;
384                 i = j * __NFDBITS;
385                 if (i >= files->max_fdset || i >= files->max_fds)
386                         break;
387                 set = files->open_fds->fds_bits[j++];
388                 while (set) {
389                         if (set & 1) {
390                                 struct file * file = xchg(&files->fd[i], NULL);
391                                 if (file) {
392                                         filp_close(file, files);
393                                         cond_resched();
394                                 }
395                                 // vx_openfd_dec(fd);
396                         }
397                         i++;
398                         set >>= 1;
399                 }
400         }
401 }
402
403 struct files_struct *get_files_struct(struct task_struct *task)
404 {
405         struct files_struct *files;
406
407         task_lock(task);
408         files = task->files;
409         if (files)
410                 atomic_inc(&files->count);
411         task_unlock(task);
412
413         return files;
414 }
415
416 void fastcall put_files_struct(struct files_struct *files)
417 {
418         if (atomic_dec_and_test(&files->count)) {
419                 close_files(files);
420                 /*
421                  * Free the fd and fdset arrays if we expanded them.
422                  */
423                 if (files->fd != &files->fd_array[0])
424                         free_fd_array(files->fd, files->max_fds);
425                 if (files->max_fdset > __FD_SETSIZE) {
426                         free_fdset(files->open_fds, files->max_fdset);
427                         free_fdset(files->close_on_exec, files->max_fdset);
428                 }
429                 kmem_cache_free(files_cachep, files);
430         }
431 }
432
433 EXPORT_SYMBOL(put_files_struct);
434
435 static inline void __exit_files(struct task_struct *tsk)
436 {
437         struct files_struct * files = tsk->files;
438
439         if (files) {
440                 task_lock(tsk);
441                 tsk->files = NULL;
442                 task_unlock(tsk);
443                 put_files_struct(files);
444         }
445 }
446
447 void exit_files(struct task_struct *tsk)
448 {
449         __exit_files(tsk);
450 }
451
452 static inline void __put_fs_struct(struct fs_struct *fs)
453 {
454         /* No need to hold fs->lock if we are killing it */
455         if (atomic_dec_and_test(&fs->count)) {
456                 dput(fs->root);
457                 mntput(fs->rootmnt);
458                 dput(fs->pwd);
459                 mntput(fs->pwdmnt);
460                 if (fs->altroot) {
461                         dput(fs->altroot);
462                         mntput(fs->altrootmnt);
463                 }
464                 kmem_cache_free(fs_cachep, fs);
465         }
466 }
467
468 void put_fs_struct(struct fs_struct *fs)
469 {
470         __put_fs_struct(fs);
471 }
472
473 static inline void __exit_fs(struct task_struct *tsk)
474 {
475         struct fs_struct * fs = tsk->fs;
476
477         if (fs) {
478                 task_lock(tsk);
479                 tsk->fs = NULL;
480                 task_unlock(tsk);
481                 __put_fs_struct(fs);
482         }
483 }
484
485 void exit_fs(struct task_struct *tsk)
486 {
487         __exit_fs(tsk);
488 }
489
490 EXPORT_SYMBOL_GPL(exit_fs);
491
492 /*
493  * Turn us into a lazy TLB process if we
494  * aren't already..
495  */
496 static inline void __exit_mm(struct task_struct * tsk)
497 {
498         struct mm_struct *mm = tsk->mm;
499
500         mm_release(tsk, mm);
501         if (!mm)
502                 return;
503         /*
504          * Serialize with any possible pending coredump.
505          * We must hold mmap_sem around checking core_waiters
506          * and clearing tsk->mm.  The core-inducing thread
507          * will increment core_waiters for each thread in the
508          * group with ->mm != NULL.
509          */
510         down_read(&mm->mmap_sem);
511         if (mm->core_waiters) {
512                 up_read(&mm->mmap_sem);
513                 down_write(&mm->mmap_sem);
514                 if (!--mm->core_waiters)
515                         complete(mm->core_startup_done);
516                 up_write(&mm->mmap_sem);
517
518                 wait_for_completion(&mm->core_done);
519                 down_read(&mm->mmap_sem);
520         }
521         atomic_inc(&mm->mm_count);
522         if (mm != tsk->active_mm) BUG();
523         /* more a memory barrier than a real lock */
524         task_lock(tsk);
525         tsk->mm = NULL;
526         up_read(&mm->mmap_sem);
527 #ifdef CONFIG_CKRM_RES_MEM
528         spin_lock(&mm->peertask_lock);
529         list_del_init(&tsk->mm_peers);
530         ckrm_mem_evaluate_mm(mm);
531         spin_unlock(&mm->peertask_lock);
532 #endif
533         enter_lazy_tlb(mm, current);
534         task_unlock(tsk);
535         mmput(mm);
536 }
537
538 void exit_mm(struct task_struct *tsk)
539 {
540         __exit_mm(tsk);
541 }
542
543 EXPORT_SYMBOL(exit_mm);
544
545 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
546 {
547         /*
548          * Make sure we're not reparenting to ourselves and that
549          * the parent is not a zombie.
550          */
551         if (p == reaper || reaper->state >= TASK_ZOMBIE)
552                 p->real_parent = child_reaper;
553         else
554                 p->real_parent = reaper;
555         if (p->parent == p->real_parent)
556                 BUG();
557 }
558
559 static inline void reparent_thread(task_t *p, task_t *father, int traced)
560 {
561         /* We don't want people slaying init.  */
562         if (p->exit_signal != -1)
563                 p->exit_signal = SIGCHLD;
564         p->self_exec_id++;
565
566         if (p->pdeath_signal)
567                 /* We already hold the tasklist_lock here.  */
568                 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
569
570         /* Move the child from its dying parent to the new one.  */
571         if (unlikely(traced)) {
572                 /* Preserve ptrace links if someone else is tracing this child.  */
573                 list_del_init(&p->ptrace_list);
574                 if (p->parent != p->real_parent)
575                         list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
576         } else {
577                 /* If this child is being traced, then we're the one tracing it
578                  * anyway, so let go of it.
579                  */
580                 p->ptrace = 0;
581                 list_del_init(&p->sibling);
582                 p->parent = p->real_parent;
583                 list_add_tail(&p->sibling, &p->parent->children);
584
585                 /* If we'd notified the old parent about this child's death,
586                  * also notify the new parent.
587                  */
588                 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
589                     thread_group_empty(p))
590                         do_notify_parent(p, p->exit_signal);
591         }
592
593         /*
594          * process group orphan check
595          * Case ii: Our child is in a different pgrp
596          * than we are, and it was the only connection
597          * outside, so the child pgrp is now orphaned.
598          */
599         if ((process_group(p) != process_group(father)) &&
600             (p->signal->session == father->signal->session)) {
601                 int pgrp = process_group(p);
602
603                 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
604                         __kill_pg_info(SIGHUP, (void *)1, pgrp);
605                         __kill_pg_info(SIGCONT, (void *)1, pgrp);
606                 }
607         }
608 }
609
610 /*
611  * When we die, we re-parent all our children.
612  * Try to give them to another thread in our thread
613  * group, and if no such member exists, give it to
614  * the global child reaper process (ie "init")
615  */
616 static inline void forget_original_parent(struct task_struct * father,
617                                           struct list_head *to_release)
618 {
619         struct task_struct *p, *reaper = father;
620         struct list_head *_p, *_n;
621
622         /* FIXME handle vchild_reaper/initpid */
623         reaper = father->group_leader;
624         if (reaper == father)
625                 reaper = child_reaper;
626
627         /*
628          * There are only two places where our children can be:
629          *
630          * - in our child list
631          * - in our ptraced child list
632          *
633          * Search them and reparent children.
634          */
635         list_for_each_safe(_p, _n, &father->children) {
636                 int ptrace;
637                 p = list_entry(_p,struct task_struct,sibling);
638
639                 ptrace = p->ptrace;
640
641                 /* if father isn't the real parent, then ptrace must be enabled */
642                 BUG_ON(father != p->real_parent && !ptrace);
643
644                 if (father == p->real_parent) {
645                         /* reparent with a reaper, real father it's us */
646                         choose_new_parent(p, reaper, child_reaper);
647                         reparent_thread(p, father, 0);
648                 } else {
649                         /* reparent ptraced task to its real parent */
650                         __ptrace_unlink (p);
651                         if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
652                             thread_group_empty(p))
653                                 do_notify_parent(p, p->exit_signal);
654                 }
655
656                 /*
657                  * if the ptraced child is a zombie with exit_signal == -1
658                  * we must collect it before we exit, or it will remain
659                  * zombie forever since we prevented it from self-reap itself
660                  * while it was being traced by us, to be able to see it in wait4.
661                  */
662                 if (unlikely(ptrace && p->state == TASK_ZOMBIE && p->exit_signal == -1))
663                         list_add(&p->ptrace_list, to_release);
664         }
665         list_for_each_safe(_p, _n, &father->ptrace_children) {
666                 p = list_entry(_p,struct task_struct,ptrace_list);
667                 choose_new_parent(p, reaper, child_reaper);
668                 reparent_thread(p, father, 1);
669         }
670 }
671
672 /*
673  * Send signals to all our closest relatives so that they know
674  * to properly mourn us..
675  */
676 static void exit_notify(struct task_struct *tsk)
677 {
678         int state;
679         struct task_struct *t;
680         struct list_head ptrace_dead, *_p, *_n;
681
682         ckrm_cb_exit(tsk);
683
684         if (signal_pending(tsk) && !tsk->signal->group_exit
685             && !thread_group_empty(tsk)) {
686                 /*
687                  * This occurs when there was a race between our exit
688                  * syscall and a group signal choosing us as the one to
689                  * wake up.  It could be that we are the only thread
690                  * alerted to check for pending signals, but another thread
691                  * should be woken now to take the signal since we will not.
692                  * Now we'll wake all the threads in the group just to make
693                  * sure someone gets all the pending signals.
694                  */
695                 read_lock(&tasklist_lock);
696                 spin_lock_irq(&tsk->sighand->siglock);
697                 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
698                         if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
699                                 recalc_sigpending_tsk(t);
700                                 if (signal_pending(t))
701                                         signal_wake_up(t, 0);
702                         }
703                 spin_unlock_irq(&tsk->sighand->siglock);
704                 read_unlock(&tasklist_lock);
705         }
706
707         write_lock_irq(&tasklist_lock);
708
709         /*
710          * This does two things:
711          *
712          * A.  Make init inherit all the child processes
713          * B.  Check to see if any process groups have become orphaned
714          *      as a result of our exiting, and if they have any stopped
715          *      jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
716          */
717
718         INIT_LIST_HEAD(&ptrace_dead);
719         forget_original_parent(tsk, &ptrace_dead);
720         BUG_ON(!list_empty(&tsk->children));
721         BUG_ON(!list_empty(&tsk->ptrace_children));
722
723         /*
724          * Check to see if any process groups have become orphaned
725          * as a result of our exiting, and if they have any stopped
726          * jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
727          *
728          * Case i: Our father is in a different pgrp than we are
729          * and we were the only connection outside, so our pgrp
730          * is about to become orphaned.
731          */
732          
733         t = tsk->real_parent;
734         
735         if ((process_group(t) != process_group(tsk)) &&
736             (t->signal->session == tsk->signal->session) &&
737             will_become_orphaned_pgrp(process_group(tsk), tsk) &&
738             has_stopped_jobs(process_group(tsk))) {
739                 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
740                 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
741         }
742
743         /* Let father know we died 
744          *
745          * Thread signals are configurable, but you aren't going to use
746          * that to send signals to arbitary processes. 
747          * That stops right now.
748          *
749          * If the parent exec id doesn't match the exec id we saved
750          * when we started then we know the parent has changed security
751          * domain.
752          *
753          * If our self_exec id doesn't match our parent_exec_id then
754          * we have changed execution domain as these two values started
755          * the same after a fork.
756          *      
757          */
758         
759         if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
760             ( tsk->parent_exec_id != t->self_exec_id  ||
761               tsk->self_exec_id != tsk->parent_exec_id)
762             && !capable(CAP_KILL))
763                 tsk->exit_signal = SIGCHLD;
764
765
766         /* If something other than our normal parent is ptracing us, then
767          * send it a SIGCHLD instead of honoring exit_signal.  exit_signal
768          * only has special meaning to our real parent.
769          */
770         if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
771                 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
772                 do_notify_parent(tsk, signal);
773         } else if (tsk->ptrace) {
774                 do_notify_parent(tsk, SIGCHLD);
775         }
776
777         state = TASK_ZOMBIE;
778         if (tsk->exit_signal == -1 && tsk->ptrace == 0)
779                 state = TASK_DEAD;
780         tsk->state = state;
781         tsk->flags |= PF_DEAD;
782
783         /*
784          * Clear these here so that update_process_times() won't try to deliver
785          * itimer, profile or rlimit signals to this task while it is in late exit.
786          */
787         tsk->it_virt_value = 0;
788         tsk->it_prof_value = 0;
789         tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
790
791         /*
792          * In the preemption case it must be impossible for the task
793          * to get runnable again, so use "_raw_" unlock to keep
794          * preempt_count elevated until we schedule().
795          *
796          * To avoid deadlock on SMP, interrupts must be unmasked.  If we
797          * don't, subsequently called functions (e.g, wait_task_inactive()
798          * via release_task()) will spin, with interrupt flags
799          * unwittingly blocked, until the other task sleeps.  That task
800          * may itself be waiting for smp_call_function() to answer and
801          * complete, and with interrupts blocked that will never happen.
802          */
803         _raw_write_unlock(&tasklist_lock);
804         local_irq_enable();
805
806         list_for_each_safe(_p, _n, &ptrace_dead) {
807                 list_del_init(_p);
808                 t = list_entry(_p,struct task_struct,ptrace_list);
809                 release_task(t);
810         }
811
812         /* If the process is dead, release it - nobody will wait for it */
813         if (state == TASK_DEAD)
814                 release_task(tsk);
815
816 }
817
818 asmlinkage NORET_TYPE void do_exit(long code)
819 {
820         struct task_struct *tsk = current;
821
822         if (unlikely(in_interrupt()))
823                 panic("Aiee, killing interrupt handler!");
824         if (unlikely(!tsk->pid))
825                 panic("Attempted to kill the idle task!");
826         if (unlikely(tsk->pid == 1))
827                 panic("Attempted to kill init!");
828         if (tsk->io_context)
829                 exit_io_context();
830         tsk->flags |= PF_EXITING;
831         del_timer_sync(&tsk->real_timer);
832
833         if (unlikely(in_atomic()))
834                 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
835                                 current->comm, current->pid,
836                                 preempt_count());
837
838         profile_exit_task(tsk);
839  
840         if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
841                 current->ptrace_message = code;
842                 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
843         }
844
845         acct_process(code);
846         if (current->tux_info) {
847 #ifdef CONFIG_TUX_DEBUG
848                 printk("Possibly unexpected TUX-thread exit(%ld) at %p?\n",
849                         code, __builtin_return_address(0));
850 #endif
851                 current->tux_exit();
852         }
853         __exit_mm(tsk);
854
855         exit_sem(tsk);
856         __exit_files(tsk);
857         __exit_fs(tsk);
858         exit_namespace(tsk);
859         exit_thread();
860
861         if (tsk->signal->leader)
862                 disassociate_ctty(1);
863
864         module_put(tsk->thread_info->exec_domain->module);
865         if (tsk->binfmt)
866                 module_put(tsk->binfmt->module);
867
868         tsk->exit_code = code;
869         exit_notify(tsk);
870 #ifdef CONFIG_NUMA
871         mpol_free(tsk->mempolicy);
872         tsk->mempolicy = NULL;
873 #endif
874         schedule();
875         BUG();
876         /* Avoid "noreturn function does return".  */
877         for (;;) ;
878 }
879
880 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
881 {
882         if (comp)
883                 complete(comp);
884         
885         do_exit(code);
886 }
887
888 EXPORT_SYMBOL(complete_and_exit);
889
890 asmlinkage long sys_exit(int error_code)
891 {
892         do_exit((error_code&0xff)<<8);
893 }
894
895 task_t fastcall *next_thread(const task_t *p)
896 {
897         const struct pid_link *link = p->pids + PIDTYPE_TGID;
898         const struct list_head *tmp, *head = &link->pidptr->task_list;
899
900 #ifdef CONFIG_SMP
901         if (!p->sighand)
902                 BUG();
903         if (!spin_is_locked(&p->sighand->siglock) &&
904                                 !rwlock_is_locked(&tasklist_lock))
905                 BUG();
906 #endif
907         tmp = link->pid_chain.next;
908         if (tmp == head)
909                 tmp = head->next;
910
911         return pid_task(tmp, PIDTYPE_TGID);
912 }
913
914 EXPORT_SYMBOL(next_thread);
915
916 /*
917  * Take down every thread in the group.  This is called by fatal signals
918  * as well as by sys_exit_group (below).
919  */
920 NORET_TYPE void
921 do_group_exit(int exit_code)
922 {
923         BUG_ON(exit_code & 0x80); /* core dumps don't get here */
924
925         if (current->signal->group_exit)
926                 exit_code = current->signal->group_exit_code;
927         else if (!thread_group_empty(current)) {
928                 struct signal_struct *const sig = current->signal;
929                 struct sighand_struct *const sighand = current->sighand;
930                 read_lock(&tasklist_lock);
931                 spin_lock_irq(&sighand->siglock);
932                 if (sig->group_exit)
933                         /* Another thread got here before we took the lock.  */
934                         exit_code = sig->group_exit_code;
935                 else {
936                         sig->group_exit = 1;
937                         sig->group_exit_code = exit_code;
938                         zap_other_threads(current);
939                 }
940                 spin_unlock_irq(&sighand->siglock);
941                 read_unlock(&tasklist_lock);
942         }
943
944         do_exit(exit_code);
945         /* NOTREACHED */
946 }
947
948 /*
949  * this kills every thread in the thread group. Note that any externally
950  * wait4()-ing process will get the correct exit code - even if this
951  * thread is not the thread group leader.
952  */
953 asmlinkage void sys_exit_group(int error_code)
954 {
955         do_group_exit((error_code & 0xff) << 8);
956 }
957
958 static int eligible_child(pid_t pid, int options, task_t *p)
959 {
960         if (pid > 0) {
961                 if (p->pid != pid)
962                         return 0;
963         } else if (!pid) {
964                 if (process_group(p) != process_group(current))
965                         return 0;
966         } else if (pid != -1) {
967                 if (process_group(p) != -pid)
968                         return 0;
969         }
970
971         /*
972          * Do not consider detached threads that are
973          * not ptraced:
974          */
975         if (p->exit_signal == -1 && !p->ptrace)
976                 return 0;
977
978         /* Wait for all children (clone and not) if __WALL is set;
979          * otherwise, wait for clone children *only* if __WCLONE is
980          * set; otherwise, wait for non-clone children *only*.  (Note:
981          * A "clone" child here is one that reports to its parent
982          * using a signal other than SIGCHLD.) */
983         if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
984             && !(options & __WALL))
985                 return 0;
986         /*
987          * Do not consider thread group leaders that are
988          * in a non-empty thread group:
989          */
990         if (current->tgid != p->tgid && delay_group_leader(p))
991                 return 2;
992
993         if (security_task_wait(p))
994                 return 0;
995
996         return 1;
997 }
998
999 /*
1000  * Handle sys_wait4 work for one task in state TASK_ZOMBIE.  We hold
1001  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1002  * the lock and this task is uninteresting.  If we return nonzero, we have
1003  * released the lock and the system call should return.
1004  */
1005 static int wait_task_zombie(task_t *p, unsigned int __user *stat_addr, struct rusage __user *ru)
1006 {
1007         unsigned long state;
1008         int retval;
1009
1010         /*
1011          * Try to move the task's state to DEAD
1012          * only one thread is allowed to do this:
1013          */
1014         state = xchg(&p->state, TASK_DEAD);
1015         if (state != TASK_ZOMBIE) {
1016                 BUG_ON(state != TASK_DEAD);
1017                 return 0;
1018         }
1019         if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1020                 /*
1021                  * This can only happen in a race with a ptraced thread
1022                  * dying on another processor.
1023                  */
1024                 return 0;
1025
1026         /*
1027          * Now we are sure this task is interesting, and no other
1028          * thread can reap it because we set its state to TASK_DEAD.
1029          */
1030         read_unlock(&tasklist_lock);
1031
1032         retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1033         if (!retval && stat_addr) {
1034                 if (p->signal->group_exit)
1035                         retval = put_user(p->signal->group_exit_code, stat_addr);
1036                 else
1037                         retval = put_user(p->exit_code, stat_addr);
1038         }
1039         if (retval) {
1040                 p->state = TASK_ZOMBIE;
1041                 return retval;
1042         }
1043         retval = p->pid;
1044         if (p->real_parent != p->parent) {
1045                 write_lock_irq(&tasklist_lock);
1046                 /* Double-check with lock held.  */
1047                 if (p->real_parent != p->parent) {
1048                         __ptrace_unlink(p);
1049                         p->state = TASK_ZOMBIE;
1050                         /*
1051                          * If this is not a detached task, notify the parent.  If it's
1052                          * still not detached after that, don't release it now.
1053                          */
1054                         if (p->exit_signal != -1) {
1055                                 do_notify_parent(p, p->exit_signal);
1056                                 if (p->exit_signal != -1)
1057                                         p = NULL;
1058                         }
1059                 }
1060                 write_unlock_irq(&tasklist_lock);
1061         }
1062         if (p != NULL)
1063                 release_task(p);
1064         BUG_ON(!retval);
1065         return retval;
1066 }
1067
1068 /*
1069  * Handle sys_wait4 work for one task in state TASK_STOPPED.  We hold
1070  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1071  * the lock and this task is uninteresting.  If we return nonzero, we have
1072  * released the lock and the system call should return.
1073  */
1074 static int wait_task_stopped(task_t *p, int delayed_group_leader,
1075                              unsigned int __user *stat_addr,
1076                              struct rusage __user *ru)
1077 {
1078         int retval, exit_code;
1079
1080         if (!p->exit_code)
1081                 return 0;
1082         if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1083             p->signal && p->signal->group_stop_count > 0)
1084                 /*
1085                  * A group stop is in progress and this is the group leader.
1086                  * We won't report until all threads have stopped.
1087                  */
1088                 return 0;
1089
1090         /*
1091          * Now we are pretty sure this task is interesting.
1092          * Make sure it doesn't get reaped out from under us while we
1093          * give up the lock and then examine it below.  We don't want to
1094          * keep holding onto the tasklist_lock while we call getrusage and
1095          * possibly take page faults for user memory.
1096          */
1097         get_task_struct(p);
1098         read_unlock(&tasklist_lock);
1099         write_lock_irq(&tasklist_lock);
1100
1101         /*
1102          * This uses xchg to be atomic with the thread resuming and setting
1103          * it.  It must also be done with the write lock held to prevent a
1104          * race with the TASK_ZOMBIE case.
1105          */
1106         exit_code = xchg(&p->exit_code, 0);
1107         if (unlikely(p->state > TASK_STOPPED)) {
1108                 /*
1109                  * The task resumed and then died.  Let the next iteration
1110                  * catch it in TASK_ZOMBIE.  Note that exit_code might
1111                  * already be zero here if it resumed and did _exit(0).
1112                  * The task itself is dead and won't touch exit_code again;
1113                  * other processors in this function are locked out.
1114                  */
1115                 p->exit_code = exit_code;
1116                 exit_code = 0;
1117         }
1118         if (unlikely(exit_code == 0)) {
1119                 /*
1120                  * Another thread in this function got to it first, or it
1121                  * resumed, or it resumed and then died.
1122                  */
1123                 write_unlock_irq(&tasklist_lock);
1124                 put_task_struct(p);
1125                 read_lock(&tasklist_lock);
1126                 return 0;
1127         }
1128
1129         /* move to end of parent's list to avoid starvation */
1130         remove_parent(p);
1131         add_parent(p, p->parent);
1132
1133         write_unlock_irq(&tasklist_lock);
1134
1135         retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1136         if (!retval && stat_addr)
1137                 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1138         if (!retval)
1139                 retval = p->pid;
1140         put_task_struct(p);
1141
1142         BUG_ON(!retval);
1143         return retval;
1144 }
1145
1146 asmlinkage long sys_wait4(pid_t pid,unsigned int __user *stat_addr, int options, struct rusage __user *ru)
1147 {
1148         DECLARE_WAITQUEUE(wait, current);
1149         struct task_struct *tsk;
1150         int flag, retval;
1151
1152         if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
1153                 return -EINVAL;
1154
1155         add_wait_queue(&current->wait_chldexit,&wait);
1156 repeat:
1157         flag = 0;
1158         current->state = TASK_INTERRUPTIBLE;
1159         read_lock(&tasklist_lock);
1160         tsk = current;
1161         do {
1162                 struct task_struct *p;
1163                 struct list_head *_p;
1164                 int ret;
1165
1166                 list_for_each(_p,&tsk->children) {
1167                         p = list_entry(_p,struct task_struct,sibling);
1168
1169                         ret = eligible_child(pid, options, p);
1170                         if (!ret)
1171                                 continue;
1172                         flag = 1;
1173
1174                         switch (p->state) {
1175                         case TASK_STOPPED:
1176                                 if (!(options & WUNTRACED) &&
1177                                     !(p->ptrace & PT_PTRACED))
1178                                         continue;
1179                                 retval = wait_task_stopped(p, ret == 2,
1180                                                            stat_addr, ru);
1181                                 if (retval != 0) /* He released the lock.  */
1182                                         goto end_wait4;
1183                                 break;
1184                         case TASK_ZOMBIE:
1185                                 /*
1186                                  * Eligible but we cannot release it yet:
1187                                  */
1188                                 if (ret == 2)
1189                                         continue;
1190                                 retval = wait_task_zombie(p, stat_addr, ru);
1191                                 if (retval != 0) /* He released the lock.  */
1192                                         goto end_wait4;
1193                                 break;
1194                         }
1195                 }
1196                 if (!flag) {
1197                         list_for_each (_p,&tsk->ptrace_children) {
1198                                 p = list_entry(_p,struct task_struct,ptrace_list);
1199                                 if (!eligible_child(pid, options, p))
1200                                         continue;
1201                                 flag = 1;
1202                                 break;
1203                         }
1204                 }
1205                 if (options & __WNOTHREAD)
1206                         break;
1207                 tsk = next_thread(tsk);
1208                 if (tsk->signal != current->signal)
1209                         BUG();
1210         } while (tsk != current);
1211         read_unlock(&tasklist_lock);
1212         if (flag) {
1213                 retval = 0;
1214                 if (options & WNOHANG)
1215                         goto end_wait4;
1216                 retval = -ERESTARTSYS;
1217                 if (signal_pending(current))
1218                         goto end_wait4;
1219                 schedule();
1220                 goto repeat;
1221         }
1222         retval = -ECHILD;
1223 end_wait4:
1224         current->state = TASK_RUNNING;
1225         remove_wait_queue(&current->wait_chldexit,&wait);
1226         return retval;
1227 }
1228
1229 #ifdef __ARCH_WANT_SYS_WAITPID
1230
1231 /*
1232  * sys_waitpid() remains for compatibility. waitpid() should be
1233  * implemented by calling sys_wait4() from libc.a.
1234  */
1235 asmlinkage long sys_waitpid(pid_t pid, unsigned __user *stat_addr, int options)
1236 {
1237         return sys_wait4(pid, stat_addr, options, NULL);
1238 }
1239
1240 #endif