]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/RESTEnvironment.pm
fix #1956: return controlling terminal to parent
[pve-common.git] / src / PVE / RESTEnvironment.pm
index 69af004bab6d27cdf9d2f7c16d904f6646493eae..cba062d6cff24b9523b46b8cd193f8bc4867bfee 100644 (file)
@@ -496,10 +496,12 @@ sub fork_worker {
        $SIG{CHLD} = $SIG{PIPE} = 'DEFAULT';
        $SIG{TTOU} = 'IGNORE';
 
+       my $ppgid;
        # set session/process group allows to kill the process group
        if ($sync && -t STDIN) {
            # some sync'ed workers operate on the tty but setsid sessions lose
            # the tty, so just create a new pgroup and give it the tty
+           $ppgid = POSIX::getpgrp() or die "failed to get old pgid: $!\n";
            POSIX::setpgid(0, 0) or die "failed to setpgid: $!\n";
            POSIX::tcsetpgrp(fileno(STDIN), $$) or die "failed to tcsetpgrp: $!\n";
        } else {
@@ -579,22 +581,28 @@ sub fork_worker {
            }
            &$function($upid);
        };
+       my ($msg, $exitcode);
        my $err = $@;
        if ($err) {
            chomp $err;
            $err =~ s/\n/ /mg;
            syslog('err', $err);
-           my $msg = "TASK ERROR: $err\n";
-           POSIX::write($resfh, $msg, length($msg));
-           POSIX::close($resfh) if $sync;
-           POSIX::_exit(-1);
+           $msg = "TASK ERROR: $err\n";
+           $exitcode = -1;
        } else {
-           my $msg = "TASK OK\n";
-           POSIX::write($resfh, $msg, length($msg));
-           POSIX::close($resfh) if $sync;
-           POSIX::_exit(0);
+           $msg = "TASK OK\n";
+           $exitcode = 0;
        }
-       kill(-9, $$);
+       POSIX::write($resfh, $msg, length($msg));
+
+       if ($sync) {
+           POSIX::close($resfh);
+           if ( -t STDIN) {
+               POSIX::tcsetpgrp(fileno(STDIN), $ppgid) or
+                   die "failed to tcsetpgrp to parent: $!\n";
+           }
+       }
+       POSIX::_exit($exitcode);
     }
 
     # parent