Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 13 Mar 2005 14:55:28 +0200
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        Ola Theander <ola.theander@otsystem.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Confused about connection between an option in rc.conf and the associated action?
Message-ID:  <20050313125528.GA1762@gothmog.gr>
In-Reply-To: <20050313121519.WCWB23781.mxfep02.bredband.com@c0003>
References:  <20050313021330.GA81926@gothmog.gr> <20050313121519.WCWB23781.mxfep02.bredband.com@c0003>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2005-03-13 13:15, Ola Theander <ola.theander@otsystem.com> wrote:
> Hello Giorgos and everybody else
> Thank you for your answers. It clarified things a bit but there is still a
> few things that I don't understand. Say I want to create my own script that
> should run depending on the setting in rc.conf:
>
> - Do I need to parse rc.conf myself in the script or is rc.conf parsed once
> and for all by rcorder (or someone else) and the settings are available e.g.
> as environment variables?

This is done implicitly, by the common code present in ``/etc/rc.subr''.  A
typical rc.d script that uses the rc.subr stuff to do its work will contain
near the beginning a line like:

	. /etc/rc.subr

This pulls in a lot of useful shell code.  In particular, the load_rc_config()
and run_rc_command() shell functions are defined after rc.subr has been pulled
in.  These two functions can then be used as an abstraction around the rc.conf
options, as shown in the final two lines of every /etc/rc.d script:

	load_rc_config "${name}"
	run_rc_command "$1"

The first of these two lines takes care of finding out which options of
rc.conf apply for this particular rc.d script and preparing internal
``rc.subr'' variables that are used to perform the action requested
(start, stop, restart the service associated with the rc.d script, etc).

> - How is my own script found and executed by the rc script? Does the rc
> script execute all *.sh files in the /etc/rc.d/ directory or is some other
> mechanism used?

All the *.sh and all the executable files under ``/etc/rc.d'' are considered
rc scripts and are run by the following part of ``/etc/rc'':

    files=`rcorder ${skip} /etc/rc.d/* 2>/dev/null`

    for _rc_elem in ${files}; do
            run_rc_script ${_rc_elem} ${_boot}
    done

The logic of the tests and what _is_ an rc script is hidden a bit, in the
definition of run_rc_script() within ``/etc/rc.subr''.

> - Must the config string in rc.conf have any correspondence to the actual
> script file using it? E.g. if I add a setting test_enable="YES" my script
> must be named test.sh?

Not necessarily.  The load_rc_config() shell function takes care of setting
the ``rc.conf'' variables that you ask for.  You could call your rc script
``/etc/rc.d/ola'' and then set "$name" in the script itself to something
different:

	% grep name /etc/rc.d/ola
	name='myrc'

Then ``/etc/rc'' would run as a script called ``ola'' but use myrc_XXX="YES"
options from the ``/etc/rc.conf'' file.

> - This last question is somewhat related to the first. Say that I don't have
> to parse rc.conf myself, which variable name to I test in my script for the
> rc.conf setting?

You don't have to explicitly test anything, unless you really need to
(i.e. your rc script depends on at least one other option being set).

> E.g. if I have a config string in rc.conf named test_enable="YES", how do I
> test for this variable's value?

You don't.  At least you don't have to.  You just set $name to the proper
value and let run_rc_command() do the checks for you.

If you find yourself in the need to _do_ some check though, the checkyesno()
shell function of ``/etc/rc.subr'' can help a lot:

	if checkyesno "${test_special}" ;then
		# The special ``test_special'' option is set.
		# Do something about it.
	fi

> Do I use "test_enable" == "YES" or do I use "test" == "YES", i.e. the
> "_enable" part may be stripped by the parser?

The _enable part *is* necessary, AFAIK.

Regards,
Giorgos



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050313125528.GA1762>