Skip to content

Commit

Permalink
[fork-tester.c] Get PPID from env MANAGERPID when systemd run with --…
Browse files Browse the repository at this point in the history
…user

Fix [#3148](#3148)
On certain distributions, `systemd` run with the flag `--user`. This
means that the manager PID is not 1 but one of it's child process.
To fix this, we call the environment variable `MANAGERPID` set by systemd
that expose this PID.
As `make test` uses sudo rights, you need to pass your local `MANAGERPID`
env var to sudo. If the variable is not set, the flag is ignored

Signed-off-by: Tristan d'Audibert <[email protected]>
  • Loading branch information
ScriptSathi committed Nov 22, 2024
1 parent d07a110 commit f154425
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ copy-golangci-lint:

.PHONY: test
test: tester-progs tetragon-bpf ## Run Go tests.
$(GO) test -exec "$(SUDO)" -p 1 -parallel 1 $(GOFLAGS) -gcflags=$(GO_BUILD_GCFLAGS) -timeout $(GO_TEST_TIMEOUT) -failfast -cover ./pkg/... ./cmd/... ./operator/... ${EXTRA_TESTFLAGS}
$(GO) test -exec "$(SUDO) --preserve-env=MANAGERPID" -p 1 -parallel 1 $(GOFLAGS) -gcflags=$(GO_BUILD_GCFLAGS) -timeout $(GO_TEST_TIMEOUT) -failfast -cover ./pkg/... ./cmd/... ./operator/... ${EXTRA_TESTFLAGS}

.PHONY: tester-progs
tester-progs: ## Compile helper programs for unit testing.
Expand Down
11 changes: 9 additions & 2 deletions contrib/tester-progs/fork-tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ void child1()
perror("fork");
exit(1);
} else if (pid == 0) {
int systemd_manager_pid = 1;
const char *manager_pid_str = getenv("MANAGERPID");
if (manager_pid_str) {
int user_manager_pid = atoi(manager_pid_str);
if (user_manager_pid) {
systemd_manager_pid = user_manager_pid;
}
}
for (int i = 0;; i++) {
int ppid = getppid();
if (ppid == 1) {
if (ppid == systemd_manager_pid)
break;
}
if (i == 30) {
fprintf(stderr, "giving up on waiting our parent to die\n");
exit(1);
Expand Down

0 comments on commit f154425

Please sign in to comment.