2023-08-08 07:47:01 +00:00
|
|
|
which()
|
|
|
|
{
|
|
|
|
# on some systems 'which' command is considered deprecated and not installed by default
|
|
|
|
# 'command -v' replacement does not work exactly the same way. it outputs shell aliases if present
|
|
|
|
# $1 - executable name
|
|
|
|
local IFS=:
|
|
|
|
for p in $PATH; do
|
|
|
|
[ -x "$p/$1" ] && {
|
|
|
|
echo "$p/$1"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
2022-02-15 14:15:36 +00:00
|
|
|
exists()
|
|
|
|
{
|
|
|
|
which "$1" >/dev/null 2>/dev/null
|
|
|
|
}
|
|
|
|
existf()
|
|
|
|
{
|
|
|
|
type "$1" >/dev/null 2>/dev/null
|
|
|
|
}
|
|
|
|
whichq()
|
|
|
|
{
|
|
|
|
which $1 2>/dev/null
|
|
|
|
}
|
|
|
|
exist_all()
|
|
|
|
{
|
|
|
|
while [ -n "$1" ]; do
|
|
|
|
exists "$1" || return 1
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
on_off_function()
|
|
|
|
{
|
|
|
|
# $1 : function name on
|
|
|
|
# $2 : function name off
|
|
|
|
# $3 : 0 - off, 1 - on
|
|
|
|
local F="$1"
|
|
|
|
[ "$3" = "1" ] || F="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
"$F" "$@"
|
|
|
|
}
|
|
|
|
contains()
|
|
|
|
{
|
|
|
|
# check if substring $2 contains in $1
|
|
|
|
[ "${1#*$2}" != "$1" ]
|
|
|
|
}
|
|
|
|
find_str_in_list()
|
|
|
|
{
|
|
|
|
[ -n "$1" ] && {
|
|
|
|
for v in $2; do
|
|
|
|
[ "$v" = "$1" ] && return 0
|
|
|
|
done
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
end_with_newline()
|
|
|
|
{
|
|
|
|
local c=$(tail -c 1)
|
|
|
|
[ "$c" = "" ]
|
|
|
|
}
|
2022-02-16 18:48:02 +00:00
|
|
|
|
|
|
|
append_separator_list()
|
2022-02-15 14:15:36 +00:00
|
|
|
{
|
|
|
|
# $1 - var name to receive result
|
|
|
|
# $2 - separator
|
2022-02-16 18:48:02 +00:00
|
|
|
# $3 - quoter
|
|
|
|
# $4,$5,... - elements
|
|
|
|
local _var="$1" sep="$2" quo="$3" i
|
2022-02-15 14:15:36 +00:00
|
|
|
|
2022-02-16 18:48:02 +00:00
|
|
|
eval i="\$$_var"
|
|
|
|
shift; shift; shift
|
2022-02-15 14:15:36 +00:00
|
|
|
while [ -n "$1" ]; do
|
|
|
|
if [ -n "$i" ] ; then
|
2022-02-16 18:48:02 +00:00
|
|
|
i="$i$sep$quo$1$quo"
|
2022-02-15 14:15:36 +00:00
|
|
|
else
|
2022-02-16 18:48:02 +00:00
|
|
|
i="$quo$1$quo"
|
2022-02-15 14:15:36 +00:00
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
2022-02-16 18:48:02 +00:00
|
|
|
eval $_var="\$i"
|
|
|
|
}
|
|
|
|
make_separator_list()
|
|
|
|
{
|
|
|
|
eval $1=''
|
|
|
|
append_separator_list "$@"
|
2022-02-15 14:15:36 +00:00
|
|
|
}
|
|
|
|
make_comma_list()
|
|
|
|
{
|
|
|
|
# $1 - var name to receive result
|
|
|
|
# $2,$3,... - elements
|
|
|
|
local var="$1"
|
|
|
|
shift
|
2022-02-16 18:48:02 +00:00
|
|
|
make_separator_list $var , '' "$@"
|
2022-02-15 14:15:36 +00:00
|
|
|
}
|
2022-02-16 19:08:01 +00:00
|
|
|
make_quoted_comma_list()
|
|
|
|
{
|
|
|
|
# $1 - var name to receive result
|
|
|
|
# $2,$3,... - elements
|
|
|
|
local var="$1"
|
|
|
|
shift
|
|
|
|
make_separator_list $var , '"' "$@"
|
|
|
|
}
|
2022-02-15 20:11:43 +00:00
|
|
|
unique()
|
|
|
|
{
|
|
|
|
local i
|
|
|
|
for i in "$@"; do echo $i; done | sort -u | xargs
|
|
|
|
}
|
2022-02-15 14:15:36 +00:00
|
|
|
|
|
|
|
is_linked_to_busybox()
|
|
|
|
{
|
|
|
|
local IFS F P
|
|
|
|
|
|
|
|
IFS=:
|
|
|
|
for path in $PATH; do
|
|
|
|
F=$path/$1
|
|
|
|
P="$(readlink $F)"
|
|
|
|
if [ -z "$P" ] && [ -x $F ] && [ ! -L $F ]; then return 1; fi
|
|
|
|
[ "${P%busybox*}" != "$P" ] && return
|
|
|
|
done
|
|
|
|
}
|
|
|
|
get_dir_inode()
|
|
|
|
{
|
|
|
|
local dir="$1"
|
|
|
|
[ -L "$dir" ] && dir=$(readlink "$dir")
|
|
|
|
ls -id "$dir" | awk '{print $1}'
|
|
|
|
}
|
|
|
|
|
|
|
|
linux_min_version()
|
|
|
|
{
|
|
|
|
# $1 - major ver
|
|
|
|
# $2 - minor ver
|
|
|
|
local V1=$(sed -nre 's/^Linux version ([0-9]+)\.[0-9]+.*$/\1/p' /proc/version)
|
|
|
|
local V2=$(sed -nre 's/^Linux version [0-9]+\.([0-9]+).*$/\1/p' /proc/version)
|
|
|
|
[ -n "$V1" -a -n "$V2" ] && [ "$V1" -gt "$1" -o "$V1" -eq "$1" -a "$V2" -ge "$2" ]
|
|
|
|
}
|
|
|
|
linux_get_subsys()
|
|
|
|
{
|
|
|
|
local INIT=$(sed 's/\x0/\n/g' /proc/1/cmdline | head -n 1)
|
|
|
|
|
|
|
|
[ -L "$INIT" ] && INIT=$(readlink "$INIT")
|
|
|
|
INIT=$(basename "$INIT")
|
|
|
|
if [ -f "/etc/openwrt_release" ] && [ "$INIT" = "procd" ] ; then
|
|
|
|
SUBSYS=openwrt
|
|
|
|
else
|
|
|
|
# generic linux
|
|
|
|
SUBSYS=
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
openwrt_fw3()
|
|
|
|
{
|
|
|
|
[ ! -x /sbin/fw4 -a -x /sbin/fw3 ]
|
|
|
|
}
|
|
|
|
openwrt_fw4()
|
|
|
|
{
|
|
|
|
[ -x /sbin/fw4 ]
|
|
|
|
}
|
|
|
|
openwrt_fw3_integration()
|
|
|
|
{
|
|
|
|
[ "$FWTYPE" = iptables ] && openwrt_fw3
|
|
|
|
}
|
|
|
|
|
|
|
|
create_dev_stdin()
|
|
|
|
{
|
|
|
|
[ -e /dev/stdin ] || ln -s /proc/self/fd/0 /dev/stdin
|
|
|
|
}
|
2022-02-15 20:11:43 +00:00
|
|
|
|
|
|
|
call_for_multiple_items()
|
|
|
|
{
|
|
|
|
# $1 - function to get an item
|
|
|
|
# $2 - variable name to put result into
|
|
|
|
# $3 - space separated parameters to function $1
|
|
|
|
|
|
|
|
local i item items
|
|
|
|
for i in $3; do
|
|
|
|
$1 item $i
|
|
|
|
[ -n "$item" ] && {
|
|
|
|
if [ -n "$items" ]; then
|
|
|
|
items="$items $item"
|
|
|
|
else
|
|
|
|
items="$item"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
done
|
|
|
|
eval $2=\"$items\"
|
|
|
|
}
|
2022-03-27 15:58:47 +00:00
|
|
|
|
|
|
|
fix_sbin_path()
|
|
|
|
{
|
|
|
|
local IFS=':'
|
|
|
|
printf "%s\n" $PATH | grep -Fxq '/usr/sbin' || PATH="/usr/sbin:$PATH"
|
|
|
|
printf "%s\n" $PATH | grep -Fxq '/sbin' || PATH="/sbin:$PATH"
|
|
|
|
export PATH
|
|
|
|
}
|
2023-10-31 11:19:41 +00:00
|
|
|
|
2023-11-03 07:08:21 +00:00
|
|
|
# it can calculate floating point expr
|
|
|
|
calc()
|
|
|
|
{
|
|
|
|
awk "BEGIN { print $*}";
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:19:41 +00:00
|
|
|
fsleep_setup()
|
|
|
|
{
|
|
|
|
[ -n "$FSLEEP" ] || {
|
2023-10-31 13:42:38 +00:00
|
|
|
if sleep 0.001 2>/dev/null; then
|
2023-10-31 11:19:41 +00:00
|
|
|
FSLEEP=1
|
|
|
|
elif busybox usleep 1 2>/dev/null; then
|
|
|
|
FSLEEP=2
|
|
|
|
else
|
|
|
|
local errtext=$(read -t 0.001 2>&1)
|
|
|
|
if [ -z "$errtext" ]; then
|
|
|
|
FSLEEP=3
|
2023-10-31 13:19:49 +00:00
|
|
|
# newer openwrt has ucode with system function that supports timeout in ms
|
2023-11-02 18:29:26 +00:00
|
|
|
elif ucode -e "system(['sleep','1'], 1)" 2>/dev/null; then
|
2023-10-31 13:19:49 +00:00
|
|
|
FSLEEP=4
|
2023-11-02 18:29:26 +00:00
|
|
|
# older openwrt may have lua and nixio lua module
|
2023-11-03 06:47:15 +00:00
|
|
|
elif lua -e 'require "nixio".nanosleep(0,1)' 2>/dev/null ; then
|
2023-11-02 18:29:26 +00:00
|
|
|
FSLEEP=5
|
2023-10-31 11:19:41 +00:00
|
|
|
else
|
2023-11-02 18:40:45 +00:00
|
|
|
FSLEEP=0
|
2023-10-31 11:19:41 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
2023-11-03 07:08:21 +00:00
|
|
|
msleep()
|
2023-10-31 11:19:41 +00:00
|
|
|
{
|
2023-11-03 07:08:21 +00:00
|
|
|
# $1 - milliseconds
|
2023-10-31 11:19:41 +00:00
|
|
|
case "$FSLEEP" in
|
|
|
|
1)
|
2023-11-03 07:08:21 +00:00
|
|
|
sleep $(calc $1/1000)
|
2023-10-31 11:19:41 +00:00
|
|
|
;;
|
|
|
|
2)
|
2023-11-03 07:08:21 +00:00
|
|
|
busybox usleep $(calc $1*1000)
|
2023-10-31 11:19:41 +00:00
|
|
|
;;
|
|
|
|
3)
|
2023-11-03 07:08:21 +00:00
|
|
|
read -t $(calc $1/1000)
|
2023-10-31 11:19:41 +00:00
|
|
|
;;
|
2023-10-31 13:19:49 +00:00
|
|
|
4)
|
2023-11-03 07:08:21 +00:00
|
|
|
ucode -e "system(['sleep','2147483647'], $1)"
|
2023-10-31 13:19:49 +00:00
|
|
|
;;
|
2023-11-02 18:29:26 +00:00
|
|
|
5)
|
2023-11-03 07:08:21 +00:00
|
|
|
lua -e "require 'nixio'.nanosleep($(($1/1000)),$(calc $1%1000*1000000))"
|
2023-11-02 18:29:26 +00:00
|
|
|
;;
|
2023-10-31 11:19:41 +00:00
|
|
|
*)
|
2023-11-03 07:45:18 +00:00
|
|
|
sleep $((($1+999)/1000))
|
2023-10-31 11:19:41 +00:00
|
|
|
esac
|
|
|
|
}
|
2023-11-03 07:08:21 +00:00
|
|
|
minsleep()
|
|
|
|
{
|
|
|
|
msleep 100
|
|
|
|
}
|