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