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