aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/exit.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/exit.c')
-rw-r--r--kernel/exit.c294
1 files changed, 148 insertions, 146 deletions
diff --git a/kernel/exit.c b/kernel/exit.c
index b6c90b5ef509..869dc221733e 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -12,7 +12,6 @@
12#include <linux/completion.h> 12#include <linux/completion.h>
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>
16#include <linux/iocontext.h> 15#include <linux/iocontext.h>
17#include <linux/key.h> 16#include <linux/key.h>
18#include <linux/security.h> 17#include <linux/security.h>
@@ -375,9 +374,8 @@ static void set_special_pids(struct pid *pid)
375} 374}
376 375
377/* 376/*
378 * Let kernel threads use this to say that they 377 * Let kernel threads use this to say that they allow a certain signal.
379 * allow a certain signal (since daemonize() will 378 * Must not be used if kthread was cloned with CLONE_SIGHAND.
380 * have disabled all of them by default).
381 */ 379 */
382int allow_signal(int sig) 380int allow_signal(int sig)
383{ 381{
@@ -385,14 +383,14 @@ int allow_signal(int sig)
385 return -EINVAL; 383 return -EINVAL;
386 384
387 spin_lock_irq(&current->sighand->siglock); 385 spin_lock_irq(&current->sighand->siglock);
386 /* This is only needed for daemonize()'ed kthreads */
388 sigdelset(&current->blocked, sig); 387 sigdelset(&current->blocked, sig);
389 if (!current->mm) { 388 /*
390 /* Kernel threads handle their own signals. 389 * Kernel threads handle their own signals. Let the signal code
391 Let the signal code know it'll be handled, so 390 * know it'll be handled, so that they don't get converted to
392 that they don't get converted to SIGKILL or 391 * SIGKILL or just silently dropped.
393 just silently dropped */ 392 */
394 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2; 393 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
395 }
396 recalc_sigpending(); 394 recalc_sigpending();
397 spin_unlock_irq(&current->sighand->siglock); 395 spin_unlock_irq(&current->sighand->siglock);
398 return 0; 396 return 0;
@@ -591,7 +589,7 @@ retry:
591 /* 589 /*
592 * Search in the siblings 590 * Search in the siblings
593 */ 591 */
594 list_for_each_entry(c, &p->parent->children, sibling) { 592 list_for_each_entry(c, &p->real_parent->children, sibling) {
595 if (c->mm == mm) 593 if (c->mm == mm)
596 goto assign_new_owner; 594 goto assign_new_owner;
597 } 595 }
@@ -758,7 +756,7 @@ static void reparent_thread(struct task_struct *father, struct task_struct *p,
758 p->exit_signal = SIGCHLD; 756 p->exit_signal = SIGCHLD;
759 757
760 /* If it has exited notify the new parent about this child's death. */ 758 /* If it has exited notify the new parent about this child's death. */
761 if (!p->ptrace && 759 if (!task_ptrace(p) &&
762 p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) { 760 p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
763 do_notify_parent(p, p->exit_signal); 761 do_notify_parent(p, p->exit_signal);
764 if (task_detached(p)) { 762 if (task_detached(p)) {
@@ -783,7 +781,7 @@ static void forget_original_parent(struct task_struct *father)
783 list_for_each_entry_safe(p, n, &father->children, sibling) { 781 list_for_each_entry_safe(p, n, &father->children, sibling) {
784 p->real_parent = reaper; 782 p->real_parent = reaper;
785 if (p->parent == father) { 783 if (p->parent == father) {
786 BUG_ON(p->ptrace); 784 BUG_ON(task_ptrace(p));
787 p->parent = p->real_parent; 785 p->parent = p->real_parent;
788 } 786 }
789 reparent_thread(father, p, &dead_children); 787 reparent_thread(father, p, &dead_children);
@@ -1081,6 +1079,18 @@ SYSCALL_DEFINE1(exit_group, int, error_code)
1081 return 0; 1079 return 0;
1082} 1080}
1083 1081
1082struct wait_opts {
1083 enum pid_type wo_type;
1084 int wo_flags;
1085 struct pid *wo_pid;
1086
1087 struct siginfo __user *wo_info;
1088 int __user *wo_stat;
1089 struct rusage __user *wo_rusage;
1090
1091 int notask_error;
1092};
1093
1084static struct pid *task_pid_type(struct task_struct *task, enum pid_type type) 1094static struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
1085{ 1095{
1086 struct pid *pid = NULL; 1096 struct pid *pid = NULL;
@@ -1091,13 +1101,12 @@ static struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
1091 return pid; 1101 return pid;
1092} 1102}
1093 1103
1094static int eligible_child(enum pid_type type, struct pid *pid, int options, 1104static int eligible_child(struct wait_opts *wo, struct task_struct *p)
1095 struct task_struct *p)
1096{ 1105{
1097 int err; 1106 int err;
1098 1107
1099 if (type < PIDTYPE_MAX) { 1108 if (wo->wo_type < PIDTYPE_MAX) {
1100 if (task_pid_type(p, type) != pid) 1109 if (task_pid_type(p, wo->wo_type) != wo->wo_pid)
1101 return 0; 1110 return 0;
1102 } 1111 }
1103 1112
@@ -1106,8 +1115,8 @@ static int eligible_child(enum pid_type type, struct pid *pid, int options,
1106 * set; otherwise, wait for non-clone children *only*. (Note: 1115 * set; otherwise, wait for non-clone children *only*. (Note:
1107 * A "clone" child here is one that reports to its parent 1116 * A "clone" child here is one that reports to its parent
1108 * using a signal other than SIGCHLD.) */ 1117 * using a signal other than SIGCHLD.) */
1109 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0)) 1118 if (((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
1110 && !(options & __WALL)) 1119 && !(wo->wo_flags & __WALL))
1111 return 0; 1120 return 0;
1112 1121
1113 err = security_task_wait(p); 1122 err = security_task_wait(p);
@@ -1117,14 +1126,15 @@ static int eligible_child(enum pid_type type, struct pid *pid, int options,
1117 return 1; 1126 return 1;
1118} 1127}
1119 1128
1120static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid, 1129static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
1121 int why, int status, 1130 pid_t pid, uid_t uid, int why, int status)
1122 struct siginfo __user *infop,
1123 struct rusage __user *rusagep)
1124{ 1131{
1125 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0; 1132 struct siginfo __user *infop;
1133 int retval = wo->wo_rusage
1134 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1126 1135
1127 put_task_struct(p); 1136 put_task_struct(p);
1137 infop = wo->wo_info;
1128 if (!retval) 1138 if (!retval)
1129 retval = put_user(SIGCHLD, &infop->si_signo); 1139 retval = put_user(SIGCHLD, &infop->si_signo);
1130 if (!retval) 1140 if (!retval)
@@ -1148,19 +1158,18 @@ static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1148 * the lock and this task is uninteresting. If we return nonzero, we have 1158 * the lock and this task is uninteresting. If we return nonzero, we have
1149 * released the lock and the system call should return. 1159 * released the lock and the system call should return.
1150 */ 1160 */
1151static int wait_task_zombie(struct task_struct *p, int options, 1161static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
1152 struct siginfo __user *infop,
1153 int __user *stat_addr, struct rusage __user *ru)
1154{ 1162{
1155 unsigned long state; 1163 unsigned long state;
1156 int retval, status, traced; 1164 int retval, status, traced;
1157 pid_t pid = task_pid_vnr(p); 1165 pid_t pid = task_pid_vnr(p);
1158 uid_t uid = __task_cred(p)->uid; 1166 uid_t uid = __task_cred(p)->uid;
1167 struct siginfo __user *infop;
1159 1168
1160 if (!likely(options & WEXITED)) 1169 if (!likely(wo->wo_flags & WEXITED))
1161 return 0; 1170 return 0;
1162 1171
1163 if (unlikely(options & WNOWAIT)) { 1172 if (unlikely(wo->wo_flags & WNOWAIT)) {
1164 int exit_code = p->exit_code; 1173 int exit_code = p->exit_code;
1165 int why, status; 1174 int why, status;
1166 1175
@@ -1173,8 +1182,7 @@ static int wait_task_zombie(struct task_struct *p, int options,
1173 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED; 1182 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1174 status = exit_code & 0x7f; 1183 status = exit_code & 0x7f;
1175 } 1184 }
1176 return wait_noreap_copyout(p, pid, uid, why, 1185 return wait_noreap_copyout(wo, p, pid, uid, why, status);
1177 status, infop, ru);
1178 } 1186 }
1179 1187
1180 /* 1188 /*
@@ -1188,11 +1196,13 @@ static int wait_task_zombie(struct task_struct *p, int options,
1188 } 1196 }
1189 1197
1190 traced = ptrace_reparented(p); 1198 traced = ptrace_reparented(p);
1191 1199 /*
1192 if (likely(!traced)) { 1200 * It can be ptraced but not reparented, check
1201 * !task_detached() to filter out sub-threads.
1202 */
1203 if (likely(!traced) && likely(!task_detached(p))) {
1193 struct signal_struct *psig; 1204 struct signal_struct *psig;
1194 struct signal_struct *sig; 1205 struct signal_struct *sig;
1195 struct task_cputime cputime;
1196 1206
1197 /* 1207 /*
1198 * The resource counters for the group leader are in its 1208 * The resource counters for the group leader are in its
@@ -1205,26 +1215,23 @@ static int wait_task_zombie(struct task_struct *p, int options,
1205 * p->signal fields, because they are only touched by 1215 * p->signal fields, because they are only touched by
1206 * __exit_signal, which runs with tasklist_lock 1216 * __exit_signal, which runs with tasklist_lock
1207 * write-locked anyway, and so is excluded here. We do 1217 * write-locked anyway, and so is excluded here. We do
1208 * need to protect the access to p->parent->signal fields, 1218 * need to protect the access to parent->signal fields,
1209 * as other threads in the parent group can be right 1219 * as other threads in the parent group can be right
1210 * here reaping other children at the same time. 1220 * here reaping other children at the same time.
1211 *
1212 * We use thread_group_cputime() to get times for the thread
1213 * group, which consolidates times for all threads in the
1214 * group including the group leader.
1215 */ 1221 */
1216 thread_group_cputime(p, &cputime); 1222 spin_lock_irq(&p->real_parent->sighand->siglock);
1217 spin_lock_irq(&p->parent->sighand->siglock); 1223 psig = p->real_parent->signal;
1218 psig = p->parent->signal;
1219 sig = p->signal; 1224 sig = p->signal;
1220 psig->cutime = 1225 psig->cutime =
1221 cputime_add(psig->cutime, 1226 cputime_add(psig->cutime,
1222 cputime_add(cputime.utime, 1227 cputime_add(p->utime,
1223 sig->cutime)); 1228 cputime_add(sig->utime,
1229 sig->cutime)));
1224 psig->cstime = 1230 psig->cstime =
1225 cputime_add(psig->cstime, 1231 cputime_add(psig->cstime,
1226 cputime_add(cputime.stime, 1232 cputime_add(p->stime,
1227 sig->cstime)); 1233 cputime_add(sig->stime,
1234 sig->cstime)));
1228 psig->cgtime = 1235 psig->cgtime =
1229 cputime_add(psig->cgtime, 1236 cputime_add(psig->cgtime,
1230 cputime_add(p->gtime, 1237 cputime_add(p->gtime,
@@ -1246,7 +1253,7 @@ static int wait_task_zombie(struct task_struct *p, int options,
1246 sig->oublock + sig->coublock; 1253 sig->oublock + sig->coublock;
1247 task_io_accounting_add(&psig->ioac, &p->ioac); 1254 task_io_accounting_add(&psig->ioac, &p->ioac);
1248 task_io_accounting_add(&psig->ioac, &sig->ioac); 1255 task_io_accounting_add(&psig->ioac, &sig->ioac);
1249 spin_unlock_irq(&p->parent->sighand->siglock); 1256 spin_unlock_irq(&p->real_parent->sighand->siglock);
1250 } 1257 }
1251 1258
1252 /* 1259 /*
@@ -1255,11 +1262,14 @@ static int wait_task_zombie(struct task_struct *p, int options,
1255 */ 1262 */
1256 read_unlock(&tasklist_lock); 1263 read_unlock(&tasklist_lock);
1257 1264
1258 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; 1265 retval = wo->wo_rusage
1266 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1259 status = (p->signal->flags & SIGNAL_GROUP_EXIT) 1267 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1260 ? p->signal->group_exit_code : p->exit_code; 1268 ? p->signal->group_exit_code : p->exit_code;
1261 if (!retval && stat_addr) 1269 if (!retval && wo->wo_stat)
1262 retval = put_user(status, stat_addr); 1270 retval = put_user(status, wo->wo_stat);
1271
1272 infop = wo->wo_info;
1263 if (!retval && infop) 1273 if (!retval && infop)
1264 retval = put_user(SIGCHLD, &infop->si_signo); 1274 retval = put_user(SIGCHLD, &infop->si_signo);
1265 if (!retval && infop) 1275 if (!retval && infop)
@@ -1327,15 +1337,18 @@ static int *task_stopped_code(struct task_struct *p, bool ptrace)
1327 * the lock and this task is uninteresting. If we return nonzero, we have 1337 * the lock and this task is uninteresting. If we return nonzero, we have
1328 * released the lock and the system call should return. 1338 * released the lock and the system call should return.
1329 */ 1339 */
1330static int wait_task_stopped(int ptrace, struct task_struct *p, 1340static int wait_task_stopped(struct wait_opts *wo,
1331 int options, struct siginfo __user *infop, 1341 int ptrace, struct task_struct *p)
1332 int __user *stat_addr, struct rusage __user *ru)
1333{ 1342{
1343 struct siginfo __user *infop;
1334 int retval, exit_code, *p_code, why; 1344 int retval, exit_code, *p_code, why;
1335 uid_t uid = 0; /* unneeded, required by compiler */ 1345 uid_t uid = 0; /* unneeded, required by compiler */
1336 pid_t pid; 1346 pid_t pid;
1337 1347
1338 if (!(options & WUNTRACED)) 1348 /*
1349 * Traditionally we see ptrace'd stopped tasks regardless of options.
1350 */
1351 if (!ptrace && !(wo->wo_flags & WUNTRACED))
1339 return 0; 1352 return 0;
1340 1353
1341 exit_code = 0; 1354 exit_code = 0;
@@ -1349,7 +1362,7 @@ static int wait_task_stopped(int ptrace, struct task_struct *p,
1349 if (!exit_code) 1362 if (!exit_code)
1350 goto unlock_sig; 1363 goto unlock_sig;
1351 1364
1352 if (!unlikely(options & WNOWAIT)) 1365 if (!unlikely(wo->wo_flags & WNOWAIT))
1353 *p_code = 0; 1366 *p_code = 0;
1354 1367
1355 /* don't need the RCU readlock here as we're holding a spinlock */ 1368 /* don't need the RCU readlock here as we're holding a spinlock */
@@ -1371,14 +1384,15 @@ unlock_sig:
1371 why = ptrace ? CLD_TRAPPED : CLD_STOPPED; 1384 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1372 read_unlock(&tasklist_lock); 1385 read_unlock(&tasklist_lock);
1373 1386
1374 if (unlikely(options & WNOWAIT)) 1387 if (unlikely(wo->wo_flags & WNOWAIT))
1375 return wait_noreap_copyout(p, pid, uid, 1388 return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
1376 why, exit_code,
1377 infop, ru);
1378 1389
1379 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; 1390 retval = wo->wo_rusage
1380 if (!retval && stat_addr) 1391 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1381 retval = put_user((exit_code << 8) | 0x7f, stat_addr); 1392 if (!retval && wo->wo_stat)
1393 retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
1394
1395 infop = wo->wo_info;
1382 if (!retval && infop) 1396 if (!retval && infop)
1383 retval = put_user(SIGCHLD, &infop->si_signo); 1397 retval = put_user(SIGCHLD, &infop->si_signo);
1384 if (!retval && infop) 1398 if (!retval && infop)
@@ -1405,15 +1419,13 @@ unlock_sig:
1405 * the lock and this task is uninteresting. If we return nonzero, we have 1419 * the lock and this task is uninteresting. If we return nonzero, we have
1406 * released the lock and the system call should return. 1420 * released the lock and the system call should return.
1407 */ 1421 */
1408static int wait_task_continued(struct task_struct *p, int options, 1422static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
1409 struct siginfo __user *infop,
1410 int __user *stat_addr, struct rusage __user *ru)
1411{ 1423{
1412 int retval; 1424 int retval;
1413 pid_t pid; 1425 pid_t pid;
1414 uid_t uid; 1426 uid_t uid;
1415 1427
1416 if (!unlikely(options & WCONTINUED)) 1428 if (!unlikely(wo->wo_flags & WCONTINUED))
1417 return 0; 1429 return 0;
1418 1430
1419 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) 1431 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
@@ -1425,7 +1437,7 @@ static int wait_task_continued(struct task_struct *p, int options,
1425 spin_unlock_irq(&p->sighand->siglock); 1437 spin_unlock_irq(&p->sighand->siglock);
1426 return 0; 1438 return 0;
1427 } 1439 }
1428 if (!unlikely(options & WNOWAIT)) 1440 if (!unlikely(wo->wo_flags & WNOWAIT))
1429 p->signal->flags &= ~SIGNAL_STOP_CONTINUED; 1441 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1430 uid = __task_cred(p)->uid; 1442 uid = __task_cred(p)->uid;
1431 spin_unlock_irq(&p->sighand->siglock); 1443 spin_unlock_irq(&p->sighand->siglock);
@@ -1434,17 +1446,17 @@ static int wait_task_continued(struct task_struct *p, int options,
1434 get_task_struct(p); 1446 get_task_struct(p);
1435 read_unlock(&tasklist_lock); 1447 read_unlock(&tasklist_lock);
1436 1448
1437 if (!infop) { 1449 if (!wo->wo_info) {
1438 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; 1450 retval = wo->wo_rusage
1451 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1439 put_task_struct(p); 1452 put_task_struct(p);
1440 if (!retval && stat_addr) 1453 if (!retval && wo->wo_stat)
1441 retval = put_user(0xffff, stat_addr); 1454 retval = put_user(0xffff, wo->wo_stat);
1442 if (!retval) 1455 if (!retval)
1443 retval = pid; 1456 retval = pid;
1444 } else { 1457 } else {
1445 retval = wait_noreap_copyout(p, pid, uid, 1458 retval = wait_noreap_copyout(wo, p, pid, uid,
1446 CLD_CONTINUED, SIGCONT, 1459 CLD_CONTINUED, SIGCONT);
1447 infop, ru);
1448 BUG_ON(retval == 0); 1460 BUG_ON(retval == 0);
1449 } 1461 }
1450 1462
@@ -1454,19 +1466,16 @@ static int wait_task_continued(struct task_struct *p, int options,
1454/* 1466/*
1455 * Consider @p for a wait by @parent. 1467 * Consider @p for a wait by @parent.
1456 * 1468 *
1457 * -ECHILD should be in *@notask_error before the first call. 1469 * -ECHILD should be in ->notask_error before the first call.
1458 * Returns nonzero for a final return, when we have unlocked tasklist_lock. 1470 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1459 * Returns zero if the search for a child should continue; 1471 * Returns zero if the search for a child should continue;
1460 * then *@notask_error is 0 if @p is an eligible child, 1472 * then ->notask_error is 0 if @p is an eligible child,
1461 * or another error from security_task_wait(), or still -ECHILD. 1473 * or another error from security_task_wait(), or still -ECHILD.
1462 */ 1474 */
1463static int wait_consider_task(struct task_struct *parent, int ptrace, 1475static int wait_consider_task(struct wait_opts *wo, struct task_struct *parent,
1464 struct task_struct *p, int *notask_error, 1476 int ptrace, struct task_struct *p)
1465 enum pid_type type, struct pid *pid, int options,
1466 struct siginfo __user *infop,
1467 int __user *stat_addr, struct rusage __user *ru)
1468{ 1477{
1469 int ret = eligible_child(type, pid, options, p); 1478 int ret = eligible_child(wo, p);
1470 if (!ret) 1479 if (!ret)
1471 return ret; 1480 return ret;
1472 1481
@@ -1478,17 +1487,17 @@ static int wait_consider_task(struct task_struct *parent, int ptrace,
1478 * to look for security policy problems, rather 1487 * to look for security policy problems, rather
1479 * than for mysterious wait bugs. 1488 * than for mysterious wait bugs.
1480 */ 1489 */
1481 if (*notask_error) 1490 if (wo->notask_error)
1482 *notask_error = ret; 1491 wo->notask_error = ret;
1483 return 0; 1492 return 0;
1484 } 1493 }
1485 1494
1486 if (likely(!ptrace) && unlikely(p->ptrace)) { 1495 if (likely(!ptrace) && unlikely(task_ptrace(p))) {
1487 /* 1496 /*
1488 * This child is hidden by ptrace. 1497 * This child is hidden by ptrace.
1489 * We aren't allowed to see it now, but eventually we will. 1498 * We aren't allowed to see it now, but eventually we will.
1490 */ 1499 */
1491 *notask_error = 0; 1500 wo->notask_error = 0;
1492 return 0; 1501 return 0;
1493 } 1502 }
1494 1503
@@ -1499,34 +1508,30 @@ static int wait_consider_task(struct task_struct *parent, int ptrace,
1499 * We don't reap group leaders with subthreads. 1508 * We don't reap group leaders with subthreads.
1500 */ 1509 */
1501 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p)) 1510 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
1502 return wait_task_zombie(p, options, infop, stat_addr, ru); 1511 return wait_task_zombie(wo, p);
1503 1512
1504 /* 1513 /*
1505 * It's stopped or running now, so it might 1514 * It's stopped or running now, so it might
1506 * later continue, exit, or stop again. 1515 * later continue, exit, or stop again.
1507 */ 1516 */
1508 *notask_error = 0; 1517 wo->notask_error = 0;
1509 1518
1510 if (task_stopped_code(p, ptrace)) 1519 if (task_stopped_code(p, ptrace))
1511 return wait_task_stopped(ptrace, p, options, 1520 return wait_task_stopped(wo, ptrace, p);
1512 infop, stat_addr, ru);
1513 1521
1514 return wait_task_continued(p, options, infop, stat_addr, ru); 1522 return wait_task_continued(wo, p);
1515} 1523}
1516 1524
1517/* 1525/*
1518 * Do the work of do_wait() for one thread in the group, @tsk. 1526 * Do the work of do_wait() for one thread in the group, @tsk.
1519 * 1527 *
1520 * -ECHILD should be in *@notask_error before the first call. 1528 * -ECHILD should be in ->notask_error before the first call.
1521 * Returns nonzero for a final return, when we have unlocked tasklist_lock. 1529 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1522 * Returns zero if the search for a child should continue; then 1530 * Returns zero if the search for a child should continue; then
1523 * *@notask_error is 0 if there were any eligible children, 1531 * ->notask_error is 0 if there were any eligible children,
1524 * or another error from security_task_wait(), or still -ECHILD. 1532 * or another error from security_task_wait(), or still -ECHILD.
1525 */ 1533 */
1526static int do_wait_thread(struct task_struct *tsk, int *notask_error, 1534static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
1527 enum pid_type type, struct pid *pid, int options,
1528 struct siginfo __user *infop, int __user *stat_addr,
1529 struct rusage __user *ru)
1530{ 1535{
1531 struct task_struct *p; 1536 struct task_struct *p;
1532 1537
@@ -1535,9 +1540,7 @@ static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1535 * Do not consider detached threads. 1540 * Do not consider detached threads.
1536 */ 1541 */
1537 if (!task_detached(p)) { 1542 if (!task_detached(p)) {
1538 int ret = wait_consider_task(tsk, 0, p, notask_error, 1543 int ret = wait_consider_task(wo, tsk, 0, p);
1539 type, pid, options,
1540 infop, stat_addr, ru);
1541 if (ret) 1544 if (ret)
1542 return ret; 1545 return ret;
1543 } 1546 }
@@ -1546,22 +1549,12 @@ static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1546 return 0; 1549 return 0;
1547} 1550}
1548 1551
1549static int ptrace_do_wait(struct task_struct *tsk, int *notask_error, 1552static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
1550 enum pid_type type, struct pid *pid, int options,
1551 struct siginfo __user *infop, int __user *stat_addr,
1552 struct rusage __user *ru)
1553{ 1553{
1554 struct task_struct *p; 1554 struct task_struct *p;
1555 1555
1556 /*
1557 * Traditionally we see ptrace'd stopped tasks regardless of options.
1558 */
1559 options |= WUNTRACED;
1560
1561 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) { 1556 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1562 int ret = wait_consider_task(tsk, 1, p, notask_error, 1557 int ret = wait_consider_task(wo, tsk, 1, p);
1563 type, pid, options,
1564 infop, stat_addr, ru);
1565 if (ret) 1558 if (ret)
1566 return ret; 1559 return ret;
1567 } 1560 }
@@ -1569,65 +1562,59 @@ static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
1569 return 0; 1562 return 0;
1570} 1563}
1571 1564
1572static long do_wait(enum pid_type type, struct pid *pid, int options, 1565static long do_wait(struct wait_opts *wo)
1573 struct siginfo __user *infop, int __user *stat_addr,
1574 struct rusage __user *ru)
1575{ 1566{
1576 DECLARE_WAITQUEUE(wait, current); 1567 DECLARE_WAITQUEUE(wait, current);
1577 struct task_struct *tsk; 1568 struct task_struct *tsk;
1578 int retval; 1569 int retval;
1579 1570
1580 trace_sched_process_wait(pid); 1571 trace_sched_process_wait(wo->wo_pid);
1581 1572
1582 add_wait_queue(&current->signal->wait_chldexit,&wait); 1573 add_wait_queue(&current->signal->wait_chldexit,&wait);
1583repeat: 1574repeat:
1584 /* 1575 /*
1585 * If there is nothing that can match our critiera just get out. 1576 * If there is nothing that can match our critiera just get out.
1586 * We will clear @retval to zero if we see any child that might later 1577 * We will clear ->notask_error to zero if we see any child that
1587 * match our criteria, even if we are not able to reap it yet. 1578 * might later match our criteria, even if we are not able to reap
1579 * it yet.
1588 */ 1580 */
1589 retval = -ECHILD; 1581 wo->notask_error = -ECHILD;
1590 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type]))) 1582 if ((wo->wo_type < PIDTYPE_MAX) &&
1591 goto end; 1583 (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
1584 goto notask;
1592 1585
1593 current->state = TASK_INTERRUPTIBLE; 1586 set_current_state(TASK_INTERRUPTIBLE);
1594 read_lock(&tasklist_lock); 1587 read_lock(&tasklist_lock);
1595 tsk = current; 1588 tsk = current;
1596 do { 1589 do {
1597 int tsk_result = do_wait_thread(tsk, &retval, 1590 retval = do_wait_thread(wo, tsk);
1598 type, pid, options, 1591 if (retval)
1599 infop, stat_addr, ru); 1592 goto end;
1600 if (!tsk_result) 1593
1601 tsk_result = ptrace_do_wait(tsk, &retval, 1594 retval = ptrace_do_wait(wo, tsk);
1602 type, pid, options, 1595 if (retval)
1603 infop, stat_addr, ru);
1604 if (tsk_result) {
1605 /*
1606 * tasklist_lock is unlocked and we have a final result.
1607 */
1608 retval = tsk_result;
1609 goto end; 1596 goto end;
1610 }
1611 1597
1612 if (options & __WNOTHREAD) 1598 if (wo->wo_flags & __WNOTHREAD)
1613 break; 1599 break;
1614 tsk = next_thread(tsk); 1600 } while_each_thread(current, tsk);
1615 BUG_ON(tsk->signal != current->signal);
1616 } while (tsk != current);
1617 read_unlock(&tasklist_lock); 1601 read_unlock(&tasklist_lock);
1618 1602
1619 if (!retval && !(options & WNOHANG)) { 1603notask:
1604 retval = wo->notask_error;
1605 if (!retval && !(wo->wo_flags & WNOHANG)) {
1620 retval = -ERESTARTSYS; 1606 retval = -ERESTARTSYS;
1621 if (!signal_pending(current)) { 1607 if (!signal_pending(current)) {
1622 schedule(); 1608 schedule();
1623 goto repeat; 1609 goto repeat;
1624 } 1610 }
1625 } 1611 }
1626
1627end: 1612end:
1628 current->state = TASK_RUNNING; 1613 __set_current_state(TASK_RUNNING);
1629 remove_wait_queue(&current->signal->wait_chldexit,&wait); 1614 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1630 if (infop) { 1615 if (wo->wo_info) {
1616 struct siginfo __user *infop = wo->wo_info;
1617
1631 if (retval > 0) 1618 if (retval > 0)
1632 retval = 0; 1619 retval = 0;
1633 else { 1620 else {
@@ -1656,6 +1643,7 @@ end:
1656SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *, 1643SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1657 infop, int, options, struct rusage __user *, ru) 1644 infop, int, options, struct rusage __user *, ru)
1658{ 1645{
1646 struct wait_opts wo;
1659 struct pid *pid = NULL; 1647 struct pid *pid = NULL;
1660 enum pid_type type; 1648 enum pid_type type;
1661 long ret; 1649 long ret;
@@ -1685,7 +1673,14 @@ SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1685 1673
1686 if (type < PIDTYPE_MAX) 1674 if (type < PIDTYPE_MAX)
1687 pid = find_get_pid(upid); 1675 pid = find_get_pid(upid);
1688 ret = do_wait(type, pid, options, infop, NULL, ru); 1676
1677 wo.wo_type = type;
1678 wo.wo_pid = pid;
1679 wo.wo_flags = options;
1680 wo.wo_info = infop;
1681 wo.wo_stat = NULL;
1682 wo.wo_rusage = ru;
1683 ret = do_wait(&wo);
1689 put_pid(pid); 1684 put_pid(pid);
1690 1685
1691 /* avoid REGPARM breakage on x86: */ 1686 /* avoid REGPARM breakage on x86: */
@@ -1696,6 +1691,7 @@ SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1696SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr, 1691SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1697 int, options, struct rusage __user *, ru) 1692 int, options, struct rusage __user *, ru)
1698{ 1693{
1694 struct wait_opts wo;
1699 struct pid *pid = NULL; 1695 struct pid *pid = NULL;
1700 enum pid_type type; 1696 enum pid_type type;
1701 long ret; 1697 long ret;
@@ -1717,7 +1713,13 @@ SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1717 pid = find_get_pid(upid); 1713 pid = find_get_pid(upid);
1718 } 1714 }
1719 1715
1720 ret = do_wait(type, pid, options | WEXITED, NULL, stat_addr, ru); 1716 wo.wo_type = type;
1717 wo.wo_pid = pid;
1718 wo.wo_flags = options | WEXITED;
1719 wo.wo_info = NULL;
1720 wo.wo_stat = stat_addr;
1721 wo.wo_rusage = ru;
1722 ret = do_wait(&wo);
1721 put_pid(pid); 1723 put_pid(pid);
1722 1724
1723 /* avoid REGPARM breakage on x86: */ 1725 /* avoid REGPARM breakage on x86: */