On Thu, May 10, 2007 at 10:47:29PM +0200, Petter Reinholdtsen wrote:
[David Härdeman]
Here's a more complete stub (/lib/init/splash-functions-base) file which should allow many more scripts to use the generic functions.

This API looks quite good, and I suspect it will handle most needs.
We might need some splash_start_if_not_running() or similar, for the
cases where it need to be restarted.

Also, this function need to have more features:

# Tells the splash the current boot progress in percent (as $1)
splash_progress () { return 0; }

In usplash, the progress bar can go both ways, depending on the sign
of the progress value.  It is used during shutdown.  We probably want
to support it in the API.

I've thought a bit more about the API and also experimented with implementing it in some different scripts which uncovered some more functionality that was needed.

I've attached a new version of the hooks and an example implementation for usplash to give you a fell of how it would work when implemented.

Please review.

--
David Härdeman

# Usplash hooks for /lib/init/splash-functions-base

# Internal function, do not use in external scripts
usplash_pidfound()
{
        pidof usplash > /dev/null 2>&1 || return 1
        return 0
}

splash_running()
{
        if [ -x /sbin/usplash ] && usplash_pidfound; then
                return 0
        fi
        return 1
}

splash_stop ()
{
        local i

        splash_running || return 0

        # Wait until it is gone or forcibly kill it
        i=0
        while usplash_pidfound; do
                i=$(($i + 1))
                if [ $i -gt 10 ]; then
                        kill -9 $(pidof usplash)
                        break
                fi
                sleep 1
        done
        return 0
}

splash_start ()
{
        if splash_running; then
                return 0
        elif [ ! -x /sbin/usplash ] || [ ! -x /sbin/usplash_down ]; then
                return 1
        else
                /sbin/usplash_down || return 1
                return 0
        fi
}

private_splash_progress ()
{
        splash_running || return 0
        /sbin/usplash_write "PROGRESS $1" || return 1
        return 0
}

splash_start_indefinate ()
{
        splash_running || return 0
        /sbin/usplash_write "TIMEOUT 0" || return 1
        /sbin/usplash_write "PULSATE" || return 1
        return 0
}

splash_stop_indefinate ()
{
        splash_running || return 0
        /sbin/usplash_write "CLEAR" || return 1
        /sbin/usplash_write "TIMEOUT 15" || return 1
        return 0
}

splash_user_input ()
{
        splash_running || return 1
        [ -p /dev/.initramfs/usplash_outfifo ] || return 1

        case "$2" in
                regular)
                        /sbin/usplash_write "INPUT $1" || return 1
                        ;;
                password)
                        /sbin/usplash_write "INPUTQUIET $1" || return 1
                        ;;
                enter)
                        /sbin/usplash_write "INPUTENTER $1" || return 1
                        ;;
                *)
                        return 1
                        ;;
        esac
        cat /dev/.initramfs/usplash_outfifo 2> /dev/null || return 1
        return 0
}

# This script contains hooks to allow init scripts to control
# a splash program during boot and shutdown.
#
# To override these, provide a /lib/init/splash-functions scripts
# with new functions (it is sourced at the end of this file)
#
# Note that scripts have a number of constraints:
#  1) Should avoid using any binaries not found in the initramfs so that 
#     the same hooks can be used there.
#  2) This also means that bashisms can't be used.
#  3) Scripts must work when running under "set -e".
#  4) "local" should be used to avoid overwriting global variables.


# Detects whether a splash is running
splash_running() { return 1; }

# Tells the splash to quit
splash_stop () { return 0; }

# Tells the splash to start if not already running
splash_start () { return 1; }

# Tells the splash the current boot/shutdown progress
# $1 contains the progress as a percentage value between -100 and 100
# Positive values indicate boot progress
# Negative values indicate shutdown progress
splash_progress ()
{
        local progress tmp
        progress="$1"

        splash_running || return 0

        # Sanity check step 1 - must match ^-[0-9]*$
        tmp="$progress"

        # Strip trailing numbers
        while [ "${tmp%[0-9]}" != "$tmp" ]; do
                tmp="${tmp%[0-9]}"
        done

        # Now "-" or no characters should remain
        if [ -n "$tmp" ] && [ "$tmp" != "-" ]; then
                return 1
        fi

        #  Sanity check step 2 - check for values >= -100 and <= 100
        if [ "$progress" != "${progress#-}" ]; then
                # Negative value
                if [ "$progress" -lt -100 ]; then
                        return 1
                fi
        else
                # Positive value
                if [ "$progress" -gt 100 ]; then
                        return 1
                fi
        fi

        # Sanity checks passed
        private_splash_progress "$progress" || return 1
        return 0
}

# Customizations should replace this function instead of splash_progress above
private_splash_progress () { return 0; }

# Tells the splash that a task which may take an unknown amount of
# time has started (such as a fsck). This is useful to make sure the
# splash doesn't time out and to give visual feedback to the user.
splash_start_indefinate () { return 0; }

# Tells the splash that an indefinate task is done
splash_stop_indefinate () { return 0; }

# Gets user input from a splash
# $1 contains the text for the user prompt
# $2 describes the type of input:
#     regular  = regular input, e.g. a user name
#     password = input which should not be echoed to screen, e.g. a password
#     enter    = A "press enter to continue" type of prompt
#
# Returns 1 if no user input is possible
# Should be called with an alternative non-splash input fallback:
#   INPUT="$(splash_user_input "Enter password:" password)" || \
#   INPUT="$(manual_method)"
splash_user_input () { return 1; }

# Allow these to be overridden with custom scripts
[ -e /lib/init/splash-functions ] && . /lib/init/splash-functions

Reply via email to