diff options
Diffstat (limited to 'net-scripts/net.modules.d/helpers.d/functions')
-rw-r--r-- | net-scripts/net.modules.d/helpers.d/functions | 134 |
1 files changed, 51 insertions, 83 deletions
diff --git a/net-scripts/net.modules.d/helpers.d/functions b/net-scripts/net.modules.d/helpers.d/functions index 52f3da1..ff67435 100644 --- a/net-scripts/net.modules.d/helpers.d/functions +++ b/net-scripts/net.modules.d/helpers.d/functions @@ -60,23 +60,23 @@ remove_state() { apply_state() { local iface="$1" - if [[ -z ${iface} ]]; then - iface=$( select_best_interface ) + if [[ -z ${iface} ]] ; then + iface="$(select_best_interface)" [[ -z ${iface} ]] && return fi local d="${statedir}/${iface}" - if [[ -d ${d} ]]; then - local files=$( ls "${d}" ) + if [[ -d ${d} ]] ; then + local files="$(ls "${d}")" if [[ -n ${files} ]] ; then - if [[ ${RC_AUTO_INTERFACE} == "yes" ]]; then + if [[ ${RC_AUTO_INTERFACE} == "yes" ]] ; then cp -pPR "${d}"/* "${netdir}" local file for file in ${files} ; do # Skip .sv files [[ ${file} == *".sv" ]] && continue - local link=$( readlink "/etc/${file}" 2>/dev/null ) - if [[ ${link} != "${netdir}/${file}" ]]; then + local link="$(readlink "/etc/${file}" 2>/dev/null)" + if [[ ${link} != "${netdir}/${file}" ]] ; then [[ -e "/etc/${file}" ]] && rm -f "/etc/${file}" ln -snf "${netdir}/${file}" "/etc/${file}" fi @@ -99,8 +99,9 @@ order_interfaces() { local ifaces extra ${1:-false} && extra=' && $2=="00000000"' - ifaces=$(awk '{if (NR>1 && $1!="lo"'"${extra}"') print $7, $1}' /proc/net/route \ - | sort -n | cut -d' ' -f2 | uniq) + ifaces="$(awk '{if (NR>1 && $1!="lo"'"${extra}"') print $7, $1}' \ + /proc/net/route \ + | sort -n | cut -d' ' -f2 | uniq)" # Append lo if it's up if ! ${1:-false} ; then @@ -140,16 +141,18 @@ merge_resolv() { j=0 for (( i=0; i<${#ifaces[@]}; i++ )); do f="${statedir}/${ifaces[i]}/resolv.conf" - srvs[i]=$( sed -n -e 's/^[ \t]*nameserver[ \t]*\([^#]*\).*/\1/p' "${f}" \ - | sed 2q ) + srvs[i]="$(sed -n -e 's/^[ \t]*nameserver[ \t]*\([^#]*\).*/\1/p' \ + "${f}" | sed 2q)" - search[i]=$( sed -n -e 's/^[ \t]*\(domain\|search\)[ \t]*\([^#]*\).*/\2/p' \ - "${f}" | sed -e '$!d' ) + search[i]="$(sed -n -e 's/^[ \t]*\(domain\|search\)[ \t]*\([^#]*\).*/\2/p' \ + "${f}" | sed -e '$!d')" - opts[i]=$( sed -n -e 's/^[ \t]*options[ \t]*\([^#]*\).*/\1#/p;' "${f}" | xargs ) - sortlist[i]=$( sed -n -e 's/^[ \t]*sortlist[ \t]*\([^#]*\).*/\1/p' "${f}" ) + opts[i]="$(sed -n -e 's/^[ \t]*options[ \t]*\([^#]*\).*/\1#/p;' "${f}" \ + | xargs )" + sortlist[i]="$(sed -n -e 's/^[ \t]*sortlist[ \t]*\([^#]*\).*/\1/p' \ + "${f}")" - if [[ -z ${srvs[i]} && -z ${opts[i]} && -z ${sortlist[i]} && -z ${search[i]} ]]; then + if [[ -z ${srvs[i]} && -z ${opts[i]} && -z ${sortlist[i]} && -z ${search[i]} ]] ; then unset srvs[i] unset search[i] unset opts[i] @@ -171,7 +174,7 @@ merge_resolv() { # Add interface primary nameservers for (( i=0;i<${#srvs[@]}; i++ )); do local -a n=( ${srvs[i]} ) - if [[ " ${new_srvs} " != *" ${n[0]} "* ]]; then + if [[ " ${new_srvs} " != *" ${n[0]} "* ]] ; then new_srvs="${new_srvs} ${n[0]}" # libc can only handle 3 name servers (( j++ )) @@ -180,11 +183,11 @@ merge_resolv() { done # Add interface secondary nameservers - if [[ ${j} -lt 3 ]]; then + if [[ ${j} -lt 3 ]] ; then for (( i=0;i<${#srvs[@]}; i++ )); do local -a n=( ${srvs[i]} ) [[ -z ${n[1]} ]] && continue - if [[ " ${new_srvs} " != *" ${n[1]} "* ]]; then + if [[ " ${new_srvs} " != *" ${n[1]} "* ]] ; then new_srvs="${new_srvs} ${n[1]}" # libc can only handle 3 name servers (( j++ )) @@ -195,7 +198,7 @@ merge_resolv() { local new_search n_search=0 for i in ${search[@]}; do - if [[ " ${new_search} " != *" ${i} "* ]]; then + if [[ " ${new_search} " != *" ${i} "* ]] ; then new_search="${new_search} ${i}" # libc limits us to 6 search domains (( n_search++ )) @@ -209,14 +212,14 @@ merge_resolv() { done # Remove duplicate options - new_opts=$( + new_opts="$( echo -e "${new_opts//\#/\n}" \ - | sort -u | sed -e 's/^/#/g' | xargs - ) + | sort -u | sed -e 's/^/#/g' | xargs \ + )" local new_sortlist n_sortlist=0 for i in ${sortlist[@]}; do - if [[ " ${new_sortlist} " != *" ${i} "* ]]; then + if [[ " ${new_sortlist} " != *" ${i} "* ]] ; then new_sortlist="${new_sortlist} ${i}" # libc limits us to 10 items (( n_sortlist++ )) @@ -231,8 +234,8 @@ merge_resolv() { for i in ${new_srvs[@]}; do echo "nameserver ${i}" >> "${f}" done - if [[ -n ${new_search} ]]; then - if [[ ${n_search} == "1" ]]; then + if [[ -n ${new_search} ]] ; then + if [[ ${n_search} == "1" ]] ; then echo "domain${new_search}" >> "${f}" else echo "search${new_search}" >> "${f}" @@ -284,7 +287,7 @@ merge_ntp() { echo -e "$@" | sort -u } - srvs=$( sort_unique ${srvs} ) + srvs="$(sort_unique ${srvs})" f="${netdir}/ntp.conf.$$" echo "# Generated by net-scripts for interfaces ${ifaces[@]}" > "${f}" @@ -325,7 +328,7 @@ select_best_interface() { local iface for iface in ${ifs[@]} ; do - if [[ -e "${statedir}/${iface}/resolv.conf" ]]; then + if [[ -e "${statedir}/${iface}/resolv.conf" ]] ; then echo "${iface}" return 0 fi @@ -343,20 +346,20 @@ calculate_metric() { local iface="$1" exclude='$1!="Iface" && $1!="lo"' # Have we already got a metric? - local m=$( awk '$1=="'${iface}'" && $2=="00000000" { print $7 }' \ - /proc/net/route ) - if [[ -n ${m} ]]; then + local m="$( awk '$1=="'${iface}'" && $2=="00000000" { print $7 }' \ + /proc/net/route )" + if [[ -n ${m} ]] ; then echo "${m}" return 0 fi - local itype=$( interface_type "${iface}" ) x i + local itype="$(interface_type "${iface}")" x i # If we're not a wireless device then exclude wireless from the # routing table so we stay < 1000 - if [[ -e /proc/net/wireless ]]; then + if [[ -e /proc/net/wireless ]] ; then if ! grep -q "^[ \t]*${iface}:[ \t]" /proc/net/wireless ; then - local i=$( sed -n -e 's/^[ \t]*\(.*\):.*/\1/p' /proc/net/wireless ) + local i="$(sed -n -e 's/^[ \t]*\(.*\):.*/\1/p' /proc/net/wireless)" for x in ${i} ; do exclude="${exclude} && "'$1'"!=\"${x}\"" done @@ -367,18 +370,18 @@ calculate_metric() { local ix="ppp|ippp" [[ ${itype} == "ppp" ]] && ix="ippp" [[ ${itype} == "ippp" ]] && ix="ppp" - i=$( sed -n -e 's/^[ ]*\('${ix}'[0-9]*\):.*$/\1/p' /proc/net/dev ) + i="$( sed -n -e 's/^[ ]*\('"${ix}"'[0-9]*\):.*$/\1/p' /proc/net/dev )" for x in ${i} ; do exclude="${exclude} && "'$1'"!=\"${x}\"" done - local m=$( awk "${exclude} { print "'$7'" }" /proc/net/route \ - | sort -rn | head -n 1 | cut -d' ' -f2 ) + local m="$( awk "${exclude} { print "'$7'" }" /proc/net/route \ + | sort -rn | head -n 1 | cut -d' ' -f2 )" m="${m:--1}" (( m ++ )) # If we're a wireless device then add 1000 so that wired interfaces take preference - if [[ -e /proc/net/wireless ]]; then + if [[ -e /proc/net/wireless ]] ; then grep -q "^[ \t]*${iface}:[ \t]" /proc/net/wireless && (( m+= 1000 )) fi @@ -397,7 +400,7 @@ netmask2cidr() { for i in ${1//./ }; do bin="" - while [[ ${i} != "0" ]]; do + while [[ ${i} != "0" ]] ; do bin=$[${i}%2]${bin} (( i=i>>1 )) done @@ -416,13 +419,13 @@ cidr2netmask() { (( octets=cidr/8 )) (( frac=cidr%8 )) - while [[ octets -gt 0 ]]; do + while [[ octets -gt 0 ]] ; do netmask="${netmask}.255" (( octets-- )) (( done++ )) done - if [[ ${done} -lt 4 ]]; then + if [[ ${done} -lt 4 ]] ; then for (( i=0; i<${frac}; i++ )); do (( sum+=cur )) (( cur/=2 )) @@ -430,7 +433,7 @@ cidr2netmask() { netmask="${netmask}.${sum}" (( done++ )) - while [[ ${done} -lt 4 ]]; do + while [[ ${done} -lt 4 ]] ; do netmask="${netmask}.0" (( done++ )) done @@ -452,10 +455,10 @@ ip_network() { [[ ${ip} != *.*.*.* ]] && return # If we didn't get parameter 2 then assume we have a CIDR - if [[ -z ${mask} ]]; then + if [[ -z ${mask} ]] ; then mask="${ip##*/}" [[ -z ${mask} || ${mask} == ${ip} ]] && return 1 - mask=$( cidr2netmask "${mask}" ) + mask="$(cidr2netmask "${mask}")" ip="${ip%%/*}" fi @@ -471,41 +474,6 @@ ip_network() { echo "${network}" } -# bool clean_pidfile(char *file) -# -# Removes the given pidfile if the process is not running -# Returns 1 if the process is still running otherwise 0 -clean_pidfile() { - local pidfile="$1" - - [[ ! -f ${pidfile} ]] && return 0 - local pid=$( < "${pidfile}" ) - - if [[ -n ${pid} ]]; then - local cmd="${pidfile##*/}" - cmd="${cmd%%-*}" - ps p "${pid}" 2>/dev/null | grep -q "${cmd}" && return 1 - fi - - rm -f "${pidfile}" - return 0 -} - -# bool process_finished(int pid, char* cmd) -# -# We wait for 10 seconds until the command ${cmd} -# stops running on the process ${pid} -process_finished() { - local i pid="$1" cmd="$2" secs="${3:-9}" - - for (( i=0; i<secs; i++ )); do - ps p "${pid}" 2>/dev/null | grep -q "${cmd}" || return 0 - sleep 1 - done - - return 1 -} - # bool is_function(char* name) # # Returns 0 if the given name is a shell function, otherwise 1 @@ -560,7 +528,7 @@ configure_variables() { local mod func x i local -a ivars ovars1 ovars2 - local ifvar=$( bash_variable "${iface}" ) + local ifvar="$(bash_variable "${iface}")" for mod in ${MODULES[@]}; do is_function ${mod}_variables || continue @@ -576,18 +544,18 @@ configure_variables() { } # Provide a wrapper for hostname if it's not available -if [[ -z $(type -p hostname) ]]; then +if [[ -z $(type -p hostname) ]] ; then hostname() { # Linux and *BSD seem to differ local kernel="kern" ctl="hostname" [[ $(uname) == "Linux" ]] && kernel="kernel" - if [[ $1 == "-y" || $1 == "--yp" || $1 == "nis" ]]; then + if [[ $1 == "-y" || $1 == "--yp" || $1 == "nis" ]] ; then ctl="domainname" shift fi - if [[ -n $1 ]]; then + if [[ -n $1 ]] ; then sysctl -q -w "${kernel}.${ctl}=$1" else sysctl -n "${kernel}.${ctl}" |