telinit (提供进程控制初始化)

rose1 发表于 2020-07-20 16:48
浏览次数:
在手机上阅读

在类似Unix的操作系统上,telinit和init提供进程控制初始化。系统引导时将使用它们。 本文档介绍telinit和init的Linux版本。

查看英文版

目录

1 telinit 运行系统环境

2 telinit 说明

3 telinit 语法

telinit 运行系统环境

Linux

telinit 说明

init是所有进程的父级。它的主要作用是从存储在文件/ etc / inittab中的脚本创建进程。该文件通常包含一些条目,这些条目会导致init在用户可以登录的每一行上生成getty。它还控制任何特定系统所需的自治过程。
init is the parent of all processes. Its primary role is to create processes from a script stored in the file /etc/inittab. This file usually has entries which cause init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system.

查看英文版

查看中文版

telinit 语法

/sbin/init [ -a ] [ -s ] [ -b ] [ -z xxx ] [ 0123456Ss ]
/sbin/telinit [ -t sec ] [ 0123456sSQqabcUu ]

运行级别

甲运行级别是只允许的处理的选择的组中存在的系统的软件配置。由init生成的每个运行级别的进程都在/ etc / inittab文件中定义。Init可以是八个运行级别之一:06以及Ss。通过特权用户运行telinit来更改运行级别,该运行会将适当的信号发送到init,告诉它要更改为哪个运行级别。

运行级别01,和6被保留。运行级别0用于停止系统,运行级别6用于重新引导系统,运行级别1用于使系统进入单用户模式。运行级别S并不是要直接使用,而是用于输入运行级别1时执行的脚本。

运行级别7 - 9也是有效的,虽然不是真正的记录。这是因为“传统” Unix变体不使用它们。如果您感到好奇,运行级别Ss实际上是相同的。在内部,它们是同一运行级别的别名。

正在启动

在将init作为内核 引导序列的最后一步调用之后,它将查找文件/ etc / inittab以查看是否存在initdefault类型的条目。initdefault条目确定系统的初始运行级别。如果没有这样的条目(或根本没有/ etc / inittab),则必须在系统控制台上输入运行级别。

运行级别Ss使系统进入单用户模式,并且不需要/ etc / inittab文件。在单用户模式下,将在/ dev / console上打开根shell 。

进入单用户模式时,init会将控制台的stty设置初始化为合理的值。设置为“ CLocal”模式。硬件速度和握手不变。

当进入一个多用户首次模式,初始化执行的引导和bootwait条目,以允许安装的文件系统之前,用户可以登录,然后匹配的运行级别的所有条目都被处理。

启动新进程时,init首先检查文件/ etc / initscript是否存在。如果是这样,它将使用此脚本启动该过程。

每次孩子终止,初始化记录的事实,它在死亡原因的/ var / run / utmp文件和在/ var /日志/ wtmp文件,提供这些文件是否存在。

更改运行级别

生成所有指定的进程后,init会等待其后代进程之一死亡,以等待“ powerfail”信号,或者直到telinit发出信号更改系统的运行级别。当出现上述三种情况之一时,它将重新检查/ etc / inittab文件。新条目可以随时添加到此文件中。但是,init仍在等待以上三个条件之一发生。为了提供瞬时响应,telinit Q(或q)命令可以唤醒init来重新检查/ etc / inittab文件。

如果init不在单用户模式下并且接收到powerfail信号(SIGPWR),它将读取文件/ etc / powerstatus。然后,它将基于此文件的内容启动命令:

  • F (失败) - 电源出现故障,UPS正在供电。执行powerwaitpowerfail条目。
  • O (确定) - 电源已恢复,执行powerokwait条目。
  • L (LOW) - 电源出现故障,UPS电池电量不足。执行powerfailnow条目。

如果/ etc / powerstatus不存在或包含其他任何内容,则字母F,O或L,init的行为就像读取了字母F一样。

不建议使用SIGPWR和/ etc / powerstatus。希望与init进行交互的人应该使用/ dev / initctl控制通道。有关更多信息,请查看sysvinit软件包的源代码。

当请求init更改运行级别时,它将向新运行级别中未定义的所有进程发送警告信号SIGTERM。然后等待5秒钟,然后通过SIGKILL信号强制终止这些过程。请注意,init假定所有这些进程(及其后代)都与init最初为其创建的进程组相同。如果任何进程更改了其进程组的隶属关系,它将不会收到这些信号。此类过程需要单独终止。

环境

Init为其所有子代设置以下环境变量:

PATH /bin:/usr/bin:/sbin:/usr/sbin
INIT_VERSION 顾名思义。有助于确定脚本是否直接从init运行。
RUNLEVEL 当前系统运行级别。
PREVLEVEL 上一个运行级别(在更改运行级别后特别有用)。
CONSOLE 系统控制台。这实际上是从内核继承的;但是,如果未设置init,则默认情况下会将其设置为/ dev / console。

Telinit

/sbin/telinit is linked to /sbin/init. 它采用一个字符的参数,并向init发出信号以执行适当的操作。以下参数用作telinit的指令:


