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