1#!/bin/sh
2
3# $Id: poff,v 1.1 2002/11/24 23:30:44 etbe Exp $
4# Written by John Hasler <john@dhh.gt.org> and based on work 
5# by Phil Hands <phil@hands.com>.  Distributed under the GNU GPL
6
7if [ -x /usr/bin/kill ]; then
8  KILL="/usr/bin/kill"
9else
10  KILL="/bin/kill"
11fi
12SIG=TERM
13DONE="stopped"
14MODE=""
15
16usage ()
17{
18   cat <<!EOF!
19usage: $0 [option] [provider]
20options:
21  -r        Cause pppd to drop the line and redial.
22  -d        Toggle the state of pppd's debug option.
23  -c        Cause pppd to renegotiate compression.
24  -a        Stop all pppd's.  'provider' will be ignored.
25  -h        Print this help summary and exit.
26  -v        Print version and exit.
27  none      Stop pppd.
28
29Options may not be combined.
30
31If 'provider' is omitted pppd will be stopped or signalled if and only if
32there is exactly one running unless the '-a' option was given.  If
33'provider' is supplied the pppd controlling the connection to that
34provider will be stopped or signalled.
35!EOF!
36}
37
38# Get option.  If there are none replace the "?" that getopts puts in
39# FLAG on error with "null".
40getopts rdcavh FLAG
41if [ "$?" -ne 0 ]; then
42    FLAG="null"
43fi
44
45# Check for additional options.  Should be none.
46getopts :rdcavh DUMMY
47if [ "$?" -eq 0 ]; then
48    echo "$0: Illegal option -- ${OPTARG}."
49    exit 1
50fi
51
52case $FLAG in
53 "r") SIG=HUP;  DONE=signalled; shift ;;
54 "d") SIG=USR1; DONE=signalled; shift ;;
55 "c") SIG=USR2; DONE=signalled; shift ;;
56 "a") MODE="all"; shift ;;
57 "v") echo "$0$Revision: 1.1 $_TrickToPrint_RCS_Revision"; exit 0 ;;
58 "h") usage; exit 0 ;;
59 "?") exit 1;
60esac
61
62# Get the PIDs of all the pppds running.  Could also get these from
63# /var/run, but pppd doesn't create .pid files until ppp is up.
64PIDS=`pidof pppd`
65
66# poff is pointless if pppd isn't running.
67if test -z "$PIDS"; then
68    echo "$0: No pppd is running.  None ${DONE}."
69    exit 1
70fi
71
72# Find out how many pppd's are running.
73N=`echo "$PIDS" | wc -w`
74
75# If there are no arguments we can't do anything if there is more than one
76# pppd running.
77if test "$#" -eq 0 -a "$N" -gt 1 -a $FLAG != "a" ; then
78    echo "$0: More than one pppd running and no "-a" option and 
79no arguments supplied. Nothing ${DONE}."
80    exit 1
81fi
82
83# If either there are no arguments or '-a' was specified kill all the
84# pppd's.
85if test "$#" -eq 0 -o "$MODE" = "all" ; then
86    $KILL -$SIG $PIDS || {
87        echo "$0: $KILL failed.  None ${DONE}."
88        exit 1
89    }
90    exit 0
91fi
92
93# There is an argument, so kill the pppd started on that provider.
94PID=`ps axw | grep "[ /]pppd call $1 *\$" | awk '{print $1}'`
95if test -n "$PID" ; then
96    $KILL -$SIG $PID || {
97        echo "$0: $KILL failed.  None ${DONE}."
98        exit 1
99    }
100else
101   echo "$0: I could not find a pppd process for provider '$1'. None ${DONE}."
102   exit 1
103fi
104exit 0
105