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