diff options
author | Martin Whitaker <mageia@martin-whitaker.me.uk> | 2022-11-26 21:11:55 +0000 |
---|---|---|
committer | Martin Whitaker <mageia@martin-whitaker.me.uk> | 2022-11-26 21:11:55 +0000 |
commit | 26934d4f834d28c5a29eaf0f3132ebfd6c3e7b72 (patch) | |
tree | c6dbc577b7fdcfdd0d4003e2fe197eb486eee086 | |
parent | 5c0de23eeee977ebfe2da45adfb8487419fb59c7 (diff) | |
download | drakx-26934d4f834d28c5a29eaf0f3132ebfd6c3e7b72.tar drakx-26934d4f834d28c5a29eaf0f3132ebfd6c3e7b72.tar.gz drakx-26934d4f834d28c5a29eaf0f3132ebfd6c3e7b72.tar.bz2 drakx-26934d4f834d28c5a29eaf0f3132ebfd6c3e7b72.tar.xz drakx-26934d4f834d28c5a29eaf0f3132ebfd6c3e7b72.zip |
run_program: add helper routine to terminate/kill a process.
-rw-r--r-- | perl-install/NEWS | 1 | ||||
-rw-r--r-- | perl-install/run_program.pm | 27 |
2 files changed, 28 insertions, 0 deletions
diff --git a/perl-install/NEWS b/perl-install/NEWS index cab9f1e85..f01dbb2d4 100644 --- a/perl-install/NEWS +++ b/perl-install/NEWS @@ -1,6 +1,7 @@ - run_program: o add optional callback when waiting for program to terminate * this allows GUI applications to respond to check-alive pings (mga#31105) + o add helper routine to terminate/kill a process - make installer routines for selecting a mirror and downloader available for use by draklive-install - use better algorithm for determining nearest mirror (taken from urpmi) diff --git a/perl-install/run_program.pm b/perl-install/run_program.pm index 5ac4bf972..b3a65d13a 100644 --- a/perl-install/run_program.pm +++ b/perl-install/run_program.pm @@ -368,6 +368,33 @@ sub raw { } +=item terminate($pid, $o_timeout) + +Sends the TERM signal to the process identified by $pid and waits for it +to terminate. If it hasn't terminated in $o_timeout seconds, sends the +KILL signal and returns without waiting. If $o_timeout is not specified, +the default timeout is 5 seconds. If $o_timeout is less than or equal to +zero, the TERM signal is not sent and the process is killed immediately. + +=cut + +sub terminate { + my ($pid, $o_timeout) = @_; + + if (!defined $o_timeout || $o_timeout > 0) { + kill 'TERM', $pid; + eval { + local $SIG{ALRM} = sub { die "ALARM" }; + my $old_remaining = alarm($o_timeout || 5); + waitpid $pid, 0; + alarm($old_remaining); + }; + return if !$@; + log::l("ERROR: killing runaway process (pid=$pid, error=$@)"); + } + kill 'KILL', $pid; +} + package bg_command; =item bg_command::new($class, $sub) |