public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 2/3] add run_in_background
Date: Tue, 10 Nov 2020 15:15:29 +0100	[thread overview]
Message-ID: <20201110141530.30904-3-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20201110141530.30904-1-s.ivanov@proxmox.com>

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@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





  parent reply	other threads:[~2020-11-10 14:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-10 14:15 [pve-devel] [PATCH installer 0/3] memorize keyboard layout and run setupcon in background Stoiko Ivanov
2020-11-10 14:15 ` [pve-devel] [PATCH installer 1/3] memorize keyboard layout selection Stoiko Ivanov
2020-11-10 14:15 ` Stoiko Ivanov [this message]
2020-11-10 14:15 ` [pve-devel] [PATCH installer 3/3] set the keymap on the installer console Stoiko Ivanov
2020-11-10 14:33 ` [pve-devel] [PATCH installer 0/3] memorize keyboard layout and run setupcon in background Oguz Bektas
2020-12-10 19:07 ` [pve-devel] applied-series: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201110141530.30904-3-s.ivanov@proxmox.com \
    --to=s.ivanov@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal