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