#!/bin/sh # /etc/ppp ip-goingdown -ci "-vD" -s "ip-up ip-down" -- \ # ip-goingdown -- This script handles ip-(up|goingdown|down) for ppp|diald # Copyright (C) 2003 by Richard Hawes # This is free software; see the GNU General Public License version 2 # or later for copying conditions. There is NO warranty. # $Revision: 1.18 $ # latest version: www.dma.org/~rhawes/programs/ip-goingdown # check /tmp/ip-goingdown.errors for fatal errors # USAGE: make symbolic links to this program # in /etc/ppp/ for ip-up ip-down # # ip-goingdown can be called from diald or a script such as poff # the first parameter has to be the interface name # NAME SPACE # unexported variables begin with underscore or single letter prefix # functions and variables in sourced files should begin with another prefix # to avoid conflicts with future versions exec 3<>/dev/null; p_init() { SCRIPTNAME="$1"; VARNAME="$2"; eval export PARAM_$2=\"$3\"; # source ip-conf _if_test -f . "$_ETC/ip-conf"||:; TIME_STAMP="$TMPDIR/${IFNAME}.goingdown"; PPP_EXPORTS="$TMPDIR/${IFNAME}.exports"; hash $_CMD_LIST||{ hash $_CMD_LIST&>/tmp/ip-goingdown.errors;}||exit; } p_log() { logger $_LOG_OPT -t "$SCRIPTNAME[$$]";} p_param() { export PPP_IPPARAM="$6" PPP_TTYNAME=`basename "$DEVICE"`; #the other parameters are in the environment # $1 == IFNAME # $2 == DEVICE # $3 == SPEED # $4 == IPLOCAL # $5 == IPREMOTE # but because scripts use these variables . . . export PPP_IFACE="$1" PPP_TTY="$2" PPP_SPEED="$3" export PPP_LOCAL="$4" PPP_REMOTE="$5" } r_pts() { export SCRIPTNAME VARNAME; eval "$1"; eval "$2"; _if_source "$3"||:; _if_source "${3}.${PPP_TTYNAME}"||:; _if_test -d run-parts "${4}.${PPP_TTYNAME}"|| _if_test -d run-parts "${4}" _if_source "$5"||:; _if_source "${5}.${PPP_TTYNAME}"||:; eval "$6"; eval "$7"; } r_parts() { eval _if_unempty renice \"\$_NICE_$1_${PPP_TTYNAME}\" $$|| eval _if_unempty renice \"\$_NICE_$1\" $$; eval r_pts \"\$E_PRE$1\" \"\$E_PRE$1_${PPP_TTYNAME}\" \"\$S_PRE$1\" \ \"\$D_$1\" \"\$S_POST$1\" \"\$E_POST$1\" \"\$E_POST$1_${PPP_TTYNAME}\" } r_pts_goingdown() { [ -f "$TIME_STAMP" ]&&[ "$PPP_EXPORTS" -ot "$TIME_STAMP" ]|| { touch "$TIME_STAMP"; r_parts GOINGDOWN; } } r_pts_up() { _if_test -f "mv -vfb" "$TMPDIR" "${TMPDIR}.orig.$$"||:; _if_test '! -d' "mkdir -vp" "$TMPDIR"||:; export IP_UP_PID=$$; # save exports for goingdown export -p >"$PPP_EXPORTS"; # give kernel time to set up routes eval _if_unempty sleep \"\$_DELAY_${PPP_TTYNAME}\"|| eval _if_unempty sleep \"\$_DELAY\"; r_parts UP; } # functions that could be used in ip-conf _if_test() { [ $1 "$3" ]&&{ shift; eval "$@";};} _if_source() { [ -f "$1" ]&&. $1;} _if_unempty() { [ "$2" ]&&"$@";} _ETC=/etc/ppp; # variables that could be changed in ip-conf _CMD_LIST="echo grep kill logger ls renice run-parts sleep touch"; _DELAY=; _LOG_OPT="-p user.debug"; _NICE_GOINGDOWN=; _NICE_DOWN=; _NICE_UP=; D_DOWN="$_ETC/ip-down.d"; D_GOINGDOWN="$_ETC/ip-goingdown.d"; D_UP="$_ETC/ip-up.d"; E_PREDOWN=":"; E_PREGOINGDOWN=":"; E_PREUP=":"; E_POSTDOWN=":"; E_POSTGOINGDOWN=":"; E_POSTUP=":"; S_PREDOWN="$_ETC/ip-predown"; S_PREGOINGDOWN="$_ETC/ip-pregoingdown"; S_PREUP="$_ETC/ip-preup"; S_POSTDOWN="$_ETC/ip-postdown"; S_POSTGOINGDOWN="$_ETC/ip-postgoingdown"; S_POSTUP="$_ETC/ip-postup"; PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin; TMPDIR=/var/tmp/ppp; export PATH TMPDIR; #base names of symbolic links to this program P_UP="ip-up"; P_DOWN="ip-down"; P_GOINGDOWN="ip-goingdown"; case "$0" in *"$P_UP") p_param "$@"; p_init "$P_UP" IP_UP "$*"; r_pts_up 2>&1 | p_log;; *"$P_GOINGDOWN") IFNAME="$1"; p_init "$P_GOINGDOWN" IP_GOINGDOWN "$*"; { . "$PPP_EXPORTS"; # send ip-up HUP if it is still running kill -0 $IP_UP_PID 2>&3 && kill -HUP $IP_UP_PID; r_pts_goingdown; } 2>&1 | p_log;; *"$P_DOWN") p_param "$@"; p_init "$P_DOWN" IP_DOWN "$*"; { r_pts_goingdown; r_parts DOWN; } 2>&1 | p_log;; *) echo "called with invalid name: $0" >&2; exit 1;; esac