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