- PVE::Cluster::log_msg('info', $user, "starting task $upid");
-
- my $tlist = active_workers($upid, $sync);
- PVE::Cluster::broadcast_tasklist($tlist);
-
- my $res = 0;
-
- if ($sync) {
- my $count;
- my $outbuf = '';
- my $int_count = 0;
- eval {
- local $SIG{INT} = local $SIG{QUIT} = local $SIG{TERM} = sub {
- # always send signal to all pgrp members
- my $kpid = -$cpid;
- if ($int_count < 3) {
- kill(15, $kpid); # send TERM signal
- } else {
- kill(9, $kpid); # send KILL signal
- }
- $int_count++;
- };
- local $SIG{PIPE} = sub { die "broken pipe\n"; };
-
- my $select = new IO::Select;
- my $fh = IO::Handle->new_from_fd($psync[0], 'r');
- $select->add($fh);
-
- while ($select->count) {
- my @handles = $select->can_read(1);
- if (scalar(@handles)) {
- my $count = sysread ($handles[0], $readbuf, 4096);
- if (!defined ($count)) {
- my $err = $!;
- die "sync pipe read error: $err\n";
- }
- last if $count == 0; # eof
-
- $outbuf .= $readbuf;
- while ($outbuf =~ s/^(([^\010\r\n]*)(\r|\n|(\010)+|\r\n))//s) {
- my $line = $1;
- my $data = $2;
- if ($data =~ m/^TASK OK$/) {
- # skip
- } elsif ($data =~ m/^TASK ERROR: (.+)$/) {
- print STDERR "$1\n";
- } else {
- print $line;
- }
- if ($outfh) {
- print $outfh $line;
- $outfh->flush();
- }
- }
- } else {
- # some commands daemonize without closing stdout
- last if !PVE::ProcFSTools::check_process_running($cpid);
- }
- }
- };
- my $err = $@;
-
- POSIX::close($psync[0]);
-
- if ($outbuf) { # just to be sure
- print $outbuf;
- if ($outfh) {
- print $outfh $outbuf;
- }
- }
-
- if ($err) {
- $err =~ s/\n/ /mg;
- print STDERR "$err\n";
- if ($outfh) {
- print $outfh "TASK ERROR: $err\n";
- }
- }
-
- &$kill_process_group($cpid, $pstart); # make sure it gets killed
-
- close($outfh);