This commit was manufactured by cvs2svn to create tag
[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/cpu.h>
19 #include <linux/acct.h>
20 #include <linux/file.h>
21 #include <linux/binfmts.h>
22 #include <linux/ptrace.h>
23 #include <linux/profile.h>
24 #include <linux/mount.h>
25 #include <linux/proc_fs.h>
26 #include <linux/mempolicy.h>
27 #include <linux/ckrm.h>
28 #include <linux/ckrm_tsk.h>
29 #include <linux/vs_limit.h>
30 #include <linux/ckrm_mem.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/unistd.h>
34 #include <asm/pgtable.h>
35 #include <asm/mmu_context.h>
36
37 extern void sem_exit (void);
38 extern struct task_struct *child_reaper;
39
40 int getrusage(struct task_struct *, int, struct rusage __user *);
41
42 static void __unhash_process(struct task_struct *p)
43 {
44         nr_threads--;
45         /* tasklist_lock is held, is this sufficient? */
46         if (p->vx_info) {
47                 atomic_dec(&p->vx_info->cvirt.nr_threads);
48                 vx_nproc_dec(p);
49         }
50         detach_pid(p, PIDTYPE_PID);
51         detach_pid(p, PIDTYPE_TGID);
52         if (thread_group_leader(p)) {
53                 detach_pid(p, PIDTYPE_PGID);
54                 detach_pid(p, PIDTYPE_SID);
55                 if (p->pid)
56                         __get_cpu_var(process_counts)--;
57         }
58
59         REMOVE_LINKS(p);
60 }
61
62 void release_task(struct task_struct * p)
63 {
64         int zap_leader;
65         task_t *leader;
66         struct dentry *proc_dentry;
67
68 repeat: 
69         atomic_dec(&p->user->processes);
70         spin_lock(&p->proc_lock);
71         proc_dentry = proc_pid_unhash(p);
72         write_lock_irq(&tasklist_lock);
73         if (unlikely(p->ptrace))
74                 __ptrace_unlink(p);
75         BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
76         __exit_signal(p);
77         __exit_sighand(p);
78         __unhash_process(p);
79
80         /*
81          * If we are the last non-leader member of the thread
82          * group, and the leader is zombie, then notify the
83          * group leader's parent process. (if it wants notification.)
84          */
85         zap_leader = 0;
86         leader = p->group_leader;
87         if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
88                 BUG_ON(leader->exit_signal == -1);
89                 do_notify_parent(leader, leader->exit_signal);
90                 /*
91                  * If we were the last child thread and the leader has
92                  * exited already, and the leader's parent ignores SIGCHLD,
93                  * then we are the one who should release the leader.
94                  *
95                  * do_notify_parent() will have marked it self-reaping in
96                  * that case.
97                  */
98                 zap_leader = (leader->exit_signal == -1);
99         }
100
101         sched_exit(p);
102         write_unlock_irq(&tasklist_lock);
103         spin_unlock(&p->proc_lock);
104         dput(proc_dentry);
105         release_thread(p);
106         put_task_struct(p);
107
108         p = leader;
109         if (unlikely(zap_leader))
110                 goto repeat;
111 }
112
113 /* we are using it only for SMP init */
114
115 void unhash_process(struct task_struct *p)
116 {
117         struct dentry *proc_dentry;
118
119         spin_lock(&p->proc_lock);
120         proc_dentry = proc_pid_unhash(p);
121         write_lock_irq(&tasklist_lock);
122         __unhash_process(p);
123         write_unlock_irq(&tasklist_lock);
124         spin_unlock(&p->proc_lock);
125         proc_pid_flush(proc_dentry);
126 }
127
128 /*
129  * This checks not only the pgrp, but falls back on the pid if no
130  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
131  * without this...
132  */
133 int session_of_pgrp(int pgrp)
134 {
135         struct task_struct *p;
136         int sid = -1;
137
138         read_lock(&tasklist_lock);
139         do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
140                 if (p->signal->session > 0) {
141                         sid = p->signal->session;
142                         goto out;
143                 }
144         } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
145         p = find_task_by_pid(pgrp);
146         if (p)
147                 sid = p->signal->session;
148 out:
149         read_unlock(&tasklist_lock);
150         
151         return sid;
152 }
153
154 /*
155  * Determine if a process group is "orphaned", according to the POSIX
156  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
157  * by terminal-generated stop signals.  Newly orphaned process groups are
158  * to receive a SIGHUP and a SIGCONT.
159  *
160  * "I ask you, have you ever known what it is to be an orphan?"
161  */
162 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
163 {
164         struct task_struct *p;
165         int ret = 1;
166
167         do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
168                 if (p == ignored_task
169                                 || p->exit_state >= EXIT_ZOMBIE
170                                 || p->real_parent->pid == 1)
171                         continue;
172                 if (process_group(p->real_parent) != pgrp
173                             && p->real_parent->signal->session == p->signal->session) {
174                         ret = 0;
175                         break;
176                 }
177         } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
178         return ret;     /* (sighing) "Often!" */
179 }
180
181 int is_orphaned_pgrp(int pgrp)
182 {
183         int retval;
184
185         read_lock(&tasklist_lock);
186         retval = will_become_orphaned_pgrp(pgrp, NULL);
187         read_unlock(&tasklist_lock);
188
189         return retval;
190 }
191
192 static inline int has_stopped_jobs(int pgrp)
193 {
194         int retval = 0;
195         struct task_struct *p;
196
197         do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
198                 if (p->state != TASK_STOPPED)
199                         continue;
200
201                 /* If p is stopped by a debugger on a signal that won't
202                    stop it, then don't count p as stopped.  This isn't
203                    perfect but it's a good approximation.  */
204                 if (unlikely (p->ptrace)
205                     && p->exit_code != SIGSTOP
206                     && p->exit_code != SIGTSTP
207                     && p->exit_code != SIGTTOU
208                     && p->exit_code != SIGTTIN)
209                         continue;
210
211                 retval = 1;
212                 break;
213         } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
214         return retval;
215 }
216
217 /**
218  * reparent_to_init() - Reparent the calling kernel thread to the init task.
219  *
220  * If a kernel thread is launched as a result of a system call, or if
221  * it ever exits, it should generally reparent itself to init so that
222  * it is correctly cleaned up on exit.
223  *
224  * The various task state such as scheduling policy and priority may have
225  * been inherited from a user process, so we reset them to sane values here.
226  *
227  * NOTE that reparent_to_init() gives the caller full capabilities.
228  */
229 void reparent_to_init(void)
230 {
231         write_lock_irq(&tasklist_lock);
232
233         ptrace_unlink(current);
234         /* Reparent to init */
235         REMOVE_LINKS(current);
236         /* FIXME handle vchild_reaper/initpid */
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 __user *)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                                         cond_resched();
382                                 }
383                                 // vx_openfd_dec(fd);
384                         }
385                         i++;
386                         set >>= 1;
387                 }
388         }
389 }
390
391 struct files_struct *get_files_struct(struct task_struct *task)
392 {
393         struct files_struct *files;
394
395         task_lock(task);
396         files = task->files;
397         if (files)
398                 atomic_inc(&files->count);
399         task_unlock(task);
400
401         return files;
402 }
403
404 void fastcall put_files_struct(struct files_struct *files)
405 {
406         if (atomic_dec_and_test(&files->count)) {
407                 close_files(files);
408                 /*
409                  * Free the fd and fdset arrays if we expanded them.
410                  */
411                 if (files->fd != &files->fd_array[0])
412                         free_fd_array(files->fd, files->max_fds);
413                 if (files->max_fdset > __FD_SETSIZE) {
414                         free_fdset(files->open_fds, files->max_fdset);
415                         free_fdset(files->close_on_exec, files->max_fdset);
416                 }
417                 kmem_cache_free(files_cachep, files);
418         }
419 }
420
421 EXPORT_SYMBOL(put_files_struct);
422
423 static inline void __exit_files(struct task_struct *tsk)
424 {
425         struct files_struct * files = tsk->files;
426
427         if (files) {
428                 task_lock(tsk);
429                 tsk->files = NULL;
430                 task_unlock(tsk);
431                 put_files_struct(files);
432         }
433 }
434
435 void exit_files(struct task_struct *tsk)
436 {
437         __exit_files(tsk);
438 }
439
440 static inline void __put_fs_struct(struct fs_struct *fs)
441 {
442         /* No need to hold fs->lock if we are killing it */
443         if (atomic_dec_and_test(&fs->count)) {
444                 dput(fs->root);
445                 mntput(fs->rootmnt);
446                 dput(fs->pwd);
447                 mntput(fs->pwdmnt);
448                 if (fs->altroot) {
449                         dput(fs->altroot);
450                         mntput(fs->altrootmnt);
451                 }
452                 kmem_cache_free(fs_cachep, fs);
453         }
454 }
455
456 void put_fs_struct(struct fs_struct *fs)
457 {
458         __put_fs_struct(fs);
459 }
460
461 static inline void __exit_fs(struct task_struct *tsk)
462 {
463         struct fs_struct * fs = tsk->fs;
464
465         if (fs) {
466                 task_lock(tsk);
467                 tsk->fs = NULL;
468                 task_unlock(tsk);
469                 __put_fs_struct(fs);
470         }
471 }
472
473 void exit_fs(struct task_struct *tsk)
474 {
475         __exit_fs(tsk);
476 }
477
478 EXPORT_SYMBOL_GPL(exit_fs);
479
480 /*
481  * Turn us into a lazy TLB process if we
482  * aren't already..
483  */
484 static inline void __exit_mm(struct task_struct * tsk)
485 {
486         struct mm_struct *mm = tsk->mm;
487
488         mm_release(tsk, mm);
489         if (!mm)
490                 return;
491         /*
492          * Serialize with any possible pending coredump.
493          * We must hold mmap_sem around checking core_waiters
494          * and clearing tsk->mm.  The core-inducing thread
495          * will increment core_waiters for each thread in the
496          * group with ->mm != NULL.
497          */
498         down_read(&mm->mmap_sem);
499         if (mm->core_waiters) {
500                 up_read(&mm->mmap_sem);
501                 down_write(&mm->mmap_sem);
502                 if (!--mm->core_waiters)
503                         complete(mm->core_startup_done);
504                 up_write(&mm->mmap_sem);
505
506                 wait_for_completion(&mm->core_done);
507                 down_read(&mm->mmap_sem);
508         }
509         atomic_inc(&mm->mm_count);
510         if (mm != tsk->active_mm) BUG();
511         /* more a memory barrier than a real lock */
512         task_lock(tsk);
513         tsk->mm = NULL;
514         up_read(&mm->mmap_sem);
515 #ifdef CONFIG_CKRM_RES_MEM
516         spin_lock(&mm->peertask_lock);
517         list_del_init(&tsk->mm_peers);
518         ckrm_mem_evaluate_mm(mm);
519         spin_unlock(&mm->peertask_lock);
520 #endif
521         enter_lazy_tlb(mm, current);
522         task_unlock(tsk);
523         mmput(mm);
524 }
525
526 void exit_mm(struct task_struct *tsk)
527 {
528         __exit_mm(tsk);
529 }
530
531 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
532 {
533         /*
534          * Make sure we're not reparenting to ourselves and that
535          * the parent is not a zombie.
536          */
537         BUG_ON(p == reaper || reaper->state >= EXIT_ZOMBIE || reaper->exit_state >= EXIT_ZOMBIE);
538         p->real_parent = reaper;
539         if (p->parent == p->real_parent)
540                 BUG();
541 }
542
543 static inline void reparent_thread(task_t *p, task_t *father, int traced)
544 {
545         /* We don't want people slaying init.  */
546         if (p->exit_signal != -1)
547                 p->exit_signal = SIGCHLD;
548
549         if (p->pdeath_signal)
550                 /* We already hold the tasklist_lock here.  */
551                 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
552
553         /* Move the child from its dying parent to the new one.  */
554         if (unlikely(traced)) {
555                 /* Preserve ptrace links if someone else is tracing this child.  */
556                 list_del_init(&p->ptrace_list);
557                 if (p->parent != p->real_parent)
558                         list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
559         } else {
560                 /* If this child is being traced, then we're the one tracing it
561                  * anyway, so let go of it.
562                  */
563                 p->ptrace = 0;
564                 list_del_init(&p->sibling);
565                 p->parent = p->real_parent;
566                 list_add_tail(&p->sibling, &p->parent->children);
567
568                 /* If we'd notified the old parent about this child's death,
569                  * also notify the new parent.
570                  */
571                 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
572                     thread_group_empty(p))
573                         do_notify_parent(p, p->exit_signal);
574                 else if (p->state == TASK_TRACED) {
575                         /*
576                          * If it was at a trace stop, turn it into
577                          * a normal stop since it's no longer being
578                          * traced.
579                          */
580                         p->state = TASK_STOPPED;
581                 }
582         }
583
584         /*
585          * process group orphan check
586          * Case ii: Our child is in a different pgrp
587          * than we are, and it was the only connection
588          * outside, so the child pgrp is now orphaned.
589          */
590         if ((process_group(p) != process_group(father)) &&
591             (p->signal->session == father->signal->session)) {
592                 int pgrp = process_group(p);
593
594                 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
595                         __kill_pg_info(SIGHUP, (void *)1, pgrp);
596                         __kill_pg_info(SIGCONT, (void *)1, pgrp);
597                 }
598         }
599 }
600
601 /*
602  * When we die, we re-parent all our children.
603  * Try to give them to another thread in our thread
604  * group, and if no such member exists, give it to
605  * the global child reaper process (ie "init")
606  */
607 static inline void forget_original_parent(struct task_struct * father,
608                                           struct list_head *to_release)
609 {
610         struct task_struct *p, *reaper = father;
611         struct list_head *_p, *_n;
612
613         do {
614                 reaper = next_thread(reaper);
615                 if (reaper == father) {
616                         reaper = child_reaper;
617                         break;
618                 }
619         } while (reaper->exit_state >= EXIT_ZOMBIE);
620
621         /*
622          * There are only two places where our children can be:
623          *
624          * - in our child list
625          * - in our ptraced child list
626          *
627          * Search them and reparent children.
628          */
629         list_for_each_safe(_p, _n, &father->children) {
630                 int ptrace;
631                 p = list_entry(_p,struct task_struct,sibling);
632
633                 ptrace = p->ptrace;
634
635                 /* if father isn't the real parent, then ptrace must be enabled */
636                 BUG_ON(father != p->real_parent && !ptrace);
637
638                 if (father == p->real_parent) {
639                         /* reparent with a reaper, real father it's us */
640                         choose_new_parent(p, reaper, child_reaper);
641                         reparent_thread(p, father, 0);
642                 } else {
643                         /* reparent ptraced task to its real parent */
644                         __ptrace_unlink (p);
645                         if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
646                             thread_group_empty(p))
647                                 do_notify_parent(p, p->exit_signal);
648                 }
649
650                 /*
651                  * if the ptraced child is a zombie with exit_signal == -1
652                  * we must collect it before we exit, or it will remain
653                  * zombie forever since we prevented it from self-reap itself
654                  * while it was being traced by us, to be able to see it in wait4.
655                  */
656                 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
657                         list_add(&p->ptrace_list, to_release);
658         }
659         list_for_each_safe(_p, _n, &father->ptrace_children) {
660                 p = list_entry(_p,struct task_struct,ptrace_list);
661                 choose_new_parent(p, reaper, child_reaper);
662                 reparent_thread(p, father, 1);
663         }
664 }
665
666 /*
667  * Send signals to all our closest relatives so that they know
668  * to properly mourn us..
669  */
670 static void exit_notify(struct task_struct *tsk)
671 {
672         int state;
673         struct task_struct *t;
674         struct list_head ptrace_dead, *_p, *_n;
675
676         ckrm_cb_exit(tsk);
677
678         if (signal_pending(tsk) && !tsk->signal->group_exit
679             && !thread_group_empty(tsk)) {
680                 /*
681                  * This occurs when there was a race between our exit
682                  * syscall and a group signal choosing us as the one to
683                  * wake up.  It could be that we are the only thread
684                  * alerted to check for pending signals, but another thread
685                  * should be woken now to take the signal since we will not.
686                  * Now we'll wake all the threads in the group just to make
687                  * sure someone gets all the pending signals.
688                  */
689                 read_lock(&tasklist_lock);
690                 spin_lock_irq(&tsk->sighand->siglock);
691                 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
692                         if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
693                                 recalc_sigpending_tsk(t);
694                                 if (signal_pending(t))
695                                         signal_wake_up(t, 0);
696                         }
697                 spin_unlock_irq(&tsk->sighand->siglock);
698                 read_unlock(&tasklist_lock);
699         }
700
701         write_lock_irq(&tasklist_lock);
702
703         /*
704          * This does two things:
705          *
706          * A.  Make init inherit all the child processes
707          * B.  Check to see if any process groups have become orphaned
708          *      as a result of our exiting, and if they have any stopped
709          *      jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
710          */
711
712         INIT_LIST_HEAD(&ptrace_dead);
713         forget_original_parent(tsk, &ptrace_dead);
714         BUG_ON(!list_empty(&tsk->children));
715         BUG_ON(!list_empty(&tsk->ptrace_children));
716
717         /*
718          * Check to see if any process groups have become orphaned
719          * as a result of our exiting, and if they have any stopped
720          * jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
721          *
722          * Case i: Our father is in a different pgrp than we are
723          * and we were the only connection outside, so our pgrp
724          * is about to become orphaned.
725          */
726          
727         t = tsk->real_parent;
728         
729         if ((process_group(t) != process_group(tsk)) &&
730             (t->signal->session == tsk->signal->session) &&
731             will_become_orphaned_pgrp(process_group(tsk), tsk) &&
732             has_stopped_jobs(process_group(tsk))) {
733                 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
734                 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
735         }
736
737         /* Let father know we died 
738          *
739          * Thread signals are configurable, but you aren't going to use
740          * that to send signals to arbitary processes. 
741          * That stops right now.
742          *
743          * If the parent exec id doesn't match the exec id we saved
744          * when we started then we know the parent has changed security
745          * domain.
746          *
747          * If our self_exec id doesn't match our parent_exec_id then
748          * we have changed execution domain as these two values started
749          * the same after a fork.
750          *      
751          */
752         
753         if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
754             ( tsk->parent_exec_id != t->self_exec_id  ||
755               tsk->self_exec_id != tsk->parent_exec_id)
756             && !capable(CAP_KILL))
757                 tsk->exit_signal = SIGCHLD;
758
759
760         /* If something other than our normal parent is ptracing us, then
761          * send it a SIGCHLD instead of honoring exit_signal.  exit_signal
762          * only has special meaning to our real parent.
763          */
764         if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
765                 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
766                 do_notify_parent(tsk, signal);
767         } else if (tsk->ptrace) {
768                 do_notify_parent(tsk, SIGCHLD);
769         }
770
771         state = EXIT_ZOMBIE;
772         if (tsk->exit_signal == -1 && tsk->ptrace == 0)
773                 state = EXIT_DEAD;
774         tsk->exit_state = state;
775
776         /*
777          * Clear these here so that update_process_times() won't try to deliver
778          * itimer, profile or rlimit signals to this task while it is in late exit.
779          */
780         tsk->it_virt_value = 0;
781         tsk->it_prof_value = 0;
782         tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
783
784         write_unlock_irq(&tasklist_lock);
785
786         list_for_each_safe(_p, _n, &ptrace_dead) {
787                 list_del_init(_p);
788                 t = list_entry(_p,struct task_struct,ptrace_list);
789                 release_task(t);
790         }
791
792         /* If the process is dead, release it - nobody will wait for it */
793         if (state == EXIT_DEAD)
794                 release_task(tsk);
795
796         /* PF_DEAD causes final put_task_struct after we schedule. */
797         preempt_disable();
798         tsk->flags |= PF_DEAD;
799 }
800
801 asmlinkage NORET_TYPE void do_exit(long code)
802 {
803         struct task_struct *tsk = current;
804
805         profile_task_exit(tsk);
806
807         if (unlikely(in_interrupt()))
808                 panic("Aiee, killing interrupt handler!");
809         if (unlikely(!tsk->pid))
810                 panic("Attempted to kill the idle task!");
811         if (unlikely(tsk->pid == 1))
812                 panic("Attempted to kill init!");
813         if (tsk->io_context)
814                 exit_io_context();
815         tsk->flags |= PF_EXITING;
816         del_timer_sync(&tsk->real_timer);
817
818         if (unlikely(in_atomic()))
819                 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
820                                 current->comm, current->pid,
821                                 preempt_count());
822
823         if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
824                 current->ptrace_message = code;
825                 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
826         }
827
828         acct_process(code);
829         if (current->tux_info) {
830 #ifdef CONFIG_TUX_DEBUG
831                 printk("Possibly unexpected TUX-thread exit(%ld) at %p?\n",
832                         code, __builtin_return_address(0));
833 #endif
834                 current->tux_exit();
835         }
836         __exit_mm(tsk);
837
838         exit_sem(tsk);
839         __exit_files(tsk);
840         __exit_fs(tsk);
841         exit_namespace(tsk);
842         exit_thread();
843
844         if (tsk->signal->leader)
845                 disassociate_ctty(1);
846
847         module_put(tsk->thread_info->exec_domain->module);
848         if (tsk->binfmt)
849                 module_put(tsk->binfmt->module);
850
851         tsk->exit_code = code;
852         exit_notify(tsk);
853 #ifdef CONFIG_NUMA
854         mpol_free(tsk->mempolicy);
855         tsk->mempolicy = NULL;
856 #endif
857
858         BUG_ON(!(current->flags & PF_DEAD));
859         schedule();
860         BUG();
861         /* Avoid "noreturn function does return".  */
862         for (;;) ;
863 }
864
865 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
866 {
867         if (comp)
868                 complete(comp);
869         
870         do_exit(code);
871 }
872
873 EXPORT_SYMBOL(complete_and_exit);
874
875 asmlinkage long sys_exit(int error_code)
876 {
877         do_exit((error_code&0xff)<<8);
878 }
879
880 task_t fastcall *next_thread(const task_t *p)
881 {
882 #ifdef CONFIG_SMP
883         if (!p->sighand)
884                 BUG();
885         if (!spin_is_locked(&p->sighand->siglock) &&
886                                 !rwlock_is_locked(&tasklist_lock))
887                 BUG();
888 #endif
889         return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
890 }
891
892 EXPORT_SYMBOL(next_thread);
893
894 /*
895  * Take down every thread in the group.  This is called by fatal signals
896  * as well as by sys_exit_group (below).
897  */
898 NORET_TYPE void
899 do_group_exit(int exit_code)
900 {
901         BUG_ON(exit_code & 0x80); /* core dumps don't get here */
902
903         if (current->signal->group_exit)
904                 exit_code = current->signal->group_exit_code;
905         else if (!thread_group_empty(current)) {
906                 struct signal_struct *const sig = current->signal;
907                 struct sighand_struct *const sighand = current->sighand;
908                 read_lock(&tasklist_lock);
909                 spin_lock_irq(&sighand->siglock);
910                 if (sig->group_exit)
911                         /* Another thread got here before we took the lock.  */
912                         exit_code = sig->group_exit_code;
913                 else {
914                         sig->group_exit = 1;
915                         sig->group_exit_code = exit_code;
916                         zap_other_threads(current);
917                 }
918                 spin_unlock_irq(&sighand->siglock);
919                 read_unlock(&tasklist_lock);
920         }
921
922         do_exit(exit_code);
923         /* NOTREACHED */
924 }
925
926 /*
927  * this kills every thread in the thread group. Note that any externally
928  * wait4()-ing process will get the correct exit code - even if this
929  * thread is not the thread group leader.
930  */
931 asmlinkage void sys_exit_group(int error_code)
932 {
933         do_group_exit((error_code & 0xff) << 8);
934 }
935
936 static int eligible_child(pid_t pid, int options, task_t *p)
937 {
938         if (pid > 0) {
939                 if (p->pid != pid)
940                         return 0;
941         } else if (!pid) {
942                 if (process_group(p) != process_group(current))
943                         return 0;
944         } else if (pid != -1) {
945                 if (process_group(p) != -pid)
946                         return 0;
947         }
948
949         /*
950          * Do not consider detached threads that are
951          * not ptraced:
952          */
953         if (p->exit_signal == -1 && !p->ptrace)
954                 return 0;
955
956         /* Wait for all children (clone and not) if __WALL is set;
957          * otherwise, wait for clone children *only* if __WCLONE is
958          * set; otherwise, wait for non-clone children *only*.  (Note:
959          * A "clone" child here is one that reports to its parent
960          * using a signal other than SIGCHLD.) */
961         if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
962             && !(options & __WALL))
963                 return 0;
964         /*
965          * Do not consider thread group leaders that are
966          * in a non-empty thread group:
967          */
968         if (current->tgid != p->tgid && delay_group_leader(p))
969                 return 2;
970
971         if (security_task_wait(p))
972                 return 0;
973
974         return 1;
975 }
976
977 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
978                                int why, int status,
979                                struct siginfo __user *infop,
980                                struct rusage __user *rusagep)
981 {
982         int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
983         put_task_struct(p);
984         if (!retval)
985                 retval = put_user(SIGCHLD, &infop->si_signo);
986         if (!retval)
987                 retval = put_user(0, &infop->si_errno);
988         if (!retval)
989                 retval = put_user((short)why, &infop->si_code);
990         if (!retval)
991                 retval = put_user(pid, &infop->si_pid);
992         if (!retval)
993                 retval = put_user(uid, &infop->si_uid);
994         if (!retval)
995                 retval = put_user(status, &infop->si_status);
996         if (!retval)
997                 retval = pid;
998         return retval;
999 }
1000
1001 /*
1002  * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold
1003  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1004  * the lock and this task is uninteresting.  If we return nonzero, we have
1005  * released the lock and the system call should return.
1006  */
1007 static int wait_task_zombie(task_t *p, int noreap,
1008                             struct siginfo __user *infop,
1009                             int __user *stat_addr, struct rusage __user *ru)
1010 {
1011         unsigned long state;
1012         int retval;
1013         int status;
1014
1015         if (unlikely(noreap)) {
1016                 pid_t pid = p->pid;
1017                 uid_t uid = p->uid;
1018                 int exit_code = p->exit_code;
1019                 int why, status;
1020
1021                 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1022                         return 0;
1023                 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1024                         return 0;
1025                 get_task_struct(p);
1026                 read_unlock(&tasklist_lock);
1027                 if ((exit_code & 0x7f) == 0) {
1028                         why = CLD_EXITED;
1029                         status = exit_code >> 8;
1030                 } else {
1031                         why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1032                         status = exit_code & 0x7f;
1033                 }
1034                 return wait_noreap_copyout(p, pid, uid, why,
1035                                            status, infop, ru);
1036         }
1037
1038         /*
1039          * Try to move the task's state to DEAD
1040          * only one thread is allowed to do this:
1041          */
1042         state = xchg(&p->exit_state, EXIT_DEAD);
1043         if (state != EXIT_ZOMBIE) {
1044                 BUG_ON(state != EXIT_DEAD);
1045                 return 0;
1046         }
1047         if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1048                 /*
1049                  * This can only happen in a race with a ptraced thread
1050                  * dying on another processor.
1051                  */
1052                 return 0;
1053         }
1054
1055         if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1056                 /*
1057                  * The resource counters for the group leader are in its
1058                  * own task_struct.  Those for dead threads in the group
1059                  * are in its signal_struct, as are those for the child
1060                  * processes it has previously reaped.  All these
1061                  * accumulate in the parent's signal_struct c* fields.
1062                  *
1063                  * We don't bother to take a lock here to protect these
1064                  * p->signal fields, because they are only touched by
1065                  * __exit_signal, which runs with tasklist_lock
1066                  * write-locked anyway, and so is excluded here.  We do
1067                  * need to protect the access to p->parent->signal fields,
1068                  * as other threads in the parent group can be right
1069                  * here reaping other children at the same time.
1070                  */
1071                 spin_lock_irq(&p->parent->sighand->siglock);
1072                 p->parent->signal->cutime +=
1073                         p->utime + p->signal->utime + p->signal->cutime;
1074                 p->parent->signal->cstime +=
1075                         p->stime + p->signal->stime + p->signal->cstime;
1076                 p->parent->signal->cmin_flt +=
1077                         p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1078                 p->parent->signal->cmaj_flt +=
1079                         p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1080                 p->parent->signal->cnvcsw +=
1081                         p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1082                 p->parent->signal->cnivcsw +=
1083                         p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1084                 spin_unlock_irq(&p->parent->sighand->siglock);
1085         }
1086
1087         /*
1088          * Now we are sure this task is interesting, and no other
1089          * thread can reap it because we set its state to EXIT_DEAD.
1090          */
1091         read_unlock(&tasklist_lock);
1092
1093         retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1094         status = p->signal->group_exit
1095                 ? p->signal->group_exit_code : p->exit_code;
1096         if (!retval && stat_addr)
1097                 retval = put_user(status, stat_addr);
1098         if (!retval && infop)
1099                 retval = put_user(SIGCHLD, &infop->si_signo);
1100         if (!retval && infop)
1101                 retval = put_user(0, &infop->si_errno);
1102         if (!retval && infop) {
1103                 int why;
1104
1105                 if ((status & 0x7f) == 0) {
1106                         why = CLD_EXITED;
1107                         status >>= 8;
1108                 } else {
1109                         why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1110                         status &= 0x7f;
1111                 }
1112                 retval = put_user((short)why, &infop->si_code);
1113                 if (!retval)
1114                         retval = put_user(status, &infop->si_status);
1115         }
1116         if (!retval && infop)
1117                 retval = put_user(p->pid, &infop->si_pid);
1118         if (!retval && infop)
1119                 retval = put_user(p->uid, &infop->si_uid);
1120         if (retval) {
1121                 // TODO: is this safe?
1122                 p->exit_state = EXIT_ZOMBIE;
1123                 return retval;
1124         }
1125         retval = p->pid;
1126         if (p->real_parent != p->parent) {
1127                 write_lock_irq(&tasklist_lock);
1128                 /* Double-check with lock held.  */
1129                 if (p->real_parent != p->parent) {
1130                         __ptrace_unlink(p);
1131                         // TODO: is this safe?
1132                         p->exit_state = EXIT_ZOMBIE;
1133                         /*
1134                          * If this is not a detached task, notify the parent.
1135                          * If it's still not detached after that, don't release
1136                          * it now.
1137                          */
1138                         if (p->exit_signal != -1) {
1139                                 do_notify_parent(p, p->exit_signal);
1140                                 if (p->exit_signal != -1)
1141                                         p = NULL;
1142                         }
1143                 }
1144                 write_unlock_irq(&tasklist_lock);
1145         }
1146         if (p != NULL)
1147                 release_task(p);
1148         BUG_ON(!retval);
1149         return retval;
1150 }
1151
1152 /*
1153  * Handle sys_wait4 work for one task in state TASK_STOPPED.  We hold
1154  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1155  * the lock and this task is uninteresting.  If we return nonzero, we have
1156  * released the lock and the system call should return.
1157  */
1158 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1159                              struct siginfo __user *infop,
1160                              int __user *stat_addr, struct rusage __user *ru)
1161 {
1162         int retval, exit_code;
1163
1164         if (!p->exit_code)
1165                 return 0;
1166         if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1167             p->signal && p->signal->group_stop_count > 0)
1168                 /*
1169                  * A group stop is in progress and this is the group leader.
1170                  * We won't report until all threads have stopped.
1171                  */
1172                 return 0;
1173
1174         /*
1175          * Now we are pretty sure this task is interesting.
1176          * Make sure it doesn't get reaped out from under us while we
1177          * give up the lock and then examine it below.  We don't want to
1178          * keep holding onto the tasklist_lock while we call getrusage and
1179          * possibly take page faults for user memory.
1180          */
1181         get_task_struct(p);
1182         read_unlock(&tasklist_lock);
1183
1184         if (unlikely(noreap)) {
1185                 pid_t pid = p->pid;
1186                 uid_t uid = p->uid;
1187                 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1188
1189                 exit_code = p->exit_code;
1190                 if (unlikely(!exit_code) ||
1191                     unlikely(p->state > TASK_STOPPED))
1192                         goto bail_ref;
1193                 return wait_noreap_copyout(p, pid, uid,
1194                                            why, (exit_code << 8) | 0x7f,
1195                                            infop, ru);
1196         }
1197
1198         write_lock_irq(&tasklist_lock);
1199
1200         /*
1201          * This uses xchg to be atomic with the thread resuming and setting
1202          * it.  It must also be done with the write lock held to prevent a
1203          * race with the EXIT_ZOMBIE case.
1204          */
1205         exit_code = xchg(&p->exit_code, 0);
1206         if (unlikely(p->exit_state >= EXIT_ZOMBIE)) {
1207                 /*
1208                  * The task resumed and then died.  Let the next iteration
1209                  * catch it in EXIT_ZOMBIE.  Note that exit_code might
1210                  * already be zero here if it resumed and did _exit(0).
1211                  * The task itself is dead and won't touch exit_code again;
1212                  * other processors in this function are locked out.
1213                  */
1214                 p->exit_code = exit_code;
1215                 exit_code = 0;
1216         }
1217         if (unlikely(exit_code == 0)) {
1218                 /*
1219                  * Another thread in this function got to it first, or it
1220                  * resumed, or it resumed and then died.
1221                  */
1222                 write_unlock_irq(&tasklist_lock);
1223 bail_ref:
1224                 put_task_struct(p);
1225                 /*
1226                  * We are returning to the wait loop without having successfully
1227                  * removed the process and having released the lock. We cannot
1228                  * continue, since the "p" task pointer is potentially stale.
1229                  *
1230                  * Return -EAGAIN, and do_wait() will restart the loop from the
1231                  * beginning. Do _not_ re-acquire the lock.
1232                  */
1233                 return -EAGAIN;
1234         }
1235
1236         /* move to end of parent's list to avoid starvation */
1237         remove_parent(p);
1238         add_parent(p, p->parent);
1239
1240         write_unlock_irq(&tasklist_lock);
1241
1242         retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1243         if (!retval && stat_addr)
1244                 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1245         if (!retval && infop)
1246                 retval = put_user(SIGCHLD, &infop->si_signo);
1247         if (!retval && infop)
1248                 retval = put_user(0, &infop->si_errno);
1249         if (!retval && infop)
1250                 retval = put_user((short)((p->ptrace & PT_PTRACED)
1251                                           ? CLD_TRAPPED : CLD_STOPPED),
1252                                   &infop->si_code);
1253         if (!retval && infop)
1254                 retval = put_user(exit_code, &infop->si_status);
1255         if (!retval && infop)
1256                 retval = put_user(p->pid, &infop->si_pid);
1257         if (!retval && infop)
1258                 retval = put_user(p->uid, &infop->si_uid);
1259         if (!retval)
1260                 retval = p->pid;
1261         put_task_struct(p);
1262
1263         BUG_ON(!retval);
1264         return retval;
1265 }
1266
1267 /*
1268  * Handle do_wait work for one task in a live, non-stopped state.
1269  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1270  * the lock and this task is uninteresting.  If we return nonzero, we have
1271  * released the lock and the system call should return.
1272  */
1273 static int wait_task_continued(task_t *p, int noreap,
1274                                struct siginfo __user *infop,
1275                                int __user *stat_addr, struct rusage __user *ru)
1276 {
1277         int retval;
1278         pid_t pid;
1279         uid_t uid;
1280
1281         if (unlikely(!p->signal))
1282                 return 0;
1283
1284         if (p->signal->stop_state >= 0)
1285                 return 0;
1286
1287         spin_lock_irq(&p->sighand->siglock);
1288         if (p->signal->stop_state >= 0) { /* Re-check with the lock held.  */
1289                 spin_unlock_irq(&p->sighand->siglock);
1290                 return 0;
1291         }
1292         if (!noreap)
1293                 p->signal->stop_state = 0;
1294         spin_unlock_irq(&p->sighand->siglock);
1295
1296         pid = p->pid;
1297         uid = p->uid;
1298         get_task_struct(p);
1299         read_unlock(&tasklist_lock);
1300
1301         if (!infop) {
1302                 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1303                 put_task_struct(p);
1304                 if (!retval && stat_addr)
1305                         retval = put_user(0xffff, stat_addr);
1306                 if (!retval)
1307                         retval = p->pid;
1308         } else {
1309                 retval = wait_noreap_copyout(p, pid, uid,
1310                                              CLD_CONTINUED, SIGCONT,
1311                                              infop, ru);
1312                 BUG_ON(retval == 0);
1313         }
1314
1315         return retval;
1316 }
1317
1318
1319 static inline int my_ptrace_child(struct task_struct *p)
1320 {
1321         if (!(p->ptrace & PT_PTRACED))
1322                 return 0;
1323         /*
1324          * This child was PTRACE_ATTACH'd.  We should be seeing it only if
1325          * we are the attacher.  If we are the real parent, this is a race
1326          * inside ptrace_attach.  It is waiting for the tasklist_lock,
1327          * which we have to switch the parent links, but has already set
1328          * the flags in p->ptrace.
1329          */
1330         return (p->parent != p->real_parent);
1331 }
1332
1333 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1334                     int __user *stat_addr, struct rusage __user *ru)
1335 {
1336         DECLARE_WAITQUEUE(wait, current);
1337         struct task_struct *tsk;
1338         int flag, retval;
1339
1340         add_wait_queue(&current->wait_chldexit,&wait);
1341 repeat:
1342         /*
1343          * We will set this flag if we see any child that might later
1344          * match our criteria, even if we are not able to reap it yet.
1345          */
1346         flag = 0;
1347         current->state = TASK_INTERRUPTIBLE;
1348         read_lock(&tasklist_lock);
1349         tsk = current;
1350         do {
1351                 struct task_struct *p;
1352                 struct list_head *_p;
1353                 int ret;
1354
1355                 list_for_each(_p,&tsk->children) {
1356                         p = list_entry(_p,struct task_struct,sibling);
1357
1358                         ret = eligible_child(pid, options, p);
1359                         if (!ret)
1360                                 continue;
1361
1362                         switch (p->state) {
1363                         case TASK_TRACED:
1364                                 if (!my_ptrace_child(p))
1365                                         continue;
1366                                 /*FALLTHROUGH*/
1367                         case TASK_STOPPED:
1368                                 /*
1369                                  * It's stopped now, so it might later
1370                                  * continue, exit, or stop again.
1371                                  */
1372                                 flag = 1;
1373                                 if (!(options & WUNTRACED) &&
1374                                     !my_ptrace_child(p))
1375                                         continue;
1376                                 retval = wait_task_stopped(p, ret == 2,
1377                                                            (options & WNOWAIT),
1378                                                            infop,
1379                                                            stat_addr, ru);
1380                                 if (retval == -EAGAIN)
1381                                         goto repeat;
1382                                 if (retval != 0) /* He released the lock.  */
1383                                         goto end;
1384                                 break;
1385                         default:
1386                         // case EXIT_DEAD:
1387                                 if (p->exit_state == EXIT_DEAD)
1388                                         continue;
1389                         // case EXIT_ZOMBIE:
1390                                 if (p->exit_state == EXIT_ZOMBIE) {
1391                                         /*
1392                                          * Eligible but we cannot release
1393                                          * it yet:
1394                                          */
1395                                         if (ret == 2)
1396                                                 goto check_continued;
1397                                         if (!likely(options & WEXITED))
1398                                                 continue;
1399                                         retval = wait_task_zombie(
1400                                                 p, (options & WNOWAIT),
1401                                                 infop, stat_addr, ru);
1402                                         /* He released the lock.  */
1403                                         if (retval != 0)
1404                                                 goto end;
1405                                         break;
1406                                 }
1407 check_continued:
1408                                 /*
1409                                  * It's running now, so it might later
1410                                  * exit, stop, or stop and then continue.
1411                                  */
1412                                 flag = 1;
1413                                 if (!unlikely(options & WCONTINUED))
1414                                         continue;
1415
1416                                 retval = wait_task_continued(
1417                                         p, (options & WNOWAIT),
1418                                         infop, stat_addr, ru);
1419                                 if (retval != 0) /* He released the lock.  */
1420                                         goto end;
1421                                 break;
1422                         }
1423                 }
1424                 if (!flag) {
1425                         list_for_each(_p, &tsk->ptrace_children) {
1426                                 p = list_entry(_p, struct task_struct,
1427                                                 ptrace_list);
1428                                 if (!eligible_child(pid, options, p))
1429                                         continue;
1430                                 flag = 1;
1431                                 break;
1432                         }
1433                 }
1434                 if (options & __WNOTHREAD)
1435                         break;
1436                 tsk = next_thread(tsk);
1437                 if (tsk->signal != current->signal)
1438                         BUG();
1439         } while (tsk != current);
1440
1441         read_unlock(&tasklist_lock);
1442         if (flag) {
1443                 retval = 0;
1444                 if (options & WNOHANG)
1445                         goto end;
1446                 retval = -ERESTARTSYS;
1447                 if (signal_pending(current))
1448                         goto end;
1449                 schedule();
1450                 goto repeat;
1451         }
1452         retval = -ECHILD;
1453 end:
1454         current->state = TASK_RUNNING;
1455         remove_wait_queue(&current->wait_chldexit,&wait);
1456         if (infop) {
1457                 if (retval > 0)
1458                 retval = 0;
1459                 else {
1460                         /*
1461                          * For a WNOHANG return, clear out all the fields
1462                          * we would set so the user can easily tell the
1463                          * difference.
1464                          */
1465                         if (!retval)
1466                                 retval = put_user(0, &infop->si_signo);
1467                         if (!retval)
1468                                 retval = put_user(0, &infop->si_errno);
1469                         if (!retval)
1470                                 retval = put_user(0, &infop->si_code);
1471                         if (!retval)
1472                                 retval = put_user(0, &infop->si_pid);
1473                         if (!retval)
1474                                 retval = put_user(0, &infop->si_uid);
1475                         if (!retval)
1476                                 retval = put_user(0, &infop->si_status);
1477                 }
1478         }
1479         return retval;
1480 }
1481
1482 asmlinkage long sys_waitid(int which, pid_t pid,
1483                            struct siginfo __user *infop, int options,
1484                            struct rusage __user *ru)
1485 {
1486         long ret;
1487
1488         if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1489                 return -EINVAL;
1490         if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1491                 return -EINVAL;
1492
1493         switch (which) {
1494         case P_ALL:
1495                 pid = -1;
1496                 break;
1497         case P_PID:
1498                 if (pid <= 0)
1499                         return -EINVAL;
1500                 break;
1501         case P_PGID:
1502                 if (pid <= 0)
1503                         return -EINVAL;
1504                 pid = -pid;
1505                 break;
1506         default:
1507                 return -EINVAL;
1508         }
1509
1510         ret = do_wait(pid, options, infop, NULL, ru);
1511
1512         /* avoid REGPARM breakage on x86: */
1513         prevent_tail_call(ret);
1514         return ret;
1515 }
1516
1517 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1518                           int options, struct rusage __user *ru)
1519 {
1520         long ret;
1521
1522         if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1523                         __WNOTHREAD|__WCLONE|__WALL))
1524                 return -EINVAL;
1525         ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1526
1527         /* avoid REGPARM breakage on x86: */
1528         prevent_tail_call(ret);
1529         return ret;
1530 }
1531
1532 #ifdef __ARCH_WANT_SYS_WAITPID
1533
1534 /*
1535  * sys_waitpid() remains for compatibility. waitpid() should be
1536  * implemented by calling sys_wait4() from libc.a.
1537  */
1538 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1539 {
1540         return sys_wait4(pid, stat_addr, options, NULL);
1541 }
1542
1543 #endif