Skip to content

Commit

Permalink
catatoinit: rewrite LISTEN_PID to the child process
Browse files Browse the repository at this point in the history
In order to get systemd fd passing let's rewrite the LISTEN_PID
environment variable if it is pointing to catatonit init process.

Closes: #13

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Sep 15, 2021
1 parent 932bfab commit 0de094f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions catatonit.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Expand Down Expand Up @@ -193,6 +194,37 @@ static int make_foreground(sigset_t *sigmask)
return 0;
}

/*
* If the LISTEN_PID environment variable is set to the parent pid, rewrite it to
* point to the current pid.
*/
static void rewrite_listen_pid_env()
{
char *listen_pid = getenv("LISTEN_PID");
long long val;

if (listen_pid == NULL)
return;

errno = 0;
val = strtoll(listen_pid, NULL, 10);
if (errno == ERANGE) {
warn("LISTEN_PID has an invalid value");
return;
}

if (val == getppid()) {
char pid_str[32];
int r;

snprintf(pid_str, sizeof(pid_str), "%d", getpid());

r = setenv("LISTEN_PID", pid_str, 1);
if (r < 0)
warn("could not overwrite env variable LISTEN_PID: %m");
}
}

/*
* Spawn a child process with the given arguments and signal map and make it a
* faux-pid1 by placing it in the foreground. This is the main process which
Expand All @@ -207,6 +239,8 @@ static int spawn_pid1(char *file, char **argv, sigset_t *sigmask)
return child;
}

rewrite_listen_pid_env();

/*
* We are now in the child. Set up our sigmask, put ourselves in the
* foreground, and then finally exec (with the environment inherited).
Expand Down

0 comments on commit 0de094f

Please sign in to comment.