| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# "Flamethrower" |
| 4 |
# |
| 5 |
# Copyright (C) 2003 Bald Guy Software |
| 6 |
# Brian Elliott Finley <[email protected]> |
| 7 |
# |
| •8 |
# $Id: flamethrowerd 59 2006-04-18 23:05:44Z dannf $ |
| 9 |
# |
| 10 |
|
| 11 |
$0 = "flamethrowerd"; |
| 12 |
|
| 13 |
use strict; |
| 14 |
use File::stat; |
| •15 |
use File::Which; |
| 16 |
use Getopt::Long; |
| 17 |
use POSIX qw(setsid); |
| 18 |
|
| 19 |
use lib "/usr/lib/flamethrower"; |
| 20 |
use Flamethrower; |
| 21 |
|
| 22 |
use vars qw($config $ft_config $VERSION); |
| 23 |
|
| 24 |
# |
| 25 |
# Set some defaults |
| 26 |
# |
| 27 |
my $last_time = 0; |
| 28 |
my $conf_file = '/etc/flamethrower/flamethrower.conf'; |
| 29 |
my $pid_file = '/var/run/flamethrower-server.pid'; |
| 30 |
my $version_number = "INS_VERSION"; |
| 31 |
my $program_name = "flamethrowerd"; |
| 32 |
my $get_help = " Try \"$program_name -help\" for more options."; |
| 33 |
|
| 34 |
|
| 35 |
# |
| 36 |
# Command line options |
| 37 |
# |
| 38 |
# set version information |
| 39 |
my $version_info = <<"EOF"; |
| 40 |
$program_name (part of Flamethrower) version $version_number |
| 41 |
|
| 42 |
Copyright (C) 2003 Brian Elliott Finley <brian\@bgsw.net> |
| 43 |
This is free software; see the source for copying conditions. There is NO |
| 44 |
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 45 |
EOF |
| 46 |
|
| 47 |
# set help information |
| 48 |
my $help_info = $version_info . <<"EOF"; |
| 49 |
|
| 50 |
Usage: $program_name [OPTION]... -server HOSTNAME -image IMAGENAME |
| 51 |
|
| 52 |
Options: (options can be presented in any order) |
| 53 |
|
| 54 |
--help |
| 55 |
Display this output. |
| 56 |
|
| 57 |
--version |
| 58 |
Display version and copyright information. |
| 59 |
|
| 60 |
--config-file FILE |
| 61 |
Alternate config file location. Defaults to: $conf_file. |
| 62 |
|
| 63 |
--pid-file FILE |
| 64 |
Name of pid file. Defaults to: $pid_file. |
| 65 |
|
| 66 |
Download, report bugs, and make suggestions at: |
| 67 |
http://systemimager.org/support/ |
| 68 |
EOF |
| 69 |
|
| 70 |
# interpret command line options |
| 71 |
GetOptions( |
| 72 |
"help" => \my $help, |
| 73 |
"version" => \my $version, |
| 74 |
"config-file=s" => \$conf_file, |
| 75 |
"pid-file=s" => \$pid_file, |
| 76 |
) or die qq($help_info); |
| 77 |
|
| 78 |
# if requested, print help information |
| 79 |
if($help) { |
| 80 |
print qq($help_info); |
| 81 |
exit 0; |
| 82 |
} |
| 83 |
|
| 84 |
# if requested, print version and copyright information |
| 85 |
if($version) { |
| 86 |
print qq($version_info); |
| 87 |
exit 0; |
| 88 |
} |
| 89 |
|
| 90 |
# Read the config file and update the directory |
| 91 |
read_config_file($conf_file); |
| 92 |
|
| 93 |
# Turn on in flamethrower.conf file to see debug output. |
| 94 |
my $debug; |
| 95 |
if ( lc $ft_config->debug() eq "on" ) { |
| 96 |
$debug = 1; |
| 97 |
print "Flamethrower debug output turned on\n" if($debug); |
| 98 |
} |
| 99 |
|
| 100 |
# Should we start up? |
| 101 |
if ( $ft_config->start_flamethrower_daemon() ne "yes" ) { |
| 102 |
exit 0; |
| 103 |
} |
| 104 |
|
| 105 |
# Warn if specified interface doesn't exist. |
| 106 |
my $interface = $ft_config->interface(); |
| 107 |
if (( ! -d "/proc/sys/net/ipv4/conf/$interface" ) and ( ! -d "/proc/sys/net/ipv6/conf/$interface" )) { |
| 108 |
print STDERR "\n\n"; |
| 109 |
print STDERR "FATAL: Interface $interface, as specified in your flamethrowerd.conf file,\n"; |
| 110 |
print STDERR " does not appear to be configured on this machine.\n"; |
| 111 |
print STDERR "\n"; |
| 112 |
exit 1; |
| 113 |
} |
| 114 |
|
| 115 |
my $file = "udp-sender"; |
| 116 |
unless( Flamethrower->which($file,$ENV{PATH}) ){ |
| 117 |
print ("Couldn't find $file in path: $ENV{PATH}\n"); |
| 118 |
print ("Please install udp-sender, then try again.\n"); |
| 119 |
exit 1; |
| 120 |
} |
| 121 |
|
| 122 |
&daemonize; |
| 123 |
|
| 124 |
while(1) { |
| 125 |
&main; |
| 126 |
sleep 2; |
| 127 |
} |
| 128 |
|
| 129 |
exit 0; |
| 130 |
|
| 131 |
|
| 132 |
################################################################################ |
| 133 |
# |
| 134 |
# BEGIN subroutines |
| 135 |
# |
| 136 |
|
| 137 |
# Usage: my $cmd = build_udp_sender_cmd($module); |
| 138 |
sub build_udp_sender_cmd { |
| 139 |
|
| 140 |
my $module = shift; |
| 141 |
|
| 142 |
############################################################################ |
| 143 |
# |
| 144 |
# Set vars for all options |
| 145 |
# |
| 146 |
############################################################################ |
| 147 |
|
| 148 |
############################################################################ |
| 149 |
# |
| 150 |
# Global settings |
| 151 |
# |
| •152 |
my @min_clients; |
| 153 |
if ( $ft_config->get("min_clients") ) { |
| •154 |
@min_clients = ("--min-clients", $ft_config->get("min_clients")); |
| 155 |
} |
| 156 |
|
| •157 |
my @max_wait; |
| 158 |
if ( $ft_config->get("max_wait") ) { |
| •159 |
@max_wait = ("--max-wait" , $ft_config->get("max_wait")); |
| 160 |
} |
| 161 |
|
| •162 |
my @min_wait; |
| 163 |
if ( $ft_config->get("min_wait") ) { |
| •164 |
@min_wait = ("--min-wait", $ft_config->get("min_wait")); |
| 165 |
} |
| 166 |
|
| •167 |
my @async; |
| 168 |
if ( $ft_config->get("async") ) { |
| 169 |
if ( lc ($ft_config->get("async")) eq "on" ) { |
| •170 |
@async = ("--async"); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| •174 |
my @autostart; |
| 175 |
if ( $ft_config->get("autostart") ) { |
| •176 |
@autostart = ("--autostart", $ft_config->get("autostart")); |
| 177 |
} |
| 178 |
|
| •179 |
my @blocksize; |
| 180 |
if ( $ft_config->get("blocksize") ) { |
| •181 |
@blocksize = ("--blocksize", $ft_config->get("blocksize")); |
| 182 |
} |
| 183 |
|
| •184 |
my @broadcast; |
| 185 |
if ( $ft_config->get("broadcast") ) { |
| 186 |
if ( lc ($ft_config->get("broadcast")) eq "on" ) { |
| •187 |
@broadcast = ("--broadcast"); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| •191 |
my @fec; |
| 192 |
if ( $ft_config->get("fec") ) { |
| •193 |
@fec = ("--fec", $ft_config->get("fec")); |
| 194 |
} |
| 195 |
|
| •196 |
my @interface; |
| 197 |
if ( $ft_config->get("interface") ) { |
| •198 |
@interface = ("--interface", $ft_config->get("interface")); |
| 199 |
} |
| 200 |
|
| •201 |
my @log; |
| 202 |
if ( $ft_config->get("log") ) { |
| •203 |
@log = ("--log", $ft_config->get("log") . ".$module"); |
| 204 |
} |
| 205 |
|
| •206 |
my @max_bitrate; |
| 207 |
if ( $ft_config->get("max_bitrate") ) { |
| •208 |
@max_bitrate = ("--max-bitrate", $ft_config->get("max_bitrate")); |
| 209 |
} |
| 210 |
|
| •211 |
my @full_duplex = (); |
| 212 |
if ( $ft_config->get("full_duplex") ) { |
| 213 |
if ( lc ($ft_config->get("full_duplex")) eq "on" ) { |
| •214 |
@full_duplex = ("--full-duplex"); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| •218 |
my @mcast_addr; |
| 219 |
if ( $ft_config->get("mcast_addr") ) { |
| •220 |
@mcast_addr = ("--mcast-addr", $ft_config->get("mcast_addr")); |
| 221 |
} |
| 222 |
|
| •223 |
my @mcast_all_addr; |
| 224 |
if ( $ft_config->get("mcast_all_addr") ) { |
| •225 |
@mcast_all_addr = ("--mcast-all-addr", $ft_config->get("mcast_all_addr")); |
| 226 |
} |
| 227 |
|
| •228 |
my @min_slice_size; |
| 229 |
if ( $ft_config->get("min_slice_size") ) { |
| •230 |
@min_slice_size = ("--min-slice-size", $ft_config->get("min_slice_size")); |
| 231 |
} |
| 232 |
|
| •233 |
my @slice_size; |
| 234 |
if ( $ft_config->get("slice_size") ) { |
| •235 |
@slice_size = ("--slice-size", $ft_config->get("slice_size")); |
| 236 |
} |
| 237 |
|
| •238 |
my @pointopoint; |
| 239 |
if ( $ft_config->get("pointopoint") ) { |
| 240 |
if ( lc ($ft_config->get("pointopoint")) eq "on" ) { |
| •241 |
@pointopoint = ("--pointopoint"); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| •245 |
my @rexmit_hello_interval; |
| 246 |
if ( $ft_config->get("rexmit_hello_interval") ) { |
| •247 |
@rexmit_hello_interval = ("--rexmit-hello-interval", $ft_config->get("rexmit_hello_interval")); |
| 248 |
} |
| 249 |
|
| •250 |
my @ttl; |
| 251 |
if ( $ft_config->get("ttl") ) { |
| •252 |
@ttl = (" --ttl", $ft_config->get("ttl")); |
| 253 |
} |
| 254 |
# |
| 255 |
############################################################################ |
| 256 |
|
| 257 |
|
| 258 |
############################################################################ |
| 259 |
# |
| 260 |
# Module specific overrides |
| 261 |
# |
| 262 |
my $dir; |
| 263 |
if ( $ft_config->get("${module}_dir") ) { |
| 264 |
$dir = $ft_config->get("${module}_dir"); |
| 265 |
} |
| 266 |
|
| •267 |
my @portbase; |
| 268 |
if ( $ft_config->get("${module}_portbase") ) { |
| •269 |
@portbase = ("--portbase", $ft_config->get("${module}_portbase")); |
| 270 |
} |
| 271 |
|
| 272 |
if ($ft_config->varlist("${module}_async")) { |
| 273 |
if ( $ft_config->get("${module}_async") ) { |
| 274 |
if ( lc ($ft_config->get("${module}_async")) eq "on" ) { |
| •275 |
@async = ("--async"); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
if ($ft_config->varlist("${module}_ttl")) { |
| 281 |
if ( $ft_config->get("${module}_ttl") ) { |
| •282 |
@ttl = ("--ttl", $ft_config->get("${module}_ttl")); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if ($ft_config->varlist("${module}_mcast_all_addr")) { |
| 287 |
if ( $ft_config->get("${module}_mcast_all_addr") ) { |
| •288 |
@mcast_all_addr = ("--mcast-all-addr", $ft_config->get("${module}_mcast_all_addr")); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if ($ft_config->varlist("${module}_log")) { |
| 293 |
if ( $ft_config->get("${module}_log") ) { |
| •294 |
@log = ("--log", $ft_config->get("${module}_log")); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if ($ft_config->varlist("${module}_interface")) { |
| 299 |
if ( $ft_config->get("${module}_interface") ) { |
| •300 |
@interface = ("--interface", $ft_config->get("${module}_interface")); |
| 301 |
} |
| 302 |
} |
| 303 |
# |
| 304 |
############################################################################ |
| 305 |
|
| 306 |
# Build command |
| 307 |
# |
| 308 |
# GNU tar opts explained: |
| 309 |
# -B, --read-full-records / reblock as we read (for reading 4.2BSD pipes) |
| 310 |
# |
| 311 |
# -S, --sparse / handle sparse files efficiently |
| 312 |
# |
| •313 |
my @cmd = (which("udp-sender"), qq(--pipe), qq(tar -B -S -cpf - -C $dir .)); |
| 314 |
#my $cmd = qq(udp-sender --pipe 'tar -czpf - -C $dir .'); |
| •315 |
@cmd = (@cmd, @portbase) if(@portbase); |
| 316 |
@cmd = (@cmd, @min_clients) if(@min_clients); |
| 317 |
@cmd = (@cmd, @max_wait) if(@max_wait); |
| 318 |
@cmd = (@cmd, @min_wait) if(@min_wait); |
| 319 |
@cmd = (@cmd, @async) if(@async); |
| 320 |
@cmd = (@cmd, @autostart) if(@autostart); |
| 321 |
@cmd = (@cmd, @blocksize) if(@blocksize); |
| 322 |
@cmd = (@cmd, @broadcast) if(@broadcast); |
| 323 |
@cmd = (@cmd, @fec) if(@fec); |
| 324 |
@cmd = (@cmd, @interface) if(@interface); |
| 325 |
@cmd = (@cmd, @log) if(@log); |
| 326 |
@cmd = (@cmd, @max_bitrate) if(@max_bitrate); |
| 327 |
@cmd = (@cmd, @full_duplex) if(@full_duplex); |
| 328 |
@cmd = (@cmd, @mcast_addr) if(@mcast_addr); |
| 329 |
@cmd = (@cmd, @mcast_all_addr) if(@mcast_all_addr); |
| 330 |
@cmd = (@cmd, @min_slice_size) if(@min_slice_size); |
| 331 |
@cmd = (@cmd, @slice_size) if(@slice_size); |
| 332 |
@cmd = (@cmd, @pointopoint) if(@pointopoint); |
| 333 |
@cmd = (@cmd, @rexmit_hello_interval) if(@rexmit_hello_interval); |
| 334 |
@cmd = (@cmd, @ttl) if(@ttl); |
| 335 |
|
| 336 |
return @cmd; |
| 337 |
} |
| 338 |
|
| 339 |
# Usage: update_directory(); |
| 340 |
sub update_directory { |
| 341 |
|
| 342 |
print "update_directory()\n" if ($debug); |
| 343 |
|
| 344 |
# Create our directory directory, if necessary. |
| 345 |
my $dir = $ft_config->get("flamethrower_directory_dir"); |
| 346 |
if ( ! -e $dir ) { |
| •347 |
mkdir("$dir", 0755) or die("Couldn't mkdir $dir!"); |
| 348 |
} |
| 349 |
unlink <$dir/*>; |
| 350 |
|
| 351 |
# Make sure our state directory exists, if necessary, and clean it up. |
| 352 |
$dir = $ft_config->get("flamethrower_state_dir"); |
| 353 |
if ( ! -e $dir ) { |
| 354 |
print "FATAL: Flamethrower state dir $dir doesn't exist!\n"; |
| 355 |
print "Please create it and restart flamethrowerd!\n"; |
| 356 |
exit 1; |
| 357 |
} |
| 358 |
unlink <$dir/*>; |
| 359 |
|
| 360 |
|
| 361 |
############################################################################ |
| 362 |
# |
| 363 |
# Get list of explicitly specified portbase numbers so we don't |
| 364 |
# dynamically use one that already exists. |
| 365 |
# |
| 366 |
my %portbases = $ft_config->varlist('_portbase$'); |
| 367 |
|
| 368 |
# Be sure to include the flamethrower_directory port. |
| 369 |
$_ = $ft_config->get("flamethrower_directory_portbase"); |
| 370 |
$portbases{$_} = $_; |
| 371 |
|
| 372 |
# |
| 373 |
# Make the values the keys, so we can simply to an |
| 374 |
# if (defined()) in the module loop below. |
| 375 |
# |
| 376 |
foreach my $portbase (values %portbases) { |
| 377 |
# |
| 378 |
# Test to be sure portbase is an even number. |
| 379 |
# |
| 380 |
$_ = $portbase / 2; |
| 381 |
if (m/\./) { |
| 382 |
print qq(FATAL: portbases must be even numbers! Failed on "PORTBASE=$portbase"\n); |
| 383 |
print qq(Please check your config file.\n); |
| 384 |
exit 1; |
| 385 |
} |
| 386 |
$portbases{$portbase} = $portbase; |
| 387 |
} |
| 388 |
|
| 389 |
# |
| 390 |
############################################################################ |
| 391 |
|
| 392 |
############################################################################ |
| 393 |
# |
| 394 |
# Create the Flamethrower "directory" (sourceable files) for each module. |
| 395 |
# |
| 396 |
my $flamethrower_directory_dir = $ft_config->get("flamethrower_directory_dir"); |
| 397 |
my $modules = $ft_config->modules(); |
| 398 |
foreach my $module ( @$modules ) { |
| 399 |
|
| 400 |
# |
| 401 |
# Put directory information that is useful to clients in |
| 402 |
# sourceable files. |
| 403 |
# |
| 404 |
my $file = $flamethrower_directory_dir . "/" . $module; |
| 405 |
open(FILE, ">$file") or die("Couldn't open $file for writing!"); |
| 406 |
|
| 407 |
print FILE "MODULE=$module\n"; |
| 408 |
|
| 409 |
# DIR |
| 410 |
if ( ! defined($ft_config->get("${module}_dir")) ) { |
| 411 |
die("Please set DIR in flamethrower.conf for [$module]!"); |
| 412 |
} |
| 413 |
|
| 414 |
# PORTBASE |
| 415 |
my $portbase; |
| 416 |
if ($ft_config->varlist("${module}_portbase")) { |
| 417 |
|
| 418 |
# portbase is already set |
| 419 |
$portbase = $ft_config->get("${module}_portbase"); |
| 420 |
|
| 421 |
} else { |
| 422 |
|
| 423 |
# Start with flamethrower_directory_portbase + 2. |
| 424 |
$portbase = $ft_config->get("flamethrower_directory_portbase") + 2; |
| 425 |
|
| 426 |
# If that port is already in use, increment by two until we |
| 427 |
# get a free one. |
| 428 |
while ( defined($portbases{$portbase}) ) { |
| 429 |
$portbase += 2; |
| 430 |
} |
| 431 |
|
| 432 |
# Put it into module specific data structure |
| 433 |
$ft_config->set("${module}_portbase", $portbase); |
| 434 |
|
| 435 |
# Add our new portbase to the existing list. |
| 436 |
$portbases{$portbase} = $portbase; |
| 437 |
|
| 438 |
} |
| 439 |
print FILE "PORTBASE=$portbase\n"; |
| 440 |
|
| 441 |
# ASYNC |
| 442 |
my $async; |
| 443 |
if ($ft_config->varlist("${module}_async")) { |
| 444 |
# async is already set |
| 445 |
$async = $ft_config->get("${module}_async"); |
| 446 |
} else { |
| 447 |
# get it from global setting |
| 448 |
$async = $ft_config->get("async"); |
| 449 |
|
| 450 |
# Put it into module specific data structure |
| 451 |
$ft_config->set("${module}_async", $async); |
| 452 |
|
| 453 |
} |
| 454 |
print FILE "ASYNC=$async\n" if ($async); |
| 455 |
|
| 456 |
# TTL |
| 457 |
my $ttl; |
| 458 |
if ($ft_config->varlist("${module}_ttl")) { |
| 459 |
# ttl is already set |
| 460 |
$ttl = $ft_config->get("${module}_ttl"); |
| 461 |
} else { |
| 462 |
|
| 463 |
# May or may not be set |
| 464 |
# get it from global setting (if it exists) |
| 465 |
if ($ft_config->varlist("^ttl")) { |
| 466 |
$ttl = $ft_config->get("ttl"); |
| 467 |
|
| 468 |
# Put it into module specific data structure |
| 469 |
$ft_config->set("${module}_ttl", $ttl); |
| 470 |
} |
| 471 |
} |
| 472 |
print FILE "TTL=$ttl\n" if ($ttl); |
| 473 |
|
| 474 |
# MCAST_ALL_ADDR |
| 475 |
my $mcast_all_addr; |
| 476 |
if ($ft_config->varlist("${module}_mcast_all_addr")) { |
| 477 |
# mcast_all_addr is already set |
| 478 |
$mcast_all_addr = $ft_config->get("${module}_mcast_all_addr"); |
| 479 |
} else { |
| 480 |
|
| 481 |
# May or may not be set |
| 482 |
# get it from global setting (if it exists) |
| 483 |
if ($ft_config->varlist("^mcast_all_addr")) { |
| 484 |
$mcast_all_addr = $ft_config->get("mcast_all_addr"); |
| 485 |
|
| 486 |
# Put it into module specific data structure |
| 487 |
$ft_config->set("${module}_mcast_all_addr", $mcast_all_addr); |
| 488 |
} |
| 489 |
} |
| 490 |
print FILE "MCAST_ALL_ADDR=$mcast_all_addr\n" if ($mcast_all_addr); |
| 491 |
|
| 492 |
# NOSYNC |
| 493 |
my $nosync; |
| 494 |
if ($ft_config->varlist("${module}_nosync")) { |
| 495 |
# nosync is already set |
| 496 |
$nosync = $ft_config->get("${module}_nosync"); |
| 497 |
} else { |
| 498 |
|
| 499 |
# May or may not be set |
| 500 |
# get it from global setting (if it exists) |
| 501 |
if ($ft_config->varlist("^nosync")) { |
| 502 |
$nosync = $ft_config->get("nosync"); |
| 503 |
|
| 504 |
# Put it into module specific data structure |
| 505 |
$ft_config->set("${module}_nosync", $nosync); |
| 506 |
} |
| 507 |
} |
| 508 |
print FILE "NOSYNC=$nosync\n" if ($nosync); |
| 509 |
|
| 510 |
|
| 511 |
close(FILE); |
| 512 |
|
| 513 |
} |
| 514 |
# |
| 515 |
############################################################################ |
| 516 |
} |
| 517 |
|
| 518 |
# Usage: main(); |
| 519 |
sub main { |
| 520 |
print "main()\n" if($debug); |
| 521 |
if(conf_file_updated($conf_file)) { |
| 522 |
read_config_file($conf_file); |
| 523 |
} |
| 524 |
my $dir = $ft_config->get("flamethrower_state_dir"); |
| 525 |
my $modules = $ft_config->modules(); |
| 526 |
foreach my $module ( @$modules ) { |
| 527 |
my $file = "${dir}/flamethrowerd.${module}.pid"; |
| 528 |
unless(-e "$file") { |
| 529 |
# In case this happens to get started *immediately* |
| 530 |
sleep 1; # after the cast() function does it's unlink, give |
| 531 |
# the child time to die. |
| 532 |
cast($module, $dir); |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
# Usage: record_pid($file, $pid); |
| 538 |
sub record_pid { |
| 539 |
|
| 540 |
my $file = shift; |
| 541 |
my $pid = shift; |
| 542 |
|
| 543 |
print "record_pid($file, $pid)\n" if($debug); |
| 544 |
|
| 545 |
open(FILE, ">$file") or die("Couldn't open $file!"); |
| 546 |
print FILE "$pid\n"; |
| 547 |
close(FILE); |
| 548 |
} |
| 549 |
|
| 550 |
# Usage: cast($module, $state_dir); |
| 551 |
sub cast { |
| 552 |
|
| 553 |
my $module = shift; |
| 554 |
my $state_dir = shift; |
| 555 |
|
| 556 |
my $parent_file = "${state_dir}/flamethrowerd.${module}.pid"; |
| 557 |
my $child_file = "${state_dir}/flamethrowerd.${module}.udp-sender.pid"; |
| 558 |
|
| 559 |
my $file; |
| 560 |
my $parent_pid; |
| 561 |
my $child_pid; |
| 562 |
|
| 563 |
print "cast($module, $state_dir)\n" if($debug); |
| 564 |
|
| 565 |
# Fork and cast |
| 566 |
if ($parent_pid = fork) { |
| 567 |
record_pid($parent_file, $parent_pid); |
| 568 |
} elsif (defined $parent_pid) { # send the forked child off |
| 569 |
|
| 570 |
setsid or die "Can't start a new session: $!"; |
| 571 |
|
| 572 |
print "casting $module\n" if($debug); |
| 573 |
|
| •574 |
my @cmd = build_udp_sender_cmd($module); |
| 575 |
print "cmd @cmd\n" if($debug); |
| 576 |
|
| •577 |
my $child_pid = open(SENDER, "-|", @cmd) or die "Couldn't fork: @cmd"; |
| 578 |
record_pid($child_file, $child_pid); |
| 579 |
while(<SENDER>){ |
| 580 |
print $_ if($debug); |
| 581 |
} |
| 582 |
close(SENDER); |
| 583 |
|
| 584 |
$file = $child_file; |
| 585 |
print " unlinking $file\n" if($debug); |
| 586 |
unlink($file) or die("Couldn't remove $file!\n"); |
| 587 |
|
| 588 |
$file = $parent_file; |
| 589 |
print " unlinking $file\n" if($debug); |
| 590 |
unlink($file) or die("Couldn't remove $file!\n"); |
| 591 |
|
| 592 |
exit 0; |
| 593 |
|
| 594 |
} else { |
| 595 |
die "Can't fork: $!\n"; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
# Usage: read_config_file($file); |
| 600 |
sub read_config_file { |
| 601 |
my $file = shift; |
| 602 |
Flamethrower->read_config($file) or die("Couldn't read $file!"); |
| 603 |
&update_directory; |
| 604 |
} |
| 605 |
|
| 606 |
# Usage: if(conf_file_updated($conf_file)) {}; |
| 607 |
sub conf_file_updated { |
| 608 |
|
| 609 |
my $file = shift; |
| 610 |
|
| 611 |
my $st = stat($file); |
| 612 |
my $this_time = $st->ctime; |
| 613 |
|
| 614 |
if($this_time ne $last_time) { |
| 615 |
print " $file updated\n" if($debug); |
| 616 |
$last_time = $this_time; |
| 617 |
return 1; |
| 618 |
} |
| 619 |
return undef; |
| 620 |
} |
| 621 |
|
| 622 |
# Usage: $SIG{CHLD} = \&REAPER; |
| 623 |
sub REAPER { |
| 624 |
$SIG{CHLD} = \&REAPER; |
| 625 |
my $waitedpid = wait; |
| 626 |
print STDERR " Process $waitedpid has exited.\n" if($debug); |
| 627 |
} |
| 628 |
|
| 629 |
# Usage: &daemonize; |
| 630 |
sub daemonize { |
| 631 |
|
| 632 |
chdir '/' or die "Can't chdir to /: $!"; |
| 633 |
# |
| 634 |
# Commenting out the line below allows the udp-senders to behave in the way |
| 635 |
# you would expect, by automatically starting according to the --min-client, |
| 636 |
# --min-wait, and --max-wait parameters. |
| 637 |
# |
| 638 |
#open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; |
| 639 |
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!" unless($debug); |
| 640 |
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!" unless($debug); |
| 641 |
defined(my $pid = fork) or die "Can't fork: $!"; |
| 642 |
exit if $pid; |
| 643 |
setsid or die "Can't start a new session: $!"; |
| 644 |
|
| 645 |
my $file = $pid_file; |
| 646 |
local *FILE; |
| 647 |
open(FILE,">$file") or die ("FATAL: Can't open file: $file\n"); |
| 648 |
print FILE "$$\n"; |
| 649 |
close(FILE); |
| 650 |
|
| 651 |
$SIG{CHLD} = \&REAPER; |
| 652 |
} |
| 653 |
|
| 654 |
# |
| 655 |
# END subroutines |
| 656 |
# |
| 657 |
################################################################################ |
| 658 |
|