summaryrefslogtreecommitdiff
path: root/share/man/man8/rc.subr.8
blob: ca2590bf19c6e015d69728e852242f932de1889b (plain)
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
.\" 	$OpenBSD: rc.subr.8,v 1.9 2011/03/05 12:56:33 schwarze 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: March 5 2011 $
.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
.Va dashes
have to be converted to
.Va underscores .
.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
or
.Va rc_reload
variables
or any of the
.Ic rc_*
functions, 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_restart
.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.
.Pp
The
.Ar action
argument can be
.Cm start ,
.Cm stop ,
.Cm reload ,
.Cm restart ,
or
.Cm check ,
each running one of the functions described below.
.It Ic rc_start
Check whether the service is already running by calling
.Ar rc_check ,
If it's not running, call
.Fn rc_pre
if it exists, then start the daemon.
.It Ic rc_stop
Check that the service is running by calling
.Ar rc_check .
If it is running, stop the daemon,
then call
.Fn rc_post
if it exists.
.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,
that is, that it will reload its configuration.
.It Ic rc_restart
Perform a
.Cm stop
then a
.Cm start .
.It Ic rc_check
Return 0 if the daemon is running or 1 if it is not.
.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.
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 daemon will be used.
.It Va pexp
A regular expression to be passed to
.Xr pgrep 1
or
.Xr pkill 1
in order to find the desired process.
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_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 the
.Dv SIGHUP
signal to reload its configuration.
The same is possible, but almost never useful, for other actions.
.It Va rcexec
Holds the full
.Xr su 1
arguments used to run the daemon.
Defaults to:
.Va 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 .