diff options
Diffstat (limited to 'package/base-files/files/lib')
21 files changed, 2126 insertions, 0 deletions
diff --git a/package/base-files/files/lib/functions.sh b/package/base-files/files/lib/functions.sh new file mode 100755 index 0000000..dde1ac4 --- /dev/null +++ b/package/base-files/files/lib/functions.sh @@ -0,0 +1,325 @@ +#!/bin/sh +# Copyright (C) 2006-2014 OpenWrt.org +# Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de> +# Copyright (C) 2010 Vertical Communications + + +debug () { + ${DEBUG:-:} "$@" +} + +# newline +N=" +" + +_C=0 +NO_EXPORT=1 +LOAD_STATE=1 +LIST_SEP=" " + +append() { + local var="$1" + local value="$2" + local sep="${3:- }" + + eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\"" +} + +list_contains() { + local var="$1" + local str="$2" + local val + + eval "val=\" \${$var} \"" + [ "${val%% $str *}" != "$val" ] +} + +config_load() { + [ -n "$IPKG_INSTROOT" ] && return 0 + uci_load "$@" +} + +reset_cb() { + config_cb() { return 0; } + option_cb() { return 0; } + list_cb() { return 0; } +} +reset_cb + +package() { + return 0 +} + +config () { + local cfgtype="$1" + local name="$2" + + export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1)) + name="${name:-cfg$CONFIG_NUM_SECTIONS}" + append CONFIG_SECTIONS "$name" + [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name" + export ${NO_EXPORT:+-n} CONFIG_SECTION="$name" + export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype" +} + +option () { + local varname="$1"; shift + local value="$*" + + export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value" + [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*" +} + +list() { + local varname="$1"; shift + local value="$*" + local len + + config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0 + [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}" + len=$(($len + 1)) + config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value" + config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len" + append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP" + list_cb "$varname" "$*" +} + +config_unset() { + config_set "$1" "$2" "" +} + +# config_get <variable> <section> <option> [<default>] +# config_get <section> <option> +config_get() { + case "$3" in + "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";; + *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";; + esac +} + +# config_get_bool <variable> <section> <option> [<default>] +config_get_bool() { + local _tmp + config_get _tmp "$2" "$3" "$4" + case "$_tmp" in + 1|on|true|yes|enabled) _tmp=1;; + 0|off|false|no|disabled) _tmp=0;; + *) _tmp="$4";; + esac + export ${NO_EXPORT:+-n} "$1=$_tmp" +} + +config_set() { + local section="$1" + local option="$2" + local value="$3" + local old_section="$CONFIG_SECTION" + + CONFIG_SECTION="$section" + option "$option" "$value" + CONFIG_SECTION="$old_section" +} + +config_foreach() { + local ___function="$1" + [ "$#" -ge 1 ] && shift + local ___type="$1" + [ "$#" -ge 1 ] && shift + local section cfgtype + + [ -z "$CONFIG_SECTIONS" ] && return 0 + for section in ${CONFIG_SECTIONS}; do + config_get cfgtype "$section" TYPE + [ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue + eval "$___function \"\$section\" \"\$@\"" + done +} + +config_list_foreach() { + [ "$#" -ge 3 ] || return 0 + local section="$1"; shift + local option="$1"; shift + local function="$1"; shift + local val + local len + local c=1 + + config_get len "${section}" "${option}_LENGTH" + [ -z "$len" ] && return 0 + while [ $c -le "$len" ]; do + config_get val "${section}" "${option}_ITEM$c" + eval "$function \"\$val\" \"\$@\"" + c="$(($c + 1))" + done +} + +insert_modules() { + for m in $*; do + if [ -f /etc/modules.d/$m ]; then + sed 's/^[^#]/insmod &/' /etc/modules.d/$m | ash 2>&- || : + else + modprobe $m + fi + done +} + +default_prerm() { + local name + name=$(basename ${1%.*}) + [ -f /usr/lib/opkg/info/${name}.prerm-pkg ] && . /usr/lib/opkg/info/${name}.prerm-pkg + for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do + $i disable + $i stop + done +} + +default_postinst() { + local root="${IPKG_INSTROOT}" + local pkgname="$(basename ${1%.*})" + local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)" + local ret=0 + + if [ -n "$rusers" ]; then + local tuple oIFS="$IFS" + for tuple in $rusers; do + local uid gid uname gname + + IFS=":" + set -- $tuple; uname="$1"; gname="$2" + IFS="=" + set -- $uname; uname="$1"; uid="$2" + set -- $gname; gname="$1"; gid="$2" + IFS="$oIFS" + + if [ -n "$gname" ] && [ -n "$gid" ]; then + group_exists "$gname" || group_add "$gname" "$gid" + elif [ -n "$gname" ]; then + group_add_next "$gname"; gid=$? + fi + + if [ -n "$uname" ]; then + user_exists "$uname" || user_add "$uname" "$uid" "$gid" + fi + + if [ -n "$uname" ] && [ -n "$gname" ]; then + group_add_user "$gname" "$uname" + fi + + unset uid gid uname gname + done + fi + + if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then + ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ) + ret=$? + fi + + [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null + + if [ "$PKG_UPGRADE" != "1" ]; then + local shell="$(which bash)" + for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do + if [ -n "$root" ]; then + ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable + else + "$i" enable + "$i" start + fi + done + fi + + return $ret +} + +include() { + local file + + for file in $(ls $1/*.sh 2>/dev/null); do + . $file + done +} + +find_mtd_index() { + local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')" + local INDEX="${PART##mtd}" + + echo ${INDEX} +} + +find_mtd_part() { + local INDEX=$(find_mtd_index "$1") + local PREFIX=/dev/mtdblock + + [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/ + echo "${INDEX:+$PREFIX$INDEX}" +} + +group_add() { + local name="$1" + local gid="$2" + local rc + [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1 + [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group + echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group + rc=$? + [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group + return $rc +} + +group_exists() { + grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group +} + +group_add_next() { + local gid gids + gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3) + [ -n "$gid" ] && return $gid + gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3) + gid=100 + while [ -n "$(echo $gids | grep $gid)" ] ; do + gid=$((gid + 1)) + done + group_add $1 $gid + return $gid +} + +group_add_user() { + local grp delim="," + grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group) + [ -z "$(echo $grp | cut -d: -f4 | grep $2)" ] || return + [ -n "$(echo $grp | grep ":$")" ] && delim="" + [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd + sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group + [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd +} + +user_add() { + local name="${1}" + local uid="${2}" + local gid="${3}" + local desc="${4:-$1}" + local home="${5:-/var/run/$1}" + local shell="${6:-/bin/false}" + local rc + [ -z "$uid" ] && { + uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3) + uid=100 + while [ -n "$(echo $uids | grep $uid)" ] ; do + uid=$((uid + 1)) + done + } + [ -z "$gid" ] && gid=$uid + [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1 + [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd + echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd + echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow + rc=$? + [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd + return $rc +} + +user_exists() { + grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd +} + +[ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh diff --git a/package/base-files/files/lib/functions/leds.sh b/package/base-files/files/lib/functions/leds.sh new file mode 100644 index 0000000..d4d4512 --- /dev/null +++ b/package/base-files/files/lib/functions/leds.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# Copyright (C) 2013 OpenWrt.org + +led_set_attr() { + [ -f "/sys/class/leds/$1/$2" ] && echo "$3" > "/sys/class/leds/$1/$2" +} + +led_timer() { + led_set_attr $1 "trigger" "timer" + led_set_attr $1 "delay_on" "$2" + led_set_attr $1 "delay_off" "$3" +} + +led_on() { + led_set_attr $1 "trigger" "none" + led_set_attr $1 "brightness" 255 +} + +led_off() { + led_set_attr $1 "trigger" "none" + led_set_attr $1 "brightness" 0 +} + +led_morse() { + led_set_attr $1 "trigger" "morse" + led_set_attr $1 "delay" "$2" + led_set_attr $1 "message" "$3" +} + +status_led_set_timer() { + led_timer $status_led "$1" "$2" + [ -n "$status_led2" ] && led_timer $status_led2 "$1" "$2" +} + +status_led_set_heartbeat() { + led_set_attr $status_led "trigger" "heartbeat" +} + +status_led_set_morse() { + led_morse $status_led "$1" "$2" + [ -n "$status_led2" ] && led_morse $status_led2 "$1" "$2" +} + +status_led_on() { + led_on $status_led + [ -n "$status_led2" ] && led_on $status_led2 +} + +status_led_off() { + led_off $status_led + [ -n "$status_led2" ] && led_off $status_led2 +} + +status_led_blink_slow() { + led_timer $status_led 1000 1000 +} + +status_led_blink_fast() { + led_timer $status_led 100 100 +} + +status_led_blink_preinit() { + led_timer $status_led 100 100 +} + +status_led_blink_failsafe() { + led_timer $status_led 50 50 +} + +status_led_blink_preinit_regular() { + led_timer $status_led 200 200 +} diff --git a/package/base-files/files/lib/functions/network.sh b/package/base-files/files/lib/functions/network.sh new file mode 100644 index 0000000..1b0c717 --- /dev/null +++ b/package/base-files/files/lib/functions/network.sh @@ -0,0 +1,268 @@ +# 1: destination variable +# 2: interface +# 3: path +# 4: separator +# 5: limit +__network_ifstatus() { + local __tmp + + [ -z "$__NETWORK_CACHE" ] && \ + export __NETWORK_CACHE="$(ubus call network.interface dump)" + + __tmp="$(jsonfilter ${4:+-F "$4"} ${5:+-l "$5"} -s "$__NETWORK_CACHE" -e "$1=@.interface${2:+[@.interface='$2']}$3")" + + [ -z "$__tmp" ] && \ + unset "$1" && \ + return 1 + + eval "$__tmp" +} + +# determine first IPv4 address of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddr() { + __network_ifstatus "$1" "$2" "['ipv4-address'][0].address"; +} + +# determine first IPv6 address of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddr6() { + local __addr + + if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][0].address"; then + case "$__addr" in + *:) export "$1=${__addr}1" ;; + *) export "$1=${__addr}" ;; + esac + return 0 + fi + + unset $1 + return 1 +} + +# determine first IPv4 subnet of given logical interface +# 1: destination variable +# 2: interface +network_get_subnet() { + __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/" +} + +# determine first IPv6 subnet of given logical interface +# 1: destination variable +# 2: interface +network_get_subnet6() { + __network_ifstatus "$1" "$2" "['ipv6-address'][0]['address','mask']" "/" +} + +# determine first IPv6 prefix of given logical interface +# 1: destination variable +# 2: interface +network_get_prefix6() { + __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/" +} + +# determine all IPv4 addresses of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddrs() { + __network_ifstatus "$1" "$2" "['ipv4-address'][*].address" +} + +# determine all IPv6 addresses of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddrs6() { + local __addr + local __list="" + + if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][*].address"; then + for __addr in $__addr; do + case "$__addr" in + *:) __list="${__list:+$__list }${__addr}1" ;; + *) __list="${__list:+$__list }${__addr}" ;; + esac + done + + export "$1=$__list" + return 0 + fi + + unset "$1" + return 1 +} + +# determine all IP addresses of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddrs_all() { + local __addr + local __list="" + + if __network_ifstatus "__addr" "$2" "['ipv4-address','ipv6-address','ipv6-prefix-assignment'][*].address"; then + for __addr in $__addr; do + case "$__addr" in + *:) __list="${__list:+$__list }${__addr}1" ;; + *) __list="${__list:+$__list }${__addr}" ;; + esac + done + + export "$1=$__list" + return 0 + fi + + unset "$1" + return 1 +} + +# determine all IPv4 subnets of given logical interface +# 1: destination variable +# 2: interface +network_get_subnets() { + __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ " +} + +# determine all IPv6 subnets of given logical interface +# 1: destination variable +# 2: interface +network_get_subnets6() { + local __addr + local __list="" + + if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then + for __addr in $__addr; do + case "$__addr" in + *:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;; + *) __list="${__list:+$__list }${__addr}" ;; + esac + done + + export "$1=$__list" + return 0 + fi + + unset "$1" + return 1 +} + +# determine all IPv6 prefixes of given logical interface +# 1: destination variable +# 2: interface +network_get_prefixes6() { + __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ " +} + +# determine IPv4 gateway of given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive gateway if "true" (optional) +network_get_gateway() { + __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \ + return 0 + + [ "$3" = 1 -o "$3" = "true" ] && \ + __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 +} + +# determine IPv6 gateway of given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive gateway if "true" (optional) +network_get_gateway6() { + __network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \ + return 0 + + [ "$3" = 1 -o "$3" = "true" ] && \ + __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1 +} + +# determine the DNS servers of the given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive servers if "true" (optional) +network_get_dnsserver() { + __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0 + + [ "$3" = 1 -o "$3" = "true" ] && \ + __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]" +} + +# determine the domains of the given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive domains if "true" (optional) +network_get_dnssearch() { + __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0 + + [ "$3" = 1 -o "$3" = "true" ] && \ + __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]" +} + + +# 1: destination variable +# 2: addr +# 3: inactive +__network_wan() +{ + __network_ifstatus "$1" "" \ + "[@.route[@.target='$2' && !@.table]].interface" "" 1 && \ + return 0 + + [ "$3" = 1 -o "$3" = "true" ] && \ + __network_ifstatus "$1" "" \ + "[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1 +} + +# find the logical interface which holds the current IPv4 default route +# 1: destination variable +# 2: consider inactive default routes if "true" (optional) +network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; } + +# find the logical interface which holds the current IPv6 default route +# 1: destination variable +# 2: consider inactive dafault routes if "true" (optional) +network_find_wan6() { __network_wan "$1" "::" "$2"; } + +# test whether the given logical interface is running +# 1: interface +network_is_up() +{ + local __up + __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ] +} + +# determine the protocol of the given logical interface +# 1: destination variable +# 2: interface +network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; } + +# determine the layer 3 linux network device of the given logical interface +# 1: destination variable +# 2: interface +network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; } + +# determine the layer 2 linux network device of the given logical interface +# 1: destination variable +# 2: interface +network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; } + +# defer netifd actions on the given linux network device +# 1: device name +network_defer_device() +{ + ubus call network.device set_state \ + "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null +} + +# continue netifd actions on the given linux network device +# 1: device name +network_ready_device() +{ + ubus call network.device set_state \ + "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null +} + +# flush the internal value cache to force re-reading values from ubus +network_flush_cache() { unset __NETWORK_CACHE; } diff --git a/package/base-files/files/lib/functions/preinit.sh b/package/base-files/files/lib/functions/preinit.sh new file mode 100644 index 0000000..57862a1 --- /dev/null +++ b/package/base-files/files/lib/functions/preinit.sh @@ -0,0 +1,88 @@ +#!/bin/sh +# Copyright (C) 2006-2013 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +boot_hook_splice_start() { + export -n PI_HOOK_SPLICE=1 +} + +boot_hook_splice_finish() { + local hook + for hook in $PI_STACK_LIST; do + local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook" + export -n "${hook}=${v% }" + export -n "${hook}_splice=" + done + export -n PI_HOOK_SPLICE= +} + +boot_hook_init() { + local hook="${1}_hook" + export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook" + export -n "$hook=" +} + +boot_hook_add() { + local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}" + local func="${2}" + + [ -n "$func" ] && { + local v; eval "v=\$$hook" + export -n "$hook=${v:+$v }$func" + } +} + +boot_hook_shift() { + local hook="${1}_hook" + local rvar="${2}" + + local v; eval "v=\$$hook" + [ -n "$v" ] && { + local first="${v%% *}" + + [ "$v" != "${v#* }" ] && \ + export -n "$hook=${v#* }" || \ + export -n "$hook=" + + export -n "$rvar=$first" + return 0 + } + + return 1 +} + +boot_run_hook() { + local hook="$1" + local func + + while boot_hook_shift "$hook" func; do + local ran; eval "ran=\$PI_RAN_$func" + [ -n "$ran" ] || { + export -n "PI_RAN_$func=1" + $func "$1" "$2" + } + done +} + +pivot() { # <new_root> <old_root> + /bin/mount -o noatime,move /proc $1/proc && \ + pivot_root $1 $1$2 && { + /bin/mount -o noatime,move $2/dev /dev + /bin/mount -o noatime,move $2/tmp /tmp + /bin/mount -o noatime,move $2/sys /sys 2>&- + /bin/mount -o noatime,move $2/overlay /overlay 2>&- + return 0 + } +} + +fopivot() { # <rw_root> <work_dir> <ro_root> <dupe?> + /bin/mount -o noatime,lowerdir=/,upperdir=$1,workdir=$2 -t overlay "overlayfs:$1" /mnt + pivot /mnt $3 +} + +ramoverlay() { + mkdir -p /tmp/root + /bin/mount -t tmpfs -o noatime,mode=0755 root /tmp/root + mkdir -p /tmp/root/root /tmp/root/work + fopivot /tmp/root/root /tmp/root/work /rom 1 +} diff --git a/package/base-files/files/lib/functions/service.sh b/package/base-files/files/lib/functions/service.sh new file mode 100644 index 0000000..3d08e14 --- /dev/null +++ b/package/base-files/files/lib/functions/service.sh @@ -0,0 +1,103 @@ +# +# service: simple wrapper around start-stop-daemon +# +# Usage: service ACTION EXEC ARGS... +# +# Action: +# -C check if EXEC is alive +# -S start EXEC, passing it ARGS as its arguments +# -K kill EXEC, sending it a TERM signal if not specified otherwise +# +# Environment variables exposed: +# SERVICE_DAEMONIZE run EXEC in background +# SERVICE_WRITE_PID create a pid-file and use it for matching +# SERVICE_MATCH_EXEC use EXEC command-line for matching (default) +# SERVICE_MATCH_NAME use EXEC process name for matching +# SERVICE_USE_PID assume EXEC create its own pid-file and use it for matching +# SERVICE_NAME process name to use (default to EXEC file part) +# SERVICE_PID_FILE pid file to use (default to /var/run/$SERVICE_NAME.pid) +# SERVICE_SIG signal to send when using -K +# SERVICE_SIG_RELOAD default signal used when reloading +# SERVICE_SIG_STOP default signal used when stopping +# SERVICE_STOP_TIME time to wait for a process to stop gracefully before killing it +# SERVICE_UID user EXEC should be run as +# SERVICE_GID group EXEC should be run as +# +# SERVICE_DEBUG don't do anything, but show what would be done +# SERVICE_QUIET don't print anything +# + +SERVICE_QUIET=1 +SERVICE_SIG_RELOAD="HUP" +SERVICE_SIG_STOP="TERM" +SERVICE_STOP_TIME=5 +SERVICE_MATCH_EXEC=1 + +service() { + local ssd + local exec + local name + local start + ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}" + case "$1" in + -C) + ssd="$ssd -K -t" + ;; + -S) + ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}" + start=1 + ;; + -K) + ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}" + ;; + *) + echo "service: unknown ACTION '$1'" 1>&2 + return 1 + esac + shift + exec="$1" + [ -n "$exec" ] || { + echo "service: missing argument" 1>&2 + return 1 + } + [ -x "$exec" ] || { + echo "service: file '$exec' is not executable" 1>&2 + return 1 + } + name="${SERVICE_NAME:-${exec##*/}}" + [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \ + || ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}" + [ -z "$SERVICE_MATCH_NAME" ] || ssd="$ssd -n $name" + ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}" + [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd="$ssd -x $exec" + shift + $ssd${1:+ -- "$@"} +} + +service_check() { + service -C "$@" +} + +service_signal() { + SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@" +} + +service_start() { + service -S "$@" +} + +service_stop() { + local try + SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" || return 1 + while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do + service -C "$@" || return 0 + sleep 1 + done + SERVICE_SIG="KILL" service -K "$@" + sleep 1 + ! service -C "$@" +} + +service_reload() { + SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@" +} diff --git a/package/base-files/files/lib/functions/system.sh b/package/base-files/files/lib/functions/system.sh new file mode 100644 index 0000000..8d75a5a --- /dev/null +++ b/package/base-files/files/lib/functions/system.sh @@ -0,0 +1,112 @@ +# Copyright (C) 2006-2013 OpenWrt.org + +find_mtd_chardev() { + local INDEX=$(find_mtd_index "$1") + local PREFIX=/dev/mtd + + [ -d /dev/mtd ] && PREFIX=/dev/mtd/ + echo "${INDEX:+$PREFIX$INDEX}" +} + +mtd_get_mac_ascii() +{ + local mtdname="$1" + local key="$2" + local part + local mac_dirty + + part=$(find_mtd_part "$mtdname") + if [ -z "$part" ]; then + echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2 + return + fi + + mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p') + + # "canonicalize" mac + [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty" +} + +mtd_get_mac_binary() { + local mtdname="$1" + local offset="$2" + local part + + part=$(find_mtd_part "$mtdname") + if [ -z "$part" ]; then + echo "mtd_get_mac_binary: partition $mtdname not found!" >&2 + return + fi + + dd bs=1 skip=$offset count=6 if=$part 2>/dev/null | hexdump -v -n 6 -e '5/1 "%02x:" 1/1 "%02x"' +} + +mtd_get_part_size() { + local part_name=$1 + local first dev size erasesize name + while read dev size erasesize name; do + name=${name#'"'}; name=${name%'"'} + if [ "$name" = "$part_name" ]; then + echo $((0x$size)) + break + fi + done < /proc/mtd +} + +macaddr_add() { + local mac=$1 + local val=$2 + local oui=${mac%:*:*:*} + local nic=${mac#*:*:*:} + + nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/') + echo $oui:$nic +} + +macaddr_setbit_la() +{ + local mac=$1 + + printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:} +} + +macaddr_2bin() +{ + local mac=$1 + + echo -ne \\x${mac//:/\\x} +} + +macaddr_canonicalize() +{ + local mac="$1" + local canon="" + + mac=$(echo -n $mac | tr -d \") + [ ${#mac} -gt 17 ] && return + [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return + + for octet in ${mac//[\.:-]/ }; do + case "${#octet}" in + 1) + octet="0${octet}" + ;; + 2) + ;; + 4) + octet="${octet:0:2} ${octet:2:2}" + ;; + 12) + octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}" + ;; + *) + return + ;; + esac + canon=${canon}${canon:+ }${octet} + done + + [ ${#canon} -ne 17 ] && return + + printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null +} diff --git a/package/base-files/files/lib/functions/uci-defaults-new.sh b/package/base-files/files/lib/functions/uci-defaults-new.sh new file mode 100755 index 0000000..7222ff8 --- /dev/null +++ b/package/base-files/files/lib/functions/uci-defaults-new.sh @@ -0,0 +1,302 @@ +#!/bin/ash + +CFG=/etc/board.json + +. /usr/share/libubox/jshn.sh + +json_select_array() { + local _json_no_warning=1 + + json_select "$1" + [ $? = 0 ] && return + + json_add_array $1 + json_close_array + + json_select "$1" +} + +json_select_object() { + local _json_no_warning=1 + + json_select "$1" + [ $? = 0 ] && return + + json_add_object $1 + json_close_object + + json_select "$1" +} + +_ucidef_set_interface() { + local name=$1 + local iface=$2 + + json_select_object $name + json_add_string ifname "${iface%%.*}" + [ "$iface" = "${iface%%.*}" ] || json_add_boolean create_vlan 1 + json_select .. +} + +ucidef_set_interface_loopback() +{ + # stub + local a=$1 +} + +ucidef_set_interface_lan() { + local lan_if=$1 + + json_select_object network + _ucidef_set_interface lan $lan_if + json_select .. +} + +ucidef_set_interfaces_lan_wan() { + local lan_if=$1 + local wan_if=$2 + + json_select_object network + _ucidef_set_interface lan $lan_if + _ucidef_set_interface wan $wan_if + json_select .. +} + +ucidef_add_switch() { + local name=$1 + local reset=$2 + local enable=$3 + + json_select_object switch + + json_select_object $name + json_add_boolean enable $enable + json_add_boolean reset $reset + json_select .. + + json_select .. +} + +ucidef_add_switch_attr() { + local name=$1 + local key=$2 + local val=$3 + + json_select_object switch + + json_select_object $name + json_add_string $key $val + json_select .. + + json_select .. +} + +ucidef_add_switch_vlan() { + local name=$1 + local vlan=$2 + local ports=$3 + local cpu_port='' + + case $vlan in + 1) vlan=lan;; + 2) vlan=wan;; + *) vlan=vlan$vlan;; + esac + + json_select_object switch + json_select_object $name + json_select_object vlans + + json_add_array $vlan + for p in $ports; do + if [ ${p%t} != $p ]; then + cpu_port=$p + else + json_add_int "" $p + fi + done + json_close_array + + json_select .. + [ -n "$cpu_port" ] && json_add_int cpu_port $cpu_port + json_select .. + json_select .. +} + +ucidef_set_interface_macaddr() { + local network=$1 + local macaddr=$2 + + json_select_object network + + json_select $network + [ $? -eq 0 ] || { + json_select .. + return + } + + json_add_string macaddr $macaddr + json_select .. + + json_select .. +} + +ucidef_set_led_netdev() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local dev=$4 + + json_select_object led + + json_select_object $1 + json_add_string name $name + json_add_string type netdev + json_add_string sysfs $sysfs + json_add_string device $dev + json_select .. + + json_select .. +} + +ucidef_set_led_interface() { + local name=$1 + local sysfs=$2 + + json_select_object led + + json_select_object $1 + json_add_string name $name + json_add_string type interface + json_add_string sysfs $sysfs + json_add_string interface $name + json_select .. + + json_select .. +} + +ucidef_set_led_usbdev() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local dev=$4 + + json_select_object led + + json_select_object $1 + json_add_string name $name + json_add_string type usb + json_add_string sysfs $sysfs + json_add_string device $dev + json_select .. + + json_select .. +} + +ucidef_set_led_wlan() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local trigger=$4 + + json_select_object led + + json_select_object $1 + json_add_string name $name + json_add_string type trigger + json_add_string sysfs $sysfs + json_add_string trigger $trigger + json_select .. + + json_select .. +} + +ucidef_set_led_switch() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local trigger=$4 + local port_mask=$5 + + json_select_object led + + json_select_object $1 + json_add_string name $name + json_add_string type switch + json_add_string sysfs $sysfs + json_add_string trigger $trigger + json_add_string port_mask $port_mask + json_select .. + + json_select .. +} + +ucidef_set_led_default() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local default=$4 + + json_select_object led + + json_select_object $1 + json_add_string name $name + json_add_string sysfs $sysfs + json_add_string default $default + json_select .. + + json_select .. +} + +ucidef_set_led_rssi() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local iface=$4 + local minq=$5 + local maxq=$6 + local offset=$7 + local factor=$8 + + json_select_object led + + json_select_object rssi + json_select_object $1 + json_add_string name $name + json_add_string sysfs $sysfs + json_add_string minq $minq + json_add_string maxq $maxq + json_add_string offset $offset + json_add_string factor $factor + json_select .. + json_select .. + + json_select .. +} + +ucidef_set_rssimon() { + local dev="$1" + local refresh="$2" + local threshold="$3" + + json_select_object led + + json_select_object rssi + json_add_string type rssi + json_add_string dev $dev + json_add_string threshold $threshold + json_select .. + + json_select .. + +} + +board_config_update() { + json_init + [ -f ${CFG} ] && json_load "$(cat ${CFG})" +} + +board_config_flush() { + json_dump -i > /tmp/.board.json + mv /tmp/.board.json ${CFG} +} diff --git a/package/base-files/files/lib/functions/uci-defaults.sh b/package/base-files/files/lib/functions/uci-defaults.sh new file mode 100644 index 0000000..2658d43 --- /dev/null +++ b/package/base-files/files/lib/functions/uci-defaults.sh @@ -0,0 +1,345 @@ +#!/bin/sh +# Copyright (C) 2011 OpenWrt.org + +UCIDEF_LEDS_CHANGED=0 +UCIDEF_GPIO_SWITCHES_CHANGED=0 + +ucidef_set_led_netdev() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local dev=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='netdev' +set system.$cfg.dev='$dev' +set system.$cfg.mode='link tx rx' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_usbdev() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local dev=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='usbdev' +set system.$cfg.dev='$dev' +set system.$cfg.interval='50' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_wlan() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local trigger=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='$trigger' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_switch() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local trigger=$4 + local port_mask=$5 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='$trigger' +set system.$cfg.port_mask='$port_mask' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_default() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local default=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.default='$default' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_rssi() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local iface=$4 + local minq=$5 + local maxq=$6 + local offset=$7 + local factor=$8 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='rssi' +set system.$cfg.iface='rssid_$iface' +set system.$cfg.minq='$minq' +set system.$cfg.maxq='$maxq' +set system.$cfg.offset='$offset' +set system.$cfg.factor='$factor' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_timer() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local delayon=$4 + local delayoff=$5 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='timer' +set system.$cfg.delayon='$delayon' +set system.$cfg.delayoff='$delayoff' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_mmc() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local trigger=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='$trigger' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_trigger_gpio() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local gpio=$4 + local inverted=$5 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='gpio' +set system.$cfg.gpio='$gpio' +set system.$cfg.inverted='$inverted' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_ide_disk() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='ide-disk' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_rssimon() { + local dev="$1" + local refresh="$2" + local threshold="$3" + + local cfg="rssid_$dev" + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='rssid' +set system.$cfg.dev='$dev' +set system.$cfg.refresh='$refresh' +set system.$cfg.threshold='$threshold' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_commit_leds() +{ + [ "$UCIDEF_LEDS_CHANGED" = "1" ] && uci commit system +} + +ucidef_set_gpio_switch() { + local cfg="gpio_switch_$1" + local name="$2" + local gpio_pin="$3" + # use "0" as default value + local default="${4:-0}" + + uci -q get "system.$cfg" && return 0 + + uci batch <<EOF +set system.$cfg='gpio_switch' +set system.$cfg.name='$name' +set system.$cfg.gpio_pin='$gpio_pin' +set system.$cfg.value='$default' +EOF + UCIDEF_GPIO_SWITCHES_CHANGED=1 +} + +ucidef_commit_gpio_switches() +{ + [ "$UCIDEF_GPIO_SWITCHES_CHANGED" = "1" ] && uci commit system +} + +ucidef_set_interface_loopback() { + uci batch <<EOF +set network.loopback='interface' +set network.loopback.ifname='lo' +set network.loopback.proto='static' +set network.loopback.ipaddr='127.0.0.1' +set network.loopback.netmask='255.0.0.0' +set network.globals='globals' +set network.globals.ula_prefix='auto' +EOF +} + +ucidef_set_interface_raw() { + local cfg=$1 + local ifname=$2 + local proto=${3:-"none"} + + uci batch <<EOF +set network.$cfg='interface' +set network.$cfg.ifname='$ifname' +set network.$cfg.proto='$proto' +EOF +} + +ucidef_set_interface_lan() { + local ifname=$1 + + uci batch <<EOF +set network.lan='interface' +set network.lan.ifname='$ifname' +set network.lan.force_link=1 +set network.lan.type='bridge' +set network.lan.proto='static' +set network.lan.ipaddr='192.168.1.1' +set network.lan.netmask='255.255.255.0' +set network.lan.ip6assign='60' +EOF +} + +ucidef_set_interface_wan() { + local ifname=$1 + + uci batch <<EOF +set network.wan='interface' +set network.wan.ifname='$ifname' +set network.wan.proto='dhcp' +set network.wan6='interface' +set network.wan6.ifname='$ifname' +set network.wan6.proto='dhcpv6' +EOF +} + +ucidef_set_interfaces_lan_wan() { + local lan_ifname=$1 + local wan_ifname=$2 + + ucidef_set_interface_lan "$lan_ifname" + ucidef_set_interface_wan "$wan_ifname" +} + +ucidef_set_interface_macaddr() { + local ifname=$1 + local mac=$2 + + uci batch <<EOF +set network.$ifname.macaddr='$mac' +EOF +} + +ucidef_add_switch() { + local name=$1 + local reset=$2 + local enable=$3 + uci batch <<EOF +add network switch +set network.@switch[-1].name='$name' +set network.@switch[-1].reset='$reset' +set network.@switch[-1].enable_vlan='$enable' +EOF +} + +ucidef_add_switch_vlan() { + local device=$1 + local vlan=$2 + local ports=$3 + uci batch <<EOF +add network switch_vlan +set network.@switch_vlan[-1].device='$device' +set network.@switch_vlan[-1].vlan='$vlan' +set network.@switch_vlan[-1].ports='$ports' +EOF +} + +ucidef_add_switch_port() { + local device=$1 + local port=$2 + uci batch <<EOF +add network switch_port +set network.@switch_port[-1].device='$device' +set network.@switch_port[-1].port='$port' +EOF +} + diff --git a/package/base-files/files/lib/preinit/02_default_set_state b/package/base-files/files/lib/preinit/02_default_set_state new file mode 100644 index 0000000..df43395 --- /dev/null +++ b/package/base-files/files/lib/preinit/02_default_set_state @@ -0,0 +1,7 @@ +#!/bin/sh + +define_default_set_state() { + . /etc/diag.sh +} + +boot_hook_add preinit_main define_default_set_state diff --git a/package/base-files/files/lib/preinit/10_indicate_failsafe b/package/base-files/files/lib/preinit/10_indicate_failsafe new file mode 100644 index 0000000..6afae41 --- /dev/null +++ b/package/base-files/files/lib/preinit/10_indicate_failsafe @@ -0,0 +1,17 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +# commands for emitting messages to network in failsafe mode + +indicate_failsafe_led () { + set_state failsafe +} + +indicate_failsafe() { + echo "- failsafe -" + preinit_net_echo "Entering Failsafe!\n" + indicate_failsafe_led +} + +boot_hook_add failsafe indicate_failsafe diff --git a/package/base-files/files/lib/preinit/10_indicate_preinit b/package/base-files/files/lib/preinit/10_indicate_preinit new file mode 100644 index 0000000..1fab8a2 --- /dev/null +++ b/package/base-files/files/lib/preinit/10_indicate_preinit @@ -0,0 +1,47 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +preinit_ip() { + # if the preinit interface isn't specified and ifname is set in + # preinit.arch use that interface + if [ -z "$pi_ifname" ]; then + pi_ifname=$ifname + fi + + [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { + ip link set dev $pi_ifname up + ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $pi_ifname + } +} + +preinit_ip_deconfig() { + [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { + ip -4 address flush dev $pi_ifname + ip link set dev $pi_ifname down + } +} + +preinit_net_echo() { + [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { + { + [ "$pi_preinit_net_messages" = "y" ] || { + [ "$pi_failsafe_net_message" = "true" ] && + [ "$pi_preinit_no_failsafe_netmsg" != "y" ] + } + } && netmsg $pi_broadcast "$1" + } +} + +preinit_echo() { + preinit_net_echo $1 + echo $1 +} + +pi_indicate_preinit() { + preinit_net_echo "Doing OpenWrt Preinit\n" + set_state preinit +} + +boot_hook_add preinit_main preinit_ip +boot_hook_add preinit_main pi_indicate_preinit diff --git a/package/base-files/files/lib/preinit/10_sysinfo b/package/base-files/files/lib/preinit/10_sysinfo new file mode 100644 index 0000000..42fd5b6 --- /dev/null +++ b/package/base-files/files/lib/preinit/10_sysinfo @@ -0,0 +1,10 @@ +do_sysinfo_generic() { + [ -d /proc/device-tree ] || return + mkdir -p /tmp/sysinfo + [ -e /tmp/sysinfo/board_name ] || \ + echo "$(strings /proc/device-tree/compatible | head -1)" > /tmp/sysinfo/board_name + [ -e /tmp/sysinfo/model ] || \ + echo "$(cat /proc/device-tree/model)" > /tmp/sysinfo/model +} + +boot_hook_add preinit_main do_sysinfo_generic diff --git a/package/base-files/files/lib/preinit/30_failsafe_wait b/package/base-files/files/lib/preinit/30_failsafe_wait new file mode 100644 index 0000000..3d69baf --- /dev/null +++ b/package/base-files/files/lib/preinit/30_failsafe_wait @@ -0,0 +1,96 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +fs_wait_for_key () { + local timeout=$3 + local timer + local do_keypress + local keypress_true="$(mktemp)" + local keypress_wait="$(mktemp)" + local keypress_sec="$(mktemp)" + if [ -z "$keypress_wait" ]; then + keypress_wait=/tmp/.keypress_wait + touch $keypress_wait + fi + if [ -z "$keypress_true" ]; then + keypress_true=/tmp/.keypress_true + touch $keypress_true + fi + if [ -z "$keypress_sec" ]; then + keypress_sec=/tmp/.keypress_sec + touch $keypress_sec + fi + + trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT + trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1 + + [ -n "$timeout" ] || timeout=1 + [ $timeout -ge 1 ] || timeout=1 + timer=$timeout + lock $keypress_wait + { + while [ $timer -gt 0 ]; do + echo "$timer" >$keypress_sec + timer=$(($timer - 1)) + sleep 1 + done + lock -u $keypress_wait + rm -f $keypress_wait + } & + + echo "Press the [$1] key and hit [enter] $2" + echo "Press the [1], [2], [3] or [4] key and hit [enter] to select the debug level" + # if we're on the console we wait for input + { + while [ -r $keypress_wait ]; do + timer="$(cat $keypress_sec)" + + [ -n "$timer" ] || timer=1 + timer="${timer%%\ *}" + [ $timer -ge 1 ] || timer=1 + do_keypress="" + { + read -t "$timer" do_keypress + case "$do_keypress" in + $1) + echo "true" >$keypress_true + ;; + 1 | 2 | 3 | 4) + echo "$do_keypress" >/tmp/debug_level + ;; + *) + continue; + ;; + esac + lock -u $keypress_wait + rm -f $keypress_wait + } + done + } + lock -w $keypress_wait + + keypressed=1 + [ "$(cat $keypress_true)" = "true" ] && keypressed=0 + + rm -f $keypress_true + rm -f $keypress_wait + rm -f $keypress_sec + + return $keypressed +} + +failsafe_wait() { + FAILSAFE= + grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE + if [ "$FAILSAFE" != "true" ]; then + pi_failsafe_net_message=true + preinit_net_echo "Please press button now to enter failsafe" + pi_failsafe_net_message=false + fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true + [ -f "/tmp/failsafe_button" ] && FAILSAFE=true && echo "- failsafe button "`cat /tmp/failsafe_button`" was pressed -" + [ "$FAILSAFE" = "true" ] && export FAILSAFE && touch /tmp/failsafe + fi +} + +boot_hook_add preinit_main failsafe_wait diff --git a/package/base-files/files/lib/preinit/40_run_failsafe_hook b/package/base-files/files/lib/preinit/40_run_failsafe_hook new file mode 100644 index 0000000..cb43ad3 --- /dev/null +++ b/package/base-files/files/lib/preinit/40_run_failsafe_hook @@ -0,0 +1,12 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +run_failsafe_hook() { + if [ "$FAILSAFE" = "true" ]; then + boot_run_hook failsafe + lock -w /tmp/.failsafe + fi +} + +boot_hook_add preinit_main run_failsafe_hook diff --git a/package/base-files/files/lib/preinit/50_indicate_regular_preinit b/package/base-files/files/lib/preinit/50_indicate_regular_preinit new file mode 100644 index 0000000..5b7523f --- /dev/null +++ b/package/base-files/files/lib/preinit/50_indicate_regular_preinit @@ -0,0 +1,10 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +indicate_regular_preinit() { + preinit_net_echo "Continuing with Regular Preinit\n" + set_state preinit_regular +} + +boot_hook_add preinit_main indicate_regular_preinit diff --git a/package/base-files/files/lib/preinit/70_initramfs_test b/package/base-files/files/lib/preinit/70_initramfs_test new file mode 100644 index 0000000..8504e34 --- /dev/null +++ b/package/base-files/files/lib/preinit/70_initramfs_test @@ -0,0 +1,13 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +initramfs_test() { + if [ -n "$INITRAMFS" ]; then + boot_run_hook initramfs + preinit_ip_deconfig + break + fi +} + +boot_hook_add preinit_main initramfs_test diff --git a/package/base-files/files/lib/preinit/80_mount_root b/package/base-files/files/lib/preinit/80_mount_root new file mode 100644 index 0000000..f3fe788 --- /dev/null +++ b/package/base-files/files/lib/preinit/80_mount_root @@ -0,0 +1,15 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +do_mount_root() { + mount_root + boot_run_hook preinit_mount_root + [ -f /sysupgrade.tgz ] && { + echo "- config restore -" + cd / + tar xzf /sysupgrade.tgz + } +} + +[ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_mount_root diff --git a/package/base-files/files/lib/preinit/99_10_failsafe_login b/package/base-files/files/lib/preinit/99_10_failsafe_login new file mode 100644 index 0000000..b12e317 --- /dev/null +++ b/package/base-files/files/lib/preinit/99_10_failsafe_login @@ -0,0 +1,17 @@ +#!/bin/sh +# Copyright (C) 2006-2015 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +failsafe_netlogin () { + dropbearkey -t rsa -s 1024 -f /tmp/dropbear_failsafe_host_key + dropbear -r /tmp/dropbear_failsafe_host_key <> /dev/null 2>&1 +} + +failsafe_shell() { + lock /tmp/.failsafe + ash --login + echo "Please reboot system when done with failsafe network logins" +} + +boot_hook_add failsafe failsafe_netlogin +boot_hook_add failsafe failsafe_shell diff --git a/package/base-files/files/lib/preinit/99_10_run_init b/package/base-files/files/lib/preinit/99_10_run_init new file mode 100644 index 0000000..b4f0ec2 --- /dev/null +++ b/package/base-files/files/lib/preinit/99_10_run_init @@ -0,0 +1,9 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +run_init() { + preinit_ip_deconfig +} + +boot_hook_add preinit_main run_init diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh new file mode 100644 index 0000000..761b4c1 --- /dev/null +++ b/package/base-files/files/lib/upgrade/common.sh @@ -0,0 +1,248 @@ +#!/bin/sh + +RAM_ROOT=/tmp/root + +[ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; } +libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; } + +install_file() { # <file> [ <file> ... ] + for file in "$@"; do + dest="$RAM_ROOT/$file" + [ -f $file -a ! -f $dest ] && { + dir="$(dirname $dest)" + mkdir -p "$dir" + cp $file $dest + } + done +} + +install_bin() { # <file> [ <symlink> ... ] + src=$1 + files=$1 + [ -x "$src" ] && files="$src $(libs $src)" + install_file $files + shift + for link in "$@"; do { + dest="$RAM_ROOT/$link" + dir="$(dirname $dest)" + mkdir -p "$dir" + [ -f "$dest" ] || ln -s $src $dest + }; done +} + +supivot() { # <new_root> <old_root> + /bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1 + mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \ + /bin/mount -o noatime,move /proc $1/proc && \ + pivot_root $1 $1$2 || { + /bin/umount -l $1 $1 + return 1 + } + + /bin/mount -o noatime,move $2/sys /sys + /bin/mount -o noatime,move $2/dev /dev + /bin/mount -o noatime,move $2/tmp /tmp + /bin/mount -o noatime,move $2/overlay /overlay 2>&- + return 0 +} + +run_ramfs() { # <command> [...] + install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \ + /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd \ + /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \ + /bin/dd /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \ + /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \ + /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \ + /bin/rm /usr/bin/basename /bin/kill /bin/chmod + + install_bin /sbin/mtd + install_bin /sbin/mount_root + install_bin /sbin/snapshot + install_bin /sbin/snapshot_tool + install_bin /usr/sbin/ubiupdatevol + install_bin /usr/sbin/ubiattach + install_bin /usr/sbin/ubiblock + install_bin /usr/sbin/ubiformat + install_bin /usr/sbin/ubidetach + install_bin /usr/sbin/ubirsvol + install_bin /usr/sbin/ubirmvol + install_bin /usr/sbin/ubimkvol + for file in $RAMFS_COPY_BIN; do + install_bin ${file//:/ } + done + install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA + + [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64 + + supivot $RAM_ROOT /mnt || { + echo "Failed to switch over to ramfs. Please reboot." + exit 1 + } + + /bin/mount -o remount,ro /mnt + /bin/umount -l /mnt + + grep /overlay /proc/mounts > /dev/null && { + /bin/mount -o noatime,remount,ro /overlay + /bin/umount -l /overlay + } + + # spawn a new shell from ramdisk to reduce the probability of cache issues + exec /bin/busybox ash -c "$*" +} + +kill_remaining() { # [ <signal> ] + local sig="${1:-TERM}" + echo -n "Sending $sig to remaining processes ... " + + local my_pid=$$ + local my_ppid=$(cut -d' ' -f4 /proc/$my_pid/stat) + local my_ppisupgraded= + grep -q upgraded /proc/$my_ppid/cmdline >/dev/null && { + local my_ppisupgraded=1 + } + + local stat + for stat in /proc/[0-9]*/stat; do + [ -f "$stat" ] || continue + + local pid name state ppid rest + read pid name state ppid rest < $stat + name="${name#(}"; name="${name%)}" + + local cmdline + read cmdline < /proc/$pid/cmdline + + # Skip kernel threads + [ -n "$cmdline" ] || continue + + if [ $$ -eq 1 ] || [ $my_ppid -eq 1 ] && [ -n "$my_ppisupgraded" ]; then + # Running as init process, kill everything except me + if [ $pid -ne $$ ] && [ $pid -ne $my_ppid ]; then + echo -n "$name " + kill -$sig $pid 2>/dev/null + fi + else + case "$name" in + # Skip essential services + *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*) : ;; + + # Killable process + *) + if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then + echo -n "$name " + kill -$sig $pid 2>/dev/null + fi + ;; + esac + fi + done + echo "" +} + +run_hooks() { + local arg="$1"; shift + for func in "$@"; do + eval "$func $arg" + done +} + +ask_bool() { + local default="$1"; shift; + local answer="$default" + + [ "$INTERACTIVE" -eq 1 ] && { + case "$default" in + 0) echo -n "$* (y/N): ";; + *) echo -n "$* (Y/n): ";; + esac + read answer + case "$answer" in + y*) answer=1;; + n*) answer=0;; + *) answer="$default";; + esac + } + [ "$answer" -gt 0 ] +} + +v() { + [ "$VERBOSE" -ge 1 ] && echo "$@" +} + +rootfs_type() { + /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }' +} + +get_image() { # <source> [ <command> ] + local from="$1" + local conc="$2" + local cmd + + case "$from" in + http://*|ftp://*) cmd="wget -O- -q";; + *) cmd="cat";; + esac + if [ -z "$conc" ]; then + local magic="$(eval $cmd \"$from\" 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')" + case "$magic" in + 1f8b) conc="zcat";; + 425a) conc="bzcat";; + esac + fi + + eval "$cmd \"$from\" 2>/dev/null ${conc:+| $conc}" +} + +get_magic_word() { + (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null +} + +get_magic_long() { + (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null +} + +jffs2_copy_config() { + if grep rootfs_data /proc/mtd >/dev/null; then + # squashfs+jffs2 + mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data + else + # jffs2 + mtd jffs2write "$CONF_TAR" rootfs + fi +} + +# Flash firmware to MTD partition +# +# $(1): path to image +# $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m +default_do_upgrade() { + sync + if [ "$SAVE_CONFIG" -eq 1 ]; then + get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}" + else + get_image "$1" "$2" | mtd write - "${PART_NAME:-image}" + fi +} + +do_upgrade() { + v "Performing system upgrade..." + if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then + platform_do_upgrade "$ARGV" + else + default_do_upgrade "$ARGV" + fi + + if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then + platform_copy_config + fi + + v "Upgrade completed" + [ -n "$DELAY" ] && sleep "$DELAY" + ask_bool 1 "Reboot" && { + v "Rebooting system..." + reboot -f + sleep 5 + echo b 2>/dev/null >/proc/sysrq-trigger + } +} diff --git a/package/base-files/files/lib/upgrade/keep.d/base-files-essential b/package/base-files/files/lib/upgrade/keep.d/base-files-essential new file mode 100644 index 0000000..978d4b5 --- /dev/null +++ b/package/base-files/files/lib/upgrade/keep.d/base-files-essential @@ -0,0 +1,10 @@ +# Essential files that will be always kept +/etc/hosts +/etc/inittab +/etc/group +/etc/passwd +/etc/profile +/etc/shadow +/etc/shells +/etc/sysctl.conf +/etc/rc.local |