init 中文man页面

系统
Init 是所有进程的父进程。它的首要任务是从一个存储在文件 /etc/inittab (参阅 inittab(5))里面的脚本里创建进程。这个文件通常有一些令 init 在每条用户可登录的线上派生 getty 的记录.它同时也控制着各独特系统所要求的独立进程。

NAME

init, telinit - 进程处理初始化  

总览

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

描述

Init

Init 是所有进程的父进程。它的首要任务是从一个存储在文件 /etc/inittab (参阅 inittab(5))里面的脚本里创建进程。这个文件通常有一些令 init 在每条用户可登录的线上派生 getty 的记录.它同时也控制着各独特系统所要求的独立进程。  

运行级别

所谓 运行级别 是一种系统软件环境配置,在此特定的环境中只允许某一组选定的进程存在。 init 给不同的运行级别派生的进程在 /etc/inittab 文件中定义。 Init 可以启动到8个不同的运行级别上: 0-6Ss。运行级别可以由超级用户通过 telinit 命令来转换,此命令可以将转换信号传递给 init,告诉它切换到哪个运行级别。

运行级别 01,和 6 为系统保留的专用运行级别。运行级别 0 用来关机,运行级别 6 用来重启计算机,运行级别 1 用来使计算机进入单用户模式。运行级别 S 不是给我们直接使用的,更多是为进入运行级别 1 时运行某些可执行脚本时被调用。如果想得到更多相关的信息,请参阅手册页 shutdown(8) 和 inittab(5)。

运行级别 7-9 虽然在文档中说明,但也可以使用。不使用它们的原因只是因为"传统" Unix 变种不使用这些运行级别。另外,运行级别 S 和运行级别 s 实际上是相同的,它们只是系统内同一运行级别的两个不同的别名而己。  

系统引导

当系统内核引导到最后一步时, init 进程被启动,它会自动查找 /etc/inittab 文件,看看是否有类型为 initdefault (参阅 inittab(5))的记录. initdefault 记录决定系统初始运行级别。如果没有这条记录(或者根本就没有 /etc/inittab ),那么,你必须在系统控制台输入想要进入的运行级别。

运行级别 Ss 把系统带入单用户模式,此模式不需要 /etc/initttab 文件。单用户模式中, /sbin/sulogin 会在 /dev/console 这个设备上打开。

当进入单用户模式后, init 会从文件 /etc/ioctl.save 中读取控制台的 ioctl(2) 状态。如果该文件不存在, init 则把线路设定为 9600 baud 并且带有 CLOCAL。当 init 退出单用户模式时,它会自当前的控制台的 ioctl 设置存储到这个文件供下次使用。

当第一次进入多用户模式时,init 会执行bootbootwait 记录以便在用户可以登录之前挂载文件系统。然后再执行相应运指定的各进程。

当启动一个新的进程时, init 会检查是否存在 /etc/initscript 文件。如果存在该文件,则使用该脚本来启动该进程。

如果系统中存在文件 /var/run/utmp/var/log/wtmp,那么当每个子进程终止时,init 会将终止信息和原因记录进这两个文件中。  

改变运行级别

init 启动了所有指定的子进程后,它会不断地侦测系统进程情况,如:它的某个子进程被终止、电源失效、或由 telinit 发出的改变运行级别的信号。当它接受到以上的这些信号之一时,它会自动重新扫描 /etc/inittab 文件,并执行相应操作。所以,新的记录可以随时加入到此文件中。并且, init 仍然等待系统发出了上述信号。在更新了各种系统文件后,如果你希望得到即时的更新,你可以使用telinit Qq 命令来唤醒 init 让它即刻重新检测/etc/inittab 文件。

如果 init 不是在单用户模式并且接收到了一个电源失效信号(SIGPWR),它会读取文件 /etc/powerstatus,并执行该文件中指定的各种操作:

F(AIL)
电源失效,由 UPS 提供电力。执行 powerwaitpowerfail 记录。
O(K)
电源恢复,执行 powerokwait 记录。
L(OW)
电源失效并且 UPS 电压也太低。执行 powerfailnow 记录。

如果文件 /etc/powrestatus 不存在或其中的内容并不包含有以上所示的字母 FOL ,则 init 会当做读到了字母 F

