Skip to content
Snippets Groups Projects
exit.c 39.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!retval)
    		retval = put_user((short)why, &infop->si_code);
    	if (!retval)
    		retval = put_user(pid, &infop->si_pid);
    	if (!retval)
    		retval = put_user(uid, &infop->si_uid);
    	if (!retval)
    		retval = put_user(status, &infop->si_status);
    	if (!retval)
    		retval = pid;
    	return retval;
    }
    
    /*
     * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold
     * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
     * the lock and this task is uninteresting.  If we return nonzero, we have
     * released the lock and the system call should return.
     */
    static int wait_task_zombie(task_t *p, int noreap,
    			    struct siginfo __user *infop,
    			    int __user *stat_addr, struct rusage __user *ru)
    {
    	unsigned long state;
    	int retval;
    	int status;
    
    	if (unlikely(noreap)) {
    		pid_t pid = p->pid;
    		uid_t uid = p->uid;
    		int exit_code = p->exit_code;
    		int why, status;
    
    		if (unlikely(p->exit_state != EXIT_ZOMBIE))
    			return 0;
    		if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
    			return 0;
    		get_task_struct(p);
    		read_unlock(&tasklist_lock);
    		if ((exit_code & 0x7f) == 0) {
    			why = CLD_EXITED;
    			status = exit_code >> 8;
    		} else {
    			why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
    			status = exit_code & 0x7f;
    		}
    		return wait_noreap_copyout(p, pid, uid, why,
    					   status, infop, ru);
    	}
    
    	/*
    	 * Try to move the task's state to DEAD
    	 * only one thread is allowed to do this:
    	 */
    	state = xchg(&p->exit_state, EXIT_DEAD);
    	if (state != EXIT_ZOMBIE) {
    		BUG_ON(state != EXIT_DEAD);
    		return 0;
    	}
    	if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
    		/*
    		 * This can only happen in a race with a ptraced thread
    		 * dying on another processor.
    		 */
    		return 0;
    	}
    
    	if (likely(p->real_parent == p->parent) && likely(p->signal)) {
    		/*
    		 * The resource counters for the group leader are in its
    		 * own task_struct.  Those for dead threads in the group
    		 * are in its signal_struct, as are those for the child
    		 * processes it has previously reaped.  All these
    		 * accumulate in the parent's signal_struct c* fields.
    		 *
    		 * We don't bother to take a lock here to protect these
    		 * p->signal fields, because they are only touched by
    		 * __exit_signal, which runs with tasklist_lock
    		 * write-locked anyway, and so is excluded here.  We do
    		 * need to protect the access to p->parent->signal fields,
    		 * as other threads in the parent group can be right
    		 * here reaping other children at the same time.
    		 */
    		spin_lock_irq(&p->parent->sighand->siglock);
    		p->parent->signal->cutime =
    			cputime_add(p->parent->signal->cutime,
    			cputime_add(p->utime,
    			cputime_add(p->signal->utime,
    				    p->signal->cutime)));
    		p->parent->signal->cstime =
    			cputime_add(p->parent->signal->cstime,
    			cputime_add(p->stime,
    			cputime_add(p->signal->stime,
    				    p->signal->cstime)));
    		p->parent->signal->cmin_flt +=
    			p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
    		p->parent->signal->cmaj_flt +=
    			p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
    		p->parent->signal->cnvcsw +=
    			p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
    		p->parent->signal->cnivcsw +=
    			p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
    		spin_unlock_irq(&p->parent->sighand->siglock);
    	}
    
    	/*
    	 * Now we are sure this task is interesting, and no other
    	 * thread can reap it because we set its state to EXIT_DEAD.
    	 */
    	read_unlock(&tasklist_lock);
    
    	retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
    	status = (p->signal->flags & SIGNAL_GROUP_EXIT)
    		? p->signal->group_exit_code : p->exit_code;
    	if (!retval && stat_addr)
    		retval = put_user(status, stat_addr);
    	if (!retval && infop)
    		retval = put_user(SIGCHLD, &infop->si_signo);
    	if (!retval && infop)
    		retval = put_user(0, &infop->si_errno);
    	if (!retval && infop) {
    		int why;
    
    		if ((status & 0x7f) == 0) {
    			why = CLD_EXITED;
    			status >>= 8;
    		} else {
    			why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
    			status &= 0x7f;
    		}
    		retval = put_user((short)why, &infop->si_code);
    		if (!retval)
    			retval = put_user(status, &infop->si_status);
    	}
    	if (!retval && infop)
    		retval = put_user(p->pid, &infop->si_pid);
    	if (!retval && infop)
    		retval = put_user(p->uid, &infop->si_uid);
    	if (retval) {
    		// TODO: is this safe?
    		p->exit_state = EXIT_ZOMBIE;
    		return retval;
    	}
    	retval = p->pid;
    	if (p->real_parent != p->parent) {
    		write_lock_irq(&tasklist_lock);
    		/* Double-check with lock held.  */
    		if (p->real_parent != p->parent) {
    			__ptrace_unlink(p);
    			// TODO: is this safe?
    			p->exit_state = EXIT_ZOMBIE;
    			/*
    			 * If this is not a detached task, notify the parent.
    			 * If it's still not detached after that, don't release
    			 * it now.
    			 */
    			if (p->exit_signal != -1) {
    				do_notify_parent(p, p->exit_signal);
    				if (p->exit_signal != -1)
    					p = NULL;
    			}
    		}
    		write_unlock_irq(&tasklist_lock);
    	}
    	if (p != NULL)
    		release_task(p);
    	BUG_ON(!retval);
    	return retval;
    }
    
    /*
     * Handle sys_wait4 work for one task in state TASK_STOPPED.  We hold
     * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
     * the lock and this task is uninteresting.  If we return nonzero, we have
     * released the lock and the system call should return.
     */
    static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
    			     struct siginfo __user *infop,
    			     int __user *stat_addr, struct rusage __user *ru)
    {
    	int retval, exit_code;
    
    	if (!p->exit_code)
    		return 0;
    	if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
    	    p->signal && p->signal->group_stop_count > 0)
    		/*
    		 * A group stop is in progress and this is the group leader.
    		 * We won't report until all threads have stopped.
    		 */
    		return 0;
    
    	/*
    	 * Now we are pretty sure this task is interesting.
    	 * Make sure it doesn't get reaped out from under us while we
    	 * give up the lock and then examine it below.  We don't want to
    	 * keep holding onto the tasklist_lock while we call getrusage and
    	 * possibly take page faults for user memory.
    	 */
    	get_task_struct(p);
    	read_unlock(&tasklist_lock);
    
    	if (unlikely(noreap)) {
    		pid_t pid = p->pid;
    		uid_t uid = p->uid;
    		int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
    
    		exit_code = p->exit_code;
    		if (unlikely(!exit_code) ||
    
    		    unlikely(p->state & TASK_TRACED))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
    			goto bail_ref;
    		return wait_noreap_copyout(p, pid, uid,
    					   why, (exit_code << 8) | 0x7f,
    					   infop, ru);
    	}
    
    	write_lock_irq(&tasklist_lock);
    
    	/*
    	 * This uses xchg to be atomic with the thread resuming and setting
    	 * it.  It must also be done with the write lock held to prevent a
    	 * race with the EXIT_ZOMBIE case.
    	 */
    	exit_code = xchg(&p->exit_code, 0);
    	if (unlikely(p->exit_state)) {
    		/*
    		 * The task resumed and then died.  Let the next iteration
    		 * catch it in EXIT_ZOMBIE.  Note that exit_code might
    		 * already be zero here if it resumed and did _exit(0).
    		 * The task itself is dead and won't touch exit_code again;
    		 * other processors in this function are locked out.
    		 */
    		p->exit_code = exit_code;
    		exit_code = 0;
    	}
    	if (unlikely(exit_code == 0)) {
    		/*
    		 * Another thread in this function got to it first, or it
    		 * resumed, or it resumed and then died.
    		 */
    		write_unlock_irq(&tasklist_lock);
    bail_ref:
    		put_task_struct(p);
    		/*
    		 * We are returning to the wait loop without having successfully
    		 * removed the process and having released the lock. We cannot
    		 * continue, since the "p" task pointer is potentially stale.
    		 *
    		 * Return -EAGAIN, and do_wait() will restart the loop from the
    		 * beginning. Do _not_ re-acquire the lock.
    		 */
    		return -EAGAIN;
    	}
    
    	/* move to end of parent's list to avoid starvation */
    	remove_parent(p);
    	add_parent(p, p->parent);
    
    	write_unlock_irq(&tasklist_lock);
    
    	retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
    	if (!retval && stat_addr)
    		retval = put_user((exit_code << 8) | 0x7f, stat_addr);
    	if (!retval && infop)
    		retval = put_user(SIGCHLD, &infop->si_signo);
    	if (!retval && infop)
    		retval = put_user(0, &infop->si_errno);
    	if (!retval && infop)
    		retval = put_user((short)((p->ptrace & PT_PTRACED)
    					  ? CLD_TRAPPED : CLD_STOPPED),
    				  &infop->si_code);
    	if (!retval && infop)
    		retval = put_user(exit_code, &infop->si_status);
    	if (!retval && infop)
    		retval = put_user(p->pid, &infop->si_pid);
    	if (!retval && infop)
    		retval = put_user(p->uid, &infop->si_uid);
    	if (!retval)
    		retval = p->pid;
    	put_task_struct(p);
    
    	BUG_ON(!retval);
    	return retval;
    }
    
    /*
     * Handle do_wait work for one task in a live, non-stopped state.
     * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
     * the lock and this task is uninteresting.  If we return nonzero, we have
     * released the lock and the system call should return.
     */
    static int wait_task_continued(task_t *p, int noreap,
    			       struct siginfo __user *infop,
    			       int __user *stat_addr, struct rusage __user *ru)
    {
    	int retval;
    	pid_t pid;
    	uid_t uid;
    
    	if (unlikely(!p->signal))
    		return 0;
    
    	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
    		return 0;
    
    	spin_lock_irq(&p->sighand->siglock);
    	/* Re-check with the lock held.  */
    	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
    		spin_unlock_irq(&p->sighand->siglock);
    		return 0;
    	}
    	if (!noreap)
    		p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
    	spin_unlock_irq(&p->sighand->siglock);
    
    	pid = p->pid;
    	uid = p->uid;
    	get_task_struct(p);
    	read_unlock(&tasklist_lock);
    
    	if (!infop) {
    		retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
    		put_task_struct(p);
    		if (!retval && stat_addr)
    			retval = put_user(0xffff, stat_addr);
    		if (!retval)
    			retval = p->pid;
    	} else {
    		retval = wait_noreap_copyout(p, pid, uid,
    					     CLD_CONTINUED, SIGCONT,
    					     infop, ru);
    		BUG_ON(retval == 0);
    	}
    
    	return retval;
    }
    
    
    static inline int my_ptrace_child(struct task_struct *p)
    {
    	if (!(p->ptrace & PT_PTRACED))
    		return 0;
    	if (!(p->ptrace & PT_ATTACHED))
    		return 1;
    	/*
    	 * This child was PTRACE_ATTACH'd.  We should be seeing it only if
    	 * we are the attacher.  If we are the real parent, this is a race
    	 * inside ptrace_attach.  It is waiting for the tasklist_lock,
    	 * which we have to switch the parent links, but has already set
    	 * the flags in p->ptrace.
    	 */
    	return (p->parent != p->real_parent);
    }
    
    static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
    		    int __user *stat_addr, struct rusage __user *ru)
    {
    	DECLARE_WAITQUEUE(wait, current);
    	struct task_struct *tsk;
    	int flag, retval;
    
    	add_wait_queue(&current->signal->wait_chldexit,&wait);
    repeat:
    	/*
    	 * We will set this flag if we see any child that might later
    	 * match our criteria, even if we are not able to reap it yet.
    	 */
    	flag = 0;
    	current->state = TASK_INTERRUPTIBLE;
    	read_lock(&tasklist_lock);
    	tsk = current;
    	do {
    		struct task_struct *p;
    		struct list_head *_p;
    		int ret;
    
    		list_for_each(_p,&tsk->children) {
    			p = list_entry(_p,struct task_struct,sibling);
    
    			ret = eligible_child(pid, options, p);
    			if (!ret)
    				continue;
    
    			switch (p->state) {
    			case TASK_TRACED:
    				if (!my_ptrace_child(p))
    					continue;
    				/*FALLTHROUGH*/
    			case TASK_STOPPED:
    				/*
    				 * It's stopped now, so it might later
    				 * continue, exit, or stop again.
    				 */
    				flag = 1;
    				if (!(options & WUNTRACED) &&
    				    !my_ptrace_child(p))
    					continue;
    				retval = wait_task_stopped(p, ret == 2,
    							   (options & WNOWAIT),
    							   infop,
    							   stat_addr, ru);
    				if (retval == -EAGAIN)
    					goto repeat;
    				if (retval != 0) /* He released the lock.  */
    					goto end;
    				break;
    			default:
    			// case EXIT_DEAD:
    				if (p->exit_state == EXIT_DEAD)
    					continue;
    			// case EXIT_ZOMBIE:
    				if (p->exit_state == EXIT_ZOMBIE) {
    					/*
    					 * Eligible but we cannot release
    					 * it yet:
    					 */
    					if (ret == 2)
    						goto check_continued;
    					if (!likely(options & WEXITED))
    						continue;
    					retval = wait_task_zombie(
    						p, (options & WNOWAIT),
    						infop, stat_addr, ru);
    					/* He released the lock.  */
    					if (retval != 0)
    						goto end;
    					break;
    				}
    check_continued:
    				/*
    				 * It's running now, so it might later
    				 * exit, stop, or stop and then continue.
    				 */
    				flag = 1;
    				if (!unlikely(options & WCONTINUED))
    					continue;
    				retval = wait_task_continued(
    					p, (options & WNOWAIT),
    					infop, stat_addr, ru);
    				if (retval != 0) /* He released the lock.  */
    					goto end;
    				break;
    			}
    		}
    		if (!flag) {
    			list_for_each(_p, &tsk->ptrace_children) {
    				p = list_entry(_p, struct task_struct,
    						ptrace_list);
    				if (!eligible_child(pid, options, p))
    					continue;
    				flag = 1;
    				break;
    			}
    		}
    		if (options & __WNOTHREAD)
    			break;
    		tsk = next_thread(tsk);
    		if (tsk->signal != current->signal)
    			BUG();
    	} while (tsk != current);
    
    	read_unlock(&tasklist_lock);
    	if (flag) {
    		retval = 0;
    		if (options & WNOHANG)
    			goto end;
    		retval = -ERESTARTSYS;
    		if (signal_pending(current))
    			goto end;
    		schedule();
    		goto repeat;
    	}
    	retval = -ECHILD;
    end:
    	current->state = TASK_RUNNING;
    	remove_wait_queue(&current->signal->wait_chldexit,&wait);
    	if (infop) {
    		if (retval > 0)
    		retval = 0;
    		else {
    			/*
    			 * For a WNOHANG return, clear out all the fields
    			 * we would set so the user can easily tell the
    			 * difference.
    			 */
    			if (!retval)
    				retval = put_user(0, &infop->si_signo);
    			if (!retval)
    				retval = put_user(0, &infop->si_errno);
    			if (!retval)
    				retval = put_user(0, &infop->si_code);
    			if (!retval)
    				retval = put_user(0, &infop->si_pid);
    			if (!retval)
    				retval = put_user(0, &infop->si_uid);
    			if (!retval)
    				retval = put_user(0, &infop->si_status);
    		}
    	}
    	return retval;
    }
    
    asmlinkage long sys_waitid(int which, pid_t pid,
    			   struct siginfo __user *infop, int options,
    			   struct rusage __user *ru)
    {
    	long ret;
    
    	if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
    		return -EINVAL;
    	if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
    		return -EINVAL;
    
    	switch (which) {
    	case P_ALL:
    		pid = -1;
    		break;
    	case P_PID:
    		if (pid <= 0)
    			return -EINVAL;
    		break;
    	case P_PGID:
    		if (pid <= 0)
    			return -EINVAL;
    		pid = -pid;
    		break;
    	default:
    		return -EINVAL;
    	}
    
    	ret = do_wait(pid, options, infop, NULL, ru);
    
    	/* avoid REGPARM breakage on x86: */
    	prevent_tail_call(ret);
    	return ret;
    }
    
    asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
    			  int options, struct rusage __user *ru)
    {
    	long ret;
    
    	if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
    			__WNOTHREAD|__WCLONE|__WALL))
    		return -EINVAL;
    	ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
    
    	/* avoid REGPARM breakage on x86: */
    	prevent_tail_call(ret);
    	return ret;
    }
    
    #ifdef __ARCH_WANT_SYS_WAITPID
    
    /*
     * sys_waitpid() remains for compatibility. waitpid() should be
     * implemented by calling sys_wait4() from libc.a.
     */
    asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
    {
    	return sys_wait4(pid, stat_addr, options, NULL);
    }
    
    #endif