]>
git.proxmox.com Git - pve-access-control.git/blob - PVE/RPCEnvironment.pm
eb7b3c7645da60ca19106f5ec0281b9da2b13224
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 (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
299 # we are owner - allow access
300 } elsif ($vtype eq 'backup' && $ownervm) {
301 $self->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
302 $self->check($user, "/vms/$ownervm", ['VM.Backup']);
304 # allow if we are Datastore administrator
305 $self->check($user, "/storage/$sid", ['Datastore.Allocate']);
308 die "Only root can pass arbitrary filesystem paths."
309 if $user ne 'root@pam';
311 $path = abs_path
($volid);
312 if ($path =~ m
|^(/.+)$|) {
313 $path = $1; # untaint any path
319 sub is_group_member
{
320 my ($self, $group, $user) = @_;
322 my $cfg = $self->{user_cfg
};
324 return 0 if !$cfg->{groups
}->{$group};
326 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
330 my ($self, $user, $privs, $any) = @_;
332 my $cfg = $self->{user_cfg
};
335 foreach my $group (keys %{$cfg->{groups
}}) {
336 my $path = "/access/groups/$group";
337 if ($self->check_full($user, $path, $privs, $any, 1)) {
338 $groups->{$group} = $cfg->{groups
}->{$group};
345 sub group_member_join
{
346 my ($self, $grouplist) = @_;
350 my $cfg = $self->{user_cfg
};
351 foreach my $group (@$grouplist) {
352 my $data = $cfg->{groups
}->{$group};
354 foreach my $user (keys %{$data->{users
}}) {
362 sub check_perm_modify
{
363 my ($self, $username, $path, $noerr) = @_;
365 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
367 my $testperms = [ 'Permissions.Modify' ];
368 if ($path =~ m
|^/storage/.+$|) {
369 push @$testperms, 'Datastore.Allocate';
370 } elsif ($path =~ m
|^/vms/.+$|) {
371 push @$testperms, 'VM.Allocate';
372 } elsif ($path =~ m
|^/pool/.+$|) {
373 push @$testperms, 'Pool.Allocate';
376 return $self->check_any($username, $path, $testperms, $noerr);
379 sub exec_api2_perm_check
{
380 my ($self, $check, $username, $param, $noerr) = @_;
382 # syslog("info", "CHECK " . join(', ', @$check));
385 my $test = $check->[$ind++];
386 die "no permission test specified" if !$test;
388 if ($test eq 'and') {
389 while (my $subcheck = $check->[$ind++]) {
390 $self->exec_api2_perm_check($subcheck, $username, $param);
393 } elsif ($test eq 'or') {
394 while (my $subcheck = $check->[$ind++]) {
395 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
399 } elsif ($test eq 'perm') {
400 my ($t, $tmplpath, $privs, %options) = @$check;
401 my $any = $options{any
};
402 die "missing parameters" if !($tmplpath && $privs);
403 my $require_param = $options{require_param
};
404 if ($require_param && !defined($param->{$require_param})) {
408 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
409 $path = PVE
::AccessControl
::normalize_path
($path);
410 return $self->check_full($username, $path, $privs, $any, $noerr);
411 } elsif ($test eq 'userid-group') {
412 my $userid = $param->{userid
};
413 my ($t, $privs, %options) = @$check;
414 return 0 if !$options{groups_param
} && !$self->check_user_exist($userid, $noerr);
415 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
416 my $groups = $self->filter_groups($username, $privs, 1);
417 if ($options{groups_param
}) {
418 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
419 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
420 foreach my $pg (@group_param) {
421 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
425 my $allowed_users = $self->group_member_join([keys %$groups]);
426 if (!$allowed_users->{$userid}) {
433 } elsif ($test eq 'userid-param') {
434 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
435 my ($t, $subtest) = @$check;
436 die "missing parameters" if !$subtest;
437 if ($subtest eq 'self') {
438 return 0 if !$self->check_user_exist($userid, $noerr);
439 return 1 if $username eq $userid;
442 } elsif ($subtest eq 'Realm.AllocateUser') {
443 my $path = "/access/realm/$realm";
444 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
446 die "unknown userid-param test";
448 } elsif ($test eq 'perm-modify') {
449 my ($t, $tmplpath) = @$check;
450 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
451 $path = PVE
::AccessControl
::normalize_path
($path);
452 return $self->check_perm_modify($username, $path, $noerr);
454 die "unknown permission test";
458 sub check_api2_permissions
{
459 my ($self, $perm, $username, $param) = @_;
461 return 1 if !$username && $perm->{user
} eq 'world';
463 raise_perm_exc
("user != null") if !$username;
465 return 1 if $username eq 'root@pam';
467 raise_perm_exc
('user != root@pam') if !$perm;
469 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
471 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
477 # initialize environment - must be called once at program startup
479 my ($class, $type, %params) = @_;
481 $class = ref($class) || $class;
483 die "already initialized" if $pve_env;
485 die "unknown environment type" if !$type || $type !~ m/^(cli|pub|priv|ha)$/;
487 $SIG{CHLD
} = $worker_reaper;
490 # cli ... command started fron command line
491 # pub ... access from public server (apache)
492 # priv ... access from private server (pvedaemon)
493 # ha ... access from HA resource manager agent (rgmanager)
504 foreach my $p (keys %params) {
505 if ($p eq 'atfork') {
506 $self->{$p} = $params{$p};
508 die "unknown option '$p'";
514 my ($sysname, $nodename) = POSIX
::uname
();
516 $nodename =~ s/\..*$//; # strip domain part, if any
518 $self->{nodename
} = $nodename;
526 die "not initialized" if !$pve_env;
532 my ($self, $enable_upload) = @_;
534 if ($self->{request_rec
}) {
536 if ($enable_upload) {
537 $cgi = CGI-
>new($self->{request_rec
});
539 # disable upload using empty upload_hook
540 $cgi = CGI-
>new($self->{request_rec
}, sub {}, undef, 0);
543 my $params = $cgi->Vars();
544 return PVE
::Tools
::decode_utf8_parameters
($params);
545 } elsif ($self->{params
}) {
546 return $self->{params
};
548 die "no parameters registered";
552 sub get_upload_info
{
553 my ($self, $param) = @_;
555 my $cgi = $self->{cgi
};
556 die "CGI not initialized" if !$cgi;
558 my $pd = $cgi->param($param);
559 die "unable to get cgi parameter info\n" if !$pd;
560 my $info = $cgi->uploadInfo($pd);
561 die "unable to get cgi upload info\n" if !$info;
563 my $res = { %$info };
565 my $tmpfilename = $cgi->tmpFileName($pd);
566 die "unable to get cgi upload file name\n" if !$tmpfilename;
567 $res->{tmpfilename
} = $tmpfilename;
569 #my $hndl = $cgi->upload($param);
570 #die "unable to get cgi upload handle\n" if !$hndl;
571 #$res->{handle} = $hndl->handle;
576 # init_request - must be called before each RPC request
578 my ($self, %params) = @_;
580 PVE
::Cluster
::cfs_update
();
582 $self->{result_attributes
} = {};
584 my $userconfig; # we use this for regression tests
585 foreach my $p (keys %params) {
586 if ($p eq 'userconfig') {
587 $userconfig = $params{$p};
588 } elsif ($p eq 'request_rec') {
589 # pass Apache2::RequestRec
590 $self->{request_rec
} = $params{$p};
591 } elsif ($p eq 'params') {
592 $self->{params
} = $params{$p};
594 die "unknown parameter '$p'";
599 $self->{aclcache
} = {};
601 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
602 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
603 $self->{user_cfg
} = $cfg;
606 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
607 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
608 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
609 $self->{aclversion
} = $ucvers;
610 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
611 $self->{user_cfg
} = $cfg;
616 $self->{user_cfg
} = {};
617 die "Unable to load access control list: $err";
622 my ($self, $ip) = @_;
624 $self->{client_ip
} = $ip;
630 return $self->{client_ip
};
633 sub set_result_attrib
{
634 my ($self, $key, $value) = @_;
636 $self->{result_attributes
}->{$key} = $value;
639 sub get_result_attrib
{
640 my ($self, $key) = @_;
642 return $self->{result_attributes
}->{$key};
646 my ($self, $lang) = @_;
648 # fixme: initialize I18N
650 $self->{language
} = $lang;
656 return $self->{language
};
660 my ($self, $user) = @_;
664 $self->{user
} = $user;
670 die "user name not set\n" if !$self->{user
};
672 return $self->{user
};
675 # read/update list of active workers
676 # we move all finished tasks to the archive index,
677 # but keep aktive and most recent task in the active file.
678 # $nocheck ... consider $new_upid still running (avoid that
679 # we try to read the reult to early.
681 my ($new_upid, $nocheck) = @_;
683 my $lkfn = "/var/log/pve/tasks/.active.lock";
689 my $tasklist = PVE
::INotify
::read_file
('active');
693 my $thash = {}; # only list task once
695 my $check_task = sub {
696 my ($task, $running) = @_;
698 if ($running || PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
})) {
704 delete $task->{pstart
};
707 foreach my $task (@$tasklist) {
708 my $upid = $task->{upid
};
709 next if $thash->{$upid};
710 $thash->{$upid} = $task;
714 if ($new_upid && !(my $task = $thash->{$new_upid})) {
715 $task = PVE
::Tools
::upid_decode
($new_upid);
716 $task->{upid
} = $new_upid;
717 $thash->{$new_upid} = $task;
718 &$check_task($task, $nocheck);
722 @ta = sort { $b->{starttime
} cmp $a->{starttime
} } @ta;
724 my $save = defined($new_upid);
726 foreach my $task (@ta) {
727 next if $task->{endtime
};
728 $task->{endtime
} = time();
729 $task->{status
} = PVE
::Tools
::upid_read_status
($task->{upid
});
735 foreach my $task (@ta) {
736 if (!$task->{saved
}) {
737 $archive .= sprintf("$task->{upid} %08X $task->{status}\n", $task->{endtime
});
746 my $filename = "/var/log/pve/tasks/index";
748 my $fh = IO
::File-
>new($filename, '>>', 0644) ||
749 die "unable to open file '$filename' - $!\n";
750 PVE
::Tools
::safe_print
($filename, $fh, $archive);
753 die "unable to close file '$filename' - $!\n";
758 foreach my $task (@arlist) { # mark as not saved
762 my $maxsize = 50000; # about 1000 entries
763 if ($size > $maxsize) {
764 rename($filename, "$filename.1");
768 # we try to reduce the amount of data
769 # list all running tasks and task and a few others
770 # try to limit to 25 tasks
772 my $max = 25 - scalar(@$tlist);
773 foreach my $task (@ta) {
779 PVE
::INotify
::write_file
('active', $tlist) if $save;
784 my $res = PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
790 my $kill_process_group = sub {
791 my ($pid, $pstart) = @_;
793 # send kill to process group (negative pid)
796 # always send signal to all pgrp members
797 kill(15, $kpid); # send TERM signal
799 # give max 5 seconds to shut down
800 for (my $i = 0; $i < 5; $i++) {
801 return if !PVE
::ProcFSTools
::check_process_running
($pid, $pstart);
810 my ($upid, $killit) = @_;
812 my $task = PVE
::Tools
::upid_decode
($upid);
814 my $running = PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
});
816 return 0 if !$running;
819 &$kill_process_group($task->{pid
});
826 # start long running workers
827 # STDIN is redirected to /dev/null
828 # STDOUT,STDERR are redirected to the filename returned by upid_decode
829 # NOTE: we simulate running in foreground if ($self->{type} eq 'cli')
831 my ($self, $dtype, $id, $user, $function, $background) = @_;
833 $dtype = 'unknown' if !defined ($dtype);
834 $id = '' if !defined ($id);
836 $user = 'root@pve' if !defined ($user);
838 my $sync = ($self->{type
} eq 'cli' && !$background) ?
1 : 0;
843 local $SIG{TERM
} = 'IGNORE';
845 my $starttime = time ();
847 my @psync = POSIX
::pipe();
848 my @csync = POSIX
::pipe();
850 my $node = $self->{nodename
};
853 die "unable to fork worker - $!" if !defined($cpid);
855 my $workerpuid = $cpid ?
$cpid : $$;
857 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($workerpuid) ||
858 die "unable to read process start time";
860 my $upid = PVE
::Tools
::upid_encode
({
861 node
=> $node, pid
=> $workerpuid, pstart
=> $pstart,
862 starttime
=> $starttime, type
=> $dtype, id
=> $id, user
=> $user });
866 if (!$cpid) { # child
870 $SIG{INT
} = $SIG{QUIT
} = $SIG{TERM
} = sub { die "received interrupt\n"; };
872 $SIG{CHLD
} = $SIG{PIPE
} = 'DEFAULT';
874 # set sess/process group - we want to be able to kill the
875 # whole process group
878 POSIX
::close ($psync[0]);
879 POSIX
::close ($csync[1]);
881 $outfh = $sync ?
$psync[1] : undef;
884 PVE
::INotify
::inotify_close
();
886 if (my $atfork = $self->{atfork
}) {
890 # same algorythm as used inside SA
892 my $fd = fileno (STDIN
);
896 POSIX
::close(0) if $fd != 0;
898 die "unable to redirect STDIN - $!"
899 if !open(STDIN
, "</dev/null");
901 $outfh = PVE
::Tools
::upid_open
($upid);
906 $fd = fileno(STDOUT
);
908 POSIX
::close (1) if $fd != 1;
910 die "unable to redirect STDOUT - $!"
911 if !open(STDOUT
, ">&", $outfh);
913 STDOUT-
>autoflush (1);
915 # redirect STDERR to STDOUT
916 $fd = fileno (STDERR
);
918 POSIX
::close(2) if $fd != 2;
920 die "unable to redirect STDERR - $!"
921 if !open(STDERR
, ">&1");
923 STDERR-
>autoflush(1);
926 my $msg = "ERROR: $err";
927 POSIX
::write($psync[1], $msg, length ($msg));
928 POSIX
::close($psync[1]);
933 # sync with parent (signal that we are ready)
937 POSIX
::write($psync[1], $upid, length ($upid));
938 POSIX
::close($psync[1]);
942 # sync with parent (wait until parent is ready)
943 POSIX
::read($csync[0], $readbuf, 4096);
944 die "parent setup error\n" if $readbuf ne 'OK';
946 if ($self->{type
} eq 'ha') {
947 print "task started by HA resource agent\n";
949 eval { &$function($upid); };
955 print STDERR
"TASK ERROR: $err\n";
958 print STDERR
"TASK OK\n";
966 POSIX
::close ($psync[1]);
967 POSIX
::close ($csync[0]);
970 # sync with child (wait until child starts)
971 POSIX
::read($psync[0], $readbuf, 4096);
974 POSIX
::close($psync[0]);
975 &$register_worker($cpid, $user, $upid);
981 die "got no worker upid - start worker failed\n" if !$readbuf;
983 if ($readbuf =~ m/^ERROR:\s*(.+)$/m) {
984 die "starting worker failed: $1\n";
987 if ($readbuf ne $upid) {
988 die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n";
992 $outfh = PVE
::Tools
::upid_open
($upid);
999 POSIX
::write($csync[1], $msg, length ($msg));
1000 POSIX
::close($csync[1]);
1003 POSIX
::close($csync[1]);
1004 kill(-9, $cpid); # make sure it gets killed
1008 PVE
::Cluster
::log_msg
('info', $user, "starting task $upid");
1010 my $tlist = active_workers
($upid, $sync);
1011 PVE
::Cluster
::broadcast_tasklist
($tlist);
1020 local $SIG{INT
} = local $SIG{QUIT
} = local $SIG{TERM
} = sub {
1021 # always send signal to all pgrp members
1023 if ($int_count < 3) {
1024 kill(15, $kpid); # send TERM signal
1026 kill(9, $kpid); # send KILL signal
1030 local $SIG{PIPE
} = sub { die "broken pipe\n"; };
1032 my $select = new IO
::Select
;
1033 my $fh = IO
::Handle-
>new_from_fd($psync[0], 'r');
1036 while ($select->count) {
1037 my @handles = $select->can_read(1);
1038 if (scalar(@handles)) {
1039 my $count = sysread ($handles[0], $readbuf, 4096);
1040 if (!defined ($count)) {
1042 die "sync pipe read error: $err\n";
1044 last if $count == 0; # eof
1046 $outbuf .= $readbuf;
1047 while ($outbuf =~ s/^(([^\010\r\n]*)(\r|\n|(\010)+|\r\n))//s) {
1050 if ($data =~ m/^TASK OK$/) {
1052 } elsif ($data =~ m/^TASK ERROR: (.+)$/) {
1053 print STDERR
"$1\n";
1063 # some commands daemonize without closing stdout
1064 last if !PVE
::ProcFSTools
::check_process_running
($cpid);
1070 POSIX
::close($psync[0]);
1072 if ($outbuf) { # just to be sure
1075 print $outfh $outbuf;
1081 print STDERR
"$err\n";
1083 print $outfh "TASK ERROR: $err\n";
1087 &$kill_process_group($cpid, $pstart); # make sure it gets killed
1093 &$log_task_result($upid, $user, $res);
1096 return wantarray ?
($upid, $res) : $upid;