0,1,2,3,4,5或6 告诉init切换到指定的运行级别。
abc 告诉init仅处理运行级别为abc的/ etc / inittab文件条目。
Q or q 告诉init重新检查/ etc / inittab文件。
S or s 告诉init切换到单用户模式。
U or u 告诉init重新执行自身(保留状态)。不会重新检查/ etc / inittab文件。运行级别应的一个小号,小号,1,2,3,4,或5,否则请求将被忽略。

telinit还可以告诉init发送信号SIGTERMSIGKILL信号之间应等待多长时间。缺省值为5秒,但是可以使用-t sec选项更改。

telinit只能由具有适当特权的用户调用。

的初始化如果二进制检查初始化或使用telinit通过查看它的进程ID; 实际的init的进程ID始终为1。由此可见,除了调用telinit之外,还可以只使用init作为快捷方式。

接口

Init在/ dev(/ dev / initctl)中的fifo上侦听消息。Telinit使用它与init进行通信。该接口没有很好的文档说明;要了解有关界面的更多信息,用户必须查看源本身。

讯号

初始化对多个信号做出反应,包括:

SIGHUP telinit q具有相同的效果。
SIGUSR1 收到此信号后,init将关闭并重新打开其控制fifo / dev / initctl。当重新挂载/ dev时,对引导脚本很有用。
SIGINT 通常,当按下Ctrl-Alt-Del时,内核会将此信号发送给init。它激活ctrlaltdel操作。
SIGWINCH 当敲击KeyboardSignal键时,内核发送此信号。它激活kbrequest动作。

档案

/etc/inittab
/etc/initscript
/dev/console
/var/run/utmp
/var/log/wtmp
/dev/initctl

初始化启动标志

可以从引导监视器(例如LILO)传递许多标志到init 。初始化接受以下标志:

-sSsingle 单用户模式启动。在这种模式下,将检查/ etc / inittab并通常在启动单用户模式外壳程序之前运行启动rc脚本。
1-5 要启动的运行级别。
-bemergency 直接启动到单个用户外壳程序中,而无需运行任何其他启动脚本。
-aauto 如果LILO引导加载程序使用默认命令行(无需用户干预)引导内核,则会在命令行中添加单词“ auto ”。如果找到此文件,则init 会将环境变量“ AUTOBOOT ” 设置为“ yes ”。请注意,您不能将其用于任何安全措施。当然,用户可以在命令行上手动指定“ auto ”或-a
-z xxx -z的参数将被忽略。您可以使用它来扩展命令行,以便在堆栈上占用更多空间。然后,Init可以操纵命令行,以便ps显示当前运行级别。

telinit还可以告诉init发送信号SIGTERMSIGKILL信号之间应等待多长时间。缺省值为5秒,但是可以使用-t sec选项更改。

telinit只能由具有适当特权的用户调用。

的初始化如果二进制检查初始化或使用telinit通过查看它的进程ID; 实际的init的进程ID始终为1。由此可见,除了调用telinit之外,还可以只使用init作为快捷方式。

/sbin/init [ -a ] [ -s ] [ -b ] [ -z xxx ] [ 0123456Ss ]
/sbin/telinit [ -t sec ] [ 0123456sSQqabcUu ]

Runlevels

A runlevel is a software configuration of the system that allows only a selected group of processes to exist. The processes spawned by init for each of these runlevels are defined in the /etc/inittab file. Init can be in one of eight runlevels: 0 through 6, and S or s. The runlevel is changed by having a privileged user run telinit, which sends appropriate signals to init, telling it which runlevel to change to.

Runlevels 01, and 6 are reserved. Runlevel 0 is used to halt the system, runlevel 6 is used to reboot the system, and runlevel 1 is used to get the system down into single user mode. Runlevel S is not really meant to be used directly, but more for the scripts that are executed when entering runlevel 1.

Runlevels 7 - 9 are also valid, though not really documented. This is because "traditional" Unix variants don't use them. In case you're curious, runlevels S and s are in fact the same. Internally they are aliases for the same runlevel.

Booting

After init is invoked as the last step of the kernel boot sequence, it looks for the file /etc/inittab to see if there is an entry of the type initdefault. The initdefault entry determines the initial runlevel of the system. If there is no such entry (or no /etc/inittab at all), a runlevel must be entered at the system console.

Runlevel S or s bring the system to single user mode and do not require an /etc/inittab file. In single user mode, a root shell is opened on /dev/console.

When entering single user mode, init initializes the consoles stty settings to sane values. "CLocal" mode is set. Hardware speed and handshaking are not changed.

When entering a multi-user mode for the first time, init performs the boot and bootwait entries to allow file systems to be mounted before users can log in. Then all entries matching the runlevel are processed.

When starting a new process, init first checks whether the file /etc/initscript exists. If it does, it uses this script to start the process.

Each time a child terminates, init records the fact and the reason it died in /var/run/utmp and /var/log/wtmp, provided that these files exist.

Changing Runlevels

