forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignalblocker.pm
56 lines (47 loc) · 1.91 KB
/
signalblocker.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright © 2020 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
package signalblocker;
use Mojo::Base -base;
use bmwqemu;
use POSIX ':signal_h';
# OpenCV forks a lot of threads and the TERM signal we may get from the
# parent process would be delivered to an undefined thread. But as those
# threads do not have a perl interpreter, the perl signal handler (we set
# later) would crash. So we need to block the TERM signal in the forked
# processes before we set the signal handler of our choice.
sub new {
my ($class, @args) = @_;
# block signals
bmwqemu::diag('Blocking SIGTERM');
my %old_sig = %SIG;
$SIG{TERM} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{HUP} = 'IGNORE';
my $sigset = POSIX::SigSet->new(SIGTERM);
die "Could not block SIGTERM\n" unless defined sigprocmask(SIG_BLOCK, $sigset, undef);
# create the actual object holding the information to restore the previous state
my $self = $class->SUPER::new(@args);
$self->{_old_sig} = \%old_sig;
$self->{_sigset} = $sigset;
return $self;
}
sub DESTROY {
my ($self) = @_;
# set back signal handling to default to be able to terminate properly
bmwqemu::diag('Unblocking SIGTERM');
die "Could not unblock SIGTERM\n" unless defined sigprocmask(SIG_UNBLOCK, $self->{_sigset}, undef);
%SIG = %{$self->{_old_sig}};
}
1;