]>
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);
307 if ($path =~ m
|^(/.+)$|) {
308 $path = $1; # untaint any path
314 sub is_group_member
{
315 my ($self, $group, $user) = @_;
317 my $cfg = $self->{user_cfg
};
319 return 0 if !$cfg->{groups
}->{$group};
321 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
325 my ($self, $user, $privs, $any) = @_;
327 my $cfg = $self->{user_cfg
};
330 foreach my $group (keys %{$cfg->{groups
}}) {
331 my $path = "/access/groups/$group";
332 if ($self->check_full($user, $path, $privs, $any, 1)) {
333 $groups->{$group} = $cfg->{groups
}->{$group};
340 sub group_member_join
{
341 my ($self, $grouplist) = @_;
345 my $cfg = $self->{user_cfg
};
346 foreach my $group (@$grouplist) {
347 my $data = $cfg->{groups
}->{$group};
349 foreach my $user (keys %{$data->{users
}}) {
357 sub check_perm_modify
{
358 my ($self, $username, $path, $noerr) = @_;
360 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
362 my $testperms = [ 'Permissions.Modify' ];
363 if ($path =~ m
|^/storage/.+$|) {
364 push @$testperms, 'Datastore.Allocate';
365 } elsif ($path =~ m
|^/vms/.+$|) {
366 push @$testperms, 'VM.Allocate';
367 } elsif ($path =~ m
|^/pool/.+$|) {
368 push @$testperms, 'Pool.Allocate';
371 return $self->check_any($username, $path, $testperms, $noerr);
374 sub exec_api2_perm_check
{
375 my ($self, $check, $username, $param, $noerr) = @_;
377 # syslog("info", "CHECK " . join(', ', @$check));
380 my $test = $check->[$ind++];
381 die "no permission test specified" if !$test;
383 if ($test eq 'and') {
384 while (my $subcheck = $check->[$ind++]) {
385 $self->exec_api2_perm_check($subcheck, $username, $param);
388 } elsif ($test eq 'or') {
389 while (my $subcheck = $check->[$ind++]) {
390 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
394 } elsif ($test eq 'perm') {
395 my ($t, $tmplpath, $privs, %options) = @$check;
396 my $any = $options{any
};
397 die "missing parameters" if !($tmplpath && $privs);
398 my $require_param = $options{require_param
};
399 if ($require_param && !defined($param->{$require_param})) {
403 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
404 $path = PVE
::AccessControl
::normalize_path
($path);
405 return $self->check_full($username, $path, $privs, $any, $noerr);
406 } elsif ($test eq 'userid-group') {
407 my $userid = $param->{userid
};
408 my ($t, $privs, %options) = @$check;
409 return 0 if !$options{groups_param
} && !$self->check_user_exist($userid, $noerr);
410 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
411 my $groups = $self->filter_groups($username, $privs, 1);
412 if ($options{groups_param
}) {
413 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
414 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
415 foreach my $pg (@group_param) {
416 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
420 my $allowed_users = $self->group_member_join([keys %$groups]);
421 if (!$allowed_users->{$userid}) {
428 } elsif ($test eq 'userid-param') {
429 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
430 my ($t, $subtest) = @$check;
431 die "missing parameters" if !$subtest;
432 if ($subtest eq 'self') {
433 return 0 if !$self->check_user_exist($userid, $noerr);
434 return 1 if $username eq $userid;
437 } elsif ($subtest eq 'Realm.AllocateUser') {
438 my $path = "/access/realm/$realm";
439 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
441 die "unknown userid-param test";
443 } elsif ($test eq 'perm-modify') {
444 my ($t, $tmplpath) = @$check;
445 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
446 $path = PVE
::AccessControl
::normalize_path
($path);
447 return $self->check_perm_modify($username, $path, $noerr);
449 die "unknown permission test";
453 sub check_api2_permissions
{
454 my ($self, $perm, $username, $param) = @_;
456 return 1 if !$username && $perm->{user
} eq 'world';
458 raise_perm_exc
("user != null") if !$username;
460 return 1 if $username eq 'root@pam';
462 raise_perm_exc
('user != root@pam') if !$perm;
464 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
466 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
472 # initialize environment - must be called once at program startup
474 my ($class, $type, %params) = @_;
476 $class = ref($class) || $class;
478 die "already initialized" if $pve_env;
480 die "unknown environment type" if !$type || $type !~ m/^(cli|pub|priv|ha)$/;
482 $SIG{CHLD
} = $worker_reaper;
485 # cli ... command started fron command line
486 # pub ... access from public server (apache)
487 # priv ... access from private server (pvedaemon)
488 # ha ... access from HA resource manager agent (rgmanager)
499 foreach my $p (keys %params) {
500 if ($p eq 'atfork') {
501 $self->{$p} = $params{$p};
503 die "unknown option '$p'";
509 my ($sysname, $nodename) = POSIX
::uname
();
511 $nodename =~ s/\..*$//; # strip domain part, if any
513 $self->{nodename
} = $nodename;
521 die "not initialized" if !$pve_env;
527 my ($self, $enable_upload) = @_;
529 if ($self->{request_rec
}) {
531 if ($enable_upload) {
532 $cgi = CGI-
>new($self->{request_rec
});
534 # disable upload using empty upload_hook
535 $cgi = CGI-
>new($self->{request_rec
}, sub {}, undef, 0);
538 my $params = $cgi->Vars();
539 return PVE
::Tools
::decode_utf8_parameters
($params);
540 } elsif ($self->{params
}) {
541 return $self->{params
};
543 die "no parameters registered";
547 sub get_upload_info
{
548 my ($self, $param) = @_;
550 my $cgi = $self->{cgi
};
551 die "CGI not initialized" if !$cgi;
553 my $pd = $cgi->param($param);
554 die "unable to get cgi parameter info\n" if !$pd;
555 my $info = $cgi->uploadInfo($pd);
556 die "unable to get cgi upload info\n" if !$info;
558 my $res = { %$info };
560 my $tmpfilename = $cgi->tmpFileName($pd);
561 die "unable to get cgi upload file name\n" if !$tmpfilename;
562 $res->{tmpfilename
} = $tmpfilename;
564 #my $hndl = $cgi->upload($param);
565 #die "unable to get cgi upload handle\n" if !$hndl;
566 #$res->{handle} = $hndl->handle;
571 # init_request - must be called before each RPC request
573 my ($self, %params) = @_;
575 PVE
::Cluster
::cfs_update
();
577 $self->{result_attributes
} = {};
579 my $userconfig; # we use this for regression tests
580 foreach my $p (keys %params) {
581 if ($p eq 'userconfig') {
582 $userconfig = $params{$p};
583 } elsif ($p eq 'request_rec') {
584 # pass Apache2::RequestRec
585 $self->{request_rec
} = $params{$p};
586 } elsif ($p eq 'params') {
587 $self->{params
} = $params{$p};
589 die "unknown parameter '$p'";
594 $self->{aclcache
} = {};
596 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
597 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
598 $self->{user_cfg
} = $cfg;
601 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
602 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
603 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
604 $self->{aclversion
} = $ucvers;
605 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
606 $self->{user_cfg
} = $cfg;
611 $self->{user_cfg
} = {};
612 die "Unable to load access control list: $err";
617 my ($self, $ip) = @_;
619 $self->{client_ip
} = $ip;
625 return $self->{client_ip
};
628 sub set_result_attrib
{
629 my ($self, $key, $value) = @_;
631 $self->{result_attributes
}->{$key} = $value;
634 sub get_result_attrib
{
635 my ($self, $key) = @_;
637 return $self->{result_attributes
}->{$key};
641 my ($self, $lang) = @_;
643 # fixme: initialize I18N
645 $self->{language
} = $lang;
651 return $self->{language
};
655 my ($self, $user) = @_;
659 $self->{user
} = $user;
665 die "user name not set\n" if !$self->{user
};
667 return $self->{user
};
670 # read/update list of active workers
671 # we move all finished tasks to the archive index,
672 # but keep aktive and most recent task in the active file.
673 # $nocheck ... consider $new_upid still running (avoid that
674 # we try to read the reult to early.
676 my ($new_upid, $nocheck) = @_;
678 my $lkfn = "/var/log/pve/tasks/.active.lock";
684 my $tasklist = PVE
::INotify
::read_file
('active');
688 my $thash = {}; # only list task once
690 my $check_task = sub {
691 my ($task, $running) = @_;
693 if ($running || PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
})) {
699 delete $task->{pstart
};
702 foreach my $task (@$tasklist) {
703 my $upid = $task->{upid
};
704 next if $thash->{$upid};
705 $thash->{$upid} = $task;
709 if ($new_upid && !(my $task = $thash->{$new_upid})) {
710 $task = PVE
::Tools
::upid_decode
($new_upid);
711 $task->{upid
} = $new_upid;
712 $thash->{$new_upid} = $task;
713 &$check_task($task, $nocheck);
717 @ta = sort { $b->{starttime
} cmp $a->{starttime
} } @ta;
719 my $save = defined($new_upid);
721 foreach my $task (@ta) {
722 next if $task->{endtime
};
723 $task->{endtime
} = time();
724 $task->{status
} = PVE
::Tools
::upid_read_status
($task->{upid
});
730 foreach my $task (@ta) {
731 if (!$task->{saved
}) {
732 $archive .= sprintf("$task->{upid} %08X $task->{status}\n", $task->{endtime
});
741 my $filename = "/var/log/pve/tasks/index";
743 my $fh = IO
::File-
>new($filename, '>>', 0644) ||
744 die "unable to open file '$filename' - $!\n";
745 PVE
::Tools
::safe_print
($filename, $fh, $archive);
748 die "unable to close file '$filename' - $!\n";
753 foreach my $task (@arlist) { # mark as not saved
757 my $maxsize = 50000; # about 1000 entries
758 if ($size > $maxsize) {
759 rename($filename, "$filename.1");
763 # we try to reduce the amount of data
764 # list all running tasks and task and a few others
765 # try to limit to 25 tasks
767 my $max = 25 - scalar(@$tlist);
768 foreach my $task (@ta) {
774 PVE
::INotify
::write_file
('active', $tlist) if $save;
779 my $res = PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
785 my $kill_process_group = sub {
786 my ($pid, $pstart) = @_;
788 # send kill to process group (negative pid)
791 # always send signal to all pgrp members
792 kill(15, $kpid); # send TERM signal
794 # give max 5 seconds to shut down
795 for (my $i = 0; $i < 5; $i++) {
796 return if !PVE
::ProcFSTools
::check_process_running
($pid, $pstart);
805 my ($upid, $killit) = @_;
807 my $task = PVE
::Tools
::upid_decode
($upid);
809 my $running = PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
});
811 return 0 if !$running;
814 &$kill_process_group($task->{pid
});
821 # start long running workers
822 # STDIN is redirected to /dev/null
823 # STDOUT,STDERR are redirected to the filename returned by upid_decode
824 # NOTE: we simulate running in foreground if ($self->{type} eq 'cli')
826 my ($self, $dtype, $id, $user, $function, $background) = @_;
828 $dtype = 'unknown' if !defined ($dtype);
829 $id = '' if !defined ($id);
831 $user = 'root@pve' if !defined ($user);
833 my $sync = ($self->{type
} eq 'cli' && !$background) ?
1 : 0;
838 local $SIG{TERM
} = 'IGNORE';
840 my $starttime = time ();
842 my @psync = POSIX
::pipe();
843 my @csync = POSIX
::pipe();
845 my $node = $self->{nodename
};
848 die "unable to fork worker - $!" if !defined($cpid);
850 my $workerpuid = $cpid ?
$cpid : $$;
852 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($workerpuid) ||
853 die "unable to read process start time";
855 my $upid = PVE
::Tools
::upid_encode
({
856 node
=> $node, pid
=> $workerpuid, pstart
=> $pstart,
857 starttime
=> $starttime, type
=> $dtype, id
=> $id, user
=> $user });
861 if (!$cpid) { # child
865 $SIG{INT
} = $SIG{QUIT
} = $SIG{TERM
} = sub { die "received interrupt\n"; };
867 $SIG{CHLD
} = $SIG{PIPE
} = 'DEFAULT';
869 # set sess/process group - we want to be able to kill the
870 # whole process group
873 POSIX
::close ($psync[0]);
874 POSIX
::close ($csync[1]);
876 $outfh = $sync ?
$psync[1] : undef;
879 PVE
::INotify
::inotify_close
();
881 if (my $atfork = $self->{atfork
}) {
885 # same algorythm as used inside SA
887 my $fd = fileno (STDIN
);
891 POSIX
::close(0) if $fd != 0;
893 die "unable to redirect STDIN - $!"
894 if !open(STDIN
, "</dev/null");
896 $outfh = PVE
::Tools
::upid_open
($upid);
901 $fd = fileno(STDOUT
);
903 POSIX
::close (1) if $fd != 1;
905 die "unable to redirect STDOUT - $!"
906 if !open(STDOUT
, ">&", $outfh);
908 STDOUT-
>autoflush (1);
910 # redirect STDERR to STDOUT
911 $fd = fileno (STDERR
);
913 POSIX
::close(2) if $fd != 2;
915 die "unable to redirect STDERR - $!"
916 if !open(STDERR
, ">&1");
918 STDERR-
>autoflush(1);
921 my $msg = "ERROR: $err";
922 POSIX
::write($psync[1], $msg, length ($msg));
923 POSIX
::close($psync[1]);
928 # sync with parent (signal that we are ready)
932 POSIX
::write($psync[1], $upid, length ($upid));
933 POSIX
::close($psync[1]);
937 # sync with parent (wait until parent is ready)
938 POSIX
::read($csync[0], $readbuf, 4096);
939 die "parent setup error\n" if $readbuf ne 'OK';
941 if ($self->{type
} eq 'ha') {
942 print "task started by HA resource agent\n";
944 eval { &$function($upid); };
950 print STDERR
"TASK ERROR: $err\n";
953 print STDERR
"TASK OK\n";
961 POSIX
::close ($psync[1]);
962 POSIX
::close ($csync[0]);
965 # sync with child (wait until child starts)
966 POSIX
::read($psync[0], $readbuf, 4096);
969 POSIX
::close($psync[0]);
970 &$register_worker($cpid, $user, $upid);
976 die "got no worker upid - start worker failed\n" if !$readbuf;
978 if ($readbuf =~ m/^ERROR:\s*(.+)$/m) {
979 die "starting worker failed: $1\n";
982 if ($readbuf ne $upid) {
983 die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n";
987 $outfh = PVE
::Tools
::upid_open
($upid);
994 POSIX
::write($csync[1], $msg, length ($msg));
995 POSIX
::close($csync[1]);
998 POSIX
::close($csync[1]);
999 kill(-9, $cpid); # make sure it gets killed
1003 PVE
::Cluster
::log_msg
('info', $user, "starting task $upid");
1005 my $tlist = active_workers
($upid, $sync);
1006 PVE
::Cluster
::broadcast_tasklist
($tlist);
1015 local $SIG{INT
} = local $SIG{QUIT
} = local $SIG{TERM
} = sub {
1016 # always send signal to all pgrp members
1018 if ($int_count < 3) {
1019 kill(15, $kpid); # send TERM signal
1021 kill(9, $kpid); # send KILL signal
1025 local $SIG{PIPE
} = sub { die "broken pipe\n"; };
1027 my $select = new IO
::Select
;
1028 my $fh = IO
::Handle-
>new_from_fd($psync[0], 'r');
1031 while ($select->count) {
1032 my @handles = $select->can_read(1);
1033 if (scalar(@handles)) {
1034 my $count = sysread ($handles[0], $readbuf, 4096);
1035 if (!defined ($count)) {
1037 die "sync pipe read error: $err\n";
1039 last if $count == 0; # eof
1041 $outbuf .= $readbuf;
1042 while ($outbuf =~ s/^(([^\010\r\n]*)(\r|\n|(\010)+|\r\n))//s) {
1045 if ($data =~ m/^TASK OK$/) {
1047 } elsif ($data =~ m/^TASK ERROR: (.+)$/) {
1048 print STDERR
"$1\n";
1058 # some commands daemonize without closing stdout
1059 last if !PVE
::ProcFSTools
::check_process_running
($cpid);
1065 POSIX
::close($psync[0]);
1067 if ($outbuf) { # just to be sure
1070 print $outfh $outbuf;
1076 print STDERR
"$err\n";
1078 print $outfh "TASK ERROR: $err\n";
1082 &$kill_process_group($cpid, $pstart); # make sure it gets killed
1088 &$log_task_result($upid, $user, $res);
1091 return wantarray ?
($upid, $res) : $upid;