xref: /openbsd-src/share/man/man8/rc.subr.8 (revision 78fec973f57e9fc9edd564490c79661460ad807b)
1.\" 	$OpenBSD: rc.subr.8,v 1.45 2022/05/27 12:27:39 ajacoutot Exp $
2.\"
3.\" Copyright (c) 2021 Antoine Jacoutot
4.\" Copyright (c) 2011 Robert Nagy, Antoine Jacoutot, Ingo Schwarze
5.\" All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\"
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20.\" IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.Dd $Mdocdate: May 27 2022 $
29.Dt RC.SUBR 8
30.Os
31.Sh NAME
32.Nm rc.subr
33.Nd daemon control scripts routines
34.Sh SYNOPSIS
35.Nm daemon Ns = Ns Ar path_to_executable
36.Nm .\&
37.Pa /etc/rc.d/rc.subr
38.Nm rc_cmd
39.Ar action
40.Sh DESCRIPTION
41Apart from a few notable exceptions, rc scripts must follow this
42naming policy:
43.Pp
44.Bl -enum -compact
45.It
46When possible, use the same name as the
47.Nm daemon
48it is referring to.
49.It
50It must follow
51.Xr ksh 1
52variable naming: begin with an alphabetic or underscore character, followed by
53one or more alphanumeric or underscore characters.
54Dashes
55.Pq Sq -
56have to be converted to
57underscores
58.Pq Sq _ .
59.El
60.Pp
61Every script under
62.Pa /etc/rc.d
63follows this pattern:
64.Pp
65.Bl -enum -compact
66.It
67Define the
68.Va daemon
69variable.
70.It
71Define service-specific defaults for one or more
72.Va daemon_*
73variables (optional).
74.It
75Source
76.Nm ,
77which defines default shell functions and variable values.
78.It
79Override the
80.Va pexp
81variable or any of the
82.Ic rc_*
83functions and set the
84.Va rc_bg
85or
86.Va rc_reload
87variables, if needed.
88.It
89Define an
90.Ic rc_pre
91and/or
92.Ic rc_post
93function, if needed.
94.It
95Call the
96.Ic rc_cmd
97function as
98.Dq "rc_cmd $1" .
99.El
100.Pp
101The following shell functions are defined by
102.Nm :
103.Bl -tag -width rc_reload
104.It Ic rc_check
105Search for processes of the service with
106.Xr pgrep 1
107using the regular expression given in the
108.Va pexp
109variable.
110.It Ic rc_cmd Ar action
111Run the
112.Ar action
113for the current
114.Nm rc.d
115script, based on the settings of various shell variables.
116.Ic rc_cmd
117is extremely flexible, and allows fully functional
118.Nm rc.d
119scripts to be implemented in a small amount of shell code.
120For a given
121.Ar action ,
122if the
123.Ic rc_${action}
124function is not defined, then a default function is provided by
125.Nm rc.subr .
126In addition actions can be disabled by setting the
127.Va rc_${action}
128variable to
129.Dq NO .
130For example, if
131.Dq rc_reload=NO
132is set in the
133.Nm rc.d
134script, and
135.Ic rc_cmd
136is called for the reload action, an error will be raised.
137Similarly, the special variable
138.Va rc_usercheck
139must be set to
140.Dq NO
141if the
142.Cm check
143.Ar action
144requires root privileges.
145.Pp
146The
147.Ar action
148argument can be
149.Cm start ,
150.Cm stop ,
151.Cm reload ,
152.Cm restart ,
153or
154.Cm check :
155.Bl -tag -width restart
156.It Ic check
157Call
158.Ic rc_check .
159Return 0 if the daemon is running or 1 if it is not.
160.It Ic start
161Check that the service is running by calling
162.Ic rc_check .
163If it's not running, call
164.Ic rc_pre
165if it exists, then
166.Ic rc_start .
167.It Ic stop
168Check that the service is running by calling
169.Ic rc_check .
170If it is running,
171call
172.Ic rc_stop
173and wait up to 30 seconds for the daemon to properly shutdown.
174If successful, run
175.Ic rc_post
176if it exists.
177.It Ic restart
178Run the
179.Ar action
180argument
181.Cm stop ,
182then if successful run
183.Cm start .
184.It Ic reload
185Check that the service is running by calling
186.Ic rc_check .
187If it is running,
188call
189.Ic rc_reload .
190.El
191.It Ic rc_exec
192Execute argument using
193.Xr su 1
194according to
195.Va daemon_class ,
196.Va daemon_execdir ,
197.Va daemon_user ,
198.Va daemon_rtable
199and
200.Va daemon_logger
201values.
202.It Ic rc_reload
203Send the
204.Va rc_reload_signal
205using
206.Xr pkill 1
207on the regular expression given in the
208.Va pexp
209variable.
210One has to make sure that sending
211.Dv SIGHUP
212to a daemon will have the desired effect,
213i.e. that it will reload its configuration.
214.It Ic rc_start
215Start the daemon.
216Defaults to:
217.Bd -literal -offset indent
218rc_exec "${daemon} ${daemon_flags}"
219.Ed
220.It Ic rc_stop
221Stop the daemon.
222Send the
223.Va rc_stop_signal
224using
225.Xr pkill 1
226on the regular expression given in the
227.Va pexp
228variable.
229.El
230.Sh ENVIRONMENT
231.Ic rc_cmd
232uses the following shell variables to control its behaviour.
233.Bl -tag -width "daemon_timeout"
234.It Va daemon
235The path to the daemon, optionally followed by one or more
236whitespace separated arguments.
237Arguments included here are always used, even if
238.Va daemon_flags
239is empty.
240This variable is required.
241It is an error to source
242.Nm
243without defining
244.Va daemon
245first.
246.It Va daemon_class
247Login class to run the daemon with, using
248.Xr su 1 .
249This is a read only variable that gets set by
250.Nm rc.subr
251itself.
252It searches
253.Xr login.conf 5
254for a login class that has the same name as the
255.Nm rc.d
256script itself and uses that.
257If no such login class exists then
258.Dq daemon
259will be used.
260.It Va daemon_execdir
261Change to this directory before running
262.Ic rc_exec .
263.It Va daemon_flags
264Arguments to call the daemon with.
265.It Ar daemon_logger
266Redirect standard output and error to
267.Xr logger 1
268using the configured priority (e.g. "daemon.info").
269.It Va daemon_rtable
270Routing table to run the daemon under, using
271.Xr route 8 .
272.It Va daemon_timeout
273Maximum time in seconds to wait for the
274.Cm start ,
275.Cm stop
276and
277.Cm reload
278actions to return.
279This is only guaranteed with the default
280.Ic rc_start ,
281.Ic rc_stop
282and
283.Ic rc_reload
284functions.
285.It Va daemon_user
286User to run the daemon as, using
287.Xr su 1 .
288.It Va pexp
289A regular expression to be passed to
290.Xr pgrep 1
291in order to find the desired process or to be passed to
292.Xr pkill 1
293to stop it.
294By default this variable contains the
295.Va daemon
296and
297.Va daemon_flags
298variables.
299To override the default value, an
300.Nm rc.d
301script has to redefine this variable
302.Em after
303sourcing
304.Nm .
305.It Va rc_bg
306Can be set to
307.Cm YES
308in an
309.Nm rc.d
310script to force starting the daemon in background when using the default
311.Ic rc_start .
312.It Va rc_reload
313Can be set to
314.Dq NO
315in an
316.Nm rc.d
317script to disable the reload action if the respective daemon
318does not support reloading its configuration.
319.Em The same is possible, but almost never useful, for other actions.
320.It Va rc_reload_signal
321Signal sent to the daemon process
322.Pq Va pexp
323by the default
324.Fn rc_reload
325function.
326Default to
327.Em HUP .
328.It Va rc_stop_signal
329Signal sent to the daemon process
330.Pq Va pexp
331by the default
332.Fn rc_stop
333function.
334Default to
335.Em TERM .
336.It Va rc_usercheck
337Can be set to
338.Dq NO
339in an
340.Nm rc.d
341script, if the
342.Cm check
343action needs root privileges.
344.El
345.Pp
346All
347.Va daemon_*
348variables are set in the following ways:
349.Bl -enum
350.It
351Global defaults are provided by
352.Nm :
353.Bd -literal -offset indent
354daemon_class=daemon
355daemon_execdir=
356daemon_flags=
357daemon_logger=
358daemon_rtable=0
359daemon_timeout=30
360daemon_user=root
361.Ed
362.It
363Service-specific defaults may be provided in the respective
364.Nm rc.d
365script
366.Em before
367sourcing
368.Nm ,
369thus overriding the global defaults.
370.It
371As noted in
372.Xr rc.d 8 ,
373site-specific values provided in
374.Xr rc.conf.local 8
375for
376.Va daemon_execdir ,
377.Va daemon_flags ,
378.Va daemon_logger ,
379.Va daemon_rtable ,
380.Va daemon_timeout ,
381and
382.Va daemon_user
383will override those defaults.
384.El
385.Sh FILES
386.Bl -tag -width Ds
387.It Pa /etc/rc.d/
388Directory containing daemon control scripts.
389.It Pa /etc/rc.d/rc.subr
390Functions and variables used by
391.Nm rc.d
392scripts.
393.It Pa /usr/ports/infrastructure/templates/rc.template
394A sample
395.Nm rc.d
396script.
397.El
398.Sh SEE ALSO
399.Xr rc 8 ,
400.Xr rc.conf 8 ,
401.Xr rc.d 8
402.Sh HISTORY
403The
404.Nm
405framework
406first appeared in
407.Ox 4.9 .
408.Sh AUTHORS
409.An -nosplit
410The
411.Nm
412framework was written by
413.An Robert Nagy Aq Mt robert@openbsd.org ,
414.An Antoine Jacoutot Aq Mt ajacoutot@openbsd.org ,
415and
416.An Ingo Schwarze Aq Mt schwarze@openbsd.org .
417