shepherd.texi 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. \input texinfo @c -*- texinfo -*-
  2. @c shepherd.texi -- The documentation in Texinfo format.
  3. @documentencoding UTF-8
  4. @setfilename shepherd.info
  5. @settitle The GNU Shepherd Manual
  6. @include version.texi
  7. @set OLD-YEARS 2002, 2003
  8. @set NEW-YEARS 2013, 2016, 2018, 2019, 2020
  9. @copying
  10. Copyright @copyright{} @value{OLD-YEARS} Wolfgang J@"ahrling@*
  11. Copyright @copyright{} @value{NEW-YEARS} Ludovic Courtès@*
  12. Copyright @copyright{} 2020 Brice Waegeneire@*
  13. Copyright @copyright{} 2020 Oleg Pykhalov
  14. Copyright @copyright{} 2020 Jan (janneke) Nieuwenhuizen@*
  15. Permission is granted to copy, distribute and/or modify this document
  16. under the terms of the GNU Free Documentation License, Version 1.3 or
  17. any later version published by the Free Software Foundation; with no
  18. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
  19. copy of the license is included in the section entitled ``GNU Free
  20. Documentation License''.
  21. @end copying
  22. @dircategory System software
  23. @direntry
  24. * shepherd: (shepherd). The Shepherd service manager.
  25. * herd: (shepherd)Invoking herd
  26. Controlling the Shepherd service manager.
  27. * reboot: (shepherd)Invoking reboot
  28. Rebooting a Shepherd-controlled system.
  29. * halt: (shepherd)Invoking halt
  30. Turning off a Shepherd-controlled system.
  31. @end direntry
  32. @titlepage
  33. @title The GNU Shepherd Manual
  34. @subtitle For use with the GNU Shepherd @value{VERSION}
  35. @subtitle Last updated @value{UPDATED}
  36. @author Wolfgang J@"ahrling
  37. @author Ludovic Courtès
  38. @insertcopying
  39. @end titlepage
  40. @contents
  41. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  42. @ifnottex
  43. @node Top
  44. @top The GNU Shepherd Manual
  45. This manual documents the GNU@tie{}Shepherd version @value{VERSION}, a
  46. service manager for the GNU system.
  47. @menu
  48. * Introduction:: Introduction to the Shepherd service manager.
  49. * Jump Start:: How to do simple things with the Shepherd.
  50. * herd and shepherd:: User interface to service management.
  51. * Services:: Details on services.
  52. * Misc Facilities:: Generally useful things provided by the Shepherd.
  53. * Internals:: Hacking shepherd.
  54. * GNU Free Documentation License:: The license of this manual.
  55. * Concept Index::
  56. * Procedure and Macro Index::
  57. * Variable Index::
  58. * Type Index::
  59. @end menu
  60. @end ifnottex
  61. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  62. @node Introduction
  63. @chapter Introduction
  64. @cindex service manager
  65. This manual documents the GNU@tie{}Daemon Shepherd, or GNU@tie{}Shepherd
  66. for short. The Shepherd looks after system services, typically @dfn{daemons}.
  67. It is used to start and stop them in a reliable
  68. fashion. For instance it will dynamically determine and start any
  69. other services that our desired service depends upon. As another
  70. example, the Shepherd might detect conflicts among services. In this
  71. situation it would simply prevent the conflicting services from
  72. running concurrently.
  73. The Shepherd is the @dfn{init system} of the GNU operating system---it is the
  74. first user process that gets started, typically with PID 1, and runs
  75. as @code{root}. Normally the purpose of init systems is to manage all
  76. system-wide services, but the Shepherd can also be a useful tool assisting
  77. unprivileged users in the management of their own daemons.
  78. Flexible software requires some time to master and
  79. the Shepherd is no different. But don't worry: this manual should allow you to
  80. get started quickly. Its first chapter is designed as a practical
  81. introduction to the Shepherd and should be all you need for everyday use
  82. (@pxref{Jump Start}). In chapter two we will describe the
  83. @command{herd} and @command{shepherd} programs, and their relationship, in
  84. more detail (@ref{herd and shepherd}). Subsequent chapters provide a full
  85. reference manual and plenty of examples, covering all of Shepherd's
  86. capabilities. Finally, the last chapter provides information for
  87. those souls brave enough to hack the Shepherd itself.
  88. @cindex dmd
  89. The Shepherd was formerly known as ``dmd'', which stands for @dfn{Daemon
  90. Managing Daemons} (or @dfn{Daemons-Managing Daemon}?).
  91. @cindex Guile
  92. @cindex Scheme
  93. @cindex GOOPS
  94. This program is written in Guile, an implementation of the
  95. Scheme programming language, using the GOOPS extension for
  96. object-orientation. Guile is also the Shepherd's configuration language.
  97. @xref{Introduction,,, guile, GNU Guile Reference Manual}, for an
  98. introduction to Guile. We have tried to
  99. make the Shepherd's basic features as accessible as possible---you should be
  100. able to use these even if you do not know how to program in Scheme. A
  101. basic grasp of Guile and GOOPS is required only if you wish to make
  102. use of the Shepherd's more advanced features.
  103. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  104. @node Jump Start
  105. @chapter Jump Start
  106. @cindex prefix
  107. This chapter gives a short overview of the Shepherd. It is enough if you just
  108. need the basic features of it. As it is not assumed that readers are
  109. familiar with all the involved issues, a very experienced user might
  110. be annoyed by the often very detailed descriptions in this
  111. introduction. Those users are encouraged to just skip to the
  112. reference section.
  113. Note that all the full file names in the following text are based on
  114. the assumption that you have installed the Shepherd with an empty prefix. If
  115. your Shepherd installation for example resides in @code{/usr/local}
  116. instead, add this directory name in front of the absolute file names
  117. mentioned below.
  118. @cindex Configuration file
  119. When @command{shepherd} gets started, it reads and evaluates a
  120. configuration file. When it is started with superuser privileges, it
  121. tries to use @code{/etc/shepherd.scm}. When started as normal user, it
  122. looks for a file called @code{$XDG_CONFIG_HOME/shepherd/init.scm}. If
  123. the @code{XDG_CONFIG_HOME} environment variable is not defined,
  124. @code{$HOME/.config/shepherd/init.scm} is used instead (@pxref{Managing
  125. User Services }). With the option @code{--config} (or, for short,
  126. @code{-c}), you can specify where to look instead. So if you want to
  127. start @command{shepherd} with an alternative file, use one of the
  128. following commands:
  129. @example
  130. shepherd --config=/etc/shepherd.scm.old
  131. shepherd -c /etc/shepherd.scm.old
  132. @end example
  133. @cindex Starting a service
  134. As the final ``d'' suggests, @command{shepherd} is just
  135. a daemon that (usually) runs in the
  136. background, so you will not interact with it directly. After it is
  137. started, @command{shepherd} will listen on a socket special file, usually
  138. @code{/var/run/shepherd/socket}, for further commands. You use the tool
  139. @dfn{herd} to send these commands to @command{shepherd}. Usage of herd is simple and
  140. straightforward: To start a service called @code{apache}, you use:
  141. @example
  142. herd start apache
  143. @end example
  144. @cindex Status (of services)
  145. @cindex Service status
  146. When you do this, all its dependencies will get resolved. For
  147. example, a webserver is quite likely to depend on working networking,
  148. thus it will depend on a service called @code{networking}. So if you
  149. want to start @code{apache}, and @code{networking} is not yet running, it
  150. will automatically be started as well. The current status of all the
  151. services defined in the configuration file can be queried like this:
  152. @example
  153. herd status
  154. @end example
  155. @noindent
  156. Or, to get additional details about each service, run:
  157. @example
  158. herd detailed-status
  159. @end example
  160. @noindent
  161. In this example, this would show the @code{networking} and @code{apache}
  162. services as started. If you just want to know the status of the
  163. @code{apache} service, run:
  164. @example
  165. herd status apache
  166. @end example
  167. @cindex Stopping a service
  168. You can stop
  169. a service and all the services that depend on it will be stopped.
  170. Using the example above, if you stop @code{networking}, the service
  171. @code{apache} will be stopped as well---which makes perfect sense,
  172. as it cannot work without the network being up. To actually stop a
  173. service, you use the following, probably not very surprising, command:
  174. @example
  175. herd stop networking
  176. @end example
  177. There are two more actions you can perform on every service: the
  178. actions @code{enable} and @code{disable} are used to prevent and allow
  179. starting of the particular service. If a service is intended to be
  180. restarted whenever it terminates (how this can be done will not be
  181. covered in this introduction), but it is respawning too often in a
  182. short period of time (by default 5 times in 5 seconds), it will
  183. automatically be disabled. After you have fixed the problem that
  184. caused it from being respawned too fast, you can start it again with
  185. the commands:
  186. @example
  187. herd enable foo
  188. herd start foo
  189. @end example
  190. @cindex virtual services
  191. @cindex fallback services
  192. But there is far more you can do than just that. Services can not
  193. only simply depend on other services, they can also depend on
  194. @emph{virtual} services. A virtual service is a service that is
  195. provided by one or more service additionally. For instance, a service
  196. called @code{exim} might provide the virtual service
  197. @code{mailer-daemon}. That could as well be provided by a service
  198. called @code{smail}, as both are mailer-daemons. If a service needs
  199. any mailer-daemon, no matter which one, it can just depend on
  200. @code{mailer-daemon}, and one of those who provide it gets started (if
  201. none is running yet) when resolving dependencies. The nice thing is
  202. that, if trying to start one of them fails, @command{shepherd} will go on and try to
  203. start the next one, so you can also use virtual services for
  204. specifying @emph{fallbacks}.
  205. Additionally to all that, you can perform service-specific actions.
  206. Coming back to our original example, @code{apache} is able to
  207. reload its modules, therefore the action @code{reload-modules} might
  208. be available:
  209. @example
  210. herd reload-modules apache
  211. @end example
  212. Service-specific actions can only be used when the service is
  213. started, i.e. the only thing you can do to a stopped service is
  214. starting it. An exception exists, see below. (If you may at some
  215. point find this too restrictive because you want to use variants of
  216. the same service which are started in different ways, consider using
  217. different services for those variants instead, which all provide the
  218. same virtual service and thus conflict with each other, if this is
  219. desired. That's one of the reasons why virtual services exist, after
  220. all.)
  221. There are two actions which are special, because even if services
  222. can implement them on their own, a default implementation is provided
  223. by @command{shepherd} (another reason why they are special is that the default
  224. implementations can be called even when the service is not running;
  225. this inconsistency is just to make it more intuitive to get
  226. information about the status of a service, see below).
  227. These actions are @code{restart} and @code{status}. The default
  228. implementation of @code{restart} calls @code{stop} and @code{start} on the
  229. affected service, taking care to also restart any dependent services. The
  230. default implementation of @code{status} displays some general information
  231. about the service, like what it provides, what it depends on and with which
  232. other services it conflicts (because they provide a virtual service that is
  233. also provided by that particular service).
  234. A special service is @code{root}, which is used for controlling the
  235. Shepherd itself. You can also reference to this service as
  236. @code{shepherd}. It implements various actions. For example, the
  237. @code{status} action displays which services are started and which ones
  238. are stopped, whereas @code{detailed-status} has the effect of applying
  239. the default implementation of @code{status} to all services one after
  240. another. The @code{load} action is unusual insofar as it shows a
  241. feature that is actually available to all services, but which we have
  242. not seen yet: It takes an additional argument. You can use @code{load}
  243. to load arbitrary code into the Shepherd at runtime, like this:
  244. @example
  245. herd load shepherd ~/additional-services.scm
  246. @end example
  247. In the same vein the special action @code{doc} describes its service
  248. when called without an argument or describes a service-specific action
  249. when called with the action as the additional arguments. You can even
  250. get the list of the service-specific actions a service provides when
  251. using with the additional argument @code{list-actions}.
  252. @example
  253. $ herd doc root
  254. The root service is used to operate on shepherd itself.
  255. $ herd doc root list-actions
  256. root (help status halt power-off load eval unload reload daemonize persistency no-persistency cd restart)
  257. $ herd doc root action power-off
  258. power-off: Halt the system and turn it off.
  259. @end example
  260. This is enough now about the @command{herd} and @command{shepherd} programs, we
  261. will now take a look at how to configure the Shepherd. In the configuration
  262. file, we need mainly the definition of services. We can also do
  263. various other things there, like starting a few services already.
  264. FIXME: Finish. For now, look at the @code{doc/examples/} subdirectory.
  265. @example
  266. ...
  267. @end example
  268. Ok, to summarize:
  269. @itemize @bullet
  270. @item
  271. @command{shepherd} is a daemon, @command{herd} the program that controls it.
  272. @item
  273. You can start, stop, restart, enable and disable every service, as
  274. well as display its status.
  275. @item
  276. You can perform additional service-specific actions, which you can
  277. also list.
  278. @item
  279. Actions can have arguments.
  280. @item
  281. You can display the status of a service, even if the service does not
  282. provide a specific implementation for this action. The same is true
  283. for restarting.
  284. @item
  285. The @code{root}/@code{shepherd} service is used to control
  286. @command{shepherd} itself.
  287. @end itemize
  288. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  289. @node herd and shepherd
  290. @chapter @command{herd} and @command{shepherd}
  291. @cindex herd
  292. @cindex shepherd
  293. @cindex daemon
  294. @cindex daemon controller
  295. @cindex relative file names
  296. @cindex herding, of daemons
  297. The daemon that runs in the background and is responsible for
  298. controlling the services is @command{shepherd}, while the user interface
  299. tool is called @command{herd}: it's the command that allows you to
  300. actually @emph{herd} your daemons@footnote{
  301. @cindex deco, daemon controller
  302. In the past, when the
  303. GNU@tie{}Shepherd was known as GNU@tie{}dmd, the @command{herd} command
  304. was called @code{deco}, for @dfn{DaEmon COntroller}.}. To perform an
  305. action, like stopping a service or calling an action of a service, you
  306. use the herd program. It will communicate with shepherd over a Unix
  307. Domain Socket.
  308. Thus, you start @command{shepherd} once, and then always use herd whenever you want
  309. to do something service-related. Since herd passes its current
  310. working directory to @command{shepherd}, you can pass relative file names without
  311. trouble. Both @command{shepherd} and herd understand the standard arguments
  312. @code{--help}, @code{--version} and @code{--usage}.
  313. @menu
  314. * Invoking shepherd:: How to start the service damon.
  315. * Invoking herd:: Controlling daemons.
  316. * Invoking reboot:: Rebooting a shepherd-controlled system.
  317. * Invoking halt:: Turning off a shepherd-controlled system.
  318. @end menu
  319. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  320. @node Invoking shepherd
  321. @section Invoking @command{shepherd}
  322. @cindex @command{shepherd} Invocation
  323. @cindex invoking @command{shepherd}
  324. The @code{shepherd} program has the following synopsis:
  325. @example
  326. shepherd [@var{option}@dots{}]
  327. @end example
  328. It accepts the following options:
  329. @table @samp
  330. @item -c @var{file}
  331. @itemx --config=@var{file}
  332. Read and evaluate @var{file} as the configuration script on startup.
  333. @var{file} is evaluated in the context of a fresh module where bindings
  334. from the @code{(shepherd service)} module and Guile's @code{(oop goops)} are
  335. available, in addition to the default set of Guile bindings. In
  336. particular, this means that code in @var{file} may use
  337. @code{register-services}, the @code{<service>} class, and related tools
  338. (@pxref{Services}).
  339. @item -I
  340. @itemx --insecure
  341. @cindex security
  342. @cindex insecure
  343. Do not check if the directory where the socket---our communication
  344. rendez-vous with @command{herd}---is located has permissions @code{700}.
  345. If this option is not specified, @command{shepherd} will abort if the
  346. permissions are not as expected.
  347. @item -l [@var{file}]
  348. @itemx --logfile[=@var{file}]
  349. @cindex logging
  350. @cindex log file
  351. Log output into @var{file}.
  352. For unprivileged users, the default log file is
  353. @file{$XDG_DATA_DIR/.local/share/shepherd/shepherd.log}.
  354. @cindex syslog
  355. When running as root, the default behavior is to connect to
  356. @file{/dev/log}, the @dfn{syslog} socket (@pxref{Overview of Syslog,,,
  357. libc, The GNU C Library Reference Manual}). A syslog daemon,
  358. @command{syslogd}, is expected to read messages from there
  359. (@pxref{syslogd invocation, syslogd,, libc, GNU Inetutils}).
  360. When @file{/dev/log} is unavailable, for instance because
  361. @command{syslogd} is not running, as is the case during system startup
  362. and shutdown, @command{shepherd} falls back to the Linux kernel
  363. @dfn{ring buffer}, @file{/dev/kmsg}. If @file{/dev/kmsg} is missing, as
  364. is the case on other operating systems, it falls back to
  365. @file{/dev/console}.
  366. @item --pid[=@var{file}]
  367. When @command{shepherd} is ready to accept connections, write its PID to @var{file} or
  368. to the standard output if @var{file} is omitted.
  369. @item -p [@var{file}]
  370. @itemx --persistency[=@var{file}]
  371. @c FIXME-CRITICAL
  372. @item -s @var{file}
  373. @itemx --socket=@var{file}
  374. @cindex socket special file
  375. @vindex XDG_RUNTIME_DIR
  376. Receive further commands on the socket special file @var{file}. If this
  377. option is not specified, @file{@var{localstatedir}/run/shepherd/socket} is
  378. taken when running as @code{root}; when running as an unprivileged
  379. user, @command{shepherd} listens to @file{/run/user/@var{uid}/shepherd/socket},
  380. where @var{uid} is the user's numerical ID@footnote{On GNU/Linux, the
  381. @file{/run/user/@var{uid}} directory is typically created by elogind or by
  382. systemd, which are available in most distributions.}, or to
  383. @file{$XDG_RUNTIME_DIR/shepherd} when the @code{XDG_RUNTIME_DIR}
  384. environment variable is defined.
  385. If @code{-} is specified as file name, commands will be read from
  386. standard input, one per line, as would be passed on a @command{herd}
  387. command line (@pxref{Invoking herd}).
  388. @item --quiet
  389. Synonym for @code{--silent}.
  390. @end table
  391. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  392. @node Invoking herd
  393. @section Invoking @command{herd}
  394. @cindex herd
  395. The @command{herd} command is a generic client program to control a
  396. running instance of @command{shepherd} (@pxref{Invoking shepherd}). It has the
  397. following synopsis:
  398. @example
  399. herd [@var{option}@dots{}] @var{action} [@var{service} [@var{arg}@dots{}]]
  400. @end example
  401. It causes the @var{action} of the @var{service} to be invoked. When
  402. @var{service} is omitted and @var{action} is @code{status} or
  403. @code{detailed-status}, the @code{root} service is used@footnote{This
  404. shorthand does not work for other actions such as @code{stop}, because
  405. inadvertently typing @code{herd stop} would stop all the services, which
  406. could be pretty annoying.} (@pxref{The root and unknown services}, for
  407. more information on the @code{root} service.)
  408. For each action, you should pass the appropriate @var{arg}s. Actions
  409. that are available for every service are @code{start}, @code{stop},
  410. @code{restart}, @code{status}, @code{enable}, @code{disable}, and
  411. @code{doc}.
  412. If you pass a file name as an @var{arg}, it will be passed as-is to
  413. the Shepherd, thus if it is not an absolute name, it is local to the current
  414. working directory of @command{shepherd}, not to herd.
  415. The @code{herd} command understands the following option:
  416. @table @samp
  417. @item -s @var{file}
  418. @itemx --socket=@var{file}
  419. Send commands to the socket special file @var{file}. If this option is
  420. not specified, @file{@var{localstatedir}/run/shepherd/socket} is taken.
  421. @end table
  422. The @code{herd} command returns zero on success, and a non-zero exit
  423. code on failure. In particular, it returns a non-zero exit code when
  424. @var{action} or @var{service} does not exist and when the given action
  425. failed.
  426. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  427. @node Invoking reboot
  428. @section Invoking @command{reboot}
  429. @cindex herd
  430. The @command{reboot} command is a convenience client program to instruct
  431. the Shepherd (when used as an init system) to stop all running services and
  432. reboot the system. It has the following synopsis:
  433. @example
  434. reboot [@var{option}@dots{}]
  435. @end example
  436. It is equivalent to running @command{herd stop shepherd}. The
  437. @code{reboot} command understands the following option:
  438. @table @samp
  439. @item -s @var{file}
  440. @itemx --socket=@var{file}
  441. Send commands to the socket special file @var{file}. If this option is
  442. not specified, @file{@var{localstatedir}/run/shepherd/socket} is taken.
  443. @end table
  444. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  445. @node Invoking halt
  446. @section Invoking @command{halt}
  447. @cindex herd
  448. The @command{halt} command is a convenience client program to instruct
  449. the Shepherd (when used as an init system) to stop all running services and turn
  450. off the system. It has the following synopsis:
  451. @example
  452. halt [@var{option}@dots{}]
  453. @end example
  454. It is equivalent to running @command{herd power-off shepherd}. As
  455. usual, the @code{halt} command understands the following option:
  456. @table @samp
  457. @item -s @var{file}
  458. @itemx --socket=@var{file}
  459. Send commands to the socket special file @var{file}. If this option is
  460. not specified, @file{@var{localstatedir}/run/shepherd/socket} is taken.
  461. @end table
  462. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  463. @node Services
  464. @chapter Services
  465. @cindex service
  466. @tindex <service>
  467. The @dfn{service} is obviously a very important concept of the Shepherd. On the
  468. Guile level, a service is represented as an instance of
  469. @code{<service>}, a GOOPS class (@pxref{GOOPS,,, guile, GNU Guile
  470. Reference Manual}). When creating an instance of it, you can specify
  471. the initial values of its slots, and you actually must do this for some
  472. of the slots.
  473. The @code{<service>} class and its associated procedures and methods are
  474. defined in the @code{(shepherd service)} module.
  475. @menu
  476. * Slots of services:: What a <service> object consists of.
  477. * Methods of services:: What you can do with a <service> object.
  478. * Service Convenience:: How to conveniently work with services.
  479. * Service De- and Constructors:: Commonly used ways of starting and
  480. stopping services.
  481. * Service Examples:: Examples that show how services are used.
  482. * Managing User Services:: Running the Shepherd as a user.
  483. * The root and unknown services:: Special services in the Shepherd.
  484. @end menu
  485. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  486. @node Slots of services
  487. @section Slots of services
  488. @cindex <service>, slots of
  489. @cindex slots of <service>
  490. A service has the following slots, all of which can be initialized
  491. with a keyword (i.e. @code{#:provides}, used when creating the object)
  492. of the same name, except where stated otherwise. You should not
  493. access them directly with @code{slot-ref} or @code{slot-set!}
  494. usually, use the methods of the service class @ref{Methods of
  495. services} instead.
  496. @itemize @bullet
  497. @item
  498. @vindex provides (slot of <service>)
  499. @cindex canonical names of services
  500. @code{provides} is a list of symbols that are provided by the service.
  501. A symbol can only be provided by one service running at a time,
  502. i.e. if two services provide the same symbol, only one of them can
  503. run, starting the other one will fail. Therefore, these symbols are
  504. mainly used to denote conflicting services. The first symbol in the
  505. list is the canonical name for the service, thus it must be unique.
  506. This slot has no default value and must therefore be initialized.
  507. @item
  508. @vindex requires (slot of <service>)
  509. @code{requires} is, like @code{provides}, a list of symbols that
  510. specify services. In this case, they name what this service depends
  511. on, i.e. before the service can be started, services that provide
  512. those symbols must be started. If a required symbol is provided by
  513. several services, one will be started. By default, this slot
  514. contains the empty list.
  515. @item
  516. @vindex running (slot of <service>)
  517. @cindex Hook for individual services
  518. @code{running} is a hook that can be used by each service in its own
  519. way. The default value is @code{#f}, which indicates that the service
  520. is not running. When an attempt is made to start the service, it will
  521. be set to the return value of the procedure in the @code{start} slot.
  522. It will also be passed as an argument to the procedure in the
  523. @code{stop} slot. If it is set a value that is an integer, it is
  524. assumed to be a process id, and shepherd will monitor the process for
  525. unexpected exits. This slot can not be initialized with a keyword.
  526. @item
  527. @vindex respawn? (slot of <service>)
  528. @cindex Respawning services
  529. @code{respawn?} specifies whether the service should be respawned by
  530. the Shepherd. If this slot has the value @code{#t}, then assume the
  531. @code{running} slot specifies a child process PID and restart the
  532. service if that process terminates. Otherwise this slot is @code{#f},
  533. which is the default. See also the @code{last-respawns} slot.
  534. @item
  535. @vindex one-shot? (slot of <service>)
  536. @cindex one-shot services
  537. The @code{one-shot?} slot determines whether the service is a @dfn{one-shot
  538. service}. A one-shot service is a service that, as soon as it has been
  539. successfully started, is marked as ``stopped.'' Other services can
  540. nonetheless require one-shot services. One-shot services are useful to
  541. trigger an action before other services are started, such as a cleanup or an
  542. initialization action.
  543. As for other services, the @code{start} method of a one-shot service must
  544. return a truth value to indicate success, and false to indicate failure.
  545. @item
  546. @vindex start (slot of <service>)
  547. @cindex Starting a service
  548. @cindex Service constructor
  549. @code{start} contains the ``constructor'' for the service, which will
  550. be called to start the service. (Therefore, it is not a constructor
  551. in the sense that it initializes the slots of a @code{<service>}
  552. object.) This must be a procedure that accepts any amount of
  553. arguments, which will be the additional arguments supplied by the
  554. user. If the starting attempt failed, it must return @code{#f}. The
  555. value will be stored in the @code{running} slot. The default value is
  556. a procedure that returns @code{#t} and performs no further actions,
  557. therefore it is desirable to specify a different one usually.
  558. @item
  559. @vindex stop (slot of <service>)
  560. @cindex Stopping a service
  561. @cindex Service destructor
  562. @code{stop} is, similar to @code{start}, a slot containing a
  563. procedure. But in this case, it gets the current value of the
  564. @code{running} slot as first argument and the user-supplied arguments
  565. as further arguments; it gets called to stop the service. Its return
  566. value will again be stored in the @code{running} slot, so that it
  567. should return @code{#f} if it is now possible again to start the
  568. service at a later point. The default value is a procedure that
  569. returns @code{#f} and performs no further actions.
  570. @item
  571. @vindex actions (slot of <service>)
  572. @cindex Actions of services
  573. @cindex Service actions
  574. @code{actions} specifies the additional actions that can be performed
  575. on a service when it is running. A typical example for this is the
  576. @code{restart} action. The macro @code{make-actions} @ref{Service
  577. Convenience} is provided to abstract the actual data representation
  578. format for this slot. (It actually is a hash currently.)
  579. @item
  580. @vindex enabled? (slot of <service>)
  581. @code{enabled?} cannot be initialized with a keyword, and contains
  582. @code{#t} by default. When the value becomes @code{#f} at some point,
  583. this will prevent the service from getting started. A service can be
  584. enabled and disabled with the methods @code{enable} and
  585. @code{disable}, respectively @ref{Methods of services}.
  586. @item
  587. @vindex last-respawns (slot of <service>)
  588. @code{last-respawns} cannot be initialized with a keyword and is only
  589. ever used when the @code{respawn?} slot contains @code{#t}; it is a
  590. circular list with @code{(car respawn-limit)} elements, where each
  591. element contains the time when it was restarted, initially all 0,
  592. later a time in seconds since the Epoch. The first element is the one
  593. that contains the oldest one, the last one the newest.
  594. @item
  595. @vindex stop-delay? (slot of <service>)
  596. @code{stop-delay?} being false causes the @code{stop} slot to be
  597. unused; instead, stopping the service will just cause the
  598. @code{waiting-for-termination?} slot be set to @code{#t}.
  599. @item
  600. @vindex waiting-for-termination? (slot of <service>)
  601. @code{waiting-for-termination?} cannot be initialized with a keyword
  602. and should not be used by others, it is only used internally for
  603. respawnable services when the @code{stop-delay?} slot contains a true
  604. value. @code{waiting-for-termination?} contains @code{#t} if the
  605. service is still running, but the user requested that it be stopped,
  606. in which case if the service terminates the next time, the respawn
  607. handler will not start it again.
  608. otherwise @code{#f}.
  609. @item
  610. @vindex replacement (slot of <service>)
  611. @code{replacement} specifies a service to be used to replace this one
  612. when it is stopped. This service will continue to function normally
  613. until the @code{stop} action is invoked. After the service has been
  614. successfully stopped, its definition will be replaced by the value of
  615. this slot, which must itself be a service. This slot is ignored if
  616. its value is @code{#f}.
  617. @end itemize
  618. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  619. @node Methods of services
  620. @section Methods of services
  621. @deffn {method} start (obj <service>)
  622. Start the service @var{obj}, including all the services it depends on.
  623. It tries quite hard to do this: When a service that provides a
  624. required symbol can not be started, it will look for another service
  625. that also provides this symbol, until starting one such service
  626. succeeds. There is some room for theoretical improvement here, of
  627. course, but in practice the current strategy already works very well.
  628. This method returns the new value of the @code{running} slot
  629. @ref{Slots of services}, which is @code{#f} if the service could not
  630. be started.
  631. @end deffn
  632. @deffn {method} stop (obj <service>)
  633. This will stop the service @var{obj}, trying to stop services that
  634. depend in it first, so they can be shutdown cleanly. If this will
  635. fail, it will continue anyway. Stopping of services should usually
  636. succeed, though. Otherwise, the behaviour is very similar to the
  637. @code{start} method. The return value is also the new @code{running}
  638. value, thus @code{#f} if the service was stopped.
  639. @end deffn
  640. @deffn {method} action (obj <service>) the-action . args
  641. Calls the action @var{the-action} (a symbol) of the service @var{obj},
  642. with the specified @var{args}, which have a meaning depending on the
  643. particular action.
  644. @end deffn
  645. @deffn {method} conflicts-with (obj <service>)
  646. Returns a list of the canonical names of services that conflict with
  647. the service @var{obj}.
  648. @end deffn
  649. @deffn {method} canonical-name (obj <service>)
  650. Returns the canonical name of @var{obj}, which is the first element of
  651. the @code{provides} list.
  652. @end deffn
  653. @deffn {method} provided-by (obj <service>)
  654. Returns which symbols are provided by @var{obj}.
  655. @end deffn
  656. @deffn {method} required-by (obj <service>)
  657. Returns which symbols are required by @var{obj}.
  658. @end deffn
  659. @deffn {method} one-shot? (obj <service>)
  660. Returns whether the service @var{obj} is a one-shot service.
  661. @end deffn
  662. @deffn {method} running? (obj <service>)
  663. Returns whether the service @var{obj} is running.
  664. @end deffn
  665. @deffn {method} respawn? (obj <service>)
  666. Returns whether the service @var{obj} should be respawned if it
  667. terminates.
  668. @end deffn
  669. @deffn {method} default-display-status (obj <service>)
  670. Display status information about @var{obj}. This method is called
  671. when the user performs the action @code{status} on @var{obj}, but
  672. there is no specific implementation given for it. It is also called
  673. when @code{detailed-status} is applied on the @code{root} service.
  674. @end deffn
  675. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  676. @node Service Convenience
  677. @section Service Convenience
  678. In addition to the facilities listed below, there are also some
  679. procedures that provide commonly needed constructors and destructors
  680. for services @ref{Service De- and Constructors}.
  681. @deffn {procedure} register-services . services
  682. Register all @var{services}, so that they can be taken into account
  683. when trying to resolve dependencies.
  684. @end deffn
  685. @deffn {procedure} lookup-services name
  686. Return a list of all registered services which provide the symbol
  687. @var{name}.
  688. @end deffn
  689. @deffn {macro} make-actions (name proc) ...
  690. This macro is used to create a value for the @code{actions} slot of a
  691. service object @ref{Slots of services}. Each @var{name} is a symbol
  692. and each @var{proc} the corresponding procedure that will be called to
  693. perform the action. A @var{proc} has one argument, which will be the
  694. current value of the @code{running} slot of the service.
  695. @end deffn
  696. @deffn {method} start (obj <symbol>)
  697. Start a registered service providing @var{obj}.
  698. @end deffn
  699. @deffn {method} stop (obj <symbol>)
  700. Stop a registered service providing @var{obj}.
  701. @end deffn
  702. @deffn {method} action (obj <symbol>) the-action . args
  703. The same as the @code{action} method of class @code{<service>}, but
  704. uses a service that provides @var{obj} and is running.
  705. @end deffn
  706. @deffn {procedure} for-each-service proc
  707. Call @var{proc}, a procedure taking one argument, once for each
  708. registered service.
  709. @end deffn
  710. @deffn {procedure} find-running services
  711. Check if any of @var{services} is running. If this is the case,
  712. return its canonical name. If not, return @code{#f}. Only the first
  713. one will be returned; this is because this is mainly intended to be
  714. applied on the return value of @code{lookup-services}.
  715. @end deffn
  716. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  717. @node Service De- and Constructors
  718. @section Service De- and Constructors
  719. @cindex generating constructors
  720. @cindex generating destructors
  721. @cindex constructors, generation of
  722. @cindex destructors, generation of
  723. All of the procedures listed below return procedures generated from
  724. the supplied arguments. These procedures take one argument in the
  725. case of destructors and no arguments in the case of constructors.
  726. @deffn {procedure} make-system-constructor @var{command}@dots{}
  727. The returned procedure will execute @var{command} in a shell and
  728. return @code{#t} if execution was successful, otherwise @code{#f}.
  729. For convenience, it takes multiple arguments which will be
  730. concatenated first.
  731. @end deffn
  732. @deffn {procedure} make-system-destructor @var{command}@dots{}
  733. Similar to @code{make-system-constructor}, but returns @code{#f} if
  734. execution of the @var{command} was successful, @code{#t} if not.
  735. @end deffn
  736. @deffn {procedure} make-forkexec-constructor @var{command} @
  737. [#:user #f] @
  738. [#:group #f] @
  739. [#:supplementary-groups '()] @
  740. [#:pid-file #f] [#:pid-file-timeout (default-pid-file-timeout)] @
  741. [#:log-file #f] @
  742. [#:directory (default-service-directory)] @
  743. [#:file-creation-mask #f] @
  744. [#:environment-variables (default-environment-variables)]
  745. Return a procedure that forks a child process, closes all file
  746. descriptors except the standard output and standard error descriptors,
  747. sets the current directory to @var{directory}, sets the umask to
  748. @var{file-creation-mask} unless it is @code{#f}, changes the environment
  749. to @var{environment-variables} (using the @code{environ} procedure),
  750. sets the current user to @var{user} the current group to @var{group}
  751. unless they are @code{#f} and supplementary groups to
  752. @var{supplementary-groups} unless they are @code{'()}, and executes
  753. @var{command} (a list of strings.) The result of the procedure will be
  754. the PID of the child process. Note that this will not work as expected
  755. if the process ``daemonizes'' (forks); in that case, you will need to
  756. pass @code{#:pid-file}, as explained below.
  757. When @var{pid-file} is true, it must be the name of a PID file
  758. associated with the process being launched; the return value is the PID
  759. once that file has been created. If @var{pid-file} does not show up in less
  760. than @var{pid-file-timeout} seconds, the service is considered as failing to
  761. start.
  762. When @var{log-file} is true, it names the file to which the service's
  763. standard output and standard error are redirected. @var{log-file} is
  764. created if it does not exist, otherwise it is appended to.
  765. @end deffn
  766. @deffn {procedure} make-kill-destructor [@var{signal}]
  767. Return a procedure that sends @var{signal} to the process group of the
  768. PID given as argument, where @var{signal} defaults to @code{SIGTERM}.
  769. This @emph{does} work together with respawning services,
  770. because in that case the @code{stop} method of the @code{<service>}
  771. class sets the @code{running} slot to @code{#f} before actually
  772. calling the destructor; if it would not do that, killing the process
  773. in the destructor would immediately respawn the service.
  774. @end deffn
  775. The @code{make-forkexec-constructor} procedure builds upon the following
  776. procedures.
  777. @deffn {procedure} exec-command @var{command} @
  778. [#:user #f] @
  779. [#:group #f] @
  780. [#:supplementary-groups '()] @
  781. [#:log-file #f] @
  782. [#:directory (default-service-directory)] @
  783. [#:file-creation-mask #f] @
  784. [#:environment-variables (default-environment-variables)]
  785. @deffnx {procedure} fork+exec-command @var{command} @
  786. [#:user #f] @
  787. [#:group #f] @
  788. [#:supplementary-groups '()] @
  789. [#:directory (default-service-directory)] @
  790. [#:file-creation-mask #f] @
  791. [#:environment-variables (default-environment-variables)]
  792. Run @var{command} as the current process from @var{directory}, with
  793. @var{file-creation-mask} if it's true, and with
  794. @var{environment-variables} (a list of strings like @code{"PATH=/bin"}.)
  795. File descriptors 1 and 2 are kept as is or redirected to @var{log-file}
  796. if it's true, whereas file descriptor 0
  797. (standard input) points to @file{/dev/null}; all other file descriptors
  798. are closed prior to yielding control to @var{command}.
  799. By default, @var{command} is run as the current user. If the @var{user}
  800. keyword argument is present and not false, change to @var{user}
  801. immediately before invoking @var{command}. @var{user} may be a string,
  802. indicating a user name, or a number, indicating a user ID. Likewise,
  803. @var{command} will be run under the current group, unless the
  804. @var{group} keyword argument is present and not false, and
  805. @var{supplementary-groups} is not @code{'()}.
  806. @code{fork+exec-command} does the same as @code{exec-command}, but in
  807. a separate process whose PID it returns.
  808. @end deffn
  809. @defvr {Scheme Variable} default-environment-variables
  810. This parameter (@pxref{Parameters,,, guile, GNU Guile Reference Manual})
  811. specifies the default list of environment variables to be defined when
  812. the procedures above create a new process.
  813. It must be a list of strings where each string has the format
  814. @code{@var{name}=@var{value}}. It defaults to what @code{environ}
  815. returns when the program starts (@pxref{Runtime Environment,
  816. @code{environ},, guile, GNU Guile Reference Manual}).
  817. @end defvr
  818. @defvr {Scheme Variable} default-pid-file-timeout
  819. This parameter (@pxref{Parameters,,, guile, GNU Guile Reference Manual})
  820. specified the default PID file timeout in seconds, when
  821. @code{#:pid-file} is used (see above). It defaults to 5 seconds.
  822. @end defvr
  823. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  824. @node Service Examples
  825. @section Service Examples
  826. FIXME: This needs a lot of work.
  827. You can create a service and then register it this way:
  828. @lisp
  829. (define apache (make <service>
  830. #:provides '(apache)
  831. #:start (...)
  832. #:stop (...)))
  833. (register-services apache)
  834. @end lisp
  835. However, as you usually won't need a variable for the service, you can
  836. pass it directly to @code{register-services}. Here is an example that
  837. also specifies some more initial values for the slots:
  838. @lisp
  839. (register-services
  840. (make <service>
  841. #:provides '(apache-2.0 apache httpd)
  842. #:requires '()
  843. #:start (...)
  844. #:stop (...)
  845. #:actions (make-actions
  846. (reload-modules (...))
  847. (restart (...)))))
  848. @end lisp
  849. @node Managing User Services
  850. @section Managing User Services
  851. The Shepherd can be used to manage services for an unprivileged user.
  852. First, you may want to ensure it is up and running every time you log
  853. in. One way to accomplish that is by adding the following lines to
  854. @file{~/.bash_profile} (@pxref{Bash Startup Files,,, bash, The GNU Bash
  855. Reference Manual}):
  856. @verbatim
  857. if [[ ! -S ${XDG_RUNTIME_DIR-$HOME/.cache}/shepherd/socket ]]; then
  858. shepherd
  859. fi
  860. @end verbatim
  861. Then, we suggest the following top-level
  862. @file{$XDG_CONFIG_HOME/shepherd/init.scm} file, which will automatically
  863. load individual service definitions from
  864. @file{~/.config/shepherd/init.d}:
  865. @lisp
  866. (use-modules (shepherd service)
  867. ((ice-9 ftw) #:select (scandir)))
  868. ;; Load all the files in the directory 'init.d' with a suffix '.scm'.
  869. (for-each
  870. (lambda (file)
  871. (load (string-append "init.d/" file)))
  872. (scandir (string-append (dirname (current-filename)) "/init.d")
  873. (lambda (file)
  874. (string-suffix? ".scm" file))))
  875. ;; Send shepherd into the background
  876. (action 'shepherd 'daemonize)
  877. @end lisp
  878. Then, individual user services can be put in
  879. @code{$XDG_CONFIG_HOME/shepherd/init.d/}, e.g., for @command{ssh-agent}.
  880. @lisp
  881. ;;; Commentary:
  882. ;;;
  883. ;;; Add to your ~/.bash_profile:
  884. ;;;
  885. ;;; SSH_AUTH_SOCK=$@{XDG_RUNTIME_DIR-$HOME/.cache@}/ssh-agent/socket
  886. ;;; export SSH_AUTH_SOCK
  887. ;;;
  888. ;;; Code:
  889. (use-modules (shepherd support))
  890. (define ssh-agent
  891. (make <service>
  892. #:provides '(ssh-agent)
  893. #:docstring "Run `ssh-agent'"
  894. #:start (lambda ()
  895. (let ((socket-dir (string-append %user-runtime-dir "/ssh-agent")))
  896. (unless (file-exists? socket-dir)
  897. (mkdir-p socket-dir)
  898. (chmod socket-dir #o700))
  899. (fork+exec-command
  900. `("ssh-agent" "-D" "-a" ,(string-append socket-dir "/socket"))
  901. #:log-file (string-append %user-log-dir "/ssh-agent.log"))))
  902. #:stop (make-kill-destructor)
  903. #:respawn? #t))
  904. (register-services ssh-agent)
  905. (start ssh-agent)
  906. @end lisp
  907. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  908. @node The root and unknown services
  909. @section The @code{root} and @code{unknown} services
  910. @cindex root service
  911. @cindex special services
  912. The service @code{root} is special, because it is used to control the
  913. Shepherd itself. It has an alias @code{shepherd}. It provides the
  914. following actions (in addition to @code{enable}, @code{disable} and
  915. @code{restart} which do not make sense here).
  916. @table @code
  917. @item status
  918. Displays which services are started and which ones are not.
  919. @item detailed-status
  920. Displays detailed information about every registered service.
  921. @item load @var{file}
  922. Evaluate the Scheme code in @var{file} in a fresh module that uses the
  923. @code{(oop goops)} and @code{(shepherd services)} modules---as with the
  924. @code{--config} option of @command{shepherd} (@pxref{Invoking shepherd}).
  925. @item eval @var{exp}
  926. Likewise, evaluate Scheme expression @var{exp} in a fresh module with
  927. all the necessary bindings.
  928. @item unload @var{service-name}
  929. Attempt to remove the service identified by @var{service-name}.
  930. @command{shepherd} will first stop the service, if necessary, and then
  931. remove it from the list of registered services. Any services
  932. depending upon @var{service-name} will be stopped as part of this
  933. process.
  934. If @var{service-name} simply does not exist, output a warning and do
  935. nothing. If it exists, but is provided by several services, output a
  936. warning and do nothing. This latter case might occur for instance with
  937. the fictional service @code{web-server}, which might be provided by both
  938. @code{apache} and @code{nginx}. If @var{service-name} is the special
  939. string and @code{all}, attempt to remove all services except for the Shepherd
  940. itself.
  941. @item reload @var{file-name}
  942. Unload all known optional services using unload's @code{all} option,
  943. then load @var{file-name} using @code{load} functionality. If
  944. file-name does not exist or @code{load} encounters an error, you may
  945. end up with no defined services. As these can be reloaded at a later
  946. stage this is not considered a problem. If the @code{unload} stage
  947. fails, @code{reload} will not attempt to load @var{file-name}.
  948. @item daemonize
  949. Fork and go into the background. This should be called before
  950. respawnable services are started, as otherwise we would not get the
  951. @code{SIGCHLD} signals when they terminate.
  952. @item enable-persistency
  953. When terminating, save the list of running services in a file.
  954. @c FIXME-CRITICAL: How can we specify which one?
  955. @item disable-persistency
  956. Don't save the list of running services when terminating.
  957. @end table
  958. @cindex unknown service
  959. @cindex fallback service
  960. The @code{unknown} service must be defined by the user and if it
  961. exists, is used as a fallback whenever we try to invoke an unknown
  962. action of an existing service or use a service that does not exist.
  963. This is useful only in few cases, but enables you to do various sorts
  964. of unusual things.
  965. @c FIXME-CRITICAL: finish
  966. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  967. @node Misc Facilities
  968. @chapter Misc Facilities
  969. This is a list of facilities which are available to code running
  970. inside of the Shepherd and is considered generally useful, but is not directly
  971. related to one of the other topic covered in this manual.
  972. @menu
  973. * Errors:: Signalling, handling and ignoring errors.
  974. * Communication:: Input/Output in various ways.
  975. * Others:: Stuff that is useful, but is homeless.
  976. @end menu
  977. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  978. @node Errors
  979. @section Errors
  980. @cindex assertions
  981. @deffn {macro} assert expr
  982. If @var{expr} yields @code{#f}, display an appropriate error
  983. message and throw an @code{assertion-failed} exception.
  984. @end deffn
  985. @deffn {procedure} caught-error key args
  986. Tell the Shepherd that a @var{key} error with @var{args} has occurred. This is
  987. the simplest way to cause caught error result in uniformly formatted
  988. warning messages. The current implementation is not very good,
  989. though.
  990. @end deffn
  991. @cindex system errors
  992. @deffn {macro} without-system-error expr@dots{}
  993. Evaluates the @var{expr}s, not going further if a system error occurs,
  994. but also doing nothing about it.
  995. @end deffn
  996. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  997. @node Communication
  998. @section Communication
  999. The @code{(shepherd comm)} module provides primitives that allow clients such
  1000. as @command{herd} to connect to @command{shepherd} and send it commands to
  1001. control or change its behavior (@pxref{Slots of services, actions of
  1002. services}).
  1003. @tindex <shepherd-command>
  1004. Currently, clients may only send @dfn{commands}, represented by the
  1005. @code{<shepherd-command>} type. Each command specifies a service it
  1006. applies to, an action name, a list of strings to be used as arguments,
  1007. and a working directory. Commands are instantiated with
  1008. @code{shepherd-command}:
  1009. @deffn {procedure} shepherd-command @var{action} @var{service} @
  1010. [#:@var{arguments} '()] [#:@var{directory} (getcwd)]
  1011. Return a new command (a @code{<shepherd-command>}) object for
  1012. @var{action} on @var{service}.
  1013. @end deffn
  1014. @noindent
  1015. Commands may then be written to or read from a communication channel
  1016. with the following procedures:
  1017. @deffn {procedure} write-command @var{command} @var{port}
  1018. Write @var{command} to @var{port}.
  1019. @end deffn
  1020. @deffn {procedure} read-command @var{port}
  1021. Receive a command from @var{port} and return it.
  1022. @end deffn
  1023. In practice, communication with @command{shepherd} takes place over a
  1024. Unix-domain socket, as discussed earlier (@pxref{Invoking shepherd}).
  1025. Clients may open a connection with the procedure below.
  1026. @deffn {procedure} open-connection [@var{file}]
  1027. Open a connection to the daemon, using the Unix-domain socket at
  1028. @var{file}, and return the socket.
  1029. When @var{file} is omitted, the default socket is used.
  1030. @end deffn
  1031. @cindex output
  1032. The daemon writes output to be logged or passed to the
  1033. currently-connected client using @code{local-output}:
  1034. @deffn {procedure} local-output format-string . args
  1035. This procedure should be used for all output operations in the Shepherd. It
  1036. outputs the @var{args} according to the @var{format-string}, then
  1037. inserts a newline. It writes to whatever is the main output target of
  1038. the Shepherd, which might be multiple at the same time in future versions.
  1039. @end deffn
  1040. @cindex protocol, between @command{shepherd} and its clients
  1041. Under the hood, @code{write-command} and @code{read-command} write/read
  1042. commands as s-expressions (sexps). Each sexp is intelligible and
  1043. specifies a protocol version. The idea is that users can write their
  1044. own clients rather than having to invoke @command{herd}. For instance,
  1045. when you type @command{herd status}, what is sent over the wire is the
  1046. following sexp:
  1047. @lisp
  1048. (shepherd-command
  1049. (version 0)
  1050. (action status) (service root)
  1051. (arguments ()) (directory "/data/src/dmd"))
  1052. @end lisp
  1053. The reply is also an sexp, along these lines:
  1054. @lisp
  1055. (reply (version 0)
  1056. (result (((service @dots{}) @dots{})))
  1057. (error #f) (messages ()))
  1058. @end lisp
  1059. This reply indicates that the @code{status} action was successful,
  1060. because @code{error} is @code{#f}, and gives a list of sexps denoting
  1061. the status of services as its @code{result}. The @code{messages} field
  1062. is a possibly-empty list of strings meant to be displayed as is to the
  1063. user.
  1064. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1065. @node Others
  1066. @section Others
  1067. @cindex hashes
  1068. @deffn {procedure} copy-hashq-table table new-size
  1069. Create a hash-table with size @var{new-size}, and insert all values
  1070. from @var{table} into it, using @code{eq?} when inserting. This
  1071. procedure is mainly used internally, but is a generally useful
  1072. utillity, so it can by used by everyone.
  1073. @end deffn
  1074. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1075. @node Internals
  1076. @chapter Internals
  1077. This chapter contains information about the design and the
  1078. implementation details of the Shepherd for people who want to hack it.
  1079. The GNU@tie{}Shepherd is developed by a group of people in connection
  1080. with @uref{https://www.gnu.org/software/guix/, Guix System}, GNU's
  1081. advanced distribution, but it can be used on other distros as well.
  1082. You're very much welcome to join us! You can report bugs to
  1083. @email{bug-guix@@gnu.org} and send patches or suggestions to
  1084. @email{guix-devel@@gnu.org}.
  1085. @menu
  1086. * Coding standards:: How to properly hack the Shepherd.
  1087. * Design decisions:: Why the Shepherd is what it is.
  1088. * Service Internals:: How services actually work.
  1089. @end menu
  1090. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1091. @node Coding standards
  1092. @section Coding standards
  1093. About formatting: Use common sense and GNU Emacs (which actually is
  1094. the same, of course), and you almost can't get the formatting wrong.
  1095. Formatting should be as in Guile and Guix, basically. @xref{Coding
  1096. Style,,, guix, GNU Guix Reference Manual}, for more info.
  1097. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1098. @node Design decisions
  1099. @section Design decisions
  1100. @quotation Note
  1101. This section was written by Wolfgang Jährling back in 2003 and documents
  1102. the original design of what was then known as GNU@tie{}dmd. The main
  1103. ideas remain valid but some implementation details and goals have
  1104. changed.
  1105. @end quotation
  1106. The general idea of a service manager that uses dependencies, similar
  1107. to those of a Makefile, came from the developers of the GNU Hurd, but
  1108. as few people are satisfied with System V Init, many other people had
  1109. the same idea independently. Nevertheless, the Shepherd was written with the
  1110. goal of becoming a replacement for System V Init on GNU/Hurd, which
  1111. was one of the reasons for choosing the extension language of the GNU
  1112. project, Guile, for implementation (another reason being that it makes
  1113. it just so much easier).
  1114. The runlevel concept (i.e. thinking in @emph{groups} of services) is
  1115. sometimes useful, but often one also wants to operate on single
  1116. services. System V Init makes this hard: While you can start and stop
  1117. a service, @code{init} will not know about it, and use the runlevel
  1118. configuration as its source of information, opening the door for
  1119. inconsistencies (which fortunately are not a practical problem
  1120. usually). In the Shepherd, this was avoided by having a central entity that is
  1121. responsible for starting and stopping the services, which therefore
  1122. knows which services are actually started (if not completely
  1123. improperly used, but that is a requirement which is impossible to
  1124. avoid anyway). While runlevels are not implemented yet, it is clear
  1125. that they will sit on top of the service concept, i.e. runlevels will
  1126. merely be an optional extension that the service concept does not rely
  1127. on. This also makes changes in the runlevel design easier when it may
  1128. become necessary.
  1129. The consequence of having a daemon running that controls the services
  1130. is that we need another program as user interface which communicates
  1131. with the daemon. Fortunately, this makes the commands necessary for
  1132. controlling services pretty short and intuitive, and gives the
  1133. additional bonus of adding some more flexibility. For example, it is
  1134. easiely possible to grant password-protected control over certain
  1135. services to unprivileged users, if desired.
  1136. An essential aspect of the design of the Shepherd (which was already mentioned
  1137. above) is that it should always know exactly what is happening,
  1138. i.e. which services are started and stopped. The alternative would
  1139. have been to not use a daemon, but to save the state on the file
  1140. system, again opening the door for inconsistencies of all sorts.
  1141. Also, we would have to use a separate program for respawning a service
  1142. (which just starts the services, waits until it terminates and then
  1143. starts it again). Killing the program that does the respawning (but
  1144. not the service that is supposed to be respawned) would cause horrible
  1145. confusion. My understanding of ``The Right Thing'' is that this
  1146. conceptionally limited strategy is exactly what we do not want.
  1147. The way dependencies work in the Shepherd took a while to mature, as it was not
  1148. easy to figure out what is appropriate. I decided to not make it too
  1149. sophisticated by trying to guess what the user might want just to
  1150. theoretically fulfill the request we are processing. If something
  1151. goes wrong, it is usually better to tell the user about the problem
  1152. and let her fix it, taking care to make finding solutions or
  1153. workarounds for problems (like a misconfigured service) easy. This
  1154. way, the user is in control of what happens and we can keep the
  1155. implementation simple. To make a long story short, @emph{we don't try
  1156. to be too clever}, which is usually a good idea in developing
  1157. software.
  1158. If you wonder why I was giving a ``misconfigured service'' as an
  1159. example above, consider the following situation, which actually is a
  1160. wonderful example for what was said in the previous paragraph: Service
  1161. X depends on symbol S, which is provided by both A and B. A depends
  1162. on AA, B depends on BB. AA and BB conflict with each other. The
  1163. configuration of A contains an error, which will prevent it from
  1164. starting; no service is running, but we want to start X now. In
  1165. resolving its dependencies, we first try to start A, which will cause
  1166. AA to be started. After this is done, the attempt of starting A
  1167. fails, so we go on to B, but its dependency BB will fail to start
  1168. because it conflicts with the running service AA. So we fail to
  1169. provide S, thus X cannot be started. There are several possibilities
  1170. to deal with this:
  1171. @itemize @bullet
  1172. @item
  1173. When starting A fails, terminate those services which have been
  1174. started in order to fulfill its dependencies (directly and
  1175. indirectly). In case AA was running already, we would not want to
  1176. terminate it. Well, maybe we would, to avoid the conflict with BB.
  1177. But even if we would find out somehow that we need to terminate AA to
  1178. eventually start X, is the user aware of this and wants this to happen
  1179. (assuming AA was running already)? Probably not, she very likely has
  1180. assumed that starting A succeeds and thus terminating AA is not
  1181. necessary. Remember, unrelated (running) services might depend in AA.
  1182. Even if we ignore this issue, this strategy is not only complicated,
  1183. but also far from being perfect: Let's assume starting A succeeds, but
  1184. X also depends on a service Z, which requires BB. In that case, we
  1185. would need to detect in the first place that we should not even try to
  1186. start A, but directly satisfy X's dependency on S with B.
  1187. @item
  1188. We could do it like stated above, but stop AA only if we know we won't
  1189. need it anymore (for resolving further dependencies), and start it
  1190. only when it does not conflict with anything that needs to get
  1191. started. But should we stop it if it conflicts with something that
  1192. @emph{might} get started? (We do not always know for sure what we
  1193. will start, as starting a service might fail and we want to fall back
  1194. to a service that also provides the particular required symbol in that
  1195. case.) I think that either decision will be bad in one case or
  1196. another, even if this solution is already horribly complicated.
  1197. @item
  1198. When we are at it, we could just calculate a desired end-position, and
  1199. try to get there by starting (and stopping!) services, recalculating
  1200. what needs to be done whenever starting a service fails, also marking
  1201. that particular service as unstartable, except if it fails to start
  1202. because a dependency could not be resolved (or maybe even then?).
  1203. This is even more complicated. Instead of implementing this and
  1204. thereby producing code that (a) nobody understands, (b) certainly has
  1205. a lot of bugs, (c) will be unmaintainable and (d) causes users to
  1206. panic because they won't understand what will happen, I decided to do
  1207. the following instead:
  1208. @item
  1209. Just report the error, and let the user fix it (in this case, fix the
  1210. configuration of A) or work around it (in this case, disable A so that
  1211. we won't start AA but directly go on to starting B).
  1212. @end itemize
  1213. I hope you can agree that the latter solution after all is the best
  1214. one, because we can be sure to not do something that the user does not
  1215. want us to do. Software should not run amok. This explanation was
  1216. very long, but I think it was necessary to justify why the Shepherd uses a very
  1217. primitive algorithm to resolve dependencies, despite the fact that it
  1218. could theoretically be a bit more clever in certain situations.
  1219. One might argue that it is possible to ask the user if the planned
  1220. actions are ok with her, and if the plan changes ask again, but
  1221. especially given that services are supposed to usually work, I see few
  1222. reasons to make the source code of the Shepherd more complicated than
  1223. necessary. If you volunteer to write @emph{and} maintain a more
  1224. clever strategy (and volunteer to explain it to everyone who wants to
  1225. understand it), you are welcome to do so, of course@dots{}
  1226. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1227. @node Service Internals
  1228. @section Service Internals
  1229. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1230. @node GNU Free Documentation License
  1231. @appendix GNU Free Documentation License
  1232. @include fdl-1.3.texi
  1233. @c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1234. @node Concept Index
  1235. @unnumbered Concept Index
  1236. @printindex cp
  1237. @node Procedure and Macro Index
  1238. @unnumbered Procedure and Macro Index
  1239. @printindex fn
  1240. @node Variable Index
  1241. @unnumbered Variable Index
  1242. @printindex vr
  1243. @node Type Index
  1244. @unnumbered Type Index
  1245. @printindex tp
  1246. @bye