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