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: openSUSE#13

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Sep 2, 2021
1 parent 932bfab commit 75b9133
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 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,20 +194,46 @@ static int make_foreground(sigset_t *sigmask)
return 0;
}

/*
* If the LISTEN_PID environment variable is set to "1", rewrite it to
* point to the child process.
*/
static void rewrite_listen_pid_env(int parent_pid)
{
char *listen_pid;
char pid_str[32];

sprintf(pid_str, "%d", parent_pid);

listen_pid = getenv("LISTEN_PID");
if (listen_pid != NULL && strcmp (listen_pid, pid_str) == 0) {
int r;

sprintf(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
* catatonit is going to be managing throughout its life.
*/
static int spawn_pid1(char *file, char **argv, sigset_t *sigmask)
{
pid_t parent_pid = getpid();
pid_t child = fork();
if (child != 0) {
if (child < 0)
error("failed to fork child: %m");
return child;
}

rewrite_listen_pid_env(parent_pid);

/*
* 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 75b9133

Please sign in to comment.