# -*-Shell-script-*- # # This file is not a stand-alone shell script; it provides functions # to network scripts that source it. # Set up a default search path. PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin" export PATH need_config () { CURRENT_PROFILE=${CURRENT_PROFILE:-default} CURRENT_PROFILE=${CURRENT_PROFILE##*/} CONFIG=../networking/profiles/$CURRENT_PROFILE/ifcfg-${1} [ -f "${CONFIG}" ] && return || CONFIG=../networking/profiles/$CURRENT_PROFILE/${1} [ -f "${CONFIG}" ] && return || CONFIG=../networking/default/ifcfg-${1} [ -f "${CONFIG}" ] && return || CONFIG=../networking/default/${1} [ -f "${CONFIG}" ] && return || CONFIG="ifcfg-${1}" [ -f "${CONFIG}" ] && return || CONFIG="${1}" } source_config () { DEVNAME=`basename $CONFIG | sed 's/^ifcfg-//g'` if basename $CONFIG | grep -q '[^g]-' ; then PARENTCONFIG=`echo $CONFIG | sed 's/-[^-]*$//g'` PARENTDEVNAME=`basename $PARENTCONFIG | sed 's/^ifcfg-//g'` [ -f $PARENTCONFIG ] || { echo $"Missing config file $PARENTCONFIG." >&2 exit 1 } . $PARENTCONFIG fi . $CONFIG } expand_config () { if [ -z "${NETMASK}" ]; then eval `/bin/ipcalc --netmask ${IPADDR}` fi if [ -z "${PREFIX}" ]; then eval `/bin/ipcalc --prefix ${IPADDR} ${NETMASK}` fi if [ -z "${BROADCAST}" ]; then eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}` fi if [ -z "${NETWORK}" ]; then eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}` fi } toggle_value () { if [ "$2" = "yes" -o "$2" = "YES" ] ; then echo "$1 on" elif [ "$2" = "no" -o "$2" = "NO" ] ; then echo "$1 off" else echo '' fi } do_netreport () { # Notify programs that have requested notification ( cd /var/run/netreport || exit for i in * ; do if [ -f $i ]; then OWNER=`ls -l $i | awk '{ print $3 }'` if [ "`id -u`" = "0" ]; then su -s /bin/bash $OWNER -c "kill -SIGIO $i >/dev/null 2>&1 || rm -f $i >/dev/null 2>&1" > /dev/null 2>&1 else kill -SIGIO $i >/dev/null 2>&1 || rm -f $i >/dev/null 2>&1 fi fi done ) } is_available () { LC_ALL= LANG= ip -o link | grep -q $1 [ "$?" = "1" ] || return 0 alias=`modprobe -c | awk "/^alias $1 / { print \\$3 }"` if [ -z "$alias" -o "$alias" = "off" ]; then return 2 fi modprobe $1 > /dev/null 2>&1 || return 1 LC_ALL= LANG= ip -o link | grep -q $1 return $? } need_hostname () { CHECK_HOSTNAME=`hostname` if [ "$CHECK_HOSTNAME" = "(none)" -o "$CHECK_HOSTNAME" = "localhost" -o \ "$CHECK_HOSTNAME" = "localhost.localdomain" ]; then return 0 else return 1 fi } set_hostname () { hostname $1 if ! grep search /etc/resolv.conf >/dev/null 2>&1; then domain=`echo $1 | sed 's/^[^\.]*\.//'` echo "search $domain" >> /etc/resolv.conf fi } check_device_down () { if echo $1 | grep -q ':' ; then if LC_ALL=C ifconfig -a 2>/dev/null | grep -q $1 ; then return 1 else return 0 fi else if LC_ALL=C ip -o link ls dev $1 2>/dev/null | grep -q ",UP" ; then return 1 else return 0 fi fi } check_mii_tool () { [ -x /sbin/mii-tool ] || return 2 output=`LC_ALL=C mii-tool $1 2>&1` echo $output | LC_ALL=C grep -q "link ok" && return 1 echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2 } check_ethtool () { [ -x /sbin/ethtool ] || return 2 output=`LC_ALL=C ethtool $1 2>&1` echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1 echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2 } check_link_down () { if [ -x /sbin/mii-tool -o -x /sbin/ethtool ]; then if ! LC_ALL=C ip link show $1 2>/dev/null| grep -q UP ; then ip link set $1 up >/dev/null 2>&1 timeout=0 while [ $timeout -le 10 ]; do check_mii_tool $1 m=$? check_ethtool $1 e=$? if [ $m -eq 1 ] || [ $e -eq 1 ] ; then return 1 fi if [ $m -eq 2 ] && [ $e -eq 2 ] ; then return 1 fi usleep 500000 timeout=$((timeout+1)) done return 0 fi fi return 1 } check_default_route () { LC_ALL=C ip route list match 0/0 | grep -q default } find_gateway_dev () { . /etc/sysconfig/network if [ -n "${GATEWAY}" -a "${GATEWAY}" != "none" ] ; then dev=`LC_ALL=C /sbin/ip route | \ grep ${GATEWAY} | \ sed -e 's/.* dev \([:alnum:]*\)/\1/'` if [ -n "$dev" ]; then GATEWAYDEV="$dev" fi fi } add_default_route () { . /etc/sysconfig/network check_default_route && return 0 find_gateway_dev if [ "$GATEWAYDEV" != "" -a -n "${GATEWAY}" -a \ "${GATEWAY}" != "none" ]; then if ! check_device_down $1; then if [ "$GATEWAY" = "0.0.0.0" ]; then /sbin/ip route add default dev ${GATEWAYDEV} else /sbin/ip route add default via ${GATEWAY} fi fi elif [ -f /etc/default-route ]; then /sbin/ip route add default via `cat /etc/default-route` rm -f /etc/default-route fi } is_wireless_device () { [ -x /sbin/iwconfig ] || return 1 LC_ALL=C iwconfig $1 2>&1 | grep -q "no wireless extensions" || return 0 return 1 }