]> git.ozlabs.org Git - ppp.git/blob - NeXT/Examples/Persistent_Connection
Fix an fd leak on the discovery socket.
[ppp.git] / NeXT / Examples / Persistent_Connection
1 #! /usr/bin/perl
2
3 # ip-down -- executed by pppd on connection death
4 #
5 # This particular implementation of ip-down attempts to re-establish
6 # the ppp connection.  It does this by first forking and having the
7 # parent die (returning control to the invoking pppd for final
8 # cleanup).
9 #
10 # The child waits until the specified port (pppd passes the path to
11 # the port's device as argument 2) is available and launches pppd (or
12 # whatever is specfied by $PATH_pppd.
13 #
14 # This script requires "syslog.pl" (included with perl).  Because of
15 # this, it also requires "syslog.ph";  "syslog.ph" can be
16 # manufactured using the h2ph script included with the perl
17 # distribution.  Under NeXTSTEP, one can create syslog.ph by:
18 #
19 #        h2ph < /usr/include/bsd/sys/syslog.h > syslog.ph
20 #
21 # 'syslog.ph' should either be copied into your machines perl library
22 # (wherever syslog.pl resides), or you should add wherever syslog.ph
23 # resides to Perl's library search path.
24 #
25 # Removing all references to syslog (including openlog() and
26 # closelog()) will also work, but will render this script's execution
27 # completely silent.
28 #
29 # By default, this script logs to the default target for ppp's logs --
30 # LOCAL2.
31 #
32 # hacqued by: <bbum@friday.com>, jan 30 1995
33 #
34 # Please send any changes/improvements to <bbum@friday.com>.  And
35 # please try not to laugh at this code... or, at least, tell me why
36 # you are laughing so I won't make the same mistakes twice.
37
38 # ABSOLUTE path to PPP daemon (or whatever you want executed after the
39 # port becomes available).
40
41 $PATH_pppd = "/usr/local/ppp/bin/pppd";
42
43 # number of seconds to sleep between checking for port availability
44 $lock_sleep = 2;
45
46 require "syslog.pl";
47
48 FORK: {
49     if ($pid = fork) {
50         # this is the parent.  It must die so the old pppd can
51         # clean-up and exit.
52         exit;
53     } elsif ($! =~ /No more process/) {
54         # oops! ran out of processes.  This is supposed to be a
55         # recoverable error, so sleep and try again.
56         sleep 5;
57         redo FORK;      
58     } elsif (!defined($pid)) {
59         # fork error -- log it and die.
60         &openlog("pppd/ip-down", 'cons,pid', LOG_LOCAL2);
61         &syslog('warning',
62                 "Fork() error '$!'");
63         &closelog;
64         die "can't fork: $!\n";
65     }
66 }
67
68 # everything from here down is the child.
69 &openlog("pppd/ip-down", 'cons,pid', LOG_LOCAL2);
70
71 if ( ! @ARGV ) {
72     # no arguments -- exec specified thing (assume the process
73     # being called has a clue about what port it should use)
74
75     &syslog('info', "No device specified. Executing '$PATH_pppd'.");
76     &closelog;
77
78     exec $PATH_pppd;
79     ## NOT REACHED: exec never returns
80 }
81
82 # (assume-- it will if pppd starts ip-down)
83 # ARGV contains:
84 #          interface-name tty-device speed local-IP-address
85 #          remote-IP-address
86 ($interface_name,
87  $tty_device,
88  $speed,
89  $local_IP_address,
90  $remote_IP_address) = @ARGV;
91
92 # find the raw device name
93 @path = split ('/', $tty_device);
94 $device = pop @path;
95
96 # Generate path to lock file -- assumes NeXT style device locking
97 $lock = "/usr/spool/uucp/LCK/LCK..$device";
98
99 # log some info.
100 &syslog('info',
101         "Reconnecting '$interface_name' ($local_IP_address:$remote_IP_address) through '$tty_device' at '$speed' baud.");
102
103 # check for lock
104 if ( -e $lock) {
105
106     &syslog('info',
107             "'$device' locked. Waiting for unlock.");
108
109     # loop until unlocked
110     while ( -e $lock ) {
111         sleep $lock_sleep;
112     }
113 }
114
115 #### port available -- log and execute
116
117 &syslog('info',
118         "Port '$device' available. Launching '$PATH_pppd'");
119
120 &closelog;
121
122 exec $PATH_pppd;
123 ### NOT REACHED
124
125