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