History log of /openbsd-current/etc/rc.d/rc.subr
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.162 17-Jan-2024 ajacoutot

Zap trailing space.

from Kirill Miazine, thanks.


# 1.161 15-Dec-2023 jca

Run non-daemons services in a different process group to avoid SIGHUP at boot

12 factors apps and similar don't daemonize and are thus vulnerable to
receiving a SIGHUP signal at the end of /etc/rc. Shield them by running
them in a different process group. Do this only for services that need
rc_bg=Yes, as suggested by ajacoutot@

There have been several reports about this issue in the past years, the
last one being from edd@ who successfully tested this fix. Input from
several folks, ok sthen@ ajacoutot@


Revision tags: OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.160 19-Oct-2022 ajacoutot

Drop support for $rcexec; people should now use the rc_exec function.

prodded by jsg@


Revision tags: OPENBSD_7_2_BASE
# 1.159 08-Sep-2022 ajacoutot

_rc_quirks is only called by _rc_parse_conf, so just merge the two.

ok robert@ sthen@ kn@


# 1.158 02-Sep-2022 ajacoutot

Make rc_configtest behave like rc_pre and rc_post; i.e. don't define a default
function (each rc.d script is supposed to define its own if wanted).
This way, we can filter out the "configtest" action depending on whether the
function exists or not.
Adapt documentation.

tweak/ok kn@


# 1.157 01-Sep-2022 ajacoutot

