Crinit -- Configurable Rootfs Init
rtimcmd.c File Reference

(2023-08-31, commit: ba0a5f6)

Implementation of runtime commands to be triggered by the notification/service interface. More...

#include "rtimcmd.h"
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "common.h"
#include "fseries.h"
#include "globopt.h"
#include "logio.h"
#include "procdip.h"
#include "version.h"
Include dependency graph for rtimcmd.c:

Classes

struct  crinitShdnThrArgs_t
 
struct  crinitUnMountList_t
 

Typedefs

typedef struct crinitShdnThrArgs_t crinitShdnThrArgs_t
 
typedef struct crinitUnMountList_t crinitUnMountList_t
 

Functions

static int crinitExecRtimCmdAddTask (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdAddSeries (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdEnable (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdDisable (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdStop (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdKill (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdRestart (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdNotify (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdStatus (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdTaskList (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdGetVer (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static int crinitExecRtimCmdShutdown (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
static void * crinitShdnThread (void *args)
 
static int crinitGracePeriod (unsigned long long micros)
 
static int crinitFsPrepareShutdown (void)
 
static int crinitGenUnMountList (crinitUnMountList_t **um, bool *rootfsIsRo)
 
static void crinitFreeUnMountList (crinitUnMountList_t *um)
 
int crinitParseRtimCmd (crinitRtimCmd_t *out, const char *cmdStr)
 
int crinitRtimCmdToMsgStr (char **out, size_t *outLen, const crinitRtimCmd_t *cmd)
 
int crinitExecRtimCmd (crinitTaskDB_t *ctx, crinitRtimCmd_t *res, const crinitRtimCmd_t *cmd)
 
int crinitBuildRtimCmd (crinitRtimCmd_t *c, crinitRtimOp_t op, size_t argc,...)
 
int crinitBuildRtimCmdArray (crinitRtimCmd_t *c, crinitRtimOp_t op, int argc, const char *args[])
 
int crinitDestroyRtimCmd (crinitRtimCmd_t *c)
 

Detailed Description

Implementation of runtime commands to be triggered by the notification/service interface.

Typedef Documentation

◆ crinitShdnThrArgs_t

Argument structure for shdnThread().

◆ crinitUnMountList_t

A linked list to organize mount points that need to be handled before shutdown/reboot.

Function Documentation

◆ crinitBuildRtimCmd()

int crinitBuildRtimCmd ( crinitRtimCmd_t c,
crinitRtimOp_t  op,
size_t  argc,
  ... 
)

Create an crinitRtimCmd_t from an opcode and an argument list.

Will allocate memory for the argument string array which should be freed using crinitDestroyRtimCmd() when no longer needed.

Parameters
cThe crinitRtimCmd_t to build.
opThe opcode of the command or response.
argcThe number of arguments to the command/response.
...List of argc arguments of type const char*.
Returns
0 on success, -1 otherwise

◆ crinitBuildRtimCmdArray()

int crinitBuildRtimCmdArray ( crinitRtimCmd_t c,
crinitRtimOp_t  op,
int  argc,
const char *  args[] 
)

◆ crinitDestroyRtimCmd()

int crinitDestroyRtimCmd ( crinitRtimCmd_t c)

Free memory in an crinitRtimCmd_t allocated by crinitBuildRtimCmd() or crinitParseRtimCmd().

Will free the memory for the argument array in the structure.

Parameters
cThe crinitRtimCmd_t from which the memory should be freed.
Returns
0 on success, -1 otherwise

◆ crinitExecRtimCmd()

int crinitExecRtimCmd ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)

Executes an crinitRtimCmd_t if it contains a valid command.

For the implementations of the different possible commands see rtimcmd.c.

Parameters
ctxPointer to the crinitTaskDB_t the command shall be executed on.
resResult/response output.
cmdThe command to execute.
Returns
0 on success, -1 otherwise

◆ crinitExecRtimCmdAddSeries()

static int crinitExecRtimCmdAddSeries ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "addseries" command on an crinitTaskDB.

For documentation on the command itself, see crinitClientSeriesAdd().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdAddTask()

static int crinitExecRtimCmdAddTask ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "addtask" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientTaskAdd().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdDisable()

static int crinitExecRtimCmdDisable ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "disable" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientTaskDisable().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdEnable()

static int crinitExecRtimCmdEnable ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "enable" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientTaskEnable().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdGetVer()

static int crinitExecRtimCmdGetVer ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the version query from the client library to crinit.

For documentation on the command itself, see crinitClientGetVersion().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdKill()

static int crinitExecRtimCmdKill ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "kill" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientTaskKill().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdNotify()

static int crinitExecRtimCmdNotify ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "notify" command on an crinitTaskDB_t.

For documentation on the command itself, see sd_notify() and sd_notifyf().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdRestart()

static int crinitExecRtimCmdRestart ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "restart" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientTaskRestart().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdShutdown()

static int crinitExecRtimCmdShutdown ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "shutdown" command.

For documentation on the command itself, see crinitClientShutdown().

Parameters
ctxThe currently running crinitTaskDB, needed to inhibit spawning of new processes during shutdown.
resReturn pointer for response/result.
cmdThe crinitRtimCmd to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdStatus()

static int crinitExecRtimCmdStatus ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "status" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientTaskGetStatus().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdStop()

static int crinitExecRtimCmdStop ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "stop" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientTaskStop().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitExecRtimCmdTaskList()

static int crinitExecRtimCmdTaskList ( crinitTaskDB_t ctx,
crinitRtimCmd_t res,
const crinitRtimCmd_t cmd 
)
static

Internal implementation of the "list" command on an crinitTaskDB_t.

For documentation on the command itself, see crinitClientGetTaskList().

Parameters
ctxThe crinitTaskDB_t to operate on.
resReturn pointer for response/result.
cmdThe crinitRtimCmd_t to execute, used to pass the argument list.
Returns
0 on success, -1 on error

◆ crinitFreeUnMountList()

static void crinitFreeUnMountList ( crinitUnMountList_t um)
inlinestatic

Frees memory allocated for an crinitUnMountList_t by crinitGenUnMountList()

Parameters
umThe crinitUnMountList_t to free.

◆ crinitFsPrepareShutdown()

static int crinitFsPrepareShutdown ( void  )
inlinestatic

Prepares mounted filesystems for shutdown.

Performs MNT_DETACH if the file systems are not the root mount. The root mount is remounted read-only if it was writable before.

Returns
0 on success, -1 on error

◆ crinitGenUnMountList()

static int crinitGenUnMountList ( crinitUnMountList_t **  um,
bool *  rootfsIsRo 
)
inlinestatic

Generates a list of mount points that should be unmounted before shutdown/reboot.

The resulting list will include all entries from /proc/mounts whose source is not none and which are not the root node. The list will be ordered newest mount first. Memory for the list elements will be allocated and needs to be freed using crinitFreeUnMountList() when no longer in use.

Additionally, crinitGenUnMountList() checks if the root mount entry is read-only, setting rootfsIsRo to true in that case.

Parameters
umReturn pointer for the UnMountList.
rootfsIsRoReturn pointer indicating if we are on a read-only rootfs.
Returns
0 on success, -1 on error

◆ crinitGracePeriod()

static int crinitGracePeriod ( unsigned long long  micros)
inlinestatic

Wait function using nanosleep for the shutdown grace period.

Parameters
microsNumber of microseconds to wait.
Returns
0 on success, -1 on error

◆ crinitParseRtimCmd()

int crinitParseRtimCmd ( crinitRtimCmd_t out,
const char *  cmdStr 
)

Parses a string into an crinitRtimCmd_t.

The string must be of the form <OPCODE_STRING>\nARG1\n...\nARGn. The mapping of an opcode to a string representation is done in rtimopmap.h. crinitRtimCmdToMsgStr() can be used to obtain such a string from an crinitRtimCmd_t.

Will allocate memory for the argument array inside the output command which should be freed using crinitDestroyRtimCmd().

Parameters
outThe crinitRtimCmd_t to create.
cmdStrThe string to parse.
Returns
0 on success, -1 otherwise

◆ crinitRtimCmdToMsgStr()

int crinitRtimCmdToMsgStr ( char **  out,
size_t *  outLen,
const crinitRtimCmd_t cmd 
)

Generates a string representation of an crinitRtimCmd_t.

The generated string will be in a format parse-able by crinitParseRtimCmd(). Memory for the string will be allocated using malloc() and should be freed using free() once no longer used.

Parameters
outPointer to the output string.
outLenSize of the output string including the terminating zero.
cmdThe crinitRtimCmd_t to generate the string from.
Returns
0 on success, -1 otherwise

◆ crinitShdnThread()

static void * crinitShdnThread ( void *  args)
static

Shutdown thread function.

Run as a pthread, so that crinitExecRtimCmdShutdown() may return before the actual shutdown syscall is issued and the requesting process is not blocked.

Parameters
argsArgument pointer, see ShdnThrArgs.