Merge branch 'buck/buck-review' into process-groups

This commit is contained in:
Chris Kuehl 2015-09-03 14:59:23 -07:00
commit 427bdd25ba
5 changed files with 25 additions and 45 deletions

View file

@ -128,7 +128,7 @@ int main(int argc, char *argv[]) {
waitpid(child, &status, 0); waitpid(child, &status, 0);
exit_status = WEXITSTATUS(status); exit_status = WEXITSTATUS(status);
DEBUG("Child exited with status %d, goodbye.\n", exit_status); DEBUG("Child exited with status %d. Goodbye.\n", exit_status);
return exit_status; return exit_status;
} }

4
test
View file

@ -18,8 +18,8 @@ run_tests() {
./test-help-message "$dumb_init_bin" ./test-help-message "$dumb_init_bin"
done done
./test-pgroup "$dumb_init_bin" DUMB_INIT_PROCESS_GROUP=0 ./test-pgroup "$dumb_init_bin" 4
./test-no-pgroup "$dumb_init_bin" DUMB_INIT_PROCESS_GROUP=1 ./test-pgroup "$dumb_init_bin" 0
} }
cd tests cd tests

View file

@ -6,15 +6,15 @@ dumb_init="$1"
status=$($dumb_init > /dev/null 2>&1; echo $?) status=$($dumb_init > /dev/null 2>&1; echo $?)
if [ "$status" -ne 0 ]; then if [ "$status" -eq 0 ]; then
msg=$($dumb_init 2>&1 || true)
msg_len=${#msg}
if [ "$msg_len" -le 50 ]; then
echo "Error: Expected dumb-init with no arguments to print a useful message, but it was only ${msg_len} chars long."
exit 1
fi
else
echo "Error: Expected dumb-init with no arguments to return nonzero, but it returned ${status}." echo "Error: Expected dumb-init with no arguments to return nonzero, but it returned ${status}."
exit 1 exit 1
fi fi
msg=$($dumb_init 2>&1 || true)
msg_len=${#msg}
if [ "$msg_len" -le 50 ]; then
echo "Error: Expected dumb-init with no arguments to print a useful message, but it was only ${msg_len} chars long."
exit 1
fi

View file

@ -1,25 +0,0 @@
#!/bin/bash -euxm
# dumb-init should not proxy signals to a process group rooted at its child if
# asked not to.
dumb_init="$1"
DUMB_INIT_PROCESS_GROUP=0 $dumb_init nohup sh -c "yes 'oh, hi' | tail & yes error | tail >&2" > /dev/null &
pid="$!"
sleep 1
shell_pid=$(pgrep -P "$pid")
child_pids=$(pgrep -P $shell_pid)
kill -TERM "$pid"
sleep 1
# ensure all processes are still running
while read pid; do
if pgrep "$pid"; then
echo "Error: expected process $pid to still be alive, but it wasn't."
exit 1
else
echo "Process $pid was still alive (as expected)."
fi
done <<< "$child_pids"
xargs kill -9 <<< "$child_pids"

View file

@ -2,26 +2,31 @@
# dumb-init should proxy signals to a process group rooted at its child when # dumb-init should proxy signals to a process group rooted at its child when
# requested. # requested.
dumb_init="$1" dumb_init="$1"
after_count="$2"
DUMB_INIT_PROCESS_GROUP=1 $dumb_init nohup sh -c "yes 'oh, hi' | tail & yes error | tail >&2" > /dev/null & $dumb_init sh -c "yes 'oh, hi' | tail & yes error | tail >&2" &
pid="$!" pid="$!"
sleep 1 sleep 1
shell_pid=$(pgrep -P "$pid") pstree -p "$pid"
pids=$(pstree -p "$pid" | grep -Po '(\d+)' | grep -Po '\d+')
# ensure processes are running # ensure processes are running
child_count=$(pgrep -g "$shell_pid" | grep -v "$shell_pid" | wc -l) || true child_count=$(ps -o pid= $pids | wc -l) || true
if [ "$child_count" -ne 4 ]; then if [ "$child_count" -ne 6 ]; then
echo "Error: Expected 4 children, instead we had ${child_count}." echo "Error: Expected 6 children, instead we had ${child_count}."
exit 1 exit 1
fi fi
# ensure processes are dead after signal # ensure processes are dead after signal
kill -TERM "$pid" kill -TERM "$pid"
child_count=$(pgrep -g "$shell_pid" | grep -v "$shell_pid" | wc -l) || true sleep 1
child_count=$(ps -o pid= $pids | wc -l) || true
if [ "$child_count" -ne 0 ]; then if [ "$child_count" -ne "$after_count" ]; then
echo "Error: Expected 0 children, instead we had ${child_count}." echo "Error: Expected $after_count children, instead we had ${child_count}."
exit 1 exit 1
fi fi
xargs kill -9 <<< "$pids"