README 15 KB

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