aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/exit.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/exit.c')
-rw-r--r--kernel/exit.c459
1 files changed, 284 insertions, 175 deletions
diff --git a/kernel/exit.c b/kernel/exit.c
index 1510f78a0ffa..93d2711b9381 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -13,6 +13,7 @@
13#include <linux/personality.h> 13#include <linux/personality.h>
14#include <linux/tty.h> 14#include <linux/tty.h>
15#include <linux/mnt_namespace.h> 15#include <linux/mnt_namespace.h>
16#include <linux/iocontext.h>
16#include <linux/key.h> 17#include <linux/key.h>
17#include <linux/security.h> 18#include <linux/security.h>
18#include <linux/cpu.h> 19#include <linux/cpu.h>
@@ -70,7 +71,7 @@ static void __unhash_process(struct task_struct *p)
70 __get_cpu_var(process_counts)--; 71 __get_cpu_var(process_counts)--;
71 } 72 }
72 list_del_rcu(&p->thread_group); 73 list_del_rcu(&p->thread_group);
73 remove_parent(p); 74 list_del_init(&p->sibling);
74} 75}
75 76
76/* 77/*
@@ -126,6 +127,12 @@ static void __exit_signal(struct task_struct *tsk)
126 127
127 __unhash_process(tsk); 128 __unhash_process(tsk);
128 129
130 /*
131 * Do this under ->siglock, we can race with another thread
132 * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
133 */
134 flush_sigqueue(&tsk->pending);
135
129 tsk->signal = NULL; 136 tsk->signal = NULL;
130 tsk->sighand = NULL; 137 tsk->sighand = NULL;
131 spin_unlock(&sighand->siglock); 138 spin_unlock(&sighand->siglock);
@@ -133,7 +140,6 @@ static void __exit_signal(struct task_struct *tsk)
133 140
134 __cleanup_sighand(sighand); 141 __cleanup_sighand(sighand);
135 clear_tsk_thread_flag(tsk,TIF_SIGPENDING); 142 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
136 flush_sigqueue(&tsk->pending);
137 if (sig) { 143 if (sig) {
138 flush_sigqueue(&sig->shared_pending); 144 flush_sigqueue(&sig->shared_pending);
139 taskstats_tgid_free(sig); 145 taskstats_tgid_free(sig);
@@ -146,6 +152,18 @@ static void delayed_put_task_struct(struct rcu_head *rhp)
146 put_task_struct(container_of(rhp, struct task_struct, rcu)); 152 put_task_struct(container_of(rhp, struct task_struct, rcu));
147} 153}
148 154
155/*
156 * Do final ptrace-related cleanup of a zombie being reaped.
157 *
158 * Called with write_lock(&tasklist_lock) held.
159 */
160static void ptrace_release_task(struct task_struct *p)
161{
162 BUG_ON(!list_empty(&p->ptraced));
163 ptrace_unlink(p);
164 BUG_ON(!list_empty(&p->ptrace_entry));
165}
166
149void release_task(struct task_struct * p) 167void release_task(struct task_struct * p)
150{ 168{
151 struct task_struct *leader; 169 struct task_struct *leader;
@@ -154,8 +172,7 @@ repeat:
154 atomic_dec(&p->user->processes); 172 atomic_dec(&p->user->processes);
155 proc_flush_task(p); 173 proc_flush_task(p);
156 write_lock_irq(&tasklist_lock); 174 write_lock_irq(&tasklist_lock);
157 ptrace_unlink(p); 175 ptrace_release_task(p);
158 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
159 __exit_signal(p); 176 __exit_signal(p);
160 177
161 /* 178 /*
@@ -309,9 +326,8 @@ static void reparent_to_kthreadd(void)
309 326
310 ptrace_unlink(current); 327 ptrace_unlink(current);
311 /* Reparent to init */ 328 /* Reparent to init */
312 remove_parent(current);
313 current->real_parent = current->parent = kthreadd_task; 329 current->real_parent = current->parent = kthreadd_task;
314 add_parent(current); 330 list_move_tail(&current->sibling, &current->real_parent->children);
315 331
316 /* Set the exit signal to SIGCHLD so we signal init on exit */ 332 /* Set the exit signal to SIGCHLD so we signal init on exit */
317 current->exit_signal = SIGCHLD; 333 current->exit_signal = SIGCHLD;
@@ -686,37 +702,97 @@ static void exit_mm(struct task_struct * tsk)
686 mmput(mm); 702 mmput(mm);
687} 703}
688 704
689static void 705/*
690reparent_thread(struct task_struct *p, struct task_struct *father, int traced) 706 * Return nonzero if @parent's children should reap themselves.
707 *
708 * Called with write_lock_irq(&tasklist_lock) held.
709 */
710static int ignoring_children(struct task_struct *parent)
691{ 711{
692 if (p->pdeath_signal) 712 int ret;
693 /* We already hold the tasklist_lock here. */ 713 struct sighand_struct *psig = parent->sighand;
694 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p); 714 unsigned long flags;
715 spin_lock_irqsave(&psig->siglock, flags);
716 ret = (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
717 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT));
718 spin_unlock_irqrestore(&psig->siglock, flags);
719 return ret;
720}
695 721
696 /* Move the child from its dying parent to the new one. */ 722/*
697 if (unlikely(traced)) { 723 * Detach all tasks we were using ptrace on.
698 /* Preserve ptrace links if someone else is tracing this child. */ 724 * Any that need to be release_task'd are put on the @dead list.
699 list_del_init(&p->ptrace_list); 725 *
700 if (ptrace_reparented(p)) 726 * Called with write_lock(&tasklist_lock) held.
701 list_add(&p->ptrace_list, &p->real_parent->ptrace_children); 727 */
702 } else { 728static void ptrace_exit(struct task_struct *parent, struct list_head *dead)
703 /* If this child is being traced, then we're the one tracing it 729{
704 * anyway, so let go of it. 730 struct task_struct *p, *n;
731 int ign = -1;
732
733 list_for_each_entry_safe(p, n, &parent->ptraced, ptrace_entry) {
734 __ptrace_unlink(p);
735
736 if (p->exit_state != EXIT_ZOMBIE)
737 continue;
738
739 /*
740 * If it's a zombie, our attachedness prevented normal
741 * parent notification or self-reaping. Do notification
742 * now if it would have happened earlier. If it should
743 * reap itself, add it to the @dead list. We can't call
744 * release_task() here because we already hold tasklist_lock.
745 *
746 * If it's our own child, there is no notification to do.
747 * But if our normal children self-reap, then this child
748 * was prevented by ptrace and we must reap it now.
705 */ 749 */
706 p->ptrace = 0; 750 if (!task_detached(p) && thread_group_empty(p)) {
707 remove_parent(p); 751 if (!same_thread_group(p->real_parent, parent))
708 p->parent = p->real_parent; 752 do_notify_parent(p, p->exit_signal);
709 add_parent(p); 753 else {
754 if (ign < 0)
755 ign = ignoring_children(parent);
756 if (ign)
757 p->exit_signal = -1;
758 }
759 }
710 760
711 if (task_is_traced(p)) { 761 if (task_detached(p)) {
712 /* 762 /*
713 * If it was at a trace stop, turn it into 763 * Mark it as in the process of being reaped.
714 * a normal stop since it's no longer being
715 * traced.
716 */ 764 */
717 ptrace_untrace(p); 765 p->exit_state = EXIT_DEAD;
766 list_add(&p->ptrace_entry, dead);
718 } 767 }
719 } 768 }
769}
770
771/*
772 * Finish up exit-time ptrace cleanup.
773 *
774 * Called without locks.
775 */
776static void ptrace_exit_finish(struct task_struct *parent,
777 struct list_head *dead)
778{
779 struct task_struct *p, *n;
780
781 BUG_ON(!list_empty(&parent->ptraced));
782
783 list_for_each_entry_safe(p, n, dead, ptrace_entry) {
784 list_del_init(&p->ptrace_entry);
785 release_task(p);
786 }
787}
788
789static void reparent_thread(struct task_struct *p, struct task_struct *father)
790{
791 if (p->pdeath_signal)
792 /* We already hold the tasklist_lock here. */
793 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
794
795 list_move_tail(&p->sibling, &p->real_parent->children);
720 796
721 /* If this is a threaded reparent there is no need to 797 /* If this is a threaded reparent there is no need to
722 * notify anyone anything has happened. 798 * notify anyone anything has happened.
@@ -731,7 +807,8 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
731 /* If we'd notified the old parent about this child's death, 807 /* If we'd notified the old parent about this child's death,
732 * also notify the new parent. 808 * also notify the new parent.
733 */ 809 */
734 if (!traced && p->exit_state == EXIT_ZOMBIE && 810 if (!ptrace_reparented(p) &&
811 p->exit_state == EXIT_ZOMBIE &&
735 !task_detached(p) && thread_group_empty(p)) 812 !task_detached(p) && thread_group_empty(p))
736 do_notify_parent(p, p->exit_signal); 813 do_notify_parent(p, p->exit_signal);
737 814
@@ -748,12 +825,15 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
748static void forget_original_parent(struct task_struct *father) 825static void forget_original_parent(struct task_struct *father)
749{ 826{
750 struct task_struct *p, *n, *reaper = father; 827 struct task_struct *p, *n, *reaper = father;
751 struct list_head ptrace_dead; 828 LIST_HEAD(ptrace_dead);
752
753 INIT_LIST_HEAD(&ptrace_dead);
754 829
755 write_lock_irq(&tasklist_lock); 830 write_lock_irq(&tasklist_lock);
756 831
832 /*
833 * First clean up ptrace if we were using it.
834 */
835 ptrace_exit(father, &ptrace_dead);
836
757 do { 837 do {
758 reaper = next_thread(reaper); 838 reaper = next_thread(reaper);
759 if (reaper == father) { 839 if (reaper == father) {
@@ -762,58 +842,19 @@ static void forget_original_parent(struct task_struct *father)
762 } 842 }
763 } while (reaper->flags & PF_EXITING); 843 } while (reaper->flags & PF_EXITING);
764 844
765 /*
766 * There are only two places where our children can be:
767 *
768 * - in our child list
769 * - in our ptraced child list
770 *
771 * Search them and reparent children.
772 */
773 list_for_each_entry_safe(p, n, &father->children, sibling) { 845 list_for_each_entry_safe(p, n, &father->children, sibling) {
774 int ptrace;
775
776 ptrace = p->ptrace;
777
778 /* if father isn't the real parent, then ptrace must be enabled */
779 BUG_ON(father != p->real_parent && !ptrace);
780
781 if (father == p->real_parent) {
782 /* reparent with a reaper, real father it's us */
783 p->real_parent = reaper;
784 reparent_thread(p, father, 0);
785 } else {
786 /* reparent ptraced task to its real parent */
787 __ptrace_unlink (p);
788 if (p->exit_state == EXIT_ZOMBIE && !task_detached(p) &&
789 thread_group_empty(p))
790 do_notify_parent(p, p->exit_signal);
791 }
792
793 /*
794 * if the ptraced child is a detached zombie we must collect
795 * it before we exit, or it will remain zombie forever since
796 * we prevented it from self-reap itself while it was being
797 * traced by us, to be able to see it in wait4.
798 */
799 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && task_detached(p)))
800 list_add(&p->ptrace_list, &ptrace_dead);
801 }
802
803 list_for_each_entry_safe(p, n, &father->ptrace_children, ptrace_list) {
804 p->real_parent = reaper; 846 p->real_parent = reaper;
805 reparent_thread(p, father, 1); 847 if (p->parent == father) {
848 BUG_ON(p->ptrace);
849 p->parent = p->real_parent;
850 }
851 reparent_thread(p, father);
806 } 852 }
807 853
808 write_unlock_irq(&tasklist_lock); 854 write_unlock_irq(&tasklist_lock);
809 BUG_ON(!list_empty(&father->children)); 855 BUG_ON(!list_empty(&father->children));
810 BUG_ON(!list_empty(&father->ptrace_children));
811
812 list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_list) {
813 list_del_init(&p->ptrace_list);
814 release_task(p);
815 }
816 856
857 ptrace_exit_finish(father, &ptrace_dead);
817} 858}
818 859
819/* 860/*
@@ -1174,13 +1215,6 @@ static int eligible_child(enum pid_type type, struct pid *pid, int options,
1174 return 0; 1215 return 0;
1175 } 1216 }
1176 1217
1177 /*
1178 * Do not consider detached threads that are
1179 * not ptraced:
1180 */
1181 if (task_detached(p) && !p->ptrace)
1182 return 0;
1183
1184 /* Wait for all children (clone and not) if __WALL is set; 1218 /* Wait for all children (clone and not) if __WALL is set;
1185 * otherwise, wait for clone children *only* if __WCLONE is 1219 * otherwise, wait for clone children *only* if __WCLONE is
1186 * set; otherwise, wait for non-clone children *only*. (Note: 1220 * set; otherwise, wait for non-clone children *only*. (Note:
@@ -1191,14 +1225,10 @@ static int eligible_child(enum pid_type type, struct pid *pid, int options,
1191 return 0; 1225 return 0;
1192 1226
1193 err = security_task_wait(p); 1227 err = security_task_wait(p);
1194 if (likely(!err)) 1228 if (err)
1195 return 1; 1229 return err;
1196 1230
1197 if (type != PIDTYPE_PID) 1231 return 1;
1198 return 0;
1199 /* This child was explicitly requested, abort */
1200 read_unlock(&tasklist_lock);
1201 return err;
1202} 1232}
1203 1233
1204static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid, 1234static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
@@ -1232,7 +1262,7 @@ static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1232 * the lock and this task is uninteresting. If we return nonzero, we have 1262 * the lock and this task is uninteresting. If we return nonzero, we have
1233 * released the lock and the system call should return. 1263 * released the lock and the system call should return.
1234 */ 1264 */
1235static int wait_task_zombie(struct task_struct *p, int noreap, 1265static int wait_task_zombie(struct task_struct *p, int options,
1236 struct siginfo __user *infop, 1266 struct siginfo __user *infop,
1237 int __user *stat_addr, struct rusage __user *ru) 1267 int __user *stat_addr, struct rusage __user *ru)
1238{ 1268{
@@ -1240,7 +1270,10 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
1240 int retval, status, traced; 1270 int retval, status, traced;
1241 pid_t pid = task_pid_vnr(p); 1271 pid_t pid = task_pid_vnr(p);
1242 1272
1243 if (unlikely(noreap)) { 1273 if (!likely(options & WEXITED))
1274 return 0;
1275
1276 if (unlikely(options & WNOWAIT)) {
1244 uid_t uid = p->uid; 1277 uid_t uid = p->uid;
1245 int exit_code = p->exit_code; 1278 int exit_code = p->exit_code;
1246 int why, status; 1279 int why, status;
@@ -1390,21 +1423,24 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
1390 * the lock and this task is uninteresting. If we return nonzero, we have 1423 * the lock and this task is uninteresting. If we return nonzero, we have
1391 * released the lock and the system call should return. 1424 * released the lock and the system call should return.
1392 */ 1425 */
1393static int wait_task_stopped(struct task_struct *p, 1426static int wait_task_stopped(int ptrace, struct task_struct *p,
1394 int noreap, struct siginfo __user *infop, 1427 int options, struct siginfo __user *infop,
1395 int __user *stat_addr, struct rusage __user *ru) 1428 int __user *stat_addr, struct rusage __user *ru)
1396{ 1429{
1397 int retval, exit_code, why; 1430 int retval, exit_code, why;
1398 uid_t uid = 0; /* unneeded, required by compiler */ 1431 uid_t uid = 0; /* unneeded, required by compiler */
1399 pid_t pid; 1432 pid_t pid;
1400 1433
1434 if (!(options & WUNTRACED))
1435 return 0;
1436
1401 exit_code = 0; 1437 exit_code = 0;
1402 spin_lock_irq(&p->sighand->siglock); 1438 spin_lock_irq(&p->sighand->siglock);
1403 1439
1404 if (unlikely(!task_is_stopped_or_traced(p))) 1440 if (unlikely(!task_is_stopped_or_traced(p)))
1405 goto unlock_sig; 1441 goto unlock_sig;
1406 1442
1407 if (!(p->ptrace & PT_PTRACED) && p->signal->group_stop_count > 0) 1443 if (!ptrace && p->signal->group_stop_count > 0)
1408 /* 1444 /*
1409 * A group stop is in progress and this is the group leader. 1445 * A group stop is in progress and this is the group leader.
1410 * We won't report until all threads have stopped. 1446 * We won't report until all threads have stopped.
@@ -1415,7 +1451,7 @@ static int wait_task_stopped(struct task_struct *p,
1415 if (!exit_code) 1451 if (!exit_code)
1416 goto unlock_sig; 1452 goto unlock_sig;
1417 1453
1418 if (!noreap) 1454 if (!unlikely(options & WNOWAIT))
1419 p->exit_code = 0; 1455 p->exit_code = 0;
1420 1456
1421 uid = p->uid; 1457 uid = p->uid;
@@ -1433,10 +1469,10 @@ unlock_sig:
1433 */ 1469 */
1434 get_task_struct(p); 1470 get_task_struct(p);
1435 pid = task_pid_vnr(p); 1471 pid = task_pid_vnr(p);
1436 why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED; 1472 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1437 read_unlock(&tasklist_lock); 1473 read_unlock(&tasklist_lock);
1438 1474
1439 if (unlikely(noreap)) 1475 if (unlikely(options & WNOWAIT))
1440 return wait_noreap_copyout(p, pid, uid, 1476 return wait_noreap_copyout(p, pid, uid,
1441 why, exit_code, 1477 why, exit_code,
1442 infop, ru); 1478 infop, ru);
@@ -1470,7 +1506,7 @@ unlock_sig:
1470 * the lock and this task is uninteresting. If we return nonzero, we have 1506 * the lock and this task is uninteresting. If we return nonzero, we have
1471 * released the lock and the system call should return. 1507 * released the lock and the system call should return.
1472 */ 1508 */
1473static int wait_task_continued(struct task_struct *p, int noreap, 1509static int wait_task_continued(struct task_struct *p, int options,
1474 struct siginfo __user *infop, 1510 struct siginfo __user *infop,
1475 int __user *stat_addr, struct rusage __user *ru) 1511 int __user *stat_addr, struct rusage __user *ru)
1476{ 1512{
@@ -1478,6 +1514,9 @@ static int wait_task_continued(struct task_struct *p, int noreap,
1478 pid_t pid; 1514 pid_t pid;
1479 uid_t uid; 1515 uid_t uid;
1480 1516
1517 if (!unlikely(options & WCONTINUED))
1518 return 0;
1519
1481 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) 1520 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1482 return 0; 1521 return 0;
1483 1522
@@ -1487,7 +1526,7 @@ static int wait_task_continued(struct task_struct *p, int noreap,
1487 spin_unlock_irq(&p->sighand->siglock); 1526 spin_unlock_irq(&p->sighand->siglock);
1488 return 0; 1527 return 0;
1489 } 1528 }
1490 if (!noreap) 1529 if (!unlikely(options & WNOWAIT))
1491 p->signal->flags &= ~SIGNAL_STOP_CONTINUED; 1530 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1492 spin_unlock_irq(&p->sighand->siglock); 1531 spin_unlock_irq(&p->sighand->siglock);
1493 1532
@@ -1513,89 +1552,161 @@ static int wait_task_continued(struct task_struct *p, int noreap,
1513 return retval; 1552 return retval;
1514} 1553}
1515 1554
1555/*
1556 * Consider @p for a wait by @parent.
1557 *
1558 * -ECHILD should be in *@notask_error before the first call.
1559 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1560 * Returns zero if the search for a child should continue;
1561 * then *@notask_error is 0 if @p is an eligible child,
1562 * or another error from security_task_wait(), or still -ECHILD.
1563 */
1564static int wait_consider_task(struct task_struct *parent, int ptrace,
1565 struct task_struct *p, int *notask_error,
1566 enum pid_type type, struct pid *pid, int options,
1567 struct siginfo __user *infop,
1568 int __user *stat_addr, struct rusage __user *ru)
1569{
1570 int ret = eligible_child(type, pid, options, p);
1571 if (!ret)
1572 return ret;
1573
1574 if (unlikely(ret < 0)) {
1575 /*
1576 * If we have not yet seen any eligible child,
1577 * then let this error code replace -ECHILD.
1578 * A permission error will give the user a clue
1579 * to look for security policy problems, rather
1580 * than for mysterious wait bugs.
1581 */
1582 if (*notask_error)
1583 *notask_error = ret;
1584 }
1585
1586 if (likely(!ptrace) && unlikely(p->ptrace)) {
1587 /*
1588 * This child is hidden by ptrace.
1589 * We aren't allowed to see it now, but eventually we will.
1590 */
1591 *notask_error = 0;
1592 return 0;
1593 }
1594
1595 if (p->exit_state == EXIT_DEAD)
1596 return 0;
1597
1598 /*
1599 * We don't reap group leaders with subthreads.
1600 */
1601 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
1602 return wait_task_zombie(p, options, infop, stat_addr, ru);
1603
1604 /*
1605 * It's stopped or running now, so it might
1606 * later continue, exit, or stop again.
1607 */
1608 *notask_error = 0;
1609
1610 if (task_is_stopped_or_traced(p))
1611 return wait_task_stopped(ptrace, p, options,
1612 infop, stat_addr, ru);
1613
1614 return wait_task_continued(p, options, infop, stat_addr, ru);
1615}
1616
1617/*
1618 * Do the work of do_wait() for one thread in the group, @tsk.
1619 *
1620 * -ECHILD should be in *@notask_error before the first call.
1621 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1622 * Returns zero if the search for a child should continue; then
1623 * *@notask_error is 0 if there were any eligible children,
1624 * or another error from security_task_wait(), or still -ECHILD.
1625 */
1626static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1627 enum pid_type type, struct pid *pid, int options,
1628 struct siginfo __user *infop, int __user *stat_addr,
1629 struct rusage __user *ru)
1630{
1631 struct task_struct *p;
1632
1633 list_for_each_entry(p, &tsk->children, sibling) {
1634 /*
1635 * Do not consider detached threads.
1636 */
1637 if (!task_detached(p)) {
1638 int ret = wait_consider_task(tsk, 0, p, notask_error,
1639 type, pid, options,
1640 infop, stat_addr, ru);
1641 if (ret)
1642 return ret;
1643 }
1644 }
1645
1646 return 0;
1647}
1648
1649static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
1650 enum pid_type type, struct pid *pid, int options,
1651 struct siginfo __user *infop, int __user *stat_addr,
1652 struct rusage __user *ru)
1653{
1654 struct task_struct *p;
1655
1656 /*
1657 * Traditionally we see ptrace'd stopped tasks regardless of options.
1658 */
1659 options |= WUNTRACED;
1660
1661 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1662 int ret = wait_consider_task(tsk, 1, p, notask_error,
1663 type, pid, options,
1664 infop, stat_addr, ru);
1665 if (ret)
1666 return ret;
1667 }
1668
1669 return 0;
1670}
1671
1516static long do_wait(enum pid_type type, struct pid *pid, int options, 1672static long do_wait(enum pid_type type, struct pid *pid, int options,
1517 struct siginfo __user *infop, int __user *stat_addr, 1673 struct siginfo __user *infop, int __user *stat_addr,
1518 struct rusage __user *ru) 1674 struct rusage __user *ru)
1519{ 1675{
1520 DECLARE_WAITQUEUE(wait, current); 1676 DECLARE_WAITQUEUE(wait, current);
1521 struct task_struct *tsk; 1677 struct task_struct *tsk;
1522 int flag, retval; 1678 int retval;
1523 1679
1524 add_wait_queue(&current->signal->wait_chldexit,&wait); 1680 add_wait_queue(&current->signal->wait_chldexit,&wait);
1525repeat: 1681repeat:
1526 /* If there is nothing that can match our critier just get out */ 1682 /*
1683 * If there is nothing that can match our critiera just get out.
1684 * We will clear @retval to zero if we see any child that might later
1685 * match our criteria, even if we are not able to reap it yet.
1686 */
1527 retval = -ECHILD; 1687 retval = -ECHILD;
1528 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type]))) 1688 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type])))
1529 goto end; 1689 goto end;
1530 1690
1531 /*
1532 * We will set this flag if we see any child that might later
1533 * match our criteria, even if we are not able to reap it yet.
1534 */
1535 flag = retval = 0;
1536 current->state = TASK_INTERRUPTIBLE; 1691 current->state = TASK_INTERRUPTIBLE;
1537 read_lock(&tasklist_lock); 1692 read_lock(&tasklist_lock);
1538 tsk = current; 1693 tsk = current;
1539 do { 1694 do {
1540 struct task_struct *p; 1695 int tsk_result = do_wait_thread(tsk, &retval,
1541 1696 type, pid, options,
1542 list_for_each_entry(p, &tsk->children, sibling) { 1697 infop, stat_addr, ru);
1543 int ret = eligible_child(type, pid, options, p); 1698 if (!tsk_result)
1544 if (!ret) 1699 tsk_result = ptrace_do_wait(tsk, &retval,
1545 continue; 1700 type, pid, options,
1546 1701 infop, stat_addr, ru);
1547 if (unlikely(ret < 0)) { 1702 if (tsk_result) {
1548 retval = ret; 1703 /*
1549 } else if (task_is_stopped_or_traced(p)) { 1704 * tasklist_lock is unlocked and we have a final result.
1550 /* 1705 */
1551 * It's stopped now, so it might later 1706 retval = tsk_result;
1552 * continue, exit, or stop again. 1707 goto end;
1553 */
1554 flag = 1;
1555 if (!(p->ptrace & PT_PTRACED) &&
1556 !(options & WUNTRACED))
1557 continue;
1558
1559 retval = wait_task_stopped(p,
1560 (options & WNOWAIT), infop,
1561 stat_addr, ru);
1562 } else if (p->exit_state == EXIT_ZOMBIE &&
1563 !delay_group_leader(p)) {
1564 /*
1565 * We don't reap group leaders with subthreads.
1566 */
1567 if (!likely(options & WEXITED))
1568 continue;
1569 retval = wait_task_zombie(p,
1570 (options & WNOWAIT), infop,
1571 stat_addr, ru);
1572 } else if (p->exit_state != EXIT_DEAD) {
1573 /*
1574 * It's running now, so it might later
1575 * exit, stop, or stop and then continue.
1576 */
1577 flag = 1;
1578 if (!unlikely(options & WCONTINUED))
1579 continue;
1580 retval = wait_task_continued(p,
1581 (options & WNOWAIT), infop,
1582 stat_addr, ru);
1583 }
1584 if (retval != 0) /* tasklist_lock released */
1585 goto end;
1586 }
1587 if (!flag) {
1588 list_for_each_entry(p, &tsk->ptrace_children,
1589 ptrace_list) {
1590 flag = eligible_child(type, pid, options, p);
1591 if (!flag)
1592 continue;
1593 if (likely(flag > 0))
1594 break;
1595 retval = flag;
1596 goto end;
1597 }
1598 } 1708 }
1709
1599 if (options & __WNOTHREAD) 1710 if (options & __WNOTHREAD)
1600 break; 1711 break;
1601 tsk = next_thread(tsk); 1712 tsk = next_thread(tsk);
@@ -1603,16 +1714,14 @@ repeat:
1603 } while (tsk != current); 1714 } while (tsk != current);
1604 read_unlock(&tasklist_lock); 1715 read_unlock(&tasklist_lock);
1605 1716
1606 if (flag) { 1717 if (!retval && !(options & WNOHANG)) {
1607 if (options & WNOHANG)
1608 goto end;
1609 retval = -ERESTARTSYS; 1718 retval = -ERESTARTSYS;
1610 if (signal_pending(current)) 1719 if (!signal_pending(current)) {
1611 goto end; 1720 schedule();
1612 schedule(); 1721 goto repeat;
1613 goto repeat; 1722 }
1614 } 1723 }
1615 retval = -ECHILD; 1724
1616end: 1725end:
1617 current->state = TASK_RUNNING; 1726 current->state = TASK_RUNNING;
1618 remove_wait_queue(&current->signal->wait_chldexit,&wait); 1727 remove_wait_queue(&current->signal->wait_chldexit,&wait);