--- /usr/local/src/jabberd-2.0s4/tools/jabberd.in Thu Mar 13 23:23:42 2003 +++ tools/jabberd.in Sat Nov 13 16:12:46 2004 @@ -7,7 +7,7 @@ ############################################################################## # -# jabberd - perl wrapper script to manage launching and controling the various +# jabberd - perl wrapper script to manage launching and controlling the various # binaries that make up the 2.0 version of the jabberd server. # ############################################################################## @@ -18,6 +18,8 @@ use FileHandle; use IPC::Open3; use IO::Select; +use POSIX; +use POSIX qw(setsid); #----------------------------------------------------------------------------- @@ -28,6 +30,7 @@ my $config = $config_dir."/jabberd.cfg"; $config = "internal" unless (-e $config); my $debug = 0; +my $daemon = 0; my $select = IO::Select->new(); my ($exe) = ($0 =~ /([^\/]+)$/); my %jobs; @@ -39,7 +42,7 @@ # Process the command line arguments #----------------------------------------------------------------------------- my %opts; -getopts("c:Dh",\%opts); +getopts("c:Dhb",\%opts); &usage if exists($opts{h}); if (exists($opts{c})) { @@ -48,7 +51,7 @@ } $debug = 1 if exists($opts{D}); - +$daemon = 1 if exists($opts{b}); #----------------------------------------------------------------------------- # Catch some signals so that we can handle them later. @@ -60,7 +63,7 @@ #----------------------------------------------------------------------------- -# Setup the jobs: router, resovler, sm, c2s, s2s +# Setup the jobs: router, resolver, sm, c2s, s2s #----------------------------------------------------------------------------- $jobs{jabberd}->{prefix} = "JBRD"; @@ -105,7 +108,11 @@ next if /^\#/; next if /^\s*$/; my ($job,$config) = /^\s*(\S+)\s*(\S*)\s*$/; - push(@programs,[$job,$config]); + # Assume that all the commands are in the same directory + # as the jabberd script. The current configuration file + # format does not allow specification of pathnames for commands. + my $cmd = "$Bin/$job"; + push(@programs,[$job,$config,$cmd]); } close(CFG); } @@ -129,9 +136,22 @@ foreach my $job (@programs) { - &LaunchJob($job->[0],$job->[1]); + &LaunchJob($job->[0],$job->[1],$job->[2]); } +unless (!$daemon || $debug) +{ + # Fork and become a daemon. Exit if we are the parent process. + defined(my $pid = fork()) || die "Could not fork: $!"; + POSIX:_exit(0) if $pid; + # If we are the child process, continue (but act like a daemon). + setsid or die "Could not start a new POSIX Session: $!"; + chdir "/" or die "Could not chdir to /: $!"; + umask 0; + open STDIN, "/dev/null" or die "Could not set STDIN to /dev/null: $!"; + open STDOUT, "/dev/null" or die "Could not set STDOUT to /dev/null: $!"; + open STDERR, "/dev/null" or die "Could not set STDERR to /dev/null: $!"; +} #----------------------------------------------------------------------------- # Run the main loop. Read the output from the jobs, watch for dead jobs and @@ -168,8 +188,12 @@ { my $job = shift; my $config = shift; + my $cmd = shift; - my $cmd = $jobs{$job}->{cmd}; + if (!defined($cmd)) + { + $cmd = $jobs{$job}->{cmd}; + } if (defined($config)) { $cmd .= " -c ".$config; @@ -272,7 +296,9 @@ #$flag = "*" if ($std eq "stderr"); #printf("%s%-4s: ",$flag,$jobs{$job}->{prefix}); - printf("%-4s: ",$jobs{$job}->{prefix}); + my $prefix = $jobs{$job}->{prefix}; + $prefix = $job if ! defined($prefix); + printf("%-4s: ",$prefix); print join("",@_); } @@ -289,6 +315,7 @@ print "Options are:\n"; print " -c config file to use [default: $config]\n"; print " -D Show debug output\n"; + print " -b Push into background\n"; print " -h Show this help\n"; exit(0); }