From 572baa6c85ab8bbe181b3709c0620156633b6393 Mon Sep 17 00:00:00 2001 From: Jon duSaint Date: Sat, 16 Sep 2023 09:41:28 -0700 Subject: reolink: Simplify child process handling Since all we care about is whether the video-generating process is still running, we can remove the SIGCHLD handling that updates the list of child processes in favor of directly checking them when decideding whether to generate a video or not. --- reolink/reolink | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) (limited to 'reolink') diff --git a/reolink/reolink b/reolink/reolink index 7b5ec2c..0b18167 100755 --- a/reolink/reolink +++ b/reolink/reolink @@ -337,23 +337,6 @@ sub process_command { my %process_children; -my @sigchld_handlers; - -sub process_complete { - local ($!, $?); - - while ((my $pid = waitpid (-1, WNOHANG)) > 0) { - if (defined $process_children{$pid}) { - debug ("child process $pid ($process_children{$pid}) complete"); - delete $process_children{$pid}; - if (@sigchld_handlers) { - $SIG{CHLD} = pop @sigchld_handlers; - } - } else { - error ("SIGCHLD for pid $pid ($?), but it's not in the list!"); - } - } -} sub maybe_generate_video { # [camera list] [time] @@ -372,10 +355,16 @@ sub maybe_generate_video { CAMERA: foreach my $camera (@cameras) { - foreach my $child (values %process_children) { - if ($child eq $camera) { - debug ("child process $child running..."); + + foreach my $pid (keys %process_children) { + my $res = waitpid ($pid, WNOHANG); + debug ("$pid -> $process_children{$pid}: $res"); + if ($res == 0) { + debug ("child process $pid ($process_children{$pid}) running..."); next CAMERA; + } else { + debug ("child process $pid ($process_children{$pid}) complete"); + delete $process_children{$pid}; } } @@ -408,18 +397,9 @@ sub maybe_generate_video { } push @process_args, ('--process' => $video_prefix, '--range' => $server_params{video}, '--spool', spool ($camera)); - push @sigchld_handlers, $SIG{CHLD}; - $SIG{CHLD} = \&process_complete; - - my $sigset = POSIX::SigSet->new (SIGCHLD); - my $sigset_prev = POSIX::SigSet->new (); - - sigprocmask (SIG_BLOCK, $sigset, $sigset_prev); - my $process_child = fork (); unless (defined ($process_child)) { error ("failed to fork video process for $camera"); - pop @sigchld_handlers; next; } @@ -428,7 +408,6 @@ sub maybe_generate_video { } $process_children{$process_child} = $camera; - sigprocmask (SIG_UNBLOCK, $sigset_prev); debug ("launched video process for $camera as pid $process_child"); } -- cgit v1.2.3