From: mivirl <> Date: Thu, 25 Jan 2024 18:44:22 +0000 (-0600) Subject: Handle SIGINT; Check read permissions X-Git-Url: http://mivirl.dev/git/?a=commitdiff_plain;h=7987d0bd396fe986e86fdb28eb3736b004be232c;p=remote-logging.git Handle SIGINT; Check read permissions - Add signal handler for SIGINT that sends SIGINT to child processes - Check for read permissions before attempting to send file --- diff --git a/src/client.pl b/src/client.pl index 231834d..a7e890d 100644 --- a/src/client.pl +++ b/src/client.pl @@ -7,6 +7,13 @@ use POSIX "strftime"; 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; @@ -20,7 +27,9 @@ sub ns_system { 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; @@ -30,7 +39,9 @@ sub ns_systemFH { my $pid = open(my $fh_child, "-|"); if (!$pid) { exec { $command } $command, @arguments; + exit; } + push @child_processes, $pid; return $fh_child; } @@ -104,11 +115,14 @@ sub send_info { 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"); @@ -124,7 +138,10 @@ sub send_log { 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; @@ -144,10 +161,13 @@ sub send_command_output { 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]+)/; @@ -181,7 +201,10 @@ sub send_file { 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>) { @@ -216,3 +239,7 @@ send_command_output($name, $key, 'pspy_output.txt', './pspy64', '-f'); watch_directory($name, $key, '/tmp'); watch_directory($name, $key, '/dev/shm'); watch_directory($name, $key, '/home'); + +foreach (@child_processes) { + waitpid $_, 0; +}