File Coverage

signalblocker.pm
Criterion Covered Total %
statement 29 29 100.0
total 29 29 100.0


line stmt code
1   # Copyright 2021 SUSE LLC
2   # SPDX-License-Identifier: GPL-2.0-or-later
3    
4   use Mojo::Base -base, -signatures;
5 73  
  73  
  73  
6   use bmwqemu;
7 73 use POSIX ':signal_h';
  73  
  73  
8 73  
  73  
  73  
9   # OpenCV forks a lot of threads and the signals we may get (TERM from the
10   # parent, CHLD from children) would be delivered to an undefined thread.
11   # But as those threads do not have a perl interpreter, the perl signal
12   # handler would crash. We need to block those signals in those threads, so
13   # that they get delivered only to those threads which can handle it.
14    
15   # block signals
16 11 my %old_sig = %SIG;
  11  
  11  
  11  
17   $SIG{TERM} = 'IGNORE';
18 11 $SIG{INT} = 'IGNORE';
19 11 $SIG{HUP} = 'IGNORE';
20 11 my $sigset = POSIX::SigSet->new(SIGCHLD, SIGTERM);
21 11 die "Could not block SIGCHLD and SIGTERM\n" unless defined sigprocmask(SIG_BLOCK, $sigset, undef);
22 11  
23 11 # create the actual object holding the information to restore the previous state
24   my $self = $class->SUPER::new(@args);
25   $self->{_old_sig} = \%old_sig;
26 11 $self->{_sigset} = $sigset;
27 11 return $self;
28 11 }
29 11  
30   # set back signal handling to default to be able to terminate properly
31   die "Could not unblock SIGCHLD and SIGTERM\n" unless defined sigprocmask(SIG_UNBLOCK, $self->{_sigset}, undef);
32 11 %SIG = %{$self->{_old_sig}};
  11  
  11  
33   }
34 11  
35 11 1;
  11