diff options
Diffstat (limited to 'package/base-files/files/lib/functions')
11 files changed, 1728 insertions, 0 deletions
diff --git a/package/base-files/files/lib/functions/.svn/entries b/package/base-files/files/lib/functions/.svn/entries new file mode 100644 index 0000000..be2fb75 --- /dev/null +++ b/package/base-files/files/lib/functions/.svn/entries @@ -0,0 +1,164 @@ +10 + +dir +36060 +svn://svn.openwrt.org/openwrt/trunk/package/base-files/files/lib/functions +svn://svn.openwrt.org/openwrt + + + +2013-02-18T09:56:23.456396Z +35648 +juhosg + + + + + + + + + + + + + + +3c298f89-4303-0410-b956-a3cf2f4a3e73 + +leds.sh +file + + + + +2013-03-17T12:13:21.000000Z +4d196ba7aab9d848e0da8505d8c20ffb +2013-02-18T09:56:23.456396Z +35648 +juhosg +has-props + + + + + + + + + + + + + + + + + + + + +1274 + +network.sh +file + + + + +2013-03-17T12:13:21.000000Z +2eb07029c2d3f87fbb517a87ef1f1a4e +2013-01-22T16:47:16.091518Z +35300 +cyrus + + + + + + + + + + + + + + + + + + + + + +7941 + +service.sh +file + + + + +2013-03-17T12:13:21.000000Z +cffa973cb0e71e1d12967d843ee92179 +2012-03-20T14:29:50.022402Z +31042 +nbd + + + + + + + + + + + + + + + + + + + + + +3488 + +uci-defaults.sh +file + + + + +2013-03-17T12:13:21.000000Z +dd38ca259ffd0916168f7869ebff55e8 +2012-08-13T14:01:37.239528Z +33164 +juhosg +has-props + + + + + + + + + + + + + + + + + + + + +4252 + diff --git a/package/base-files/files/lib/functions/.svn/prop-base/leds.sh.svn-base b/package/base-files/files/lib/functions/.svn/prop-base/leds.sh.svn-base new file mode 100644 index 0000000..bdbd305 --- /dev/null +++ b/package/base-files/files/lib/functions/.svn/prop-base/leds.sh.svn-base @@ -0,0 +1,5 @@ +K 13 +svn:eol-style +V 6 +native +END diff --git a/package/base-files/files/lib/functions/.svn/prop-base/uci-defaults.sh.svn-base b/package/base-files/files/lib/functions/.svn/prop-base/uci-defaults.sh.svn-base new file mode 100644 index 0000000..bdbd305 --- /dev/null +++ b/package/base-files/files/lib/functions/.svn/prop-base/uci-defaults.sh.svn-base @@ -0,0 +1,5 @@ +K 13 +svn:eol-style +V 6 +native +END diff --git a/package/base-files/files/lib/functions/.svn/text-base/leds.sh.svn-base b/package/base-files/files/lib/functions/.svn/text-base/leds.sh.svn-base new file mode 100644 index 0000000..743c7da --- /dev/null +++ b/package/base-files/files/lib/functions/.svn/text-base/leds.sh.svn-base @@ -0,0 +1,68 @@ +#!/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 200 200 +} + +status_led_blink_failsafe() { + led_timer $status_led 50 50 +} diff --git a/package/base-files/files/lib/functions/.svn/text-base/network.sh.svn-base b/package/base-files/files/lib/functions/.svn/text-base/network.sh.svn-base new file mode 100644 index 0000000..369fa3d --- /dev/null +++ b/package/base-files/files/lib/functions/.svn/text-base/network.sh.svn-base @@ -0,0 +1,350 @@ +. /usr/share/libubox/jshn.sh + +__network_set_cache() +{ + if [ -n "$3" ]; then + eval "export -- __NETWORK_CV_$1='$3'" + __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1" + elif json_get_var "__NETWORK_CV_$1" "$2"; then + __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1" + fi +} + +__network_export() +{ + local __v="__NETWORK_CV_$2" + eval "export -- \"$1=\${$__v:+\$$__v$3}\"; [ -n \"\${$__v+x}\" ]" +} + +__network_parse_ifstatus() +{ + local __iface="$1" + local __key="${__iface}" + local __tmp + local __old_ns + + __network_export __tmp "${__key}__parsed" && return 0 + __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" + [ -n "$__tmp" ] || return 1 + + json_set_namespace "network" __old_ns + json_load "$__tmp" + + __network_set_cache "${__key}__parsed" "" "1" + + for __tmp in "" "_inactive"; do + + __key="${__key}${__tmp}" + + # parse addresses + local __family + for __family in 4 6; do + if json_is_a "ipv${__family}_address" array; then + + json_select "ipv${__family}_address" + + if json_is_a 1 object; then + + json_select 1 + __network_set_cache "${__key}_address${__family}" address + __network_set_cache "${__key}_mask${__family}" mask + json_select ".." + + fi + + json_select ".." + + fi + done + + # parse prefixes + if json_is_a "ipv6_prefix" array; then + json_select "ipv6_prefix" + + if json_is_a 1 object; then + json_select 1 + __network_set_cache "${__key}_prefix6_address" address + __network_set_cache "${__key}_prefix6_mask" mask + json_select ".." + fi + + json_select ".." + fi + + # parse routes + if json_is_a route array; then + + json_select "route" + + local __idx=1 + while json_is_a "$__idx" object; do + + json_select "$((__idx++))" + json_get_var __tmp target + + case "${__tmp}" in + 0.0.0.0) + __network_set_cache "${__key}_gateway4" nexthop + ;; + ::) + __network_set_cache "${__key}_gateway6" nexthop + ;; + esac + + json_select ".." + + done + + json_select ".." + + fi + + # parse dns info + local __field + for __field in "dns_server" "dns_search"; do + if json_is_a "$__field" array; then + + json_select "$__field" + + local __idx=1 + local __dns="" + + while json_is_a "$__idx" string; do + + json_get_var __tmp "$((__idx++))" + __dns="${__dns:+$__dns }$__tmp" + + done + + json_select ".." + + if [ -n "$__dns" ]; then + __network_set_cache "${__key}_${__field}" "" "$__dns" + fi + fi + done + + # parse up state, device and physdev + for __field in "up" "l3_device" "device"; do + if json_get_type __tmp "$__field"; then + __network_set_cache "${__key}_${__field}" "$__field" + fi + done + + # descend into inactive table + json_is_a "inactive" object && json_select "inactive" + + done + + json_cleanup + json_set_namespace "$__old_ns" + + return 0 +} + + +__network_ipaddr() +{ + local __var="$1" + local __iface="$2" + local __family="$3" + local __prefix="$4" + local __tmp + + __network_parse_ifstatus "$__iface" || return 1 + + if [ $__prefix -eq 1 ]; then + __network_export __tmp "${__iface}_mask${__family}" && \ + __network_export "$__var" "${__iface}_address${__family}" "/$__tmp" + return $? + fi + + __network_export "$__var" "${__iface}_address${__family}" + return $? + +} + +# determine IPv4 address of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddr() { __network_ipaddr "$1" "$2" 4 0; } + +# determine IPv6 address of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; } + +# determine IPv4 subnet of given logical interface +# 1: destination variable +# 2: interface +network_get_subnet() { __network_ipaddr "$1" "$2" 4 1; } + +# determine IPv6 subnet of given logical interface +# 1: destination variable +# 2: interface +network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; } + +# determine IPv6 prefix +network_get_prefix6() { + local __var="$1" + local __iface="$2" + local __address + local __mask + + __network_parse_ifstatus "$__iface" || return 1 + __network_export __mask "${__iface}_prefix6_mask" || return 1 + __network_export "$__var" "${__iface}_prefix6_address" "/$__mask" + return $? +} + + +__network_gateway() +{ + local __var="$1" + local __iface="$2" + local __family="$3" + local __inactive="$4" + + __network_parse_ifstatus "$__iface" || return 1 + + if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then + __network_export "$__var" "${__iface}_inactive_gateway${__family}" && \ + return 0 + fi + + __network_export "$__var" "${__iface}_gateway${__family}" + return $? +} + +# determine IPv4 gateway of given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive gateway if "true" (optional) +network_get_gateway() { __network_gateway "$1" "$2" 4 "${3:-0}"; } + +# determine IPv6 gateway of given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive gateway if "true" (optional) +network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; } + + +__network_dns() { + local __var="$1" + local __iface="$2" + local __field="$3" + local __inactive="$4" + + __network_parse_ifstatus "$__iface" || return 1 + + if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then + __network_export "$__var" "${__iface}_inactive_${__field}" && \ + return 0 + fi + + __network_export "$__var" "${__iface}_${__field}" + return $? +} + +# 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_dns "$1" "$2" dns_server "${3:-0}"; } + +# determine the domains of the given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive domains if "true" (optional) +network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; } + + +__network_wan() +{ + local __var="$1" + local __family="$2" + local __inactive="$3" + local __iface + + for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do + if [ "$__iface" != loopback ]; then + if __network_gateway "$__var" "$__iface" "$__family" "$__inactive"; then + eval "export -- \"$__var=$__iface\"" + return 0 + fi + fi + done + + eval "export -- \"$__var=\"" + return 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" 4 "${2:-0}"; } + +# 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" 6 "${2:-0}"; } + + +__network_device() +{ + local __var="$1" + local __iface="$2" + local __field="$3" + + __network_parse_ifstatus "$__iface" || return 1 + __network_export "$__var" "${__iface}_${__field}" + return $? +} + +# test whether the given logical interface is running +# 1: interface +network_is_up() +{ + local __up + __network_device __up "$1" up && [ $__up -eq 1 ] +} + +# determine the layer 3 linux network device of the given logical interface +# 1: destination variable +# 2: interface +network_get_device() { __network_device "$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_device "$1" "$2" device; } + + +__network_defer() +{ + local __device="$1" + local __defer="$2" + + json_init + json_add_string name "$__device" + json_add_boolean defer "$__defer" + + ubus call network.device set_state "$(json_dump)" 2>/dev/null +} + +# defer netifd actions on the given linux network device +# 1: device name +network_defer_device() { __network_defer "$1" 1; } + +# continue netifd actions on the given linux network device +# 1: device name +network_ready_device() { __network_defer "$1" 0; } + +# flush the internal value cache to force re-reading values from ubus +network_flush_cache() +{ + local __tmp + for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do + unset "$__tmp" + done +} diff --git a/package/base-files/files/lib/functions/.svn/text-base/service.sh.svn-base b/package/base-files/files/lib/functions/.svn/text-base/service.sh.svn-base new file mode 100644 index 0000000..cbb717d --- /dev/null +++ b/package/base-files/files/lib/functions/.svn/text-base/service.sh.svn-base @@ -0,0 +1,129 @@ +# +# 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 "$@" +} + +service_kill() { + cat 1>&2 << __END_OF_WARNING__ +# +# WARNING: the 'service_kill' function is now deprecated and might be +# removed soon. Consider using the other new service_* wrappers instead. +# +__END_OF_WARNING__ + local name="${1}" + local pid="${2:-$(pidof "$name")}" + local grace="${3:-5}" + + [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)" + + for pid in $pid; do + [ -d "/proc/$pid" ] || continue + local try=0 + kill -TERM $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done + kill -KILL $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done + done +} + + + diff --git a/package/base-files/files/lib/functions/.svn/text-base/uci-defaults.sh.svn-base b/package/base-files/files/lib/functions/.svn/text-base/uci-defaults.sh.svn-base new file mode 100644 index 0000000..477c00c --- /dev/null +++ b/package/base-files/files/lib/functions/.svn/text-base/uci-defaults.sh.svn-base @@ -0,0 +1,230 @@ +#!/bin/sh +# Copyright (C) 2011 OpenWrt.org + +UCIDEF_LEDS_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_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_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' +EOF +} + +ucidef_set_interface_raw() { + local cfg=$1 + local ifname=$2 + + uci batch <<EOF +set network.$cfg='interface' +set network.$cfg.ifname='$ifname' +set network.$cfg.proto='none' +EOF +} + +ucidef_set_interface_lan() { + local ifname=$1 + + uci batch <<EOF +set network.lan='interface' +set network.lan.ifname='$ifname' +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' +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' +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 +} + 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..743c7da --- /dev/null +++ b/package/base-files/files/lib/functions/leds.sh @@ -0,0 +1,68 @@ +#!/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 200 200 +} + +status_led_blink_failsafe() { + led_timer $status_led 50 50 +} 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..369fa3d --- /dev/null +++ b/package/base-files/files/lib/functions/network.sh @@ -0,0 +1,350 @@ +. /usr/share/libubox/jshn.sh + +__network_set_cache() +{ + if [ -n "$3" ]; then + eval "export -- __NETWORK_CV_$1='$3'" + __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1" + elif json_get_var "__NETWORK_CV_$1" "$2"; then + __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1" + fi +} + +__network_export() +{ + local __v="__NETWORK_CV_$2" + eval "export -- \"$1=\${$__v:+\$$__v$3}\"; [ -n \"\${$__v+x}\" ]" +} + +__network_parse_ifstatus() +{ + local __iface="$1" + local __key="${__iface}" + local __tmp + local __old_ns + + __network_export __tmp "${__key}__parsed" && return 0 + __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" + [ -n "$__tmp" ] || return 1 + + json_set_namespace "network" __old_ns + json_load "$__tmp" + + __network_set_cache "${__key}__parsed" "" "1" + + for __tmp in "" "_inactive"; do + + __key="${__key}${__tmp}" + + # parse addresses + local __family + for __family in 4 6; do + if json_is_a "ipv${__family}_address" array; then + + json_select "ipv${__family}_address" + + if json_is_a 1 object; then + + json_select 1 + __network_set_cache "${__key}_address${__family}" address + __network_set_cache "${__key}_mask${__family}" mask + json_select ".." + + fi + + json_select ".." + + fi + done + + # parse prefixes + if json_is_a "ipv6_prefix" array; then + json_select "ipv6_prefix" + + if json_is_a 1 object; then + json_select 1 + __network_set_cache "${__key}_prefix6_address" address + __network_set_cache "${__key}_prefix6_mask" mask + json_select ".." + fi + + json_select ".." + fi + + # parse routes + if json_is_a route array; then + + json_select "route" + + local __idx=1 + while json_is_a "$__idx" object; do + + json_select "$((__idx++))" + json_get_var __tmp target + + case "${__tmp}" in + 0.0.0.0) + __network_set_cache "${__key}_gateway4" nexthop + ;; + ::) + __network_set_cache "${__key}_gateway6" nexthop + ;; + esac + + json_select ".." + + done + + json_select ".." + + fi + + # parse dns info + local __field + for __field in "dns_server" "dns_search"; do + if json_is_a "$__field" array; then + + json_select "$__field" + + local __idx=1 + local __dns="" + + while json_is_a "$__idx" string; do + + json_get_var __tmp "$((__idx++))" + __dns="${__dns:+$__dns }$__tmp" + + done + + json_select ".." + + if [ -n "$__dns" ]; then + __network_set_cache "${__key}_${__field}" "" "$__dns" + fi + fi + done + + # parse up state, device and physdev + for __field in "up" "l3_device" "device"; do + if json_get_type __tmp "$__field"; then + __network_set_cache "${__key}_${__field}" "$__field" + fi + done + + # descend into inactive table + json_is_a "inactive" object && json_select "inactive" + + done + + json_cleanup + json_set_namespace "$__old_ns" + + return 0 +} + + +__network_ipaddr() +{ + local __var="$1" + local __iface="$2" + local __family="$3" + local __prefix="$4" + local __tmp + + __network_parse_ifstatus "$__iface" || return 1 + + if [ $__prefix -eq 1 ]; then + __network_export __tmp "${__iface}_mask${__family}" && \ + __network_export "$__var" "${__iface}_address${__family}" "/$__tmp" + return $? + fi + + __network_export "$__var" "${__iface}_address${__family}" + return $? + +} + +# determine IPv4 address of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddr() { __network_ipaddr "$1" "$2" 4 0; } + +# determine IPv6 address of given logical interface +# 1: destination variable +# 2: interface +network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; } + +# determine IPv4 subnet of given logical interface +# 1: destination variable +# 2: interface +network_get_subnet() { __network_ipaddr "$1" "$2" 4 1; } + +# determine IPv6 subnet of given logical interface +# 1: destination variable +# 2: interface +network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; } + +# determine IPv6 prefix +network_get_prefix6() { + local __var="$1" + local __iface="$2" + local __address + local __mask + + __network_parse_ifstatus "$__iface" || return 1 + __network_export __mask "${__iface}_prefix6_mask" || return 1 + __network_export "$__var" "${__iface}_prefix6_address" "/$__mask" + return $? +} + + +__network_gateway() +{ + local __var="$1" + local __iface="$2" + local __family="$3" + local __inactive="$4" + + __network_parse_ifstatus "$__iface" || return 1 + + if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then + __network_export "$__var" "${__iface}_inactive_gateway${__family}" && \ + return 0 + fi + + __network_export "$__var" "${__iface}_gateway${__family}" + return $? +} + +# determine IPv4 gateway of given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive gateway if "true" (optional) +network_get_gateway() { __network_gateway "$1" "$2" 4 "${3:-0}"; } + +# determine IPv6 gateway of given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive gateway if "true" (optional) +network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; } + + +__network_dns() { + local __var="$1" + local __iface="$2" + local __field="$3" + local __inactive="$4" + + __network_parse_ifstatus "$__iface" || return 1 + + if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then + __network_export "$__var" "${__iface}_inactive_${__field}" && \ + return 0 + fi + + __network_export "$__var" "${__iface}_${__field}" + return $? +} + +# 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_dns "$1" "$2" dns_server "${3:-0}"; } + +# determine the domains of the given logical interface +# 1: destination variable +# 2: interface +# 3: consider inactive domains if "true" (optional) +network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; } + + +__network_wan() +{ + local __var="$1" + local __family="$2" + local __inactive="$3" + local __iface + + for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do + if [ "$__iface" != loopback ]; then + if __network_gateway "$__var" "$__iface" "$__family" "$__inactive"; then + eval "export -- \"$__var=$__iface\"" + return 0 + fi + fi + done + + eval "export -- \"$__var=\"" + return 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" 4 "${2:-0}"; } + +# 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" 6 "${2:-0}"; } + + +__network_device() +{ + local __var="$1" + local __iface="$2" + local __field="$3" + + __network_parse_ifstatus "$__iface" || return 1 + __network_export "$__var" "${__iface}_${__field}" + return $? +} + +# test whether the given logical interface is running +# 1: interface +network_is_up() +{ + local __up + __network_device __up "$1" up && [ $__up -eq 1 ] +} + +# determine the layer 3 linux network device of the given logical interface +# 1: destination variable +# 2: interface +network_get_device() { __network_device "$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_device "$1" "$2" device; } + + +__network_defer() +{ + local __device="$1" + local __defer="$2" + + json_init + json_add_string name "$__device" + json_add_boolean defer "$__defer" + + ubus call network.device set_state "$(json_dump)" 2>/dev/null +} + +# defer netifd actions on the given linux network device +# 1: device name +network_defer_device() { __network_defer "$1" 1; } + +# continue netifd actions on the given linux network device +# 1: device name +network_ready_device() { __network_defer "$1" 0; } + +# flush the internal value cache to force re-reading values from ubus +network_flush_cache() +{ + local __tmp + for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do + unset "$__tmp" + done +} 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..cbb717d --- /dev/null +++ b/package/base-files/files/lib/functions/service.sh @@ -0,0 +1,129 @@ +# +# 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 "$@" +} + +service_kill() { + cat 1>&2 << __END_OF_WARNING__ +# +# WARNING: the 'service_kill' function is now deprecated and might be +# removed soon. Consider using the other new service_* wrappers instead. +# +__END_OF_WARNING__ + local name="${1}" + local pid="${2:-$(pidof "$name")}" + local grace="${3:-5}" + + [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)" + + for pid in $pid; do + [ -d "/proc/$pid" ] || continue + local try=0 + kill -TERM $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done + kill -KILL $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done + done +} + + + 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..477c00c --- /dev/null +++ b/package/base-files/files/lib/functions/uci-defaults.sh @@ -0,0 +1,230 @@ +#!/bin/sh +# Copyright (C) 2011 OpenWrt.org + +UCIDEF_LEDS_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_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_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' +EOF +} + +ucidef_set_interface_raw() { + local cfg=$1 + local ifname=$2 + + uci batch <<EOF +set network.$cfg='interface' +set network.$cfg.ifname='$ifname' +set network.$cfg.proto='none' +EOF +} + +ucidef_set_interface_lan() { + local ifname=$1 + + uci batch <<EOF +set network.lan='interface' +set network.lan.ifname='$ifname' +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' +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' +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 +} + |