From a203bd5e29f70f99848a8cec016c2fff1f66b779 Mon Sep 17 00:00:00 2001 From: mivirl <> Date: Wed, 24 Jan 2024 23:15:40 -0600 Subject: [PATCH] Add file existence checks; forking -> exit; fork for inotifywatch - Added checks for file existence before attempting to upload, which can in theory esult in race conditions, but in this case doesn't matter that much. - Changed forking subroutines to exit instead of return, to prevent forked versions of the script from re-running the same commands - Changed inotifywatch subroutine to fork --- src/client.pl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/client.pl b/src/client.pl index d239807..64e61e1 100644 --- a/src/client.pl +++ b/src/client.pl @@ -104,6 +104,7 @@ sub send_log { my $pid = fork; return if ($pid); + exit if (! -e $file); my $socket = login($clientName, $clientKey); my ($fileName) = ns_system('./busybox', './busybox', 'basename', "$file"); $socket->send("log\n"); @@ -115,7 +116,7 @@ sub send_log { print_log "Log: Closing $file"; close($tailLog); $socket->send("⟃---EOF---⟄\n"); - return $socket->close(); + exit; } sub send_command_output { @@ -135,7 +136,7 @@ sub send_command_output { print_log "Command: Completed @command"; close($commandLog); $socket->send("⟃---EOF---⟄\n"); - return $socket->close(); + exit; } sub send_file { @@ -143,6 +144,7 @@ sub send_file { my $pid = fork; return if ($pid); + exit if (! -e $file); my ($fileName) = ns_system('./busybox', './busybox', 'basename', "$file"); my ($fileHash) = ns_system('./busybox', './busybox', 'md5sum', "$file"); chomp $fileName; chomp $fileHash; @@ -168,20 +170,23 @@ sub send_file { $socket->recv(my $response, 128); if ($response =~ m/Transfer success/) { print_log "File: upload success ($file)"; - return 1 + exit; } else { print_log "File: upload failure ($file)"; } - return 0 + exit; } sub watch_directory { + my $pid = fork; + return if ($pid); my ($clientName, $clientKey, $dir) = @_; my $monitor = ns_systemFH('./inotifywait', '-r', '-m', '-e', 'close_write', '--format', '%w%f', $dir); while (<$monitor>) { chomp; send_file($clientName, $clientKey, $_); } + exit; } sub capture_packets { @@ -189,6 +194,7 @@ sub capture_packets { my $pid = fork; return if ($pid); my $tcpdump = ns_system('/usr/bin/tcpdump', '-w', '/dev/shm/pcap', '-W', '10', '-G', '60', '-C', '100', '-K', '-n'); + exit; } my ($name, $key) = register; -- 2.39.5