Wiki:help/howtos/development/developer_information/package_development_start/rc.<package>

rc.<package>

Responsible to start/stop the deamon with the proper parameters, and having the package included in the menu of the web-interface.
An executable Shell script.
The configdata (e.g. <package>.conf) will be created as part of the rc.<package> script, before the deamon is started.

In the build environment located at:

~/freetz-trunk/make/files/root/etc/init.d/rc.<package>

On the router located at:

/mod/etc/init.d/rc.<package>

The /mod/etc/init.d is part of the PATH variable so the rc.<package> scripts can be executed from all locations.

# echo $PATH
/mod/sbin:/mod/bin:/mod/usr/sbin:/mod/usr/bin:/mod/etc/init.d:/sbin:/bin:/usr/sbin:/usr/bin

Executing the rc script with an unrecognized parameter will return the usage information of the specific rc.<package> script. e.g.:

rc.<package> —help Usage: /mod/etc/init.d/rc.<package> [load|unload|start|stop|remove|restart|status]

rc script

To find example just use find on the build environment from ~/freetz-trunk/make/:

find ./*/files/root/etc/init.d/ -name rc.* | grep -v .svn

This gives a list of existing scripts already part of freetz you can look at.

The content of a very generic rc script:

#!/bin/sh

DAEMON=<package>
PID_FILE=/var/run/$DAEMON.pid
. /etc/init.d/modlibrc

start() {
        modlib_startdaemon $DAEMON --config /mod/etc/$DAEMON.conf
        }

case $1 in
     ""|load)
                modreg cgi '<package>' '<package>'
                modreg daemon $DAEMON

                modlib_start $<PACKANG>_ENABLED
                ;;
        unload)
                modunreg daemon $DAEMON
                modunreg cgi '<package>'
                modlib_stop
                ;;
        start)
                modlib_start
                ;;
        stop)
                modlib_stop
                ;;
        restart)
                modlib_restart
                ;;
        status)
                modlib_status
                ;;
        *)
                echo "Usage: $0 [load|unload|start|stop|restart|status]" 1>&2
                exit 1
                ;;
esac

exit 0

The /etc/init.d/modlibrc script will use the variables defined in the part before this script is called.
E.g. DAEMON and PID_FILE as shown in the generic example.

This script firsts sets the PATH variable and LD_LIBRARY_PATH variable.
The PATH defines the paths to locations of binaries that should be executable system wide.
The LD_LIBRARY_PATH defines the locations of (shared) libraries.

PATH variable:

# echo $PATH
/mod/sbin:/mod/bin:/mod/usr/sbin:/mod/usr/bin:/mod/etc/init.d:/sbin:/bin:/usr/sbin:/usr/bin


LD_LIBRARY_PATH variable:

# echo $LD_LIBRARY_PATH
/mod/lib:/mod/usr/lib

rc.<package> (or rc.<packages> load)

At system startup the rc.<package> script is invoked without any parameters. This causes the following to happen:

  1. the /etc/init.d/modlibrc script to run
  2. next the package loaded in the web-interface menu with the modreg cgi '<package>' '<package>' line
  3. modreg daemon $DAEMON validates if /mod/etc/init.d/rc.<package> exist, not sure what happens in the replace function.
  4. If parameter <PACKAGE>_ENABLED is yes (not no) the function start() is called, which should start the package with its configured parameters.

rc.<package> start

if rc.<package> start is executed, and the main case statement for start) contains modlib_start it with execute the modlib_start function of /etc/init.d/modlibrc. If modlib_start is executed without any parameters it will first verify if the daemon is already running using function moblib_check_running, which returns a interger indicating the daemon state. Possible states: : 0 already running, 3 stopped, or 5 inetd If state 0 'already running' is returned, it will just perform: echo "Starting ${DAEMON_LONG_NAME} … already running." If state 3 'stopped' is returned, it will initiate the start function part of the rc.<package> script, and return the exit status of the last command which was executed.

rc.<package> stop

if rc.<package> stop is executed, and the main case statement for stop) contains modlib_stop it with execute the modlib_stop function of /etc/init.d/modlibrc. If modlib_stop is executed it will first verify if the daemon current state using function moblib_check_running, which returns a interger indicating the daemon state. Possible states: : 0 already running, 3 stopped, or 5 inetd If state 0 'already running' is returned, it will execute the following fuctions, if available, from the rc.<pacakge> script: If availabel if will first execute the 'stop_pre' function. Next the 'stop' fuction with parameter '0', followed by the 'stop_post' function. If the stop function returned a 0 exit code, and after the 3 functions the pid released (kill -0 pid) it will execute: echo 'done.', else it will execute: echo 'failed.' with the returned exit code of the stop function. If state 3 'stopped' is returned, it will will just perform: echo 'not running.'

rc.<package> restart

if rc.<package> restart is executed, and the main case statement for restart) contains modlib_restart it with execute the modlib_restart function of /etc/init.d/modlibrc. This fuction will just execute modlib_stop, followed by modlib_start.

rc.<package> status

The rc.<package> status is executed if the web-interface needs to determine if the package is running or stopped. A return code of 1 means stopped, a return code of 0 means running.

verify rc script

If you run into issues with the script you can verify the rc script on the FritzBox with the following command:

sh -x rc.<package> [command]

This will execute each step, and show the intermediate results. This is very helpful to locate an issue.
Perform this for 'start', 'stop', and 'status'

zuletzt geändert vor 4 Jahren Zuletzt geändert am 04.04.2014 22:37:58