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