#!/usr/bin/perl use strict; use Getopt::Std; use Mgr; use Global1; my %OPT; my $ok = getopts("D", \%OPT); my $BASE = "."; daemonize(); start(); sub start { eval { $SIG{HUP} = \&catchHup; $SIG{CHLD} = "IGNORE"; $SIG{INT} = \&catchInt; $SIG{USR1} = \&catchUsr1; if ($MANAGER) { undef $MANAGER; } $MANAGER = new Mgr; $MANAGER->monitor(); }; if ($@) { Global1::error ("Got fatal error:\n$@\n"); catchInt(); } } sub daemonize { require POSIX; open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; POSIX::setsid() or Global1::error ( "Could not setsid\n$!\n" ); `echo $$ > tun_mgr.pid`; } sub catchHup { Global1::error ( "\n\t!!!Got HUP!!!\n" ); $MANAGER->killAll(); Global1::error ( "\n\tRestarting application\n" ); &start(); } sub catchInt { $MANAGER->killAll(); `rm -rf tun_mgr.pid`; Global1::error ( "Parent process $$ exiting\n" ); exit; }