diff --git a/dumb-init.c b/dumb-init.c index 9c40637..a97ab41 100644 --- a/dumb-init.c +++ b/dumb-init.c @@ -326,6 +326,11 @@ int main(int argc, char *argv[]) { } else { /* parent */ DEBUG("Child spawned with PID %d.\n", child_pid); + if (chdir("/") == -1) { + DEBUG("Unable to chdir(\"/\") (errno=%d %s)\n", + errno, + strerror(errno)); + } for (;;) { int signum; sigwait(&all_signals, &signum); diff --git a/tests/cwd_test.py b/tests/cwd_test.py new file mode 100644 index 0000000..ff4ff80 --- /dev/null +++ b/tests/cwd_test.py @@ -0,0 +1,22 @@ +import os +import shutil +from subprocess import run, PIPE + +import pytest + +@pytest.mark.usefixtures('both_debug_modes', 'both_setsid_modes') +def test_working_directories(): + """The child process must start in the working directory in which + dumb-init was invoked, but dumb-init itself should not keep a + reference to that.""" + + # We need absolute path to dumb-init since we pass cwd=/tmp to get + # predictable output - so we can't rely on dumb-init being found + # in the "." directory. + dumb_init = os.path.realpath(shutil.which('dumb-init')) + proc = run((dumb_init, + 'sh', '-c', 'readlink /proc/$PPID/cwd && readlink /proc/$$/cwd'), + cwd="/tmp", stdout=PIPE, stderr=PIPE) + + assert proc.returncode == 0 + assert proc.stdout == b'/\n/tmp\n'