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
|
.\" $OpenBSD: rc.subr.8,v 1.4 2011/01/16 14:59:19 jmc Exp $
.\"
.\" Copyright (c) 2010 Robert Nagy and Antoine Jacoutot
.\" 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 AUTHOR ``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 AUTHOR 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: January 16 2011 $
.Dt RC.SUBR 8
.Os
.Sh NAME
.Nm rc.subr ,
.Nm rc.d
.Nd functions used by rc.d scripts
.Sh SYNOPSIS
.Nm .\&
.Pa /etc/rc.d/rc.subr
.Nm rc_cmd
.Ar argument
.Sh DESCRIPTION
The
.Nm
script contains commonly used shell script functions and variable
definitions which are used by various
.Nm rc.d
scripts.
.Pp
The
.Nm
functions are accessed by sourcing
.Pa /etc/rc.d/rc.subr
into the current shell.
.Pp
The following shell functions are available:
.Bl -tag -width 4n
.It Ic rc_cmd Ar argument
Run the
.Ar argument
method 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 method
.Ar argument ,
if the
.Ar rc_${argument}()
function is not defined, then a default method is provided by
.Nm rc.subr .
In addition methods 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 with the reload argument, an error will be raised.
.Pp
.Ar argument
is searched for in the list of supported commands, which may be one
of:
.Bl -tag -width ".Cm restart" -offset indent
.It Cm start
Start the service.
It checks if the service is already running by calling
.Ar rc_check
and refuses to start if it is.
In addition if the
.Fn rc_pre
function exists, it is called before
starting the daemon.
.It Cm stop
It checks that the service is running by calling
.Ar rc_check
and if it's not, no action is taken.
In addition if the
.Fn rc_post
function exists, it is called right
after the daemon has been stopped.
.It Cm reload
Find the process using
.Xr pkill 1
with the regular expression as specified in the
.Ar pexp
variable and send a
.Dv SIGHUP
signal.
One has to make sure that sending
.Dv SIGHUP
to a daemon will have the desired effect,
which is reloading the configuration files.
.It Cm restart
Perform a
.Cm stop
then a
.Cm start .
.It Cm check
Return 0 if the daemon is running or 1 if it is not.
.El
.El
.Pp
.Ic rc_cmd
uses the following shell variables to control its behaviour.
.Bl -tag -width ".Va procname" -offset indent
.It Va daemon
The path to the daemon.
.It Va daemon_flags
Arguments to call
.Va daemon
with.
This is usually set in
.Xr rc.conf 8 ,
and not in the
.Nm rc.d
script except when there is a sane default value specified.
.Ar ${name}_flags
can be used in
.Xr rc.conf 8
to override this.
.It Va daemon_user
User to run
.Va daemon
as, using
.Xr su 1 .
This defaults to root unless specified by the
.Ar daemon_user
variable in the
.Nm rc.d
script or the
.Ar ${name}_user
variable in
.Xr rc.conf 8 .
.It Va daemon_class
Login class to run the
.Va 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 daemon_shell
Shell to run the
.Va daemon
in, using
.Xr su 1 .
This defaults to
.Pa /bin/sh
unless specified by the
.Ar daemon_shell
variable in the
.Nm rc.d
script or the
.Ar ${name}_shell
variable in
.Xr rc.conf 8 .
.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.
.El
.Sh FILES
.Bl -tag -width ".Pa /etc/rc.d/rc.subr" -compact
.It Pa /etc/rc.d/rc.subr
The
.Nm
file resides in
.Pa /etc/rc.d .
.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
.Sh HISTORY
The
.Nm
script
first appeared in
.Ox 4.9 .
.Sh AUTHORS
.An -nosplit
The
.Nm rc.subr
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 .
|