aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/exit.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/exit.c')
-rw-r--r--kernel/exit.c286
1 files changed, 143 insertions, 143 deletions
diff --git a/kernel/exit.c b/kernel/exit.c
index b6c90b5ef509..13ae64001fec 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 /*
@@ -1192,7 +1201,6 @@ static int wait_task_zombie(struct task_struct *p, int options,
1192 if (likely(!traced)) { 1201 if (likely(!traced)) {
1193 struct signal_struct *psig; 1202 struct signal_struct *psig;
1194 struct signal_struct *sig; 1203 struct signal_struct *sig;
1195 struct task_cputime cputime;
1196 1204
1197 /* 1205 /*
1198 * The resource counters for the group leader are in its 1206 * The resource counters for the group leader are in its
@@ -1205,26 +1213,23 @@ static int wait_task_zombie(struct task_struct *p, int options,
1205 * p->signal fields, because they are only touched by 1213 * p->signal fields, because they are only touched by
1206 * __exit_signal, which runs with tasklist_lock 1214 * __exit_signal, which runs with tasklist_lock
1207 * write-locked anyway, and so is excluded here. We do 1215 * write-locked anyway, and so is excluded here. We do
1208 * need to protect the access to p->parent->signal fields, 1216 * need to protect the access to parent->signal fields,
1209 * as other threads in the parent group can be right 1217 * as other threads in the parent group can be right
1210 * here reaping other children at the same time. 1218 * 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 */ 1219 */
1216 thread_group_cputime(p, &cputime); 1220 spin_lock_irq(&p->real_parent->sighand->siglock);
1217 spin_lock_irq(&p->parent->sighand->siglock); 1221 psig = p->real_parent->signal;
1218 psig = p->parent->signal;
1219 sig = p->signal; 1222 sig = p->signal;
1220 psig->cutime = 1223 psig->cutime =
1221 cputime_add(psig->cutime, 1224 cputime_add(psig->cutime,
1222 cputime_add(cputime.utime, 1225 cputime_add(p->utime,
1223 sig->cutime)); 1226 cputime_add(sig->utime,
1227 sig->cutime)));
1224 psig->cstime = 1228 psig->cstime =
1225 cputime_add(psig->cstime, 1229 cputime_add(psig->cstime,
1226 cputime_add(cputime.stime, 1230 cputime_add(p->stime,
1227 sig->cstime)); 1231 cputime_add(sig->stime,
1232 sig->cstime)));
1228 psig->cgtime = 1233 psig->cgtime =
1229 cputime_add(psig->cgtime, 1234 cputime_add(psig->cgtime,
1230 cputime_add(p->gtime, 1235 cputime_add(p->gtime,
@@ -1246,7 +1251,7 @@ static int wait_task_zombie(struct task_struct *p, int options,
1246 sig->oublock + sig->coublock; 1251 sig->oublock + sig->coublock;
1247 task_io_accounting_add(&psig->ioac, &p->ioac); 1252 task_io_accounting_add(&psig->ioac, &p->ioac);
1248 task_io_accounting_add(&psig->ioac, &sig->ioac); 1253 task_io_accounting_add(&psig->ioac, &sig->ioac);
1249 spin_unlock_irq(&p->parent->sighand->siglock); 1254 spin_unlock_irq(&p->real_parent->sighand->siglock);
1250 } 1255 }
1251 1256
1252 /* 1257 /*
@@ -1255,11 +1260,14 @@ static int wait_task_zombie(struct task_struct *p, int options,
1255 */ 1260 */
1256 read_unlock(&tasklist_lock); 1261 read_unlock(&tasklist_lock);
1257 1262
1258 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; 1263 retval = wo->wo_rusage
1264 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1259 status = (p->signal->flags & SIGNAL_GROUP_EXIT) 1265 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1260 ? p->signal->group_exit_code : p->exit_code; 1266 ? p->signal->group_exit_code : p->exit_code;
1261 if (!retval && stat_addr) 1267 if (!retval && wo->wo_stat)
1262 retval = put_user(status, stat_addr); 1268 retval = put_user(status, wo->wo_stat);
1269
1270 infop = wo->wo_info;
1263 if (!retval && infop) 1271 if (!retval && infop)
1264 retval = put_user(SIGCHLD, &infop->si_signo); 1272 retval = put_user(SIGCHLD, &infop->si_signo);
1265 if (!retval && infop) 1273 if (!retval && infop)
@@ -1327,15 +1335,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 1335 * the lock and this task is uninteresting. If we return nonzero, we have
1328 * released the lock and the system call should return. 1336 * released the lock and the system call should return.
1329 */ 1337 */
1330static int wait_task_stopped(int ptrace, struct task_struct *p, 1338static int wait_task_stopped(struct wait_opts *wo,
1331 int options, struct siginfo __user *infop, 1339 int ptrace, struct task_struct *p)
1332 int __user *stat_addr, struct rusage __user *ru)
1333{ 1340{
1341 struct siginfo __user *infop;
1334 int retval, exit_code, *p_code, why; 1342 int retval, exit_code, *p_code, why;
1335 uid_t uid = 0; /* unneeded, required by compiler */ 1343 uid_t uid = 0; /* unneeded, required by compiler */
1336 pid_t pid; 1344 pid_t pid;
1337 1345
1338 if (!(options & WUNTRACED)) 1346 /*
1347 * Traditionally we see ptrace'd stopped tasks regardless of options.
1348 */
1349 if (!ptrace && !(wo->wo_flags & WUNTRACED))
1339 return 0; 1350 return 0;
1340 1351
1341 exit_code = 0; 1352 exit_code = 0;
@@ -1349,7 +1360,7 @@ static int wait_task_stopped(int ptrace, struct task_struct *p,
1349 if (!exit_code) 1360 if (!exit_code)
1350 goto unlock_sig; 1361 goto unlock_sig;
1351 1362
1352 if (!unlikely(options & WNOWAIT)) 1363 if (!unlikely(wo->wo_flags & WNOWAIT))
1353 *p_code = 0; 1364 *p_code = 0;
1354 1365
1355 /* don't need the RCU readlock here as we're holding a spinlock */ 1366 /* don't need the RCU readlock here as we're holding a spinlock */
@@ -1371,14 +1382,15 @@ unlock_sig:
1371 why = ptrace ? CLD_TRAPPED : CLD_STOPPED; 1382 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1372 read_unlock(&tasklist_lock); 1383 read_unlock(&tasklist_lock);
1373 1384
1374 if (unlikely(options & WNOWAIT)) 1385 if (unlikely(wo->wo_flags & WNOWAIT))
1375 return wait_noreap_copyout(p, pid, uid, 1386 return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
1376 why, exit_code, 1387
1377 infop, ru); 1388 retval = wo->wo_rusage
1389 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1390 if (!retval && wo->wo_stat)
1391 retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
1378 1392
1379 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; 1393 infop = wo->wo_info;
1380 if (!retval && stat_addr)
1381 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1382 if (!retval && infop) 1394 if (!retval && infop)
1383 retval = put_user(SIGCHLD, &infop->si_signo); 1395 retval = put_user(SIGCHLD, &infop->si_signo);
1384 if (!retval && infop) 1396 if (!retval && infop)
@@ -1405,15 +1417,13 @@ unlock_sig:
1405 * the lock and this task is uninteresting. If we return nonzero, we have 1417 * the lock and this task is uninteresting. If we return nonzero, we have
1406 * released the lock and the system call should return. 1418 * released the lock and the system call should return.
1407 */ 1419 */
1408static int wait_task_continued(struct task_struct *p, int options, 1420static 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{ 1421{
1412 int retval; 1422 int retval;
1413 pid_t pid; 1423 pid_t pid;
1414 uid_t uid; 1424 uid_t uid;
1415 1425
1416 if (!unlikely(options & WCONTINUED)) 1426 if (!unlikely(wo->wo_flags & WCONTINUED))
1417 return 0; 1427 return 0;
1418 1428
1419 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) 1429 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
@@ -1425,7 +1435,7 @@ static int wait_task_continued(struct task_struct *p, int options,
1425 spin_unlock_irq(&p->sighand->siglock); 1435 spin_unlock_irq(&p->sighand->siglock);
1426 return 0; 1436 return 0;
1427 } 1437 }
1428 if (!unlikely(options & WNOWAIT)) 1438 if (!unlikely(wo->wo_flags & WNOWAIT))
1429 p->signal->flags &= ~SIGNAL_STOP_CONTINUED; 1439 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1430 uid = __task_cred(p)->uid; 1440 uid = __task_cred(p)->uid;
1431 spin_unlock_irq(&p->sighand->siglock); 1441 spin_unlock_irq(&p->sighand->siglock);
@@ -1434,17 +1444,17 @@ static int wait_task_continued(struct task_struct *p, int options,
1434 get_task_struct(p); 1444 get_task_struct(p);
1435 read_unlock(&tasklist_lock); 1445 read_unlock(&tasklist_lock);
1436 1446
1437 if (!infop) { 1447 if (!wo->wo_info) {
1438 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0; 1448 retval = wo->wo_rusage
1449 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1439 put_task_struct(p); 1450 put_task_struct(p);
1440 if (!retval && stat_addr) 1451 if (!retval && wo->wo_stat)
1441 retval = put_user(0xffff, stat_addr); 1452 retval = put_user(0xffff, wo->wo_stat);
1442 if (!retval) 1453 if (!retval)
1443 retval = pid; 1454 retval = pid;
1444 } else { 1455 } else {
1445 retval = wait_noreap_copyout(p, pid, uid, 1456 retval = wait_noreap_copyout(wo, p, pid, uid,
1446 CLD_CONTINUED, SIGCONT, 1457 CLD_CONTINUED, SIGCONT);
1447 infop, ru);
1448 BUG_ON(retval == 0); 1458 BUG_ON(retval == 0);
1449 } 1459 }
1450 1460
@@ -1454,19 +1464,16 @@ static int wait_task_continued(struct task_struct *p, int options,
1454/* 1464/*
1455 * Consider @p for a wait by @parent. 1465 * Consider @p for a wait by @parent.
1456 * 1466 *
1457 * -ECHILD should be in *@notask_error before the first call. 1467 * -ECHILD should be in ->notask_error before the first call.
1458 * Returns nonzero for a final return, when we have unlocked tasklist_lock. 1468 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1459 * Returns zero if the search for a child should continue; 1469 * Returns zero if the search for a child should continue;
1460 * then *@notask_error is 0 if @p is an eligible child, 1470 * then ->notask_error is 0 if @p is an eligible child,
1461 * or another error from security_task_wait(), or still -ECHILD. 1471 * or another error from security_task_wait(), or still -ECHILD.
1462 */ 1472 */
1463static int wait_consider_task(struct task_struct *parent, int ptrace, 1473static int wait_consider_task(struct wait_opts *wo, struct task_struct *parent,
1464 struct task_struct *p, int *notask_error, 1474 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{ 1475{
1469 int ret = eligible_child(type, pid, options, p); 1476 int ret = eligible_child(wo, p);
1470 if (!ret) 1477 if (!ret)
1471 return ret; 1478 return ret;
1472 1479
@@ -1478,17 +1485,17 @@ static int wait_consider_task(struct task_struct *parent, int ptrace,
1478 * to look for security policy problems, rather 1485 * to look for security policy problems, rather
1479 * than for mysterious wait bugs. 1486 * than for mysterious wait bugs.
1480 */ 1487 */
1481 if (*notask_error) 1488 if (wo->notask_error)
1482 *notask_error = ret; 1489 wo->notask_error = ret;
1483 return 0; 1490 return 0;
1484 } 1491 }
1485 1492
1486 if (likely(!ptrace) && unlikely(p->ptrace)) { 1493 if (likely(!ptrace) && unlikely(task_ptrace(p))) {
1487 /* 1494 /*
1488 * This child is hidden by ptrace. 1495 * This child is hidden by ptrace.
1489 * We aren't allowed to see it now, but eventually we will. 1496 * We aren't allowed to see it now, but eventually we will.
1490 */ 1497 */
1491 *notask_error = 0; 1498 wo->notask_error = 0;
1492 return 0; 1499 return 0;
1493 } 1500 }
1494 1501
@@ -1499,34 +1506,30 @@ static int wait_consider_task(struct task_struct *parent, int ptrace,
1499 * We don't reap group leaders with subthreads. 1506 * We don't reap group leaders with subthreads.
1500 */ 1507 */
1501 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p)) 1508 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
1502 return wait_task_zombie(p, options, infop, stat_addr, ru); 1509 return wait_task_zombie(wo, p);
1503 1510
1504 /* 1511 /*
1505 * It's stopped or running now, so it might 1512 * It's stopped or running now, so it might
1506 * later continue, exit, or stop again. 1513 * later continue, exit, or stop again.
1507 */ 1514 */
1508 *notask_error = 0; 1515 wo->notask_error = 0;
1509 1516
1510 if (task_stopped_code(p, ptrace)) 1517 if (task_stopped_code(p, ptrace))
1511 return wait_task_stopped(ptrace, p, options, 1518 return wait_task_stopped(wo, ptrace, p);
1512 infop, stat_addr, ru);
1513 1519
1514 return wait_task_continued(p, options, infop, stat_addr, ru); 1520 return wait_task_continued(wo, p);
1515} 1521}
1516 1522
1517/* 1523/*
1518 * Do the work of do_wait() for one thread in the group, @tsk. 1524 * Do the work of do_wait() for one thread in the group, @tsk.
1519 * 1525 *
1520 * -ECHILD should be in *@notask_error before the first call. 1526 * -ECHILD should be in ->notask_error before the first call.
1521 * Returns nonzero for a final return, when we have unlocked tasklist_lock. 1527 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1522 * Returns zero if the search for a child should continue; then 1528 * Returns zero if the search for a child should continue; then
1523 * *@notask_error is 0 if there were any eligible children, 1529 * ->notask_error is 0 if there were any eligible children,
1524 * or another error from security_task_wait(), or still -ECHILD. 1530 * or another error from security_task_wait(), or still -ECHILD.
1525 */ 1531 */
1526static int do_wait_thread(struct task_struct *tsk, int *notask_error, 1532static 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{ 1533{
1531 struct task_struct *p; 1534 struct task_struct *p;
1532 1535
@@ -1535,9 +1538,7 @@ static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1535 * Do not consider detached threads. 1538 * Do not consider detached threads.
1536 */ 1539 */
1537 if (!task_detached(p)) { 1540 if (!task_detached(p)) {
1538 int ret = wait_consider_task(tsk, 0, p, notask_error, 1541 int ret = wait_consider_task(wo, tsk, 0, p);
1539 type, pid, options,
1540 infop, stat_addr, ru);
1541 if (ret) 1542 if (ret)
1542 return ret; 1543 return ret;
1543 } 1544 }
@@ -1546,22 +1547,12 @@ static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1546 return 0; 1547 return 0;
1547} 1548}
1548 1549
1549static int ptrace_do_wait(struct task_struct *tsk, int *notask_error, 1550static 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{ 1551{
1554 struct task_struct *p; 1552 struct task_struct *p;
1555 1553
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) { 1554 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1562 int ret = wait_consider_task(tsk, 1, p, notask_error, 1555 int ret = wait_consider_task(wo, tsk, 1, p);
1563 type, pid, options,
1564 infop, stat_addr, ru);
1565 if (ret) 1556 if (ret)
1566 return ret; 1557 return ret;
1567 } 1558 }
@@ -1569,65 +1560,59 @@ static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
1569 return 0; 1560 return 0;
1570} 1561}
1571 1562
1572static long do_wait(enum pid_type type, struct pid *pid, int options, 1563static long do_wait(struct wait_opts *wo)
1573 struct siginfo __user *infop, int __user *stat_addr,
1574 struct rusage __user *ru)
1575{ 1564{
1576 DECLARE_WAITQUEUE(wait, current); 1565 DECLARE_WAITQUEUE(wait, current);
1577 struct task_struct *tsk; 1566 struct task_struct *tsk;
1578 int retval; 1567 int retval;
1579 1568
1580 trace_sched_process_wait(pid); 1569 trace_sched_process_wait(wo->wo_pid);
1581 1570
1582 add_wait_queue(&current->signal->wait_chldexit,&wait); 1571 add_wait_queue(&current->signal->wait_chldexit,&wait);
1583repeat: 1572repeat:
1584 /* 1573 /*
1585 * If there is nothing that can match our critiera just get out. 1574 * 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 1575 * 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. 1576 * might later match our criteria, even if we are not able to reap
1577 * it yet.
1588 */ 1578 */
1589 retval = -ECHILD; 1579 wo->notask_error = -ECHILD;
1590 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type]))) 1580 if ((wo->wo_type < PIDTYPE_MAX) &&
1591 goto end; 1581 (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
1582 goto notask;
1592 1583
1593 current->state = TASK_INTERRUPTIBLE; 1584 set_current_state(TASK_INTERRUPTIBLE);
1594 read_lock(&tasklist_lock); 1585 read_lock(&tasklist_lock);
1595 tsk = current; 1586 tsk = current;
1596 do { 1587 do {
1597 int tsk_result = do_wait_thread(tsk, &retval, 1588 retval = do_wait_thread(wo, tsk);
1598 type, pid, options, 1589 if (retval)
1599 infop, stat_addr, ru); 1590 goto end;
1600 if (!tsk_result) 1591
1601 tsk_result = ptrace_do_wait(tsk, &retval, 1592 retval = ptrace_do_wait(wo, tsk);
1602 type, pid, options, 1593 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; 1594 goto end;
1610 }
1611 1595
1612 if (options & __WNOTHREAD) 1596 if (wo->wo_flags & __WNOTHREAD)
1613 break; 1597 break;
1614 tsk = next_thread(tsk); 1598 } while_each_thread(current, tsk);
1615 BUG_ON(tsk->signal != current->signal);
1616 } while (tsk != current);
1617 read_unlock(&tasklist_lock); 1599 read_unlock(&tasklist_lock);
1618 1600
1619 if (!retval && !(options & WNOHANG)) { 1601notask:
1602 retval = wo->notask_error;
1603 if (!retval && !(wo->wo_flags & WNOHANG)) {
1620 retval = -ERESTARTSYS; 1604 retval = -ERESTARTSYS;
1621 if (!signal_pending(current)) { 1605 if (!signal_pending(current)) {
1622 schedule(); 1606 schedule();
1623 goto repeat; 1607 goto repeat;
1624 } 1608 }
1625 } 1609 }
1626
1627end: 1610end:
1628 current->state = TASK_RUNNING; 1611 __set_current_state(TASK_RUNNING);
1629 remove_wait_queue(&current->signal->wait_chldexit,&wait); 1612 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1630 if (infop) { 1613 if (wo->wo_info) {
1614 struct siginfo __user *infop = wo->wo_info;
1615
1631 if (retval > 0) 1616 if (retval > 0)
1632 retval = 0; 1617 retval = 0;
1633 else { 1618 else {
@@ -1656,6 +1641,7 @@ end:
1656SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *, 1641SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1657 infop, int, options, struct rusage __user *, ru) 1642 infop, int, options, struct rusage __user *, ru)
1658{ 1643{
1644 struct wait_opts wo;
1659 struct pid *pid = NULL; 1645 struct pid *pid = NULL;
1660 enum pid_type type; 1646 enum pid_type type;
1661 long ret; 1647 long ret;
@@ -1685,7 +1671,14 @@ SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1685 1671
1686 if (type < PIDTYPE_MAX) 1672 if (type < PIDTYPE_MAX)
1687 pid = find_get_pid(upid); 1673 pid = find_get_pid(upid);
1688 ret = do_wait(type, pid, options, infop, NULL, ru); 1674
1675 wo.wo_type = type;
1676 wo.wo_pid = pid;
1677 wo.wo_flags = options;
1678 wo.wo_info = infop;
1679 wo.wo_stat = NULL;
1680 wo.wo_rusage = ru;
1681 ret = do_wait(&wo);
1689 put_pid(pid); 1682 put_pid(pid);
1690 1683
1691 /* avoid REGPARM breakage on x86: */ 1684 /* avoid REGPARM breakage on x86: */
@@ -1696,6 +1689,7 @@ SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1696SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr, 1689SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1697 int, options, struct rusage __user *, ru) 1690 int, options, struct rusage __user *, ru)
1698{ 1691{
1692 struct wait_opts wo;
1699 struct pid *pid = NULL; 1693 struct pid *pid = NULL;
1700 enum pid_type type; 1694 enum pid_type type;
1701 long ret; 1695 long ret;
@@ -1717,7 +1711,13 @@ SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1717 pid = find_get_pid(upid); 1711 pid = find_get_pid(upid);
1718 } 1712 }
1719 1713
1720 ret = do_wait(type, pid, options | WEXITED, NULL, stat_addr, ru); 1714 wo.wo_type = type;
1715 wo.wo_pid = pid;
1716 wo.wo_flags = options | WEXITED;
1717 wo.wo_info = NULL;
1718 wo.wo_stat = stat_addr;
1719 wo.wo_rusage = ru;
1720 ret = do_wait(&wo);
1721 put_pid(pid); 1721 put_pid(pid);
1722 1722
1723 /* avoid REGPARM breakage on x86: */ 1723 /* avoid REGPARM breakage on x86: */