INITCTL(5)
π INITCTL(5) β the /run/initctl named pipe that passes commands to SysV init π
πΈ NAME πΈ
okay so the name is initctl β and the whole vibe is that /run/initctl is a named pipe which passes commands to SysV init. π
β¨ SYNOPSIS β¨
/run/initctl
π DESCRIPTION π
Bestie, this page is all about the communication pipe that SysV init sets up at /run/initctl. This named pipe lets programs with the proper permissions β typically programs run by root have read+write access to the pipe β send signals to the init program, aka PID 1. β¨
ngl, the init manual page has, up until recently, simply stated that people wishing to understand how to send messages to init should read the init program's source code. But that is not usually practical. π
Messages sent to the pipe to talk to init must have a special format. This format is defined as a C structure, and the technical break-down is presented here:
/*
* Because of legacy interfaces, "runlevel" and "sleeptime"
* aren't in a separate struct in the union.
*
* The weird sizes are because init expects the whole
* struct to be 384 bytes.
*/
struct init_request {
int magic; /* Magic number
*/
int cmd; /* What kind of request
*/
int runlevel; /* Runlevel to change to
*/
int sleeptime; /* Time between TERM and KILL
*/
union {
struct init_request_bsd bsd;
char data[368];
} i; };
Okay bestie, letβs go through the init_request structure one line at a time. The first variable, the magic number, must be of the value 0x03091969. The init program then knows that only programs with root access which send this magic number are authorized to communicate with init. π
The cmd variable is a value in the range of 0-8 (currently). This cmd variable tells init what we want it to do. Here are the possible options:
1β Set the current runlevel, specified by therunlevelvariable.2β The power will fail soon (probably low battery) prepare to shutdown.3β The power is failing, do shutdown immediately.4β The power is okay, cancel shutdown.6β Set an environment variable to a value to be specified in thedatavariable of this structure.
Other cmd options may be added to init later. For example, command values 0, 5 and 7 are defined but currently not implemented. π
The runlevel variable will specify the runlevel to switch to (0-6).
The sleeptime variable is to be used when we want to tell init to change the time spent waiting between sending SIGTERM and SIGKILL during the shutdown process. Changing this at run time is not yet implemented.
The data variable (in the union) can be used to pass misc data which init might need to process our request. For example, when setting environment variables.
When setting an environment variable through init's /run/initctl pipe, the data variable should have the format VARIABLE=VALUE. The string should be terminated with a NULL character.
π§ EXAMPLES π§
The following C code example shows how to send a set environment variable request to the init process using the /run/initctl pipe. This example is simplified and skips the error checking. A more complete example can be found in the shutdown.c program's init_setnv() function.
struct init_request request; /* structure defined above */
int fd; /* file descriptor for pipe */
memset(&request, 0, sizeof(request)); /* initialize structure */
request.magic = 0x03091969; /* magic number required */
request.cmd = 6; /* 6 is to set a variable */
sprintf(request.data, "VARIABLE=VALUE"); /* set VAR to VALUE in init */
if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0) /* open pipe for writing */
{
size_t s = sizeof(request); /* size of structure to write */
void *ptr = &request; /* temporary pointer */
write(fd, ptr, s); /* send structure to the pipe */
close(fd); /* close the pipe when done */
}
π NOTES π
Usually the /run/initctl pipe would only be used by low-level programs to request a power-related shutdown or change the runlevel, like telinit would do. Most of the time there is no need to talk to init directly, but this gives us an extendable approach so init can be taught how to learn more commands. π«
The commands passed through the /run/initctl pipe must be sent in a specific binary format and be of a specific length. Larger data structures or ones not using the proper format will be ignored. Typically, only root has the ability to write to the initctl pipe for security reasons.
The /run/initctl pipe can be closed by sending init (PID 1) the SIGUSR2 signal. This closes the pipe and leaves it closed. This may be useful for making sure init is not keeping any files open. However, when the pipe is closed, init no longer receives signals, such as those sent by shutdown(8) or telinit(8). In other words if we close the pipe, init cannot change its runlevel directly. The pipe may be re-opened by sending init (PID 1) the SIGUSR1 signal.
β οΈ If the/run/initctlpipe is closed then it may still be possible to bring down the system using theshutdown(8)command's-nflag, but this is not always clean and not recommended. π
π FILES π
/run/initctl/sbin/init
π AUTHOR π
Jesse Smith π
π¦ SEE ALSO π¦
π That's the whole page, bestie β source/version sysvinit, date April 13, 2018, manual page INITCTL(5). Slay! π
π¦ where this page comes from
- nixos 26.05: sysvinit 3.04
- nixpkgs unstable: sysvinit 3.04
- Copyright (C) 2018 Jesse Smith
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms