Don't need to clear utmp in rcboot.sh; dinit now does it.
[oweals/dinit.git] / README.md
1 # Dinit
2 v0.5.0 (pre-release)
3
4 This is the README for Dinit, the service manager and init system. It is
5 intended to provide an overview; For full documentation please check the manual pages. 
6
7 ## Contents
8
9 1. [Introduction](#introduction)
10 2. [Configuring services](#configuring-services)
11     1. [Service types](#service-types)
12     2. [Service description files](#service-description-files)
13 3. [Controlling services](#controlling-services)
14     1. [Service hierarchy and states](#service-hierarchy-and-states)
15     2. [Using dinitctl](#using-dinitctl)
16
17 ## Introduction
18
19 "Dinit" is a service supervisor with dependency support which can also
20 act as the system "init" program. It was created with the intention of
21 providing a portable init system that could serve as a lighter-weight
22 alternative to the Linux-only Systemd.
23
24 Specifically, Dinit can launch multiple services in parallel, with dependency
25 management (i.e. if one service's operation depends on another, the latter
26 service will be started first). It  can monitor the process corresponding to a
27 service, and re-start it if it dies, and it can do this in an intelligent way,
28 first "rolling back" all dependent services, and restarting them when their
29 dependencies are satisfied. However, the precise nature of dependency
30 relations between services is highly configurable.
31
32 Dinit includes "dinitctl", a tool to issue commands to the main Dinit
33 process in order to start or stop services and check their state, as well as
34 a "shutdown" program (with scripts "halt" and "reboot") to manage shutting
35 down and restarting the system.
36
37 Dinit is designed to work on POSIXy operating systems such as Linux and
38 OpenBSD. It is written in C++ and uses the [Dasynq](http://davmac.org/projects/dasynq/)
39 event handling library, which was written especially to support Dinit. (Note
40 that a copy of Dasynq is bundled with Dinit, so a separate copy is not
41 required for compilation; however, the bundled copy does not include the
42 documentation or test suite).
43
44 Development goals include clean design, robustness, portability, and
45 avoiding feature bloat (whilst still handling a variety of use cases).
46
47 See [doc/COMPARISON](doc/COMPARISON) for a comparison of Dinit with similar
48 software packages.
49
50 Dinit is licensed under the Apache License, version 2.0. A copy of this
51 license can be found in the LICENSE file.
52
53 Dinit was written by Davin McCall <davmac@davmac.org>.
54
55 See BUILD.txt for information on how to build Dinit.
56
57
58 ## Configuring services
59
60 ### Service types
61
62 A "service" is nominally a persistent process or system state. The two main
63 types of service are a _process_ service (represented by a an actual process)
64 and a _scripted_ service (which is started and stopped by running a process -
65 often a shell script - to completion). There are also _bgprocess_ services
66 and _internal_ services.
67
68 Many programs that you might want to run under Dinit's supervision can run
69 either "in the foreground" or as a daemon ("in the background"), and the
70 choice is dictated by a command line switch (for instance the -D and -F
71 switches to Samba's "smbd"). Although it might seem counterintuitive,
72 the "foreground" mode should be used for programs registered as process
73 services in Dinit; this allows Dinit to monitor the process.
74
75 Process services are attractive due to the ease of monitoring (and
76 restarting) the service, however, they have one inherent problem, which is
77 that Dinit cannot tell when the service is truly started. Once the process
78 has been launched, Dinit assumes that the service has started, but in fact
79 there will be a short delay before the process sets itself up, starts
80 listening on sockets, etc; during this time any other process (including
81 one from a service listed as dependent) which tries to contact it will not
82 be able to do so. In practice, this is not usually a problem (and external
83 solutions, like D-Bus, do exist).
84
85 A _scripted_ service has separate commands for startup and (optional)
86 shutdown. Scripted services can be used for tasks such as mounting file
87 systems that don't need a persistent process, and in some cases can be used
88 for daemon processes (although Dinit will not be able to supervise a
89 process that is registered as a scripted service).
90
91 A _bgprocess_ service is a mix between a process service and a scripted
92 service. A command is used to start the service, and once started, the
93 process ID is expected to be available in a file which Dinit can then
94 read. Many existing daemons can operate in this way. The process can only be
95 supervised if Dinit runs as the system "init" (PID 1), or can otherwise mark
96 itself as a subreaper (which is possible on Linux, FreeBSD and DragonFlyBSD) -
97 otherwise Dinit can not reliably know when the process has terminated.
98
99 (Note, use of bgprocess services type requires care. The file from which the
100 PID is read is trusted; Dinit may send signals to the specified PID. It
101 should not be possible for unauthorised users to modify the file contents!)
102
103 An _internal_ service is just a placeholder service that can be used to
104 describe a set of dependencies. An internal service has no corresponding
105 process.
106
107
108 ### Service description files
109
110 Dinit discovers services by reading _service description files_. These files
111 reside in a directory (/etc/dinit.d is the default "system" location, with
112 "/usr/local/lib/dinit.d" and "/lib/dinit.d" also searched) and their name
113 matches the name of the service. Service descriptions are loaded lazily, as
114 needed by Dinit.
115
116 (An example of a complete set of system service descriptions can be found in
117 the [doc/linux/services](doc/linux/services) directory).
118
119 A service description file consists of a number of parameter settings.
120 Settings in the SDF are denoted as a parameter name followed by either an
121 equal sign or colon and then the parameter value (all on the same line).
122 Comments begin with a hash mark (#) and extend to the end of the line (they
123 must be separated from setting values by at least one whitespace character).
124
125 Parameter values are interpreted literally, except that:
126  - whitespace is collapsed to a single space
127  - double quotes can be used around all or part(s) of a parameter to prevent
128    whitespace collapse and interpretation of special characters
129  - backslash can be used to 'escape' the next character, preventing any
130    special meaning from being associated with it. It can be used to include
131    non-collapsing whitespace, double-quote marks, and backslashes in the
132    parameter value.
133
134 Some examples of the available parameters are:
135
136     type = process | bgprocess | scripted | internal
137     command = ...
138     stop-command = ...
139     run-as = (user-id)
140     restart = (boolean)
141     smooth-recovery = (boolean)
142     logfile = ...
143     pid-file = ...
144     options = ...
145     depends-on = (service name)
146     depends-ms = (service name)
147     waits-for = (service name)
148     
149 Descriptions of individual parameters follows:
150
151     command = (external script or executable, and arguments)
152
153 For a 'process' service, this is the process to run.
154 For a 'scripted' service, this command is run to start the service.
155
156     stop-command = (external script or executable, and arguments)
157
158 For a 'scripted' service, this command is run to stop the service.
159
160     run-as = (user-id)
161  
162 Specifies which user to run the process(es) for this service as. The group
163 id for the process will also be set to the primary group of the specified
164 user.
165
166     restart = yes | true | no | false
167
168 Specifies whether the service should automatically restart if it becomes
169 stopped (for any reason, including being explicitly requested to stop).
170 Only active services will restart automatically.
171
172     smooth-recovery = yes | true | no | false
173    
174 For process services only. Specifies that, should the process die, it
175 can be restarted without bringing the service itself down. This means that
176 any dependent services do not need to be stopped/restarted. Such recovery
177 happens regardless of the "restart" setting (if smooth-recovery is enabled,
178 the service does not reach the stopped state when the process terminates
179 unexpectedly).
180
181     logfile = (log file path)
182
183 Specifies the log file for the service. Output from the service process
184 will go this file.
185
186     pid-file = (path to file)
187
188 For "bgprocess" type services only; specifies the path of the file where
189 daemon will write its process ID before detaching.
190
191     depends-on = (service name)
192
193 This service depends on the named service. Starting this service will
194 start the named service; the command to start this service will not be
195 executed until the named service has started. If the named service is
196 stopped then this service will also be stopped.
197
198     depends-ms = (service name)
199
200 Indicates a "milestone dependency" on the named service. This service
201 requires the named service to start before it starts itself. Once the
202 named service has started, it remains active due to the dependency, but if
203 it stops for any reason then the dependency link is broken until the next
204 time this service is started.
205
206     waits-for = (service name)
207
208 When this service is started, wait for the named service to finish
209 starting (or to fail starting) before commencing the start procedure
210 for this service. Starting this service will automatically start
211 the named service.
212
213     options = ( runs-on-console | nosigterm | starts-rwfs | starts-log ) ...
214
215 Specifies various options for this service:
216
217 `no-sigterm` : specifies that the TERM signal should not be send to the
218               process to terminate it. (Another signal can be specified using
219               the "termsignal" setting; if no other signal is specified, NO
220               signal will be sent).
221
222 `runs-on-console` : specifies that this service uses the console; its input
223               and output should be directed to the console. A service running
224               on the console prevents other services from running on the
225               console (they will queue for the console).
226               The "interrupt" key (normally control-C) will be active for
227               process / scripted services that run on the console. Handling
228               of an interrupt is determined by the service process, but
229               typically will cause it to terminate.
230               
231 `starts-on-console` : specifies that this service uses the console during
232               service startup. This is implied by runs-on-console, but can
233               be specified separately for services that need the console
234               while they start but not afterwards.
235               This setting is not applicable to regular "process" services,
236               but can be used for "scripted" and "bgprocess" services. It
237               allows for interrupting startup via the "interrupt" key
238               (normally control-C). This is useful to allow filesystem checks
239               to be interrupted/skipped.
240
241 `start-interruptible` : this service can have its startup interrupted
242               (cancelled) if it becomes inactive while still starting.
243               The SIGINT signal will be sent to the signal to cancel its
244               startup. This is meaningful only for scripted and bgprocess
245               services. 
246
247 Please see the manual page for a full list of service parameters and options.
248
249
250 ## Controlling services
251
252 ### Service hierarchy and states
253
254 Services can depend on other services for operation, and so form a
255 dependency hierarchy. Starting a service which depends on another
256 causes that other service to start (and the first service waits until
257 the latter has started before its process is launched and it is itself
258 considered started).
259
260 Services are considered _active_ when they are not stopped. Services
261 can also be explicitly marked as active (this normally happens when you
262 explicitly start a service). Finally, a service with an active dependent
263 is also considered active.
264
265 If a service stops and becomes inactive (i.e. it is not explicitly marked
266 active and has no active dependents) then any services it depends on will
267 also be marked inactive and stopped unless they have other active
268 dependents, or were explicitly started and marked active.
269
270 What this means is that, in general, starting an (inactive, stopped)
271 service and then stopping it will return the system to its prior state -
272 no dependencies which were started automatically will be left running.
273
274 ### Using dinitctl
275
276 You can use the "dinitctl" utility to start and stop services. Typical invocations
277 are:
278
279     dinitctl start <service-name>
280     dinitctl stop <service-name>
281     dinitctl release <service-name>
282
283 Note that a "start" marks the service active, as well as starting it if it is
284 not already started; the opposite of this is actually "release", which clears
285 the active mark and stops it if it has no active dependent services. The "stop"
286 command by default acts as a "release" that also forces the service to stop
287 (although it may then immediately restart, depending on how it and its
288 dependents are configured).
289
290 Use the "-s" switch to talk the "system" instance of Dinit, rather than a
291 personal instance, e.g:
292
293     dinitctl -s start mysql   # start system mysql service
294
295 For complete details on the command line, use:
296
297     dinitctl --help
298
299 You can "pin" a service in either the stopped or started state, which prevents
300 it from changing state either due to a dependency/dependent or a direct
301 command:
302
303     dinitctl -s start --pin mysql  # start mysql service, pin it as "started"
304     dinitctl -s stop mysql  # issues stop, but doesn't take effect due to pin
305     dinitctl -s unpin mysql # release pin; service will now stop
306
307 You can pin a service in the stopped state in order to make sure it doesn't
308 get started accidentally (either via a dependency or directly). You can also
309 use it to temporarily keep stopped a service that would otherwise restart
310 immediately when you stopped it (because it, or a dependent, is configured
311 to restart automatically).
312
313 Finally, you can list the state of all loaded services:
314
315     dinitctl -s list
316
317 This may result in something like the following:
318
319     [{+}     ] boot
320     [{+}     ] tty1 (pid: 300)
321     [{+}     ] tty2 (pid: 301)
322     [{+}     ] tty3 (pid: 302)
323     [{+}     ] tty4 (pid: 303)
324     [{+}     ] loginready (has console)
325     [{+}     ] rcboot
326     [{+}     ] filesystems
327     [{+}     ] udevd (pid: 4)
328     [     {-}] mysql
329
330 The above represents a number of started services and one stopped service
331 (mysql). Services transitioning state (starting or stopping) are displayed
332 with an arrow indicating the transition direction:
333
334     [{ }<<   ] mysql     # starting
335     [   >>{ }] mysql     # stopping
336     
337 The curly brackets indicate the desired state, which may not be the state to
338 which the service is currently transitioning. For example:
339
340     [   <<{ }] mysql     # starting, but will stop after starting
341     [{ }>>   ] mysql     # stopping, but will restart once stopped
342
343 Remember that a "starting" service may be waiting for its dependencies to
344 start, and a "stopping" service may be waiting for its dependencies to stop.