NAME
rc.subr
—
daemon control scripts
routines
SYNOPSIS
daemon=path_to_executable |
. |
/etc/rc.d/rc.subr |
rc_cmd |
action |
DESCRIPTION
Apart from a few notable exceptions, rc scripts must follow this naming policy:
- When possible, use the same name as the
daemon
it is referring to. - It must follow ksh(1) variable naming: begin with an alphabetic or underscore character, followed by one or more alphanumeric or underscore characters. Dashes (‘-’) have to be converted to underscores (‘_’).
Every script under /etc/rc.d follows this pattern:
- Define the daemon variable.
- Define service-specific defaults for one or more daemon_* variables (optional).
- Source
rc.subr
, which defines default shell functions and variable values. - Override the pexp variable or any of the
rc_*
functions and set the rc_bg or rc_reload variables, if needed. - Define an
rc_pre
and/orrc_post
function, if needed. - Call the
rc_cmd
function as “rc_cmd $1”.
The following shell functions are defined by
rc.subr
:
rc_cmd
action- Run the action for the current
rc.d
script, based on the settings of various shell variables.rc_cmd
is extremely flexible, and allows fully functionalrc.d
scripts to be implemented in a small amount of shell code. For a given action, if therc_${action}
function is not defined, then a default function is provided byrc.subr
. In addition actions can be disabled by setting the rc_${action} variable to “NO”. For example, if “rc_reload=NO” is set in therc.d
script, andrc_cmd
is called for the reload action, an error will be raised. Similarly, the special variable rc_usercheck must be set to “NO” if thecheck
action requires root privileges.The action argument can be
start
,stop
,reload
,restart
, orcheck
:check
- Call
rc_check
. Return 0 if the daemon is running or 1 if it is not. start
- Check that the service is running by calling
rc_check
. If it's not running, callrc_pre
if it exists, thenrc_start
. stop
- Check that the service is running by calling
rc_check
. If it is running, callrc_stop
and wait up to 30 seconds for the daemon to properly shutdown. If successful, runrc_post
if it exists. restart
- Run the action argument
stop
, then if successful runstart
. reload
- Check that the service is running by calling
rc_check
. If it is running, callrc_reload
.
rc_check
- Search for processes of the service with pgrep(1) using the regular expression given in the pexp variable.
rc_start
- Start the daemon. Defaults to:
${rcexec} "${daemon} ${daemon_flags}"
rc_stop
- Stop the daemon. Send a
SIGTERM
signal using pkill(1) on the regular expression given in the pexp variable. rc_reload
- Send a
SIGHUP
signal using pkill(1) on the regular expression given in the pexp variable. One has to make sure that sendingSIGHUP
to a daemon will have the desired effect, i.e. that it will reload its configuration.
ENVIRONMENT
rc_cmd
uses the following shell variables
to control its behaviour.
- daemon
- The path to the daemon, optionally followed by one or more whitespace
separated arguments. Arguments included here are always used, even if
daemon_flags is empty. This variable is required. It
is an error to source
rc.subr
without defining daemon first. - daemon_class
- Login class to run the daemon with, using
su(1). This is a read only variable that gets set by
rc.subr
itself. It searches login.conf(5) for a login class that has the same name as therc.d
script itself and uses that. If no such login class exists then “daemon” will be used. - daemon_flags
- Arguments to call the daemon with.
- daemon_rtable
- Routing table to run the daemon under, using route(8).
- daemon_timeout
- Maximum time in seconds to wait for the
start
,stop
andreload
actions to return. This is only guaranteed with the defaultrc_start
,rc_stop
andrc_reload
functions. - daemon_user
- User to run the daemon as, using su(1).
- pexp
- A regular expression to be passed to
pgrep(1) in order to find the desired process or to be passed to
pkill(1) to stop it. By default this variable contains the
daemon and daemon_flags
variables. To override the default value, an
rc.d
script has to redefine this variable after sourcingrc.subr
. - rc_bg
- Can be set to
YES
in anrc.d
script to force starting the daemon in background when using the defaultrc_start
. - rc_reload
- Can be set to “NO” in an
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. - rc_usercheck
- Can be set to “NO” in an
rc.d
script, if thecheck
action needs root privileges. - rcexec
- Holds the full su(1) command used to run the daemon. Defaults to:
su -l -c ${daemon_class} -s /bin/sh ${daemon_user} -c
route -T
${daemon_rtable}
All daemon_* variables are set in the following ways:
- Global defaults are provided by
rc.subr
:daemon_class=daemon daemon_flags= daemon_rtable=0 daemon_timeout=30 daemon_user=root
- Service-specific defaults may be provided in the respective
rc.d
script before sourcingrc.subr
, thus overriding the global defaults. - As noted in rc.d(8), site-specific values provided in rc.conf.local(8) for daemon_flags, daemon_rtable, daemon_timeout, and daemon_user will override those defaults.
FILES
- /etc/rc.d/
- Directory containing daemon control scripts.
- /etc/rc.d/rc.subr
- Functions and variables used by
rc.d
scripts. - /usr/ports/infrastructure/templates/rc.template
- A sample
rc.d
script.
SEE ALSO
HISTORY
The rc.subr
framework first appeared in
OpenBSD 4.9.
AUTHORS
The rc.subr
framework was written by
Robert Nagy
<robert@openbsd.org>,
Antoine Jacoutot
<ajacoutot@openbsd.org>,
and Ingo Schwarze
<schwarze@openbsd.org>.