diff options
| author | Jon duSaint | 2023-09-16 09:41:28 -0700 |
|---|---|---|
| committer | Jon duSaint | 2023-09-16 09:41:28 -0700 |
| commit | 572baa6c85ab8bbe181b3709c0620156633b6393 (patch) | |
| tree | b237808a00a2cb7ed57cf94450c171d2fd7d8a02 | |
| parent | fc553499d07c439fbe7299fbc3a8e1a2f72ca32b (diff) | |
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.
| -rwxr-xr-x | reolink/reolink | 39 |
1 files changed, 9 insertions, 30 deletions
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"); } |
