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