Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chdir to / in parent #210

Merged
merged 2 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dumb-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions tests/cwd_test.py
Original file line number Diff line number Diff line change
@@ -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'