Add a new action: "configtest", to check configuration syntax of the daemon.
A few adjustments will be done in the next days (like disabling this action if
there's no specific rc_configtest function defined).

e.g.
/etc/rc.d/sshd configtest
rcctl configtest sshd

idea from naddy@


# 1.156 29-Aug-2022 ajacoutot

When using logger(1), also log the message to standard error so we don't
have to check syslog when running in debug mode (`-d').


# 1.155 29-Aug-2022 ajacoutot

Introduce the rc_configtest() function.
By default it just returns "0" but can be overriden by rc.d scripts to check
that the daemon configuration is valid when running "start", "reload" and
"restart".


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.161 15-Dec-2023 jca

Run non-daemons services in a different process group to avoid SIGHUP at boot

12 factors apps and similar don't daemonize and are thus vulnerable to
receiving a SIGHUP signal at the end of /etc/rc. Shield them by running
them in a different process group. Do this only for services that need
rc_bg=Yes, as suggested by ajacoutot@

There have been several reports about this issue in the past years, the
last one being from edd@ who successfully tested this fix. Input from
several folks, ok sthen@ ajacoutot@


Revision tags: OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.160 19-Oct-2022 ajacoutot

Drop support for $rcexec; people should now use the rc_exec function.

prodded by jsg@


Revision tags: OPENBSD_7_2_BASE
# 1.159 08-Sep-2022 ajacoutot

_rc_quirks is only called by _rc_parse_conf, so just merge the two.

ok robert@ sthen@ kn@


# 1.158 02-Sep-2022 ajacoutot

Make rc_configtest behave like rc_pre and rc_post; i.e. don't define a default
function (each rc.d script is supposed to define its own if wanted).
This way, we can filter out the "configtest" action depending on whether the
function exists or not.
Adapt documentation.

tweak/ok kn@


# 1.157 01-Sep-2022 ajacoutot

Add a new action: "configtest", to check configuration syntax of the daemon.
A few adjustments will be done in the next days (like disabling this action if
there's no specific rc_configtest function defined).

e.g.
/etc/rc.d/sshd configtest
rcctl configtest sshd

idea from naddy@


# 1.156 29-Aug-2022 ajacoutot

When using logger(1), also log the message to standard error so we don't
have to check syslog when running in debug mode (`-d').


# 1.155 29-Aug-2022 ajacoutot

Introduce the rc_configtest() function.
By default it just returns "0" but can be overriden by rc.d scripts to check
that the daemon configuration is valid when running "start", "reload" and
"restart".


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.160 19-Oct-2022 ajacoutot

Drop support for $rcexec; people should now use the rc_exec function.

prodded by jsg@


Revision tags: OPENBSD_7_2_BASE
# 1.159 08-Sep-2022 ajacoutot

_rc_quirks is only called by _rc_parse_conf, so just merge the two.

ok robert@ sthen@ kn@


# 1.158 02-Sep-2022 ajacoutot

Make rc_configtest behave like rc_pre and rc_post; i.e. don't define a default
function (each rc.d script is supposed to define its own if wanted).
This way, we can filter out the "configtest" action depending on whether the
function exists or not.
Adapt documentation.

tweak/ok kn@


# 1.157 01-Sep-2022 ajacoutot

Add a new action: "configtest", to check configuration syntax of the daemon.
A few adjustments will be done in the next days (like disabling this action if
there's no specific rc_configtest function defined).

e.g.
/etc/rc.d/sshd configtest
rcctl configtest sshd

idea from naddy@


# 1.156 29-Aug-2022 ajacoutot

When using logger(1), also log the message to standard error so we don't
have to check syslog when running in debug mode (`-d').


# 1.155 29-Aug-2022 ajacoutot

Introduce the rc_configtest() function.
By default it just returns "0" but can be overriden by rc.d scripts to check
that the daemon configuration is valid when running "start", "reload" and
"restart".


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.159 08-Sep-2022 ajacoutot

_rc_quirks is only called by _rc_parse_conf, so just merge the two.

ok robert@ sthen@ kn@


# 1.158 02-Sep-2022 ajacoutot

Make rc_configtest behave like rc_pre and rc_post; i.e. don't define a default
function (each rc.d script is supposed to define its own if wanted).
This way, we can filter out the "configtest" action depending on whether the
function exists or not.
Adapt documentation.

tweak/ok kn@


# 1.157 01-Sep-2022 ajacoutot

Add a new action: "configtest", to check configuration syntax of the daemon.
A few adjustments will be done in the next days (like disabling this action if
there's no specific rc_configtest function defined).

e.g.
/etc/rc.d/sshd configtest
rcctl configtest sshd

idea from naddy@


# 1.156 29-Aug-2022 ajacoutot

When using logger(1), also log the message to standard error so we don't
have to check syslog when running in debug mode (`-d').


# 1.155 29-Aug-2022 ajacoutot

Introduce the rc_configtest() function.
By default it just returns "0" but can be overriden by rc.d scripts to check
that the daemon configuration is valid when running "start", "reload" and
"restart".


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.158 02-Sep-2022 ajacoutot

Make rc_configtest behave like rc_pre and rc_post; i.e. don't define a default
function (each rc.d script is supposed to define its own if wanted).
This way, we can filter out the "configtest" action depending on whether the
function exists or not.
Adapt documentation.

tweak/ok kn@


# 1.157 01-Sep-2022 ajacoutot

Add a new action: "configtest", to check configuration syntax of the daemon.
A few adjustments will be done in the next days (like disabling this action if
there's no specific rc_configtest function defined).

e.g.
/etc/rc.d/sshd configtest
rcctl configtest sshd

idea from naddy@


# 1.156 29-Aug-2022 ajacoutot

When using logger(1), also log the message to standard error so we don't
have to check syslog when running in debug mode (`-d').


# 1.155 29-Aug-2022 ajacoutot

Introduce the rc_configtest() function.
By default it just returns "0" but can be overriden by rc.d scripts to check
that the daemon configuration is valid when running "start", "reload" and
"restart".


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.157 01-Sep-2022 ajacoutot

Add a new action: "configtest", to check configuration syntax of the daemon.
A few adjustments will be done in the next days (like disabling this action if
there's no specific rc_configtest function defined).

e.g.
/etc/rc.d/sshd configtest
rcctl configtest sshd

idea from naddy@


# 1.156 29-Aug-2022 ajacoutot

When using logger(1), also log the message to standard error so we don't
have to check syslog when running in debug mode (`-d').


# 1.155 29-Aug-2022 ajacoutot

Introduce the rc_configtest() function.
By default it just returns "0" but can be overriden by rc.d scripts to check
that the daemon configuration is valid when running "start", "reload" and
"restart".


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.156 29-Aug-2022 ajacoutot

When using logger(1), also log the message to standard error so we don't
have to check syslog when running in debug mode (`-d').


# 1.155 29-Aug-2022 ajacoutot

Introduce the rc_configtest() function.
By default it just returns "0" but can be overriden by rc.d scripts to check
that the daemon configuration is valid when running "start", "reload" and
"restart".


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.154 26-May-2022 ajacoutot

Introduce a new daemon_execdir variable for changing to a specified directory
before running rc_rcexec.

Based on an proposal from openbsd.tech at aisha.cc
ok robert@ abieber@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.153 21-May-2022 ajacoutot

Replace the $rcexec variable by an rc_exec function.
It is much cleaner to the eyes and makes more sense from a functionnal point of
view.
This will allow to extend rc_exec with other functionnalities (like upcoming
rc_startdir).
Bonus point: daemon_logger will now work with manually crafted rc_start
functions.

This will require a mechanical change from ${rcexec} to rc_exec in rc.d scripts.
ports will be fixed right after this commit but we will keep compatibility to
give a chance to people to fix their custom scripts.

positive tests from a few
ok robert@


Revision tags: OPENBSD_7_1_BASE
# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.152 10-Feb-2022 robert

look for the login class in both login.conf and login.conf.d/${class}

ok aja@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.151 15-Nov-2021 ajacoutot

Tidy up; no change.


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.150 14-Nov-2021 ajacoutot

Improve and simplify timer handling in "stop" and "reload".
Rename _rc_wait to _rc_wait_for_start since only "start" uses this now (to
handle rc_bg daemons).
In any situation, none of these actions should be able to hang boot or shutdown.

We should now be able to manage most corner cases out there ("most" because it's
unreasonable to assume we can cope with all the non-default
rc_{start,stop,reload} crazyness).

Several tests have been made and no obvious regression has been found.
But that doesn't mean there isn't; if some behavior changed for the worst, talk
to me.

ok robert@ sthen@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.149 12-Nov-2021 ajacoutot

Consistency in test; no behavior change.


# 1.148 12-Nov-2021 ajacoutot

Set SECONDS to 0 in _rc_wait.
While this is not strictly required, it's a failsafe and more fair to
daemon_timeout as we will effectively wait for the start/stop/reload sequence
instead of the duration of the rc.d script itself.


# 1.147 12-Nov-2021 ajacoutot

Simplify _rc_exit.


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.146 11-Nov-2021 ajacoutot

Run "_rc_wait stop" _before_ stopping the daemon and not after...
This doesn't change the default behavior but fixes rc.d scripts with a home made
rc_stop() function that can block and potentially hangs halt/reboot forever.

ok robert@


# 1.145 11-Nov-2021 ajacoutot

Return 0 in case we reach KILL; this allows "restart" to work as expected
in this situation.

reported by and ok robert@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.144 09-Nov-2021 sthen

check that the rc script including rc.subr is using ksh; rc.subr uses ksh
features and scripts will not work correctly with sh. tweak jca/ok kn/
ok with direction aja


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.143 08-Nov-2021 ajacoutot

rc_reload_signal & rc_stop_signal are not rc.conf(8) variables; they're
only for rc.d scripts.


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.142 07-Nov-2021 ajacoutot

Speed up _rc_wait: only sleep 1/2 second between rc_check tries and make sure
we have at least 1s for SIGTERM to do its job.


# 1.141 07-Nov-2021 ajacoutot

Use built-in SECONDS instead of hand roller timer.

with a tweak from kn@
ok sthen@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.140 06-Nov-2021 ajacoutot

Always run _rc_rm_runfile when process is sent a SIGKILL or rc_post fails (at
this point the process has been terminated).


# 1.139 06-Nov-2021 ajacoutot

Drop uneeded sleep.


# 1.138 06-Nov-2021 ajacoutot

Allow passing a different signal than SIGTERM in the default rc_stop()
function. This will allow to simplify some rc.d script that cook there own
function to gracefully stop a process (e.g. web servers). There are other use
cases as well.
And do the same for rc_reload because it's cheap and can also simplify a
handful of rc.d scripts.

Behave like shutdown and if the process is still not down after daemon_timeout
(configurable; default to 30s), then send a SIGKILL.

While here, factorise pkill invocations into a _rc_sendsig() function that can
potentially be used by rc.d scripts instead of cooking pkill lines; this will
benefit from the configured routing table etc.

tested & ok robert@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.137 31-Oct-2021 ajacoutot

Shuffle things around to have FUNCS_ONLY higher in the script so consummers
source only what they really needs.
Only expose the following which are used by /etc/rc, netstart and rcctl:
_rc_parse_conf
_rc_check_name
_rc_err
_rc_do
_rc_quirk

ok sthen@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.136 27-Feb-2021 ajacoutot

Add logger(1) support for daemons that are logging to stdout/stderr (mostly from
the go ecosystem).
Properly handle failing daemon startup now that we have pipefail.

To take advantage of this new feature, just add foo_logger=facility to the
daemon rc.d(8) script or in rc.conf.local(8) or use rcctl:
rcctl set foo logger daemon.info

tweak for checking flags in rcctl(8) from martijn@
"this looks pretty good" deraadt@
ok sthen@


Revision tags: OPENBSD_6_8_BASE
# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.135 12-Jul-2020 jca

Use su -fl to avoid sourcing /etc/profile / the target user's .profile

This way rc.d and rcctl don't suffer from side effects in people's rc
files. If you somehow used those files to set environment variables,
you should have used login.conf as described in rc.d(8) instead.

ok ajacoutot@


Revision tags: OPENBSD_6_7_BASE
# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.134 29-Apr-2020 ajacoutot

Sort variables and fix a comment.
No functional change.


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.133 22-Feb-2020 ajacoutot

Do not run _rc_parse_conf of /var/run/rc.d/foobar on "start".
This is needed in case a foobar fails to start but still returns 0. Changing its
flags (in rc.conf.local) would then get ignored because of this cache (which is
around to handle stop/check/reload on flags changes).

claudio@ reported this issue when struggling with prometheus several weeks ago


Revision tags: OPENBSD_6_6_BASE
# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.132 07-Sep-2019 ajacoutot

Remove dependency on basename(1).

prodded by deraadt@
ok kn@ deraadt@ tb@


Revision tags: OPENBSD_6_5_BASE
# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.131 21-Mar-2019 ajacoutot

Start in the daemon configured routing table and not in the one we're currently
in. This fixes the case where one would be in a non default rdomain shell then
run an rdomain 0 rc.d daemon.

reported by YASUOKA Masahiko and Pierre Emeriaud
ok sthen@ claudio@ benno@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.130 20-Jan-2019 ajacoutot

The shell will strip the quotes from daemon_flags when starting a daemon so make
sure pexp matches the process (i.e. doesn't include the quotes).
It's a bit hackish but it allows things like these in rc.conf.local:
relayd_flags=-D IPS="1.2.3.4 2.3.4.5"

And we properly end up with...
$ grep ^pexp /var/run/rc.d/relayd
pexp=/usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5
... which matches what is in the process list:
root 14217 <snip> 0:00.01 /usr/sbin/relayd -D IPS=1.2.3.4 1.2.3.5

There's always the possibility that we have introduced a regressions with hand
crafted functions in rc.d scripts (mostly from packags), so watch out.

reported by and debugged with claudio@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.129 29-Oct-2018 ajacoutot

Revert previous (for now).
semarie found a few regressions with daemon that will fail if cwd is not
accessible.


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


# 1.128 16-Oct-2018 ajacoutot

Change the way we call su(1) in rcexec:
- drop `-'l' to prevent simulating a full login and running /root/.profile
(prodded by a mail from J Greely)
- use `-m' to preserve the environment because we now set HOME to "/" like
/etc/rc does; note that we now also clear the environment using `env -i' before
running su(1) which leaves us with only HOME, PATH and SHELL

Committing early to catch regressions fast, if any.
ok halex@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@


Revision tags: OPENBSD_6_2_BASE
# 1.127 05-Jun-2017 ajacoutot

Temporarily disable the second call to rc_check until I figure out what
is going on. Should fix another case of false negative reported by sthen
(redis).


# 1.126 05-Jun-2017 ajacoutot

Fix logic in _rc_wait to properly cope with setproctitle(3) daemons. It was a
regression from my recent rc.subr changes.

reported by deraadt@ and naddy@ : pflogd was marked as failed during boot while
it was properly running


# 1.125 30-May-2017 tb

Introduce a scary rc.conf(8) knob library_aslr=(YES|NO) to turn off the
reordering of libraries by rc(8). This way machines with very slow disk I/O
have a chance of booting within reasonable time now that libcrypto is also
randomized.

Discussed with various;
input & ok from deraadt ajacoutot


# 1.124 28-May-2017 ajacoutot

Move check later to mitigate a possible race.


# 1.123 28-May-2017 ajacoutot

Indent and rename var; no functional change.


# 1.122 28-May-2017 ajacoutot

When a daemon reaches its timeout when starting, display "timeout" instead
of "ok" so the user is warned and has a chance to fix it (most of the time
due to bogus flags). Daemons reaching the timeout without being able to
start are still marked as "failed" (which should also give a clue to the
user that some investigation is needed).

prodded by beck@ a while ago
discussed with and ok sthen@


# 1.121 28-May-2017 ajacoutot

Drop useless lines continuation; no functional change.


# 1.120 27-May-2017 ajacoutot

Ok turns out we still want to keep the rc_bg variable around but we need
to know which daemon cannot background themselves (actually we want to
know the opposite, but there are much more). However, it's only needed in
_rc_wait and rc.subr still does its magic without the need to add `&'.


# 1.119 27-May-2017 ajacoutot

Add an ALRM timer to cope with 2 annoying issues in rc.d(8):
- prevent a daemon from hanging the boot
(typo in your flagsm e.g. httpd_flags=-d)
- make sure we can get the status of a backgrounded daemon instead of always
returning success

Side effect of this is that we can kill a knob! rip rc_bg :-)
Ports will need love, and a second commit is coming for that.

The diff is small yet not trivial so I am committing early in the release
process in one shot so it can easily be reverted if needed. I started working on
this during g2k16 in Cambridge then finished it in Brisbane for a2k17 where
robert@, beck@ and sthen@ agreed it was the correct way to go and I should move
ahead with it post 6.1.

If you see any regression, please talk to me!


Revision tags: OPENBSD_6_1_BASE
# 1.118 17-Feb-2017 ajacoutot

Stop supporting the historical way of starting ypbind(8); that is setting
'ypbind_flags=""' when domainname is set and /var/yp/binding exists.
This can lead to inconsistent behavior at startup since /var may not be
mounted yet and /etc/rc's start_daemon() will not start ypbind.

A.K.A. make ypbind startup consistent with all other OpenBSD daemons.

ok deraadt@


# 1.117 24-Jan-2017 ajacoutot

Whitespace.


# 1.116 07-Sep-2016 ajacoutot

Introduce an _rc_check_name() function to check the input script name so that
we don't end up with cryptic error messages.
Regex help from rpe@
Issue reported by Anthony Coulter in rcctl(8), but better fix the root cause.

Also clarify the mage page that rc.d script name must follow ksh(1) variable
naming.

ok robert@ sthen@


# 1.115 31-Aug-2016 ajacoutot

use tab.


Revision tags: OPENBSD_6_0_BASE
# 1.114 03-May-2016 otto

signal name should be first, fixes reload; ok deraadt@


# 1.113 28-Apr-2016 ajacoutot

Experiment on matching on the daemon_user is over. It needs more work.
portmap isn't happy with it as reported by naddy@


# 1.112 27-Apr-2016 ajacoutot

Revert the revert and match on the daemon_user again.
We'll see if something else breaks but it's the right thing to do.

discussed with robert@


# 1.111 27-Apr-2016 ajacoutot

Revert matching on the daemon user for now; it breaks sndiod handling which
does start as root but does not have a root master process and instead
changed its uid.

reported by deraadt@


# 1.110 26-Apr-2016 ajacoutot

Bump copyright.


# 1.109 26-Apr-2016 ajacoutot

Introduce rtable(4) support to rc.subr(8).
It works by adding daemon_rtable=$id in /etc/rc.conf.local.
rcctl(8) support coming in a few and so are the man pages bits.

If you want multiple instances of the same daemon running in different
routing tables, just symlink the original rc.d script.
e.g.
# cd /etc/rc.d && ln -s dhcpd dhcpd42

Then enable your daemons as such in rc.conf.local:
dhcpd_flags=
dhcpd42_flags=
dhcpd42_rtable=42

most work done by Jiri B <jirib@devio.us>; with a few tweaks and simplifications
by yours truly
ok robert@

... should make henning happy ;-)


# 1.108 26-Apr-2016 ajacoutot

Extend _RC_RUNFILE (/var/run/rc.d/scriptname) content with:
daemon_class, daemon_flags, daemon_timeout, daemon_user, pexp

Add "pexp" as a valid variable to _rc_parse_conf() and sort the list while here.

Besides having an easy way to see how a daemon was started exactly, it opens the
door to rdomain/rtable support in rc.d(8). We need to have as much information
on a daemon as possible if we want to target a specific rtable (using -T) and it
also makes it easier to start multiple instances of the same daemon with similar
flags as a different user...
Of course that only works with rc.d scripts *not* overwritting the default rc_*
functions.

Man page tweaks coming later.

discussed with and ok robert@


# 1.107 26-Mar-2016 ajacoutot

Make it possible to get usage as a non-root user.

ok robert@


# 1.106 26-Mar-2016 ajacoutot

Handle the rc_stop=NO => rc_restart=NO case within _rc_not_supported().


Revision tags: OPENBSD_5_9_BASE
# 1.105 21-Dec-2015 ajacoutot

Revert, rc.d scripts have been fixed.


# 1.104 21-Dec-2015 ajacoutot

Comment the wait for start until I fix the x11 managers rc.d script.


# 1.103 19-Dec-2015 ajacoutot

After starting, unconditionally wait up to $daemon_timeout seconds to check that
the daemon is actually running (instead of only when using rc_bg).

This *may* trigger the following side effect: if a daemon starts then stops
right away (because of a config parsing issue for e.g.), then it will slow down
the boot process because rc will wait a bit before considering the start action
failed. But at least it will allow us to detect those daemons and fix them or
their rc.d(8) script.

At least as far as I can tell, all base daemons should cope with it just fine.

ok sthen@ robert@ on an earlier diff


# 1.102 19-Dec-2015 ajacoutot

Tidy up _rc_wait().

ok sthen@ robert@


# 1.101 05-Dec-2015 mpi

It does not make sense to insert a specific route for 224/4 when the
default one is good enough.

So merge rc.conf(8)'s 'multicast_router' and 'multicast_host' into a
single 'multicast'. If set to YES the reject route for 224/4 is not
inserted by netstart(8).

Manual bits from jmc@

ok henning@, ajacoutot@


# 1.100 16-Oct-2015 ajacoutot

Missing local.

ok schwarze@


Revision tags: OPENBSD_5_8_BASE
# 1.99 15-Jul-2015 ajacoutot

By default, require an exact match of the process name and argument list.
This allows running several instances of the same rc.d(8) script by just
linking it to different name.
e.g.
ln -s ftpproxy ftpproxy6
echo 'ftpproxy6_flags=-6' >>/etc/rc.conf.local

This is likely to break some rc.d scripts in ports. I will try and fix them all
in the next few days but I'd appreciate reports if I missed some.

ok halex@


# 1.98 15-Jul-2015 ajacoutot

Always use the default flags when running !start.
This is necessary so that rc.d scripts launched with `-f' can be properly
stopped, checked and reloaded.

ok schwarze@


# 1.97 15-Jul-2015 ajacoutot

Merge comments.


# 1.96 17-Jun-2015 ajacoutot

Really make daemon_class read-only; it's set to "daemon" of a matching
login class.


# 1.95 02-May-2015 ajacoutot

No more pf_rules ipsec_rules.


# 1.94 29-Apr-2015 ajacoutot

Check arguments before eval so we don't end up with a cryptic error message.
reported by jasper@

While here: _rc_is_supported() -> _rc_not_supported()
- saves a fork
- reduces triple negation to double negation in _rc_not_supported()
- simplifie condition for rc_restart=NO
from schwarze@

ok jasper@ schwarze@


# 1.93 28-Mar-2015 ajacoutot

_rc_err(): only display error message if there's an actual one.
Remove an exit() statement that could never be reached.


Revision tags: OPENBSD_5_7_BASE
# 1.92 02-Jan-2015 ajacoutot

Fix return code of _rc_quirks().
ok robert@


# 1.91 02-Jan-2015 ajacoutot

Add a comment about the default values being duplicated in rcctl(8).
discussed with schwarze@


# 1.90 17-Nov-2014 ajacoutot

"reload" should fail if the service is not running in the first place.

ok robert@ sthen@


# 1.89 30-Aug-2014 ajacoutot

Make it possible to pass a specific error code to _rc_err().

ok robert@ schwarze@


# 1.88 25-Aug-2014 ajacoutot

Put _rc_err argument under quotes to respect the syntax we are passing.


# 1.87 24-Aug-2014 ajacoutot

Fix reversed logic.

ok schwarze@


# 1.86 24-Aug-2014 ajacoutot

Drop uneeded parenthesis in usage().

ok schwarze@


# 1.85 24-Aug-2014 ajacoutot

Drop unused variables.


# 1.84 24-Aug-2014 ajacoutot

In debug mode, make it clear when we are using the default flags when
none are set.

initial patch from me but reworked by schwarze@
ok schwarze@


# 1.83 24-Aug-2014 ajacoutot

In debug mode (`-d'), only print the flags relevant to the rc.d(8) we are
calling instead of all flags which makes it very difficult to see the
information we actually need.

ok schwarze@ robert@


# 1.82 21-Aug-2014 ajacoutot

Drop sanitation, _rc_parse_conf does this for us already.

ok schwarze@


# 1.81 17-Aug-2014 ajacoutot

When running interactively, display the output of the "check" action
(ok or failed) like we do with all other actions.

ok jung@ rpe@


# 1.80 11-Aug-2014 ajacoutot

Typo.

ok kili@


# 1.79 11-Aug-2014 kili

Let _rc_parse_conf stick to parsing /etc/rc.conf and /etc/rc.conf.local
if it's invoked without parameters. It's simpler than adding adding
/etc/rc.conf and /etc/rc.conf.local parameters whereever _rc_parse_conf
is used (e.g. /etc/rc and /etc/netstart).

While here, replace a

for foo in "$@"; do something; done

by

for foo; do something; done

ok aja@


# 1.78 11-Aug-2014 ajacoutot

Make it possible to pass arguments to _rc_parse_conf().

ok robert@


Revision tags: OPENBSD_5_6_BASE
# 1.77 01-Aug-2014 rpe

Don't return, just skip over non existing rc.conf or rc.conf.local file.

OK ajacoutot@ halex@


# 1.76 31-Jul-2014 ajacoutot

In debug mode, properly sort and drop duplicates so that we don't end
up with a confusing output like:
multicast_host >NO<
<...>
multicast_host >YES<

Also properly evaluate values _after_ running _rc_quirks() because these
can modify flags.

ok robert@ halex@


# 1.75 30-Jul-2014 ajacoutot

Fix evil typo (multicast_hosts -> multicast_host).


# 1.74 12-Jul-2014 robert

replace the heavy shell magic in quirks to handle backward compat with simpler
methods


# 1.73 12-Jul-2014 robert

Make rc.conf a parsed configuration file and stop sourcing it as a shell
script.
From now on rc.conf has a fixed syntax (key=val) and it is not allowed
to add anything to it besides the supported syntax, it all going to be
ignored.

discussed with and help from deraadt@ and halex@


# 1.72 09-Jul-2014 ajacoutot

Add a daemon_timeout variable for rc_wait().
It represents the maximum time in seconds to wait for the start, stop
and reload actions to return. Defaults to "30".
No default behavior is changed.

ok beck@ sthen@ jasper@ giovanni@


# 1.71 09-Jul-2014 ajacoutot

White spaces.


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.70 11-Jul-2013 otto

no double negatives in error message; ok aja@


# 1.69 04-Apr-2013 zhuk

Show only available actions in RC script usage messages.
Also, prettify representation of actions list a bit.

a lot of input and ok halex@ aja@
also ok schwarze@
small nit spotted by marc@
"makes a lot of sense to me" todd@


Revision tags: OPENBSD_5_3_BASE
# 1.68 19-Nov-2012 ajacoutot

Remove useless test: rc_rm_runfile() uses rm -f which always returns 0
so we don't care if the file is present or not; also this function
should never return a failure.

ok halex@


# 1.67 18-Nov-2012 ajacoutot

Don't output "$action is not supported" when running an rc.d(8) script
from /etc/rc(8). This prevents the warning to be displayed at shutdown
time when the script has rc_stop=NO.

req. and input from fgsch@

"go ahead" schwarze@
input from an ok halex@, ok robert@


# 1.66 12-Nov-2012 ajacoutot

Don't check if ${local_rcconf} is defined before sourcing rc.conf but do
it unconditionally.
The only place local_rcconf could have been defined is from /etc/rc
sourcing rc.conf but then the variable is not exported so it will never
be seen by rc.subr.

ok robert@


# 1.65 13-Sep-2012 ajacoutot

Simplify the way we call the rc.d(8) script in "restart".

ok sthen@


# 1.64 01-Sep-2012 ajacoutot

Make '-f' only affects the "start" action.
There is no reason we couldn't reload|stop a daemon even when _flags is
set to NO.
As a bonus side effect, we can now see usage() on disabled scripts.

discussed with sthen@ and espie@
ok sthen@


# 1.63 21-Aug-2012 ajacoutot

Use the new "-q" option of pgrep(1) instead of redirecting stdout
to /dev/null.


# 1.62 13-Aug-2012 ajacoutot

unset _rcflags and _rcuser so that they don't get inherited by rc.d(8)
scripts; from robert at peichaer dot org
ok halex@

While here, put the "sanitation" commands in their own block.


# 1.61 13-Aug-2012 ajacoutot

Set the daemon_class as readonly as it should be (and is documented as
such).

from robert at peichaer dot org
modified after a discussion with halex@
ok halex@ robert@


# 1.60 11-Aug-2012 ajacoutot

Make a couple of variables local.

from robert at peichaer dot org
ok halex@


# 1.59 04-Aug-2012 ajacoutot

Add a new rc_usercheck variable (default to YES). When set to no, root
privilege is needed to run rc_check.
In effect this means /etc/rc.d/foobar check can now be run as a regular
user.
Discussed with robert@ during g2k12.

ok todd@


Revision tags: OPENBSD_5_2_BASE
# 1.58 07-Jul-2012 ajacoutot

Simplify rc_wait() (no functionnal change).

initially from robert at peichaer dot org with some enhancement by halex@
ok halex@


# 1.57 30-May-2012 espie

in -d mode, display an error message before exiting, in the case the
daemon is not active (happens often to me, as I forget which box has
which daemons).

okay ajacoutot@


Revision tags: OPENBSD_5_1_BASE
# 1.56 30-Nov-2011 ajacoutot

Fix an issue when uid != euid (e.g. when running shutdown(8) as a
regular user, member of the operator group); rm(1) was waiting for
interactive input to remove the runfiles which made no sense, so just
use `-f'.

issue spotted by weerd@
ok weerd@ robert@


# 1.55 15-Oct-2011 halex

either set or unset variables that are to be examined further on, don't
just leave them untouched

ok ajacoutot@ sthen@ schwarze@


# 1.54 12-Oct-2011 schwarze

Localize _new_pexp unconditionally, or rc_read_runfile()
might pick it up from a polluted environment.
Requested by halex@, ok ajacoutot@ halex@


# 1.53 09-Oct-2011 schwarze

When the _RC_RUNFILE (by some ill chance) happened to be empty or
reading it failed, ${pexp} ended up as the empty string and the script
would send SIGTERM to init(1), which was really inconvenient.
Fix that by never allowing pexp to become empty.
My patch considerably simplified by and ok ajacoutot@.


# 1.52 09-Oct-2011 ajacoutot

Finally make it possible to restart/stop a daemon after having changed
its _flags in rc.conf(8).
When the rc.d(8) system starts a daemon, it will record its pexp under
/var/run/rc.d/rcscriptname and use that to interact with it (errors in
creating /var/run/rc.d or missing pexp file are non fatal, the framework
will just fallback to what it currently does).

deraadt@ doesn't mind a long as it doesn't come in the way of people
manually managing their daemons.

discussed with and input from sthen@ halex@ robert@ schwarze@
ok sthen@ robert@


# 1.51 07-Oct-2011 ajacoutot

Check if we are root right from the start.

ok robert@


# 1.50 07-Oct-2011 ajacoutot

Prepend an underscore to internal variable names; this was reverted by
mistake in the previous commit.
No functionnal change.

ok robert@


# 1.49 25-Sep-2011 halex

revert previous and pass on the RC_DEBUG and RC_FORCE flags in the start+stop
dance on restart rather than exporting them and expose the script to a
potentially polluted environment

ok ajacoutot@ robert@


# 1.48 24-Sep-2011 ajacoutot

Repair "restart" debug and force modes (we need to export RC_DEBUG and
RC_FORCE since "restart" calls "stop" then "start").
While here, append "_" to these variable names to make it clear they are
internal.

ok robert@


# 1.47 22-Sep-2011 ajacoutot

Add missing "*)" case statement and make sure RC_DEBUG and RC_FORCE are
unset beforehands; prodded by halex@

Add rc_usage().

ok halex@


# 1.46 19-Sep-2011 robert

Use arugments for rc script instead of environment variables to indicate
debug mode.
This commit also adds a force mode for the script to allow one to force
operations even if daemon_flags is set to NO.
In this case the sane default values will be used for each script.

e.g.: "/etc/rc.d/foo -f start" will start sshd even if it's disabled

ok ajacoutot@


Revision tags: OPENBSD_5_0_BASE
# 1.45 31-Jul-2011 robert

when reloading do an rc_wait instead of just an rc_check
from david@, okay sthen@ and input from schwarze@


# 1.44 31-Jul-2011 robert

redirect rc_err() output to stderr, from david@


# 1.43 23-Jul-2011 sthen

Print the daemon name before starting, and ok/failed afterwards, providing
better visual cues as to what's happening. Feedback/ok aja@ schwarze@


# 1.42 08-Jul-2011 robert

add license, ok ajacoutot@, deraadt@


# 1.41 07-Jul-2011 ajacoutot

Fix reload:
* if we try to reload a non running daemon, just exit gracefully, like
we do for 'stop'
* add an rc_check call right after rc_reload to make sure the daemon is
still around; until now we only checked the pkill(1) return code but
that is not enough

ok robert@


# 1.40 06-Jul-2011 robert

Add rc.d(8) script for the system daemons that are restartable.
From now on rc(8) is going to call these scripts to start them up on boot
in the same order than before.
In addition the inetd and rwhod variables in rc.conf are deprecated so that
inetd_flags and rwhod_flags should be used. The old flags are still going
to be used for some time to allow users to switch.
There are more rc modifications to come later so let's put this in so
we can base more work on this.
It is important to mention that you can still keep using rc.local just
like the way you did before, and we have no intention to remove that either.

I'd also like to thank ajacoutot@, halex@, sthen@ and schwarze@ for working
on this with me.


# 1.39 03-Jul-2011 schwarze

let reload report (ok) or (failed) just like all other actions;
ajacoutot@ and robert@ couldn't really remember the reason to be different,
either


# 1.38 20-Jun-2011 schwarze

Refactoring for simplicity, no functional change:
* Instead of nesting subshells, perform a linear series of operations
and bail out as soon as one of them fails.
* Rename rc_print to rc_exit, let it calculate the exit code itself
and let it exit, considerably simplifying error handling; new name
suggested by sthen@.
OK ajacoutot@ sthen@


# 1.37 10-Jun-2011 ajacoutot

Finally deal with background processes: "rc_cmd start" will now return
the correct code according whether the daemon did start successfully or
not.

rc_wait()
This function has been extended, first we need to pass in which mode we
are running (start or stop) and second we can pass a number of seconds
to wait (optionnal, will default to 30s).
The function will return the correct code whether we are running during
"rc_cmd start" or "rc_cmd stop".

rc_cmd() start
If we are running in background mode, then we call rc_wait with the
"start" argument.
The sleep(1) is needed to prevent a race condition where the process
will appear in the list before failing and rc_check will see it as
running. Call rc_post() when failing to prevent being left in an
inconsistent state (because rc_pre() would have run successfully)

rc_cmd() stop
We are now calling rc_wait with the "stop" argument.

"looks good" sthen@, ok robert@


# 1.36 19-May-2011 ajacoutot

Setting RC_DEBUG is enough, it does not need to be explicitely set to
"1".

hint from MERIGHI Marcus
ok robert@


# 1.35 09-Apr-2011 schwarze

Fix multiple issues and make the code simpler and more robust:
- As noticed by ajacoutot@, re_format(7) does not treat '\t' as a tab,
so some t's got stripped from daemon names.
- The tr(1) failed to sanitize mixtures of blanks and tabs.
ok ajacoutot@


# 1.34 06-Apr-2011 robert

- in case reload fails, print the failed message (from Piotr Sikora)
- (ok) is only printed while in interactive mode, but (failed) stays
even for rc if the command fails

ok ajacoutot@


# 1.33 06-Apr-2011 ajacoutot

Remove leading/trailing spaces from daemon_flags.

ok schwarze@


# 1.32 05-Apr-2011 guenther

Handle a daemon_flags of '-n' by using printf '%s\n' instead of echo
ok otto@, aja@


# 1.31 05-Apr-2011 ajacoutot

Quote $daemon_flags to prevent echo(1) picking them as input flags.

from Piotr Sikora
ok robert@


# 1.30 25-Mar-2011 ajacoutot

Bump timeout to 30s.

ok robert@


# 1.29 24-Mar-2011 robert

if daemon_flags=NO is set, just exit
ok ajacoutot@


# 1.28 19-Mar-2011 ian

Since we now use pkill -0 for "check", it must be run as root,
so remove that test condition. Commented by schwarze@, OK ajacoutot@


# 1.27 17-Mar-2011 robert

- introduce the INRC environment variable so that rc.subr(8) knows if it
gets called from rc.local or rc.shutdown
- notify the user if a given operation was successfull or not by appending
the (ok) or (failed) strings to the end of the daemon name
- hide stdout and stdin unless RC_DEBUG=1 is set, otherwise all the function
names will be printed out and all output sent to stdin or stdout
- since from now on rc.subr is taking care of printing out the daemon names
on startup, we don't need to do this from rc.{local,shutdown} anymore

brainkilling work done by me and ajacoutot@, ok ajacoutot@


# 1.26 14-Mar-2011 ajacoutot

Return proper codes so that we don't rc_start if rc_pre failed and we
don't rc_post if rc_stop failed.

"I agree with the direction" sthen@
ok robert@


# 1.25 10-Mar-2011 ajacoutot

Add a small comment at the top to explain what this file does.
Use $() constructs as it's safer than ``.
Both req. by deraadt@

Use pkill instead of pgrep in rc_check so that we don't need to fiddle
with redirections in reload.
prodded by schwarze@

ok robert@


# 1.24 09-Mar-2011 ajacoutot

Make reload and check consistent with start/stop.

ok robert@


# 1.23 09-Mar-2011 ajacoutot

Move the redirections into the rc_cmd itself instead of rc_start and
rc_stop so scripts don't need to set it.

ok sthen@ (on a much bigger diff) robert@


# 1.22 09-Mar-2011 ajacoutot

Move rc_pre and rc_post out of the rc_start/rc_stop functions into the
rc_cmd start/stop actions. This way when rc.d(8) scripts override these
functions, we don't loose rc_{pre,post}.

Add a max 5 secs loop after rc_stop in the rc_cmd top action. This seems
to be a good default for returning to command line only after the daemon
has really stopped. This fixes "restart" for some daemons and allows to
properly stop some others at shutdown time.
Note that this is just a best-effort default, some daemons may need a
lot more time to shutdown but this case is usually handled in the
rc.d(8) script itself and we obviously do not want to hang the shutdown
process.

Call rc_cmd start/stop in restart and _not_ rc_start/rc_stop which can
get overriden in a script.

discussed with and inputs from sthen@ and schwarze@
ok sthen@ robert@


# 1.21 06-Mar-2011 ajacoutot

Add an "rc_bg" variable that is undefined by default and allows to start
a daemon in the background.

man page tweaks from schwarze@
discussed with an ok schwarze@ robert@


Revision tags: OPENBSD_4_9_BASE
# 1.20 24-Feb-2011 ajacoutot

Remove daemon_shell and force the use of /bin/sh for the su(1) call.

ok robert@ espie@ miod@


# 1.19 05-Jan-2011 ajacoutot

Put the "su -c ..." command into an rcexec variable so that we can
easily use it in rc scripts instead of copying/pasting the same huge
command lines everywhere.

ok robert@ sthen@


# 1.18 04-Jan-2011 ajacoutot

Check for a login class that match the rc script name and if we find one
let's use it. If not, then fall back to using daemon.
While here, do some reordering.

"I like this" sthen@, ok robert@


# 1.17 29-Dec-2010 ajacoutot

After the recent changes, there is no need for an rc_conf function.
As schwarze@ also stated, this will prevent anyone from calling it
(which would be an error) and makes it clearer that rc.subr is not just
a functions container.

No functionnal change, unless you were calling rc_conf from an rc script
which would have been wrong anyway.

ok schwarze@ robert@


# 1.16 27-Dec-2010 ajacoutot

There is no point in calling rc_conf manually from each and every rc
script; instead, rc.subr can call rc_conf itself right after reading in
rc.conf.

from schwarze@ (committing on his behalf)
ok robert@


# 1.15 24-Dec-2010 ajacoutot

Fix a flaw in the rc.subr framework reported by stephan@ where
local_rcconf would get overwritten by flags from the rc script itself.

Based on an original idea and diff from robert@, create an rc_conf
function that we add in the rc_script after the defaults daemon*
variables.
This way we can use defaults variables names in other part of the script
(when defining a specific pexp for instance).

While here, simplify setting up the default daemon variables so that we
don't need to do any substitution in rc_start.

rc scripts must include daemon variables before calling rc_conf. All
other locally modified variables (pexp, rc_reload, local additions...)
must come after.

feedback from and works for sthen@
ok robert@


# 1.14 13-Dec-2010 ajacoutot

Prefer '-l' over '-' which is deprecated.

Also noticed and agreed by eric@
ok robert@


# 1.13 13-Dec-2010 robert

simulate a full login with su, now that the environment is fine after
the last su(1) commit


# 1.12 11-Dec-2010 ajacoutot

Make it possible to mark an rc action as unsupported by setting the
corresponding variable to NO.
e.g.
rc_reload=NO

This will save us the trouble of copying the same rc_reload() function
(or others) all over the tree.

ok robert@


# 1.11 11-Dec-2010 ajacoutot

Sanitise daemon_flags: remove multiple whitespaces from _rcflags.

requested by okan@, ok robert@


# 1.10 11-Dec-2010 robert

Do not overwrite the settings in the rc scripts itself, so use the same
method to handle _class, _user and _shell as we do for _flags.

discussed with and ok ajacoutot@


# 1.9 10-Dec-2010 robert

Add support for setting _user _class and _shell for a specific daemon,
and use su to start the daemons.
By default if there is no class defined, daemon will be used. For a shell
it defaults to /bin/sh.


# 1.8 28-Oct-2010 robert

remove my debug printf


# 1.7 28-Oct-2010 robert

After discussing with Ingo and Antoine we decided to rename
files to not include -. This way we can easily overwrite
daemon flags from rc.conf.local.


# 1.6 28-Oct-2010 robert

add $


# 1.5 27-Oct-2010 ajacoutot

Protect variable names, to be consistent with rc(8).

ok robert@


# 1.4 27-Oct-2010 robert

Remove 'status', because 'check' is what is used internally and we don't
need two functions doing the same thing, and also allow check to run as
non-root.


# 1.3 27-Oct-2010 robert

Missing redirection to /dev/null in rc_start


# 1.2 27-Oct-2010 robert

quote $local_rcconf in [ ] and remove eval from rc_start()


# 1.1 26-Oct-2010 robert

Add a simple 'rc' system to base in order to start/stop/restart/reload
services installed by the ports system (for now).
It only uses pgrep/pkill to handle these processes. A manual page will
come later.

'put it in' deraadt@