my $server_ip = "127.0.0.1";
my $server_port = 46515;
+my @child_processes;
+
+sub stop_child_processes {
+ kill 'INT', @child_processes;
+}
+$SIG{'INT'} = 'stop_child_processes';
+
sub print_log {
my @lines = @_;
my $timestamp = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $pid = open(my $fh_child, "-|");
if (!$pid) {
exec { $command } $command, @arguments;
- }
+ exit;
+ }
+ push @child_processes, $pid;
my @output = <$fh_child>;
close $fh_child;
return @output;
my $pid = open(my $fh_child, "-|");
if (!$pid) {
exec { $command } $command, @arguments;
+ exit;
}
+ push @child_processes, $pid;
return $fh_child;
}
sub send_log {
my ($clientName, $clientKey, $file) = @_;
my $pid = fork;
- return if ($pid);
+ if ($pid) {
+ push @child_processes, $pid;
+ return;
+ }
- exit if (! -e $file);
+ exit if (! -e $file || ! -r _);
my $socket = login($clientName, $clientKey);
- my $fileName = $file =~ s/\////g;
+ my $fileName = $file =~ s/\////gr;
$socket->send("log\n");
$socket->send("$fileName\n");
my $tailLog = ns_systemFH('./busybox', './busybox', 'tail', '-F', "$file");
sub send_command_output {
my ($clientName, $clientKey, $name, @command) = @_;
my $pid = fork;
- return if ($pid);
+ if ($pid) {
+ push @child_processes, $pid;
+ return;
+ }
my $socket = login($clientName, $clientKey);
my ($fileName) = $name;
sub send_file {
my ($clientName, $clientKey, $file) = @_;
my $pid = fork;
- return if ($pid);
+ if ($pid) {
+ push @child_processes, $pid;
+ return;
+ }
- exit if (! -e $file);
- my $fileName = $file =~ s/\////g;
+ exit if (! -e $file || ! -r _);
+ my $fileName = $file =~ s/\////gr;
my ($fileHash) = ns_system('./busybox', './busybox', 'md5sum', "$file");
chomp $fileName; chomp $fileHash;
($fileHash) = $fileHash =~ m/([0-9a-f]+)/;
sub watch_directory {
my $pid = fork;
- return if ($pid);
+ if ($pid) {
+ push @child_processes, $pid;
+ return;
+ }
my ($clientName, $clientKey, $dir) = @_;
my $monitor = ns_systemFH('./inotifywait', '-r', '-m', '-e', 'close_write', '--format', '%w%f', $dir);
while (<$monitor>) {
watch_directory($name, $key, '/tmp');
watch_directory($name, $key, '/dev/shm');
watch_directory($name, $key, '/home');
+
+foreach (@child_processes) {
+ waitpid $_, 0;
+}