我们不赞成使用 SIGPWR/etc/powerstatus 。有些用户希望与 init 进行交互,那么可以使用 /dev/initctl 控制通道。关于此点的描述请参阅 sysvinit 包的源代码。

init 得到更新运行级别的请求, init会向所有没有在新运行级别中定义的进程发送一个警告信号 SIGTERM 。在等待 5 秒钟之后,它会发出强制中断所有进程的运行的信号 SIGKILL 。注意, init 假设所有的这些进程(包括它们的后代)都仍然在 init 最初创建它们的同一进程组里。如果有任何进程改变了它们的进程组,那么它就收不到这些信号。这样的进程,你需要分别进行手工的终止。  

TELINIT

/sbin/telinit 是一个到 /sbin/init 的软链接。它用一个单字符参数来通知 init 执行相应的操作。下面是相关的参数:

0,1,2,3,4,56
通知 init 将运行级别切换到指定的运行级别
a,b,c
告诉 init 只运行那些 /etc/inittab 文件中运行级别是 abc 的记录
Qq
通知 init 重新检测 /etc/inittab 文件
Ss
通知 init 将运行级别切换到单用户模式下
Uu
通知 init 自动重启(保留状态),此操作不会对文件/etc/inittab 进行重新检测。执行此操作时,运行级别必须处在 Ss12345 之一,否则,该请求将被忽略

telinit 还可以告诉 init 两次发送 SIGTERM 和 SIGKILL 信号的时间间隔。默认值是 5 秒,你可以通过使用 -t sec 的选项来修改。

telinit 只能由具有恰当权限的用户执行。

init 通过检查自己的 进程号 来判断自己是 init 还是 telinit ;真的 init 的进程号永远都是 1。从这一点来看,我们在调用 telinit 时也可以只使用 init 来少敲几个键.  

环境变量

Init 为所有的子进程设定下列环境变量

PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
INIT_VERSION
如名字表示的那样.在决定一个脚本是否直接来自 init 时很有用
RUNLEVEL
当前系统的运行级别
PREVLEVEL
前次运行的运行级别(仅当改变运行级别时有用)
CONSOLE
系统控制台。此变量是由内核继承而来,如果没有此环境变量的定义, init 会使用 /dev/console 做为默认的控制台

启动标志

在启动系统时可以通过引导管理器(比如,LILO)传递一些启动标志给 initInit 接受下面几个:

-s, S, single
以单用户模式启动系统。按这种模式启动时, /etc/inittab 需要被检查,并在单用户模式 shell 启动之前会执行引导 rc 脚本。
1-5
定义需要启动的运行级别
-b, emergency
不运行任何启动脚本而直接进入单用户模式 shell

-a, auto 如果用缺省命令行从内核引导(没有用户干预), LILO 引导管理器向命令行增加了单词 如果是这样的话, init 把环境变量 请注意你不能将这个东西用于任何安全评测 - 用户当然可以在命令行上手工输入

-z xxx -z 参数被忽略.你可以用这个选项略微扩展命令行,这样它可以在堆栈里获得更多空间.然后 Init 就可以操作命令行,好让 ps(1) 显示当前运行级别.  

接口

init监听 /dev 里的一个 fifo/dev/initctl,从中获取信息。 Telinit 也使用这些和 init 进行通信。该界面没有完整的文档。如果对 init 有兴趣,则可以学习 src/ 目录中 init 源文件包中的 initreq.h 文件。  

信号

init 对以下信号产生响应

SIGHUP
当接收到该信号后,init会对 /etc/initrunlvl/var/log/inirunlvl 文件进行检查。如果这两个文件之一存在而且文件中有 ASCII 字符的运行级别, init 会转换到相应的新的运行级别。 此特性只用于向后兼容! .通常的情况是该文件并不存在,所以 init 执行类似于 telinit q 这样的操作。
SIGUSR1
当接收到这个信号量,init 会关闭并重新打开它的控制 fifo,/dev/initctl。此操作对 /dev 被重新挂载后的启动脚本有用。
SIGINT
通常,当用户按了 CTRL-ALT-DEL 键后,内核会向 init 传递此信号。它所执行的操作与 ctrlaltdel 相同。
SIGWINCH
当键盘有 KeyboardSignal 按键按下时,内核向 init 传递此信号,它激活 kbrequest 动作.

遵循

