Crinit -- Configurable Rootfs Init
Loading...
Searching...
No Matches
procdip.c File Reference

(/usr/bin/git 2025-03-27, commit: ffe114c)

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 "globopt.h"
#include "lexers.h"
#include "logio.h"
Include dependency graph for procdip.c:

Data Structures

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, crinitDispatchThreadMode_t mode)
 
int crinitSpawnSingleCommand (const char *cmd, char *const argv[], char *const envp[], posix_spawn_file_actions_t *fileact, const char *name, size_t cmdIdx, pid_t threadId, pid_t *pid)
 
int crinitPrepareIoRedirectionsForSpawn (size_t cmdIdx, crinitIoRedir_t *redirs, size_t redirsSize, pid_t threadId, const char *name, posix_spawn_file_actions_t *fileact)
 
static int crinitCalculateVariableGroupParamLength (size_t supGroupsSize, gid_t *supGroups)
 
static char * crinitCreateSupGroupsParamString (size_t supGroupsSize, gid_t *supGroups, int expOutSize)
 
int crinitCreateLauncherParameters (crinitTaskCmd_t *taskCmd, crinitTask_t *tCopy, char *cmd, char ***argv, char **argBuffer)
 
int crinitHandleCommands (crinitTaskDB_t *ctx, pid_t threadId, char *name, crinitTaskCmd_t *cmds, size_t cmdsSize, crinitTask_t *tCopy, pid_t *pid, bool deactivateFileactions)
 
int crinitExpandPIDVariablesInSingleCommand (char *input, const pid_t pid, char **result)
 
void crinitExpandPIDVariablesInCommands (crinitTaskCmd_t *commands, size_t cmdsSize, const pid_t pid)
 Expands variable ${TASK_PID} in a command with the task's PID.
 
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

◆ crinitCalculateVariableGroupParamLength()

static int crinitCalculateVariableGroupParamLength ( size_t  supGroupsSize,
gid_t *  supGroups 
)
static

◆ crinitCreateLauncherParameters()

int crinitCreateLauncherParameters ( crinitTaskCmd_t taskCmd,
crinitTask_t tCopy,
char *  cmd,
char ***  argv,
char **  argBuffer 
)

◆ crinitCreateSupGroupsParamString()

static char * crinitCreateSupGroupsParamString ( size_t  supGroupsSize,
gid_t *  supGroups,
int  expOutSize 
)
static

◆ 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.

◆ crinitExpandPIDVariablesInCommands()

void crinitExpandPIDVariablesInCommands ( crinitTaskCmd_t commands,
size_t  cmdsSize,
const pid_t  pid 
)

Expands variable ${TASK_PID} in a command with the task's PID.

Please note that this makes only sense for a STOP_COMMAND. Currently this is the only variable that can be expanded here.

Parameters
commandsPointer to task command structure
cmdsSizeNumber of elements in commands
pidPID of task

◆ crinitExpandPIDVariablesInSingleCommand()

int crinitExpandPIDVariablesInSingleCommand ( char *  input,
const pid_t  pid,
char **  result 
)

◆ crinitHandleCommands()

int crinitHandleCommands ( crinitTaskDB_t ctx,
pid_t  threadId,
char *  name,
crinitTaskCmd_t cmds,
size_t  cmdsSize,
crinitTask_t tCopy,
pid_t *  pid,
bool  deactivateFileactions 
)

◆ 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.

◆ crinitPrepareIoRedirectionsForSpawn()

int crinitPrepareIoRedirectionsForSpawn ( size_t  cmdIdx,
crinitIoRedir_t redirs,
size_t  redirsSize,
pid_t  threadId,
const char *  name,
posix_spawn_file_actions_t *  fileact 
)

◆ crinitProcDispatchSpawnFunc()

int crinitProcDispatchSpawnFunc ( crinitTaskDB_t ctx,
const crinitTask_t t,
crinitDispatchThreadMode_t  mode 
)

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.

Parameters
ctxPointer to context
tPointer to task that needs spawning
modeDistinguishes between start and stop commands
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

◆ crinitSpawnSingleCommand()

int crinitSpawnSingleCommand ( const char *  cmd,
char *const  argv[],
char *const  envp[],
posix_spawn_file_actions_t *  fileact,
const char *  name,
size_t  cmdIdx,
pid_t  threadId,
pid_t *  pid 
)

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