]>
git.proxmox.com Git - pve-access-control.git/blob - PVE/RPCEnvironment.pm
1 package PVE
::RPCEnvironment
;
5 use POSIX
qw(:sys_wait_h EINTR);
10 use PVE
::Exception
qw(raise raise_perm_exc);
16 use PVE
::AccessControl
;
20 # we use this singleton class to pass RPC related environment values
24 # save $SIG{CHLD} handler implementation.
25 # simply set $SIG{CHLD} = $worker_reaper;
26 # and register forked processes with &$register_worker(pid)
27 # Note: using $SIG{CHLD} = 'IGNORE' or $SIG{CHLD} = sub { wait (); } or ...
28 # has serious side effects, because perls built in system() and open()
29 # functions can't get the correct exit status of a child. So we cant use
30 # that (also see perlipc)
34 my $log_task_result = sub {
35 my ($upid, $user, $status) = @_;
37 my $msg = 'successful';
40 my $ec = $status >> 8;
41 my $ic = $status & 255;
42 $msg = $ec ?
"failed ($ec)" : "interrupted ($ic)";
45 my $tlist = active_workers
($upid);
46 PVE
::Cluster
::broadcast_tasklist
($tlist);
48 foreach my $t (@$tlist) {
49 if ($t->{upid
} eq $upid) {
54 if ($task && $task->{status
}) {
55 $msg = $task->{status
};
57 PVE
::Cluster
::log_msg
($pri, $user, "end task $upid $msg");
60 my $worker_reaper = sub {
62 foreach my $pid (keys %$WORKER_PIDS) {
63 my $waitpid = waitpid ($pid, WNOHANG
);
64 if (defined($waitpid) && ($waitpid == $pid)) {
65 my $info = $WORKER_PIDS->{$pid};
66 if ($info && $info->{upid
} && $info->{user
}) {
67 &$log_task_result($info->{upid
}, $info->{user
}, $?);
69 delete ($WORKER_PIDS->{$pid});
74 my $register_worker = sub {
75 my ($pid, $user, $upid) = @_;
79 # do not register if already finished
80 my $waitpid = waitpid ($pid, WNOHANG
);
81 if (defined($waitpid) && ($waitpid == $pid)) {
82 delete ($WORKER_PIDS->{$pid});
86 $WORKER_PIDS->{$pid} = {
94 my $compile_acl_path = sub {
95 my ($self, $user, $path) = @_;
97 my $cfg = $self->{user_cfg
};
99 return undef if !$cfg->{roles
};
101 die "internal error" if $user eq 'root@pam';
103 my $cache = $self->{aclcache
};
104 $cache->{$user} = {} if !$cache->{$user};
105 my $data = $cache->{$user};
107 if (!$data->{poolroles
}) {
108 $data->{poolroles
} = {};
110 foreach my $pool (keys %{$cfg->{pools
}}) {
111 my $d = $cfg->{pools
}->{$pool};
112 my @ra = PVE
::AccessControl
::roles
($cfg, $user, "/pool/$pool"); # pool roles
113 next if !scalar(@ra);
114 foreach my $vmid (keys %{$d->{vms
}}) {
116 $data->{poolroles
}->{"/vms/$vmid"}->{$role} = 1;
119 foreach my $storeid (keys %{$d->{storage
}}) {
121 $data->{poolroles
}->{"/storage/$storeid"}->{$role} = 1;
127 my @ra = PVE
::AccessControl
::roles
($cfg, $user, $path);
129 # apply roles inherited from pools
130 # Note: assume we do not want to propagate those privs
131 if ($data->{poolroles
}->{$path}) {
132 if (!($ra[0] && $ra[0] eq 'NoAccess')) {
133 if ($data->{poolroles
}->{$path}->{NoAccess
}) {
136 foreach my $role (keys %{$data->{poolroles
}->{$path}}) {
143 $data->{roles
}->{$path} = [ @ra ];
146 foreach my $role (@ra) {
147 if (my $privset = $cfg->{roles
}->{$role}) {
148 foreach my $p (keys %$privset) {
153 $data->{privs
}->{$path} = $privs;
159 my ($self, $user, $path) = @_;
161 if ($user eq 'root@pam') { # root can do anything
162 return ('Administrator');
165 $user = PVE
::AccessControl
::verify_username
($user, 1);
168 my $cache = $self->{aclcache
};
169 $cache->{$user} = {} if !$cache->{$user};
171 my $acl = $cache->{$user};
173 my $roles = $acl->{roles
}->{$path};
174 return @$roles if $roles;
176 &$compile_acl_path($self, $user, $path);
177 $roles = $acl->{roles
}->{$path} || [];
182 my ($self, $user, $path) = @_;
184 if ($user eq 'root@pam') { # root can do anything
185 my $cfg = $self->{user_cfg
};
186 return $cfg->{roles
}->{'Administrator'};
189 $user = PVE
::AccessControl
::verify_username
($user, 1);
192 my $cache = $self->{aclcache
};
193 $cache->{$user} = {} if !$cache->{$user};
195 my $acl = $cache->{$user};
197 my $perm = $acl->{privs
}->{$path};
198 return $perm if $perm;
200 return &$compile_acl_path($self, $user, $path);
204 my ($self, $user, $path, $privs, $noerr) = @_;
206 my $perm = $self->permissions($user, $path);
208 foreach my $priv (@$privs) {
209 PVE
::AccessControl
::verify_privname
($priv);
210 if (!$perm->{$priv}) {
211 return undef if $noerr;
212 raise_perm_exc
("$path, $priv");
220 my ($self, $user, $path, $privs, $noerr) = @_;
222 my $perm = $self->permissions($user, $path);
225 foreach my $priv (@$privs) {
226 PVE
::AccessControl
::verify_privname
($priv);
227 if ($perm->{$priv}) {
235 return undef if $noerr;
237 raise_perm_exc
("$path, " . join("|", @$privs));
241 my ($self, $username, $path, $privs, $any, $noerr) = @_;
243 return $self->check_any($username, $path, $privs, $noerr);
245 return $self->check($username, $path, $privs, $noerr);
249 sub check_user_enabled
{
250 my ($self, $user, $noerr) = @_;
252 my $cfg = $self->{user_cfg
};
253 return PVE
::AccessControl
::check_user_enabled
($cfg, $user, $noerr);
256 sub check_user_exist
{
257 my ($self, $user, $noerr) = @_;
259 my $cfg = $self->{user_cfg
};
260 return PVE
::AccessControl
::check_user_exist
($cfg, $user, $noerr);
263 sub check_pool_exist
{
264 my ($self, $pool, $noerr) = @_;
266 my $cfg = $self->{user_cfg
};
268 return 1 if $cfg->{pools
}->{$pool};
270 return undef if $noerr;
272 raise_perm_exc
("pool '$pool' does not exist");
276 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
278 my $cfg = $self->{user_cfg
};
281 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
283 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
286 sub check_volume_access
{
287 my ($self, $user, $storecfg, $vmid, $volid) = @_;
289 # test if we have read access to volid
292 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
294 my ($ownervm, $vtype);
295 ($path, $ownervm, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
296 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
297 # we simply allow access
298 } elsif (!$ownervm || ($ownervm != $vmid)) {
299 # allow if we are Datastore administrator
300 $self->check($user, "/storage/$sid", ['Datastore.Allocate']);
303 die "Only root can pass arbitrary filesystem paths."
304 if $user ne 'root@pam';
306 $path = abs_path
($volid);
311 sub is_group_member
{
312 my ($self, $group, $user) = @_;
314 my $cfg = $self->{user_cfg
};
316 return 0 if !$cfg->{groups
}->{$group};
318 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
322 my ($self, $user, $privs, $any) = @_;
324 my $cfg = $self->{user_cfg
};
327 foreach my $group (keys %{$cfg->{groups
}}) {
328 my $path = "/access/groups/$group";
329 if ($self->check_full($user, $path, $privs, $any, 1)) {
330 $groups->{$group} = $cfg->{groups
}->{$group};
337 sub group_member_join
{
338 my ($self, $grouplist) = @_;
342 my $cfg = $self->{user_cfg
};
343 foreach my $group (@$grouplist) {
344 my $data = $cfg->{groups
}->{$group};
346 foreach my $user (keys %{$data->{users
}}) {
354 sub check_perm_modify
{
355 my ($self, $username, $path, $noerr) = @_;
357 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
359 my $testperms = [ 'Permissions.Modify' ];
360 if ($path =~ m
|^/storage/.+$|) {
361 push @$testperms, 'Datastore.Allocate';
362 } elsif ($path =~ m
|^/vms/.+$|) {
363 push @$testperms, 'VM.Allocate';
364 } elsif ($path =~ m
|^/pool/.+$|) {
365 push @$testperms, 'Pool.Allocate';
368 return $self->check_any($username, $path, $testperms, $noerr);
371 sub exec_api2_perm_check
{
372 my ($self, $check, $username, $param, $noerr) = @_;
374 # syslog("info", "CHECK " . join(', ', @$check));
377 my $test = $check->[$ind++];
378 die "no permission test specified" if !$test;
380 if ($test eq 'and') {
381 while (my $subcheck = $check->[$ind++]) {
382 $self->exec_api2_perm_check($subcheck, $username, $param);
385 } elsif ($test eq 'or') {
386 while (my $subcheck = $check->[$ind++]) {
387 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
391 } elsif ($test eq 'perm') {
392 my ($t, $tmplpath, $privs, %options) = @$check;
393 my $any = $options{any
};
394 die "missing parameters" if !($tmplpath && $privs);
395 my $require_param = $options{require_param
};
396 if ($require_param && !defined($param->{$require_param})) {
400 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
401 $path = PVE
::AccessControl
::normalize_path
($path);
402 return $self->check_full($username, $path, $privs, $any, $noerr);
403 } elsif ($test eq 'userid-group') {
404 my $userid = $param->{userid
};
405 my ($t, $privs, %options) = @$check;
406 return 0 if !$options{groups_param
} && !$self->check_user_exist($userid, $noerr);
407 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
408 my $groups = $self->filter_groups($username, $privs, 1);
409 if ($options{groups_param
}) {
410 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
411 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
412 foreach my $pg (@group_param) {
413 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
417 my $allowed_users = $self->group_member_join([keys %$groups]);
418 if (!$allowed_users->{$userid}) {
425 } elsif ($test eq 'userid-param') {
426 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
427 my ($t, $subtest) = @$check;
428 die "missing parameters" if !$subtest;
429 if ($subtest eq 'self') {
430 return 0 if !$self->check_user_exist($userid, $noerr);
431 return 1 if $username eq $userid;
434 } elsif ($subtest eq 'Realm.AllocateUser') {
435 my $path = "/access/realm/$realm";
436 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
438 die "unknown userid-param test";
440 } elsif ($test eq 'perm-modify') {
441 my ($t, $tmplpath) = @$check;
442 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
443 $path = PVE
::AccessControl
::normalize_path
($path);
444 return $self->check_perm_modify($username, $path, $noerr);
446 die "unknown permission test";
450 sub check_api2_permissions
{
451 my ($self, $perm, $username, $param) = @_;
453 return 1 if !$username && $perm->{user
} eq 'world';
455 raise_perm_exc
("user != null") if !$username;
457 return 1 if $username eq 'root@pam';
459 raise_perm_exc
('user != root@pam') if !$perm;
461 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
463 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
469 # initialize environment - must be called once at program startup
471 my ($class, $type, %params) = @_;
473 $class = ref($class) || $class;
475 die "already initialized" if $pve_env;
477 die "unknown environment type" if !$type || $type !~ m/^(cli|pub|priv|ha)$/;
479 $SIG{CHLD
} = $worker_reaper;
482 # cli ... command started fron command line
483 # pub ... access from public server (apache)
484 # priv ... access from private server (pvedaemon)
485 # ha ... access from HA resource manager agent (rgmanager)
496 foreach my $p (keys %params) {
497 if ($p eq 'atfork') {
498 $self->{$p} = $params{$p};
500 die "unknown option '$p'";
506 my ($sysname, $nodename) = POSIX
::uname
();
508 $nodename =~ s/\..*$//; # strip domain part, if any
510 $self->{nodename
} = $nodename;
518 die "not initialized" if !$pve_env;
524 my ($self, $enable_upload) = @_;
526 if ($self->{request_rec
}) {
528 if ($enable_upload) {
529 $cgi = CGI-
>new($self->{request_rec
});
531 # disable upload using empty upload_hook
532 $cgi = CGI-
>new($self->{request_rec
}, sub {}, undef, 0);
535 my $params = $cgi->Vars();
536 return PVE
::Tools
::decode_utf8_parameters
($params);
537 } elsif ($self->{params
}) {
538 return $self->{params
};
540 die "no parameters registered";
544 sub get_upload_info
{
545 my ($self, $param) = @_;
547 my $cgi = $self->{cgi
};
548 die "CGI not initialized" if !$cgi;
550 my $pd = $cgi->param($param);
551 die "unable to get cgi parameter info\n" if !$pd;
552 my $info = $cgi->uploadInfo($pd);
553 die "unable to get cgi upload info\n" if !$info;
555 my $res = { %$info };
557 my $tmpfilename = $cgi->tmpFileName($pd);
558 die "unable to get cgi upload file name\n" if !$tmpfilename;
559 $res->{tmpfilename
} = $tmpfilename;
561 #my $hndl = $cgi->upload($param);
562 #die "unable to get cgi upload handle\n" if !$hndl;
563 #$res->{handle} = $hndl->handle;
568 # init_request - must be called before each RPC request
570 my ($self, %params) = @_;
572 PVE
::Cluster
::cfs_update
();
574 $self->{result_attributes
} = {};
576 my $userconfig; # we use this for regression tests
577 foreach my $p (keys %params) {
578 if ($p eq 'userconfig') {
579 $userconfig = $params{$p};
580 } elsif ($p eq 'request_rec') {
581 # pass Apache2::RequestRec
582 $self->{request_rec
} = $params{$p};
583 } elsif ($p eq 'params') {
584 $self->{params
} = $params{$p};
586 die "unknown parameter '$p'";
591 $self->{aclcache
} = {};
593 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
594 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
595 $self->{user_cfg
} = $cfg;
598 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
599 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
600 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
601 $self->{aclversion
} = $ucvers;
602 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
603 $self->{user_cfg
} = $cfg;
608 $self->{user_cfg
} = {};
609 die "Unable to load access control list: $err";
614 my ($self, $ip) = @_;
616 $self->{client_ip
} = $ip;
622 return $self->{client_ip
};
625 sub set_result_attrib
{
626 my ($self, $key, $value) = @_;
628 $self->{result_attributes
}->{$key} = $value;
631 sub get_result_attrib
{
632 my ($self, $key) = @_;
634 return $self->{result_attributes
}->{$key};
638 my ($self, $lang) = @_;
640 # fixme: initialize I18N
642 $self->{language
} = $lang;
648 return $self->{language
};
652 my ($self, $user) = @_;
656 $self->{user
} = $user;
662 die "user name not set\n" if !$self->{user
};
664 return $self->{user
};
667 # read/update list of active workers
668 # we move all finished tasks to the archive index,
669 # but keep aktive and most recent task in the active file.
670 # $nocheck ... consider $new_upid still running (avoid that
671 # we try to read the reult to early.
673 my ($new_upid, $nocheck) = @_;
675 my $lkfn = "/var/log/pve/tasks/.active.lock";
681 my $tasklist = PVE
::INotify
::read_file
('active');
685 my $thash = {}; # only list task once
687 my $check_task = sub {
688 my ($task, $running) = @_;
690 if ($running || PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
})) {
696 delete $task->{pstart
};
699 foreach my $task (@$tasklist) {
700 my $upid = $task->{upid
};
701 next if $thash->{$upid};
702 $thash->{$upid} = $task;
706 if ($new_upid && !(my $task = $thash->{$new_upid})) {
707 $task = PVE
::Tools
::upid_decode
($new_upid);
708 $task->{upid
} = $new_upid;
709 $thash->{$new_upid} = $task;
710 &$check_task($task, $nocheck);
714 @ta = sort { $b->{starttime
} cmp $a->{starttime
} } @ta;
716 my $save = defined($new_upid);
718 foreach my $task (@ta) {
719 next if $task->{endtime
};
720 $task->{endtime
} = time();
721 $task->{status
} = PVE
::Tools
::upid_read_status
($task->{upid
});
727 foreach my $task (@ta) {
728 if (!$task->{saved
}) {
729 $archive .= sprintf("$task->{upid} %08X $task->{status}\n", $task->{endtime
});
738 my $filename = "/var/log/pve/tasks/index";
740 my $fh = IO
::File-
>new($filename, '>>', 0644) ||
741 die "unable to open file '$filename' - $!\n";
742 PVE
::Tools
::safe_print
($filename, $fh, $archive);
745 die "unable to close file '$filename' - $!\n";
750 foreach my $task (@arlist) { # mark as not saved
754 my $maxsize = 50000; # about 1000 entries
755 if ($size > $maxsize) {
756 rename($filename, "$filename.1");
760 # we try to reduce the amount of data
761 # list all running tasks and task and a few others
762 # try to limit to 25 tasks
764 my $max = 25 - scalar(@$tlist);
765 foreach my $task (@ta) {
771 PVE
::INotify
::write_file
('active', $tlist) if $save;
776 my $res = PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
782 my $kill_process_group = sub {
783 my ($pid, $pstart) = @_;
785 # send kill to process group (negative pid)
788 # always send signal to all pgrp members
789 kill(15, $kpid); # send TERM signal
791 # give max 5 seconds to shut down
792 for (my $i = 0; $i < 5; $i++) {
793 return if !PVE
::ProcFSTools
::check_process_running
($pid, $pstart);
802 my ($upid, $killit) = @_;
804 my $task = PVE
::Tools
::upid_decode
($upid);
806 my $running = PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
});
808 return 0 if !$running;
811 &$kill_process_group($task->{pid
});
818 # start long running workers
819 # STDIN is redirected to /dev/null
820 # STDOUT,STDERR are redirected to the filename returned by upid_decode
821 # NOTE: we simulate running in foreground if ($self->{type} eq 'cli')
823 my ($self, $dtype, $id, $user, $function) = @_;
825 $dtype = 'unknown' if !defined ($dtype);
826 $id = '' if !defined ($id);
828 $user = 'root@pve' if !defined ($user);
830 my $sync = $self->{type
} eq 'cli' ?
1 : 0;
835 local $SIG{TERM
} = 'IGNORE';
837 my $starttime = time ();
839 my @psync = POSIX
::pipe();
840 my @csync = POSIX
::pipe();
842 my $node = $self->{nodename
};
845 die "unable to fork worker - $!" if !defined($cpid);
847 my $workerpuid = $cpid ?
$cpid : $$;
849 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($workerpuid) ||
850 die "unable to read process start time";
852 my $upid = PVE
::Tools
::upid_encode
({
853 node
=> $node, pid
=> $workerpuid, pstart
=> $pstart,
854 starttime
=> $starttime, type
=> $dtype, id
=> $id, user
=> $user });
858 if (!$cpid) { # child
862 $SIG{INT
} = $SIG{QUIT
} = $SIG{TERM
} = sub { die "received interrupt\n"; };
864 $SIG{CHLD
} = $SIG{PIPE
} = 'DEFAULT';
866 # set sess/process group - we want to be able to kill the
867 # whole process group
870 POSIX
::close ($psync[0]);
871 POSIX
::close ($csync[1]);
873 $outfh = $sync ?
$psync[1] : undef;
876 PVE
::INotify
::inotify_close
();
878 if (my $atfork = $self->{atfork
}) {
882 # same algorythm as used inside SA
884 my $fd = fileno (STDIN
);
888 POSIX
::close(0) if $fd != 0;
890 die "unable to redirect STDIN - $!"
891 if !open(STDIN
, "</dev/null");
893 $outfh = PVE
::Tools
::upid_open
($upid);
898 $fd = fileno(STDOUT
);
900 POSIX
::close (1) if $fd != 1;
902 die "unable to redirect STDOUT - $!"
903 if !open(STDOUT
, ">&", $outfh);
905 STDOUT-
>autoflush (1);
907 # redirect STDERR to STDOUT
908 $fd = fileno (STDERR
);
910 POSIX
::close(2) if $fd != 2;
912 die "unable to redirect STDERR - $!"
913 if !open(STDERR
, ">&1");
915 STDERR-
>autoflush(1);
918 my $msg = "ERROR: $err";
919 POSIX
::write($psync[1], $msg, length ($msg));
920 POSIX
::close($psync[1]);
925 # sync with parent (signal that we are ready)
929 POSIX
::write($psync[1], $upid, length ($upid));
930 POSIX
::close($psync[1]);
934 # sync with parent (wait until parent is ready)
935 POSIX
::read($csync[0], $readbuf, 4096);
936 die "parent setup error\n" if $readbuf ne 'OK';
938 if ($self->{type
} eq 'ha') {
939 print "task started by HA resource agent\n";
941 eval { &$function($upid); };
947 print STDERR
"TASK ERROR: $err\n";
950 print STDERR
"TASK OK\n";
958 POSIX
::close ($psync[1]);
959 POSIX
::close ($csync[0]);
962 # sync with child (wait until child starts)
963 POSIX
::read($psync[0], $readbuf, 4096);
966 POSIX
::close($psync[0]);
967 &$register_worker($cpid, $user, $upid);
973 die "got no worker upid - start worker failed\n" if !$readbuf;
975 if ($readbuf =~ m/^ERROR:\s*(.+)$/m) {
976 die "starting worker failed: $1\n";
979 if ($readbuf ne $upid) {
980 die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n";
984 $outfh = PVE
::Tools
::upid_open
($upid);
991 POSIX
::write($csync[1], $msg, length ($msg));
992 POSIX
::close($csync[1]);
995 POSIX
::close($csync[1]);
996 kill(-9, $cpid); # make sure it gets killed
1000 PVE
::Cluster
::log_msg
('info', $user, "starting task $upid");
1002 my $tlist = active_workers
($upid, $sync);
1003 PVE
::Cluster
::broadcast_tasklist
($tlist);
1012 local $SIG{INT
} = local $SIG{QUIT
} = local $SIG{TERM
} = sub {
1013 # always send signal to all pgrp members
1015 if ($int_count < 3) {
1016 kill(15, $kpid); # send TERM signal
1018 kill(9, $kpid); # send KILL signal
1022 local $SIG{PIPE
} = sub { die "broken pipe\n"; };
1024 my $select = new IO
::Select
;
1025 my $fh = IO
::Handle-
>new_from_fd($psync[0], 'r');
1028 while ($select->count) {
1029 my @handles = $select->can_read(1);
1030 if (scalar(@handles)) {
1031 my $count = sysread ($handles[0], $readbuf, 4096);
1032 if (!defined ($count)) {
1034 die "sync pipe read error: $err\n";
1036 last if $count == 0; # eof
1038 $outbuf .= $readbuf;
1039 while ($outbuf =~ s/^(([^\010\r\n]*)(\r|\n|(\010)+|\r\n))//s) {
1042 if ($data =~ m/^TASK OK$/) {
1044 } elsif ($data =~ m/^TASK ERROR: (.+)$/) {
1045 print STDERR
"$1\n";
1055 # some commands daemonize without closing stdout
1056 last if !PVE
::ProcFSTools
::check_process_running
($cpid);
1062 POSIX
::close($psync[0]);
1064 if ($outbuf) { # just to be sure
1067 print $outfh $outbuf;
1073 print STDERR
"$err\n";
1075 print $outfh "TASK ERROR: $err\n";
1079 &$kill_process_group($cpid, $pstart); # make sure it gets killed
1085 &$log_task_result($upid, $user, $res);
1088 return wantarray ?
($upid, $res) : $upid;