Init 与 System V 的 init 相兼容。它与/etc/init.d/etc/rc{runlevel}.d 目录下的脚本紧密地工作在一起。如果你的系统使用这种惯例,在 /etc/init.d 目录下应该有一个 README 文件,它可以很好地解释了这些脚本是如何工作的。  

相关文件

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

警告

Init 假设进程和进程的后代同属于最初创建它们的进程组.如果进程改变了它们的进程组, init 就无法中止它们,因此,你可能会有两个进程读取一条终端线.  

诊断

如果 init 发现它的重启次数在最近 2 分钟里超过了 10 次,它就会认为程序命令串出错了。在系统控制台输出出错信息,并拒绝重新启动,只有等到 5 分钟以后或用户给 init 一个特定的信号,它才会重新响应。这可以防止由于用户在编辑 /etc/inittab 文件时可能出现的输入错误或由于相关程序被无意删除后导致的大量占用系统资源。  

作者

Miquel van Smoorenburg (miquels@cistron.nl) 原始帮助手册页作者:Michael Haardt (u31b3hs@pool.informatik.rwthaachen.de).  

另见

getty(1), login(1), sh(1), who(1), shutdown(8), kill(1), inittab(5), initscript(5), utmp(5)

#p#

NAME

init, telinit - process control initialization  

SYNOPSIS

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

DESCRIPTION

Init

Init is the parent of all processes. Its primary role is to create processes from a script stored in the file /etc/inittab (see inittab(5)). 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.

RUNLEVELS

A runlevel is a software configuration of the system which 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-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 0, 1, 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. For more information on this, see the manpages for shutdown(8) and inittab(5).

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 (see inittab(5)). 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, 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(AIL)
Power is failing, UPS is providing the power. Execute the powerwait and powerfail entries.
O(K)
The power has been restored, execute the powerokwait entries.
L(OW)
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 F, O or L, init 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 - see the source code of the sysvinit package for more documentation about this.

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.  

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:

0,1,2,3,4,5 or 6
tell init to switch to the specified run level.
a,b,c
tell init to process only those /etc/inittab file entries having runlevel a,b 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 Ss12345, 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.  

ENVIRONMENT

Init sets the following environment variables for all its children:

PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
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 (useful after a runlevel switch).
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.

BOOTFLAGS

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

-s, S, single
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.
-b, emergency
Boot directly into a single user shell without running any other startup scripts.
-a, auto
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(1) shows the current runlevel.

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 or finished. Those interested should study the initreq.h file in the src/ subdirectory of the init source code tar archive.  

SIGNALS

Init reacts to several signals:

SIGHUP
Has the same effect as telinit q.
SIGUSR1
On receipt of this signals, 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.

CONFORMING TO

Init is compatible with the System V init. It works closely together with the scripts in the directories /etc/init.d and /etc/rc{runlevel}.d. If your system uses this convention, there should be a README file in the directory /etc/init.d explaining how these scripts work.  

FILES

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

WARNINGS

Init assumes that processes and descendants of processes remain in the same process group which was originally created for them. If the processes change their group, init can't kill them and you may end up with two processes reading from one terminal line.  

DIAGNOSTICS

If init finds that it is continuously respawning an entry more than 10 times in 2 minutes, it will assume that there is an error in the command string, generate an error message on the system console, and refuse to respawn this entry until either 5 minutes has elapsed or it receives a signal. This prevents it from eating up system resources when someone makes a typographical error in the /etc/inittab file or the program for the entry is removed.  

AUTHOR

Miquel van Smoorenburg (miquels@cistron.nl), initial manual page by Michael Haardt (u31b3hs@pool.informatik.rwth-aachen.de).  

SEE ALSO

getty(1), login(1), sh(1), runlevel(8), shutdown(8), kill(1), inittab(5), initscript(5), utmp(5)

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-25 14:14:01

init_module中文man

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:09:38

clearerr中文man

2011-08-25 15:19:39

dirname中文man

2011-08-25 15:21:53

execl中文man

2011-08-25 16:28:50

fread中文man

2011-08-25 16:52:54

getchar中文man

2011-08-25 17:27:58

rewind中文man

2011-08-25 18:41:31

vprintf中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-23 18:00:21

LDP中文man

2011-08-25 09:40:49

UPDATE中文man

2011-08-24 15:11:15

explain中文man

2011-08-24 15:29:06

grant中文man
点赞
收藏

51CTO技术栈公众号