[pve-devel] [PATCH installer 2/3] add run_in_background

Stoiko Ivanov s.ivanov at proxmox.com
Tue Nov 10 15:15:29 CET 2020


certain tasks done during the installer need not block the GUI (e.g. setting
the keymap of the console, to have the correct one set in the shell on vt3)
and take a longer time.

This patch adds a simple run_in_background method, which forks and runs the
provided code in the child. Before exiting the children get reaped.

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 proxinstall | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/proxinstall b/proxinstall
index 9977f44..1551e18 100755
--- a/proxinstall
+++ b/proxinstall
@@ -21,6 +21,7 @@ use Data::Dumper;
 use File::Basename;
 use File::Path;
 use Time::HiRes;
+use POSIX ":sys_wait_h";
 
 use ProxmoxInstallerSetup;
 
@@ -484,6 +485,23 @@ sub run_command {
     return $ostream;
 }
 
+# forks and runs the provided coderef in the child
+# do not use syscmd or run_command as both confuse the GTK mainloop if
+# run from a child process
+sub run_in_background {
+    my ($cmd) = @_;
+
+    my $pid = fork() // die "fork failed: $!\n";
+    if (!$pid) {
+	eval { $cmd->(); };
+	if (my $err = $@) {
+	    warn "run_in_background error: $err\n";
+	    POSIX::_exit(1);
+	}
+	POSIX::_exit(0);
+    }
+}
+
 sub detect_country {
 
     print "trying to detect country...\n";
@@ -3583,4 +3601,9 @@ create_intro_view () if !$initial_error;
 
 Gtk3->main;
 
+# reap left over zombie processes
+while ((my $child = waitpid(-1, POSIX::WNOHANG)) > 0) {
+    print "reaped child $child\n";
+}
+
 exit 0;
-- 
2.20.1






More information about the pve-devel mailing list