LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File rng-tools.init of Package rng-tools (Project DISCONTINUED:openSUSE:11.1)

#! /bin/sh

### BEGIN INIT INFO
# Provides:          rng-tools
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 5
# Default-Stop:
# Short-Description: Support for hardware Random Number Generators (RNGs)
# Description:       Loads RNG-modules specific to the hardware (if not automatically
#                    loaded by udev accoding to the PCI id) and starts the userspace
#                    daemon that passes the RNG data from /dev/hwrng to /dev/random
# X-UnitedLinux-Default-Enabled: yes
### END INIT INFO

RNGD_BIN=/sbin/rngd
HWRNG_DEV=/dev/hwrng
MODPROBE=/sbin/modprobe
RNGD_PARAMS="-r $HWRNG_DEV"

# Source configuration file.  This should define the shell variable smartd_opts
    
RETVAL=0

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset


##
# Detect if HW random support is currently available (i.e. modules that provide
# a HW RNG are loaded and the device file is there)
#
# Parameters:   none
# Return value: 0 if a RNG is avilable, != 0 otherwise
##
have_rng()
{
    [ -f /sys/devices/virtual/misc/hw_random/rng_current ] && [ -r "$HWRNG_DEV" ]
}

##
# Loads HW-RNG modules if the hardware looks like it has a HW-RNG. Checks if we
# already have HW-RNG modules before. Waits (max. 5 sec) until udev generates
# the device.
#
# Parameters:   none
# Return value: none
##
load_rng_modules()
{
    loaded=0

    if ! have_rng ; then

        # VIA CPUs have a HW-RNG built-in
        if grep "model name.*VIA" /proc/cpuinfo &> /dev/null ; then
            $MODPROBE via-rng &> /dev/null && loaded=1
        fi

        # add other Hardware here in future
        # ...

    fi

    # if a module was loaded until udev generates the device, max 5 seconds
    if [ $loaded -gt 0 ] ; then
        loopcount=0
        while [ ! -r $HWRNG_DEV -a $loopcount -lt 50 ] ; do
            usleep 100000
            loopcount=$[$loopcount + 1]
        done
    fi
}

##
# Unloads HW-RNG that are loaded by load_rng_modules(). So this function doesn't unload
# modules that get loaded by their PCI id.
#
# Parameters:   none
# Return value: none
##
unload_rng_modules()
{
    if have_rng ; then

        # VIA CPUs have a HW-RNG built-in
        if grep "model name.*VIA" /proc/cpuinfo &> /dev/null ; then
            $MODPROBE -r via-rng &> /dev/null
        fi

        # add other Hardware here in future
        # ...

    fi
}


# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.
case "$1" in
    start)
        echo -n "Starting RNG daemon "
        ## Start daemon with startproc(8). If this fails
        ## the echo return value is set appropriate.
        load_rng_modules

        if have_rng ; then
        
            # startproc should return 0, even if service is
            # already running to match LSB spec.
            startproc $RNGD_BIN $RNGD_PARAMS

            # Remember status and be verbose
            rc_status -v

        else

            echo -n " No Hardware RNG support available"
            rc_status -s
            rc_failed

        fi
        
        ;;
    stop)
        echo -n "Shutting down RNG daemon "
        killproc -TERM $RNGD_BIN && unload_rng_modules
        
        # Remember status and be verbose
        rc_status -v
        ;;
    try-restart|condrestart)
        ## Do a restart only if the service was active before.
        ## Note: try-restart is now part of LSB (as of 1.9).
        ## RH has a similar command named condrestart.
        if test "$1" = "condrestart"; then
                echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if test $? = 0; then
                $0 restart 
        else 
                rc_reset        # Not running is not a failure.
        fi
        # Remember status and be quiet
        rc_status
        ;;
    restart | force-reload)
        $0 stop
        $0 start
        ;;
    reload)
        ## Like force-reload, but if daemon does not support
        ## signaling, do nothing (!)
        rc_failed 3
        rc_status -v
        ;;
    status)
        echo -n "Checking for RNG daemon "
        ## Check status with checkproc(8), if process is running
        ## checkproc will return with exit status 0.
        
        # Status has a slightly different for the status command:
        # 0 - service running
        # 1 - service dead, but /var/run/  pid  file exists
        # 2 - service dead, but /var/lock/ lock file exists
        # 3 - service not running
        
        # NOTE: checkproc returns LSB compliant status values.
        checkproc $RNGD_BIN
        rc_status -v
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload}"
        exit 1
        ;;
esac

rc_exit

# vim: set sw=4 ts=4 et: