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