After it has spawned all of the processes specified, init waits for one of its descendant processes to die, for a "powerfail" signal, or until it is signaled by telinit to change the system's runlevel. When one of the above three conditions occurs, it re-examines the /etc/inittab file. New entries can be added to this file at any time. However, init still waits for one of the above three conditions to occur. To provide for an instantaneous response, the telinit Q (or q) command can wake up init to re-examine the /etc/inittab file.

If init is not in single user mode and receives a powerfail signal (SIGPWR), it reads the file /etc/powerstatus. It then starts a command based on the contents of this file:

  • F (FAIL) - Power is failing, UPS is providing the power. Execute the powerwait and powerfail entries.
  • O (OK) - The power has been restored, execute the powerokwait entries.
  • L (LOW) - The power is failing and the UPS has a low battery. Execute the powerfailnow entries.

If /etc/powerstatus doesn't exist or contains anything else then the letters FO or Linit will behave as if it has read the letter F.

Usage of SIGPWR and /etc/powerstatus is discouraged. Someone wanting to interact with init should use the /dev/initctl control channel. More information about this is available by viewing the source code of the sysvinit package.

When init is requested to change the runlevel, it sends the warning signal SIGTERM to all processes that are undefined in the new runlevel. It then waits 5 seconds before forcibly terminating these processes via the SIGKILL signal. Note that init assumes that all these processes (and their descendants) remain in the same process group which init originally created for them. If any process changes its process group affiliation it will not receive these signals. Such processes need to be terminated separately.

Environment

Init sets the following environment variables for all its children:

PATH /bin:/usr/bin:/sbin:/usr/sbin
INIT_VERSION As the name says. Useful to determine if a script runs directly from init.
RUNLEVEL The current system runlevel.
PREVLEVEL The previous runlevel (especially useful after changing runlevel).
CONSOLE The system console. This is really inherited from the kernel; however if it is not set init will set it to /dev/console by default.

Telinit

/sbin/telinit is linked to /sbin/init. It takes a one-character argument and signals init to perform the appropriate action. The following arguments serve as directives to telinit:

012345 or 6 tell init to switch to the specified run level.
abc tell init to process only those /etc/inittab file entries having runlevel ab or c.
Q or q tell init to re-examine the /etc/inittab file.
S or s tell init to switch to single user mode.
U or u tell init to re-execute itself (preserving the state). No re-examining of /etc/inittab file happens. Run level should be one of Ss1234, or 5, otherwise request would be silently ignored.

telinit can also tell init how long it should wait between sending processes the SIGTERM and SIGKILL signals. The default is 5 seconds, but this can be changed with the -t sec option.

telinit can be invoked only by users with appropriate privileges.

The init binary checks if it is init or telinit by looking at its process id; the real init's process id is always 1. From this it follows that instead of calling telinit one can also just use init instead as a shortcut.

Interface

Init listens on a fifo in /dev (/dev/initctl) for messages. Telinit uses this to communicate with init. The interface is not very well documented; to learn more about the interface, users must view the source itself.

Signals

Init reacts to several signals, including:

SIGHUP Has the same effect as telinit q.
SIGUSR1 On receipt of this signal, init closes and re-opens its control fifo, /dev/initctl. Useful for bootscripts when /dev is remounted.
SIGINT Normally the kernel sends this signal to init when Ctrl-Alt-Del is pressed. It activates the ctrlaltdel action.
SIGWINCH The kernel sends this signal when the KeyboardSignal key is hit. It activates the kbrequest action.

Files

/etc/inittab
/etc/initscript
/dev/console
/var/run/utmp
/var/log/wtmp
/dev/initctl

Init Bootflags

It is possible to pass a number of flags to init from the boot monitor (such as LILO). Init accepts the following flags:

-sSsingle Single user mode boot. In this mode /etc/inittab is examined and the bootup rc scripts are usually run before the single user mode shell is started.
1-5 Runlevel to boot into.
-bemergency Boot directly into a single user shell without running any other startup scripts.
-aauto The LILO boot loader adds the word "auto" to the command line if it booted the kernel with the default command line (without user intervention). If this is found init sets the "AUTOBOOT" environment variable to "yes". Note that you cannot use this for any security measures; of course the user could specify "auto" or -a on the command line manually.
-z xxx The argument to -z is ignored. You can use this to expand the command line a bit, so that it takes some more space on the stack. Init can then manipulate the command line so that ps shows the current runlevel.

telinit can also tell init how long it should wait between sending processes the SIGTERM and SIGKILL signals. The default is 5 seconds, but this can be changed with the -t sec option.

telinit can be invoked only by users with appropriate privileges.

The init binary checks if it is init or telinit by looking at its process id; the real init's process id is always 1. From this it follows that instead of calling telinit one can also just use init instead as a shortcut.

查看英文版

查看中文版

其他命令行

tabs | tac | talk | tail | tcopy | tty | tar | tbl | tcpdump | tcsh | time | tee | timex | telnet | test | top | touch | tput | tr | troff | traceroute |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条