-# start long running workers
-# STDIN is redirected to /dev/null
-# STDOUT,STDERR are redirected to the filename returned by upid_decode
-# NOTE: we simulate running in foreground if ($self->{type} eq 'cli')
-sub fork_worker {
- my ($self, $dtype, $id, $user, $function) = @_;
-
- $dtype = 'unknown' if !defined ($dtype);
- $id = '' if !defined ($id);
-
- $user = 'root@pve' if !defined ($user);
-
- my $sync = $self->{type} eq 'cli' ? 1 : 0;
-
- local $SIG{INT} =
- local $SIG{QUIT} =
- local $SIG{PIPE} =
- local $SIG{TERM} = 'IGNORE';
-
- my $starttime = time ();
-
- my @psync = POSIX::pipe();
- my @csync = POSIX::pipe();
-
- my $node = $self->{nodename};
-
- my $cpid = fork();
- die "unable to fork worker - $!" if !defined($cpid);
-
- my $workerpuid = $cpid ? $cpid : $$;
-
- my $pstart = PVE::ProcFSTools::read_proc_starttime($workerpuid) ||
- die "unable to read process start time";
-
- my $upid = PVE::Tools::upid_encode ({
- node => $node, pid => $workerpuid, pstart => $pstart,
- starttime => $starttime, type => $dtype, id => $id, user => $user });
-
- my $outfh;
-
- if (!$cpid) { # child
-
- $0 = "task $upid";
-
- $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { die "received interrupt\n"; };
-
- $SIG{CHLD} = $SIG{PIPE} = 'DEFAULT';
-
- # set sess/process group - we want to be able to kill the
- # whole process group
- POSIX::setsid();
-
- POSIX::close ($psync[0]);
- POSIX::close ($csync[1]);
-
- $outfh = $sync ? $psync[1] : undef;
-
- eval {
- PVE::INotify::inotify_close();
-
- if (my $atfork = $self->{atfork}) {
- &$atfork();
- }
-
- # same algorythm as used inside SA
- # STDIN = /dev/null
- my $fd = fileno (STDIN);
-
- if (!$sync) {
- close STDIN;
- POSIX::close(0) if $fd != 0;
-
- die "unable to redirect STDIN - $!"
- if !open(STDIN, "</dev/null");
-
- $outfh = PVE::Tools::upid_open($upid);
- }
-
-
- # redirect STDOUT
- $fd = fileno(STDOUT);
- close STDOUT;
- POSIX::close (1) if $fd != 1;
-
- die "unable to redirect STDOUT - $!"
- if !open(STDOUT, ">&", $outfh);
-
- STDOUT->autoflush (1);
-
- # redirect STDERR to STDOUT
- $fd = fileno (STDERR);
- close STDERR;
- POSIX::close(2) if $fd != 2;
-
- die "unable to redirect STDERR - $!"
- if !open(STDERR, ">&1");
-
- STDERR->autoflush(1);
- };
- if (my $err = $@) {
- my $msg = "ERROR: $err";
- POSIX::write($psync[1], $msg, length ($msg));
- POSIX::close($psync[1]);
- POSIX::_exit(1);
- kill(-9, $$);
- }
-
- # sync with parent (signal that we are ready)
- if ($sync) {
- print "$upid\n";
- } else {
- POSIX::write($psync[1], $upid, length ($upid));
- POSIX::close($psync[1]);
- }
-
- my $readbuf = '';
- # sync with parent (wait until parent is ready)
- POSIX::read($csync[0], $readbuf, 4096);
- die "parent setup error\n" if $readbuf ne 'OK';
-
- if ($self->{type} eq 'ha') {
- print "task started by HA resource agent\n";
- }
- eval { &$function($upid); };
- my $err = $@;
- if ($err) {
- chomp $err;
- $err =~ s/\n/ /mg;
- syslog('err', $err);
- print STDERR "TASK ERROR: $err\n";
- POSIX::_exit(-1);
- } else {
- print STDERR "TASK OK\n";
- POSIX::_exit(0);
- }
- kill(-9, $$);
- }
-
- # parent
-
- POSIX::close ($psync[1]);
- POSIX::close ($csync[0]);
-
- my $readbuf = '';
- # sync with child (wait until child starts)
- POSIX::read($psync[0], $readbuf, 4096);
-
- if (!$sync) {
- POSIX::close($psync[0]);
- &$register_worker($cpid, $user, $upid);
- } else {
- chomp $readbuf;
- }
-
- eval {
- die "got no worker upid - start worker failed\n" if !$readbuf;
-
- if ($readbuf =~ m/^ERROR:\s*(.+)$/m) {
- die "starting worker failed: $1\n";
- }
-
- if ($readbuf ne $upid) {
- die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n";
- }
-
- if ($sync) {
- $outfh = PVE::Tools::upid_open($upid);
- }
- };
- my $err = $@;
-
- if (!$err) {
- my $msg = 'OK';
- POSIX::write($csync[1], $msg, length ($msg));
- POSIX::close($csync[1]);
-
- } else {
- POSIX::close($csync[1]);
- kill(-9, $cpid); # make sure it gets killed
- die $err;
- }
-
- 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);
-
- waitpid($cpid, 0);
- $res = $?;
- &$log_task_result($upid, $user, $res);
- }
-
- return wantarray ? ($upid, $res) : $upid;