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