aboutsummaryrefslogtreecommitdiffstats
path: root/package/base-files/files/lib/functions
diff options
context:
space:
mode:
authorblogic <blogic@3c298f89-4303-0410-b956-a3cf2f4a3e73>2012-10-05 10:12:53 +0000
committerblogic <blogic@3c298f89-4303-0410-b956-a3cf2f4a3e73>2012-10-05 10:12:53 +0000
commit5c105d9f3fd086aff195d3849dcf847d6b0bd927 (patch)
tree1229a11f725bfa58aa7c57a76898553bb5f6654a /package/base-files/files/lib/functions
downloadopenwrt-5c105d9f3fd086aff195d3849dcf847d6b0bd927.tar.gz
openwrt-5c105d9f3fd086aff195d3849dcf847d6b0bd927.zip
branch Attitude Adjustment
git-svn-id: svn://svn.openwrt.org/openwrt/branches/attitude_adjustment@33625 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/base-files/files/lib/functions')
-rw-r--r--package/base-files/files/lib/functions/boot.sh150
-rw-r--r--package/base-files/files/lib/functions/network.sh170
-rw-r--r--package/base-files/files/lib/functions/service.sh129
-rw-r--r--package/base-files/files/lib/functions/uci-defaults.sh230
4 files changed, 679 insertions, 0 deletions
diff --git a/package/base-files/files/lib/functions/boot.sh b/package/base-files/files/lib/functions/boot.sh
new file mode 100644
index 000000000..8c3f27ba4
--- /dev/null
+++ b/package/base-files/files/lib/functions/boot.sh
@@ -0,0 +1,150 @@
+#!/bin/sh
+# Copyright (C) 2006-2010 OpenWrt.org
+# Copyright (C) 2010 Vertical Communications
+
+mount() {
+ /bin/busybox mount -o noatime "$@"
+}
+
+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
+}
+
+find_mtd_part() {
+ local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
+ local PREFIX=/dev/mtdblock
+
+ PART="${PART##mtd}"
+ [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
+ echo "${PART:+$PREFIX$PART}"
+}
+
+jffs2_ready () {
+ mtdpart="$(find_mtd_part rootfs_data)"
+ [ -z "$mtdpart" ] && return 1
+ magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
+ [ "$magic" != "deadc0de" ]
+}
+
+dupe() { # <new_root> <old_root>
+ cd $1
+ echo -n "creating directories... "
+ {
+ cd $2
+ find . -xdev -type d
+ echo "./dev ./overlay ./mnt ./proc ./tmp"
+ # xdev skips mounted directories
+ cd $1
+ } | xargs mkdir -p
+ echo "done"
+
+ echo -n "setting up symlinks... "
+ for file in $(cd $2; find . -xdev -type f;); do
+ case "$file" in
+ ./rom/note) ;; #nothing
+ ./etc/config*|\
+ ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
+ *) ln -sf /rom/${file#./*} $file;;
+ esac
+ done
+ for file in $(cd $2; find . -xdev -type l;); do
+ cp -af $2/${file#./*} $file
+ done
+ echo "done"
+}
+
+pivot() { # <new_root> <old_root>
+ mount -o move /proc $1/proc && \
+ pivot_root $1 $1$2 && {
+ mount -o move $2/dev /dev
+ mount -o move $2/tmp /tmp
+ mount -o move $2/sys /sys 2>&-
+ mount -o move $2/overlay /overlay 2>&-
+ return 0
+ }
+}
+
+fopivot() { # <rw_root> <ro_root> <dupe?>
+ root=$1
+ {
+ if grep -q overlay /proc/filesystems; then
+ mount -t overlayfs -olowerdir=/,upperdir=$1 "overlayfs:$1" /mnt && root=/mnt
+ elif grep -q mini_fo /proc/filesystems; then
+ mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
+ else
+ mount --bind / /mnt
+ mount --bind -o union "$1" /mnt && root=/mnt
+ fi
+ } || {
+ [ "$3" = "1" ] && {
+ mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
+ dupe $1 $rom
+ }
+ }
+ pivot $root $2
+}
+
+ramoverlay() {
+ mkdir -p /tmp/root
+ mount -t tmpfs -o mode=0755 root /tmp/root
+ fopivot /tmp/root /rom 1
+}
+
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 000000000..a4652569b
--- /dev/null
+++ b/package/base-files/files/lib/functions/network.sh
@@ -0,0 +1,170 @@
+. /usr/share/libubox/jshn.sh
+
+__network_ipaddr()
+{
+ local __var="$1"
+ local __iface="$2"
+ local __family="$3"
+ local __prefix="${4:-0}"
+
+ local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
+
+ json_load "${__tmp:-{}}"
+ json_get_type __tmp "ipv${__family}_address"
+
+ if [ "$__tmp" = array ]; then
+
+ json_select "ipv${__family}_address"
+ json_get_type __tmp 1
+
+ if [ "$__tmp" = object ]; then
+
+ json_select 1
+ json_get_var $__var address
+
+ [ $__prefix -gt 0 ] && {
+ json_get_var __tmp mask
+ eval "export -- \"$__var=\${$__var}/$__tmp\""
+ }
+
+ return 0
+ fi
+ fi
+
+ return 1
+}
+
+network_get_ipaddr() { __network_ipaddr "$1" "$2" 4 0; }
+network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; }
+
+network_get_subnet() { __network_ipaddr "$1" "$2" 4 1; }
+network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; }
+
+
+__network_gateway()
+{
+ local __var="$1"
+ local __iface="$2"
+ local __family="$3"
+
+ local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
+ local __idx=1
+
+ json_load "${__tmp:-{}}"
+
+ if json_get_type __tmp route && [ "$__tmp" = array ]; then
+
+ json_select route
+
+ while json_get_type __tmp "$__idx" && [ "$__tmp" = object ]; do
+
+ json_select "$((__idx++))"
+ json_get_var __tmp target
+
+ case "${__family}/${__tmp}" in
+ 4/0.0.0.0|6/::)
+ json_get_var "$__var" nexthop
+ return $?
+ ;;
+ esac
+
+ json_select ".."
+
+ done
+ fi
+
+ return 1
+}
+
+network_get_gateway() { __network_gateway "$1" "$2" 4; }
+network_get_gateway6() { __network_gateway "$1" "$2" 6; }
+
+
+__network_dns() {
+ local __var="$1"
+ local __iface="$2"
+ local __field="$3"
+
+ local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
+ local __dns=""
+ local __idx=1
+
+ json_load "${__tmp:-{}}"
+
+ if json_get_type __tmp "$__field" && [ "$__tmp" = array ]; then
+
+ json_select "$__field"
+
+ while json_get_type __tmp "$__idx" && [ "$__tmp" = string ]; do
+
+ json_get_var __tmp "$((__idx++))"
+ __dns="${__dns:+$__dns }$__tmp"
+
+ done
+ fi
+
+ eval "export -- \"$__var=$__dns\""
+ [ -n "$__dns" ]
+}
+
+network_get_dnsserver() { __network_dns "$1" "$2" dns_server; }
+network_get_dnssearch() { __network_dns "$1" "$2" dns_search; }
+
+
+__network_wan() {
+ local __var="$1"
+ local __family="$2"
+ local __iface
+
+ for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
+ if __network_gateway "$__var" "$__iface" "$__family"; then
+ eval "export -- \"$__var=$__iface\""
+ return 0
+ fi
+ done
+
+ eval "export -- \"$__var=\""
+ return 1
+}
+
+network_find_wan() { __network_wan "$1" 4; }
+network_find_wan6() { __network_wan "$1" 6; }
+
+
+__network_device()
+{
+ local __var="$1"
+ local __iface="$2"
+ local __field="$3"
+
+ local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
+ [ -n "$__tmp" ] || return 1
+
+ json_load "$__tmp"
+ json_get_var "$__var" "$__field"
+}
+
+network_is_up()
+{
+ local __up
+ __network_device __up "$1" up && [ $__up -eq 1 ]
+}
+
+network_get_device() { __network_device "$1" "$2" l3_device; }
+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
+}
+
+network_defer_device() { __network_defer "$1" 1; }
+network_ready_device() { __network_defer "$1" 0; }
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 000000000..cbb717d4d
--- /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 000000000..477c00cfd
--- /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
+}
+