Crinit -- Configurable Rootfs Init
procdip.c File Reference

(2023-08-28, commit: c9b21e6)

Implementation of the Process Dispatcher. More...

#include "procdip.h"
#include <spawn.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "envset.h"
#include "logio.h"
Include dependency graph for procdip.c:

Classes

struct  crinitDispThrArgs_t
 

Macros

#define crinitGettid()   ((pid_t)syscall(SYS_gettid))
 

Typedefs

typedef struct crinitDispThrArgs_t crinitDispThrArgs_t
 

Functions

static void * crinitDispatchThreadFunc (void *args)
 
static int crinitBlockOnWaitInhibit (void)
 
static int crinitReapPid (pid_t pid)
 
static int crinitPosixSpawnAddIOFileAction (posix_spawn_file_actions_t *fileact, const crinitIoRedir_t *ior)
 
static int crinitEnsureFifo (const char *path, mode_t mode)
 
int crinitProcDispatchSpawnFunc (crinitTaskDB_t *ctx, const crinitTask_t *t)
 
int crinitSetInhibitWait (bool inh)
 

Variables

static pthread_mutex_t crinitWaitInhibitLock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_cond_t crinitWaitInhibitCond = PTHREAD_COND_INITIALIZER
 
static bool crinitWaitInhibit = false
 

Detailed Description

Implementation of the Process Dispatcher.

Macro Definition Documentation

◆ crinitGettid

#define crinitGettid ( )    ((pid_t)syscall(SYS_gettid))

Macro wrapper for the gettid syscall in case glibc is not new enough to contain one itself

Typedef Documentation

◆ crinitDispThrArgs_t

Struct wrapper for arguments to dispatchThreadFunc

Function Documentation

◆ crinitBlockOnWaitInhibit()

static int crinitBlockOnWaitInhibit ( void  )
static

Block calling thread until crinitWaitInhibit becomes false.

Called by dispatcher threads to delay reaping of zombie processes if needed.

Returns
0 on success, -1 on error

◆ crinitDispatchThreadFunc()

static void * crinitDispatchThreadFunc ( void *  args)
static

Function to be started as a pthread from crinitProcDispatchThread().

Takes care of process spawning/waiting and crinitTaskDB_t status updates.

Parameters
argsSee crinitDispThrArgs_t.

◆ crinitEnsureFifo()

static int crinitEnsureFifo ( const char *  path,
mode_t  mode 
)
static

Ensures the given path is a FIFO special file (named pipe).

If the file exists it will be checked wie stat() and the function will return an error if the file is anything else than a FIFO. If the file does not yet exist, it will be created with the given mode. Existing FIFOs will be left as is.

Parameters
pathThe path where the FIFO shall exist.
modeThe mode of a FIFO if it is newly created.
Returns
0 on success, -1 otherwise.

◆ crinitPosixSpawnAddIOFileAction()

static int crinitPosixSpawnAddIOFileAction ( posix_spawn_file_actions_t *  fileact,
const crinitIoRedir_t ior 
)
static

Adds an action to a posix_spawn_file_actions_t instance as defined by an crinitIoRedir_t instance.

The file action will either be a call to open() including an IO redirection for an crinitIoRedir_t which contains a non-NULL crinitIoRedir_t::path, or a single redirection via dup2() if crinitIoRedir_t::oldfd is positive. If both cases are true, the crinitIoRedir_t::path takes precedence and crinitIoRedir_r::oldfd is ignored.

crinitIoRedir_t::oflags is respected for files and crinitIoRedir_t::mode is respected for files that are overwritten or newly created.

Parameters
fileactThe posix_spawn_file_actions_t to be modified, must have been initialized.
iorThe crinitIoRedir_t defining the file action to be added.
Returns
0 on success, -1 otherwise.

◆ crinitProcDispatchSpawnFunc()

int crinitProcDispatchSpawnFunc ( crinitTaskDB_t ctx,
const crinitTask_t t 
)

Process dispatcher function to spawn a task that is ready.

Will create a new thread to handle process spawning of task t and status updates of ctx. The function will return successfully if the thread has been created without error. The thread is created in a detached state so no further management action is necessary.

Returns
0 on success, -1 otherwise

◆ crinitReapPid()

static int crinitReapPid ( pid_t  pid)
static

Reap a zombie process.

Will call blockOnWaitInhibit() internally.

Parameters
pidThe PID of the process to wait for.
Returns
0 on success, -1 on error

◆ crinitSetInhibitWait()

int crinitSetInhibitWait ( bool  inh)

Turn waiting for child processes on or off.

If inh is set to true, the process dispatch threads will block before waiting for a child process until waiting is reactivated, leaving terminated child processes as zombies for the time being.

Parameters
inhSet true to inhibit waiting or false to reactivate waiting.
Returns
0 on success, -1 on error

Variable Documentation

◆ crinitWaitInhibit

bool crinitWaitInhibit = false
static

If true, all terminated child processes will be kept around as zombies (see crinitBlockOnWaitInhibit()).

◆ crinitWaitInhibitCond

pthread_cond_t crinitWaitInhibitCond = PTHREAD_COND_INITIALIZER
static

Condition variable to signal threads waiting for crinitWaitInhibit to become false.

◆ crinitWaitInhibitLock

pthread_mutex_t crinitWaitInhibitLock = PTHREAD_MUTEX_INITIALIZER
static

Mutex to guard crinitWaitInhibit