]>
git.proxmox.com Git - pve-access-control.git/blob - PVE/RPCEnvironment.pm
dbfd0fdd578434a1164ae5ed0a0907231579ce63
1 package PVE
::RPCEnvironment
;
5 use POSIX
":sys_wait_h";
13 use PVE
::AccessControl
;
15 # we use this singleton class to pass RPC related environment values
19 # save $SIG{CHLD} handler implementation.
20 # simply set $SIG{CHLD} = $worker_reaper;
21 # and register forked processes with &$register_worker(pid)
22 # Note: using $SIG{CHLD} = 'IGNORE' or $SIG{CHLD} = sub { wait (); } or ...
23 # has serious side effects, because perls built in system() and open()
24 # functions can't get the correct exit status of a child. So we cant use
25 # that (also see perlipc)
29 my $log_task_result = sub {
30 my ($upid, $user, $status) = @_;
32 my $msg = 'successful';
35 my $ec = $status >> 8;
36 my $ic = $status & 255;
37 $msg = $ec ?
"failed ($ec)" : "interrupted ($ic)";
40 my $tlist = active_workers
($upid);
41 PVE
::Cluster
::broadcast_tasklist
($tlist);
43 foreach my $t (@$tlist) {
44 if ($t->{upid
} eq $upid) {
49 if ($task && $task->{status
}) {
50 $msg = $task->{status
};
52 PVE
::Cluster
::log_msg
($pri, $user, "end task $upid $msg");
55 my $worker_reaper = sub {
57 foreach my $pid (keys %$WORKER_PIDS) {
58 my $waitpid = waitpid ($pid, WNOHANG
);
59 if (defined($waitpid) && ($waitpid == $pid)) {
60 my $info = $WORKER_PIDS->{$pid};
61 if ($info && $info->{upid
} && $info->{user
}) {
62 &$log_task_result($info->{upid
}, $info->{user
}, $?);
64 delete ($WORKER_PIDS->{$pid});
69 my $register_worker = sub {
70 my ($pid, $user, $upid) = @_;
74 # do not register if already finished
75 my $waitpid = waitpid ($pid, WNOHANG
);
76 if (defined($waitpid) && ($waitpid == $pid)) {
77 delete ($WORKER_PIDS->{$pid});
81 $WORKER_PIDS->{$pid} = {
89 my $compile_acl = sub {
90 my ($self, $user) = @_;
93 my $cfg = $self->{user_cfg
};
95 return undef if !$cfg->{roles
};
97 if ($user eq 'root@pam') { # root can do anything
98 return {'/' => $cfg->{roles
}->{'Administrator'}};
101 foreach my $path (sort keys %{$cfg->{acl
}}) {
102 my @ra = PVE
::AccessControl
::roles
($cfg, $user, $path);
105 foreach my $role (@ra) {
106 if (my $privset = $cfg->{roles
}->{$role}) {
107 foreach my $p (keys %$privset) {
113 $res->{$path} = $privs;
120 my ($self, $user, $path) = @_;
122 $user = PVE
::AccessControl
::verify_username
($user, 1);
125 my $cache = $self->{aclcache
};
127 my $acl = $cache->{$user};
130 if (!($acl = &$compile_acl($self, $user))) {
133 $cache->{$user} = $acl;
138 if (!($perm = $acl->{$path})) {
140 foreach my $p (sort keys %$acl) {
141 my $final = ($path eq $p);
143 next if !(($p eq '/') || $final || ($path =~ m
|^$p/|));
147 $acl->{$path} = $perm;
154 my ($self, $user, $path, $privs) = @_;
156 my $perm = $self->permissions($user, $path);
158 foreach my $priv (@$privs) {
159 return undef if !$perm->{$priv};
166 my ($self, $user) = @_;
168 my $cfg = $self->{user_cfg
};
169 return PVE
::AccessControl
::user_enabled
($cfg, $user);
172 # initialize environment - must be called once at program startup
174 my ($class, $type, %params) = @_;
176 $class = ref($class) || $class;
178 die "already initialized" if $pve_env;
180 die "unknown environment type" if !$type || $type !~ m/^(cli|pub|priv)$/;
182 $SIG{CHLD
} = $worker_reaper;
185 # cli ... command started fron command line
186 # pub ... access from public server (apache)
187 # priv ... access from private server (pvedaemon)
198 foreach my $p (keys %params) {
199 if ($p eq 'atfork') {
200 $self->{$p} = $params{$p};
202 die "unknown option '$p'";
208 my ($sysname, $nodename) = POSIX
::uname
();
210 $nodename =~ s/\..*$//; # strip domain part, if any
212 $self->{nodename
} = $nodename;
220 die "not initialized" if !$pve_env;
225 # init_request - must be called before each RPC request
227 my ($self, %params) = @_;
229 PVE
::Cluster
::cfs_update
();
231 my $userconfig; # we use this for regression tests
232 foreach my $p (keys %params) {
233 if ($p eq 'userconfig') {
234 $userconfig = $params{$p};
236 die "unknown parameter '$p'";
241 $self->{aclcache
} = {};
243 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
244 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
245 $self->{user_cfg
} = $cfg;
247 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
248 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
249 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
250 $self->{aclversion
} = $ucvers;
251 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
252 $self->{user_cfg
} = $cfg;
257 $self->{user_cfg
} = {};
258 die "Unable to load access control list: $err";
263 my ($self, $ip) = @_;
265 $self->{client_ip
} = $ip;
271 return $self->{client_ip
};
274 sub set_result_count
{
275 my ($self, $count) = @_;
277 $self->{result_count
} = $count;
280 sub get_result_count
{
283 return $self->{result_count
};
287 my ($self, $lang) = @_;
289 # fixme: initialize I18N
291 $self->{language
} = $lang;
297 return $self->{language
};
301 my ($self, $user) = @_;
305 $self->{user
} = $user;
311 die "user name not set\n" if !$self->{user
};
313 return $self->{user
};
316 # read/update list of active workers
317 # we move all finished tasks to the archive index,
318 # but keep aktive and most recent task in the active file.
322 my $lkfn = "/var/log/pve/tasks/.active.lock";
328 my $tasklist = PVE
::INotify
::read_file
('active');
332 my $thash = {}; # only list task once
334 my $check_task = sub {
337 if (PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
})) {
343 delete $task->{pstart
};
346 foreach my $task (@$tasklist) {
347 my $upid = $task->{upid
};
348 next if $thash->{$upid};
349 $thash->{$upid} = $task;
353 if ($new_upid && !(my $task = $thash->{$new_upid})) {
354 $task = PVE
::Tools
::upid_decode
($new_upid);
355 $task->{upid
} = $new_upid;
356 $thash->{$new_upid} = $task;
361 @ta = sort { $b->{starttime
} cmp $a->{starttime
} } @ta;
363 my $save = defined($new_upid);
365 foreach my $task (@ta) {
366 next if $task->{endtime
};
367 $task->{endtime
} = time();
368 $task->{status
} = PVE
::Tools
::upid_read_status
($task->{upid
});
374 foreach my $task (@ta) {
375 if (!$task->{saved
}) {
376 $archive .= sprintf("$task->{upid} %08X $task->{status}\n", $task->{endtime
});
385 my $filename = "/var/log/pve/tasks/index";
387 my $fh = IO
::File-
>new($filename, '>>', 0644) ||
388 die "unable to open file '$filename' - $!\n";
389 PVE
::Tools
::safe_print
($filename, $fh, $archive);
392 die "unable to close file '$filename' - $!\n";
397 foreach my $task (@arlist) { # mark as not saved
401 my $maxsize = 50000; # about 1000 entries
402 if ($size > $maxsize) {
403 rename($filename, "$filename.1");
407 # we try to reduce the amount of data
408 # list all running tasks and task and a few others
409 # try to limit to 25 tasks
411 my $max = 25 - scalar(@$tlist);
412 foreach my $task (@ta) {
418 PVE
::INotify
::write_file
('active', $tlist) if $save;
423 my $res = PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
429 my $kill_process_group = sub {
430 my ($pid, $pstart) = @_;
432 # send kill to process group (negative pid)
435 # always send signal to all pgrp members
436 kill(15, $kpid); # send TERM signal
438 # give max 5 seconds to shut down
439 for (my $i = 0; $i < 5; $i++) {
440 return if !PVE
::ProcFSTools
::check_process_running
($pid, $pstart);
449 my ($upid, $killit) = @_;
451 my $task = PVE
::Tools
::upid_decode
($upid);
453 my $running = PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
});
455 return 0 if !$running;
458 &$kill_process_group($task->{pid
});
465 # start long running workers
466 # STDIN is redirected to /dev/null
467 # STDOUT,STDERR are redirected to the filename returned by upid_decode
468 # NOTE: we simulate running in foreground if ($self->{type} eq 'cli')
470 my ($self, $dtype, $id, $user, $function) = @_;
472 $dtype = 'unknown' if !defined ($dtype);
473 $id = '' if !defined ($id);
475 $user = 'root@pve' if !defined ($user);
477 my $sync = $self->{type
} eq 'cli' ?
1 : 0;
482 local $SIG{TERM
} = 'IGNORE';
484 my $starttime = time ();
486 my @psync = POSIX
::pipe();
487 my @csync = POSIX
::pipe();
489 my $node = $self->{nodename
};
492 die "unable to fork worker - $!" if !defined($cpid);
494 my $workerpuid = $cpid ?
$cpid : $$;
496 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($workerpuid) ||
497 die "unable to read process start time";
499 my $upid = PVE
::Tools
::upid_encode
({
500 node
=> $node, pid
=> $workerpuid, pstart
=> $pstart,
501 starttime
=> $starttime, type
=> $dtype, id
=> $id, user
=> $user });
505 if (!$cpid) { # child
509 $SIG{INT
} = $SIG{QUIT
} = $SIG{TERM
} = sub { die "received interrupt\n"; };
511 $SIG{CHLD
} = $SIG{PIPE
} = 'DEFAULT';
513 # set sess/process group - we want to be able to kill the
514 # whole process group
517 POSIX
::close ($psync[0]);
518 POSIX
::close ($csync[1]);
520 $outfh = $sync ?
$psync[1] : undef;
523 PVE
::INotify
::inotify_close
();
525 if (my $atfork = $self->{atfork
}) {
529 # same algorythm as used inside SA
531 my $fd = fileno (STDIN
);
533 POSIX
::close(0) if $fd != 0;
535 die "unable to redirect STDIN - $!"
536 if !open(STDIN
, "</dev/null");
538 $outfh = PVE
::Tools
::upid_open
($upid) if !$sync;
541 $fd = fileno(STDOUT
);
543 POSIX
::close (1) if $fd != 1;
545 die "unable to redirect STDOUT - $!"
546 if !open(STDOUT
, ">&", $outfh);
548 STDOUT-
>autoflush (1);
550 # redirect STDERR to STDOUT
551 $fd = fileno (STDERR
);
553 POSIX
::close(2) if $fd != 2;
555 die "unable to redirect STDERR - $!"
556 if !open(STDERR
, ">&1");
558 STDERR-
>autoflush(1);
561 my $msg = "ERROR: $err";
562 POSIX
::write($psync[1], $msg, length ($msg));
563 POSIX
::close($psync[1]);
568 # sync with parent (signal that we are ready)
572 POSIX
::write($psync[1], $upid, length ($upid));
573 POSIX
::close($psync[1]);
577 # sync with parent (wait until parent is ready)
578 POSIX
::read($csync[0], $readbuf, 4096);
579 die "parent setup error\n" if $readbuf ne 'OK';
581 eval { &$function($upid); };
587 print STDERR
"TASK ERROR: $err\n";
590 print STDERR
"TASK OK\n";
598 POSIX
::close ($psync[1]);
599 POSIX
::close ($csync[0]);
602 # sync with child (wait until child starts)
603 POSIX
::read($psync[0], $readbuf, 4096);
606 POSIX
::close($psync[0]);
607 &$register_worker($cpid, $user, $upid);
613 die "got no worker upid - start worker failed\n" if !$readbuf;
615 if ($readbuf =~ m/^ERROR:\s*(.+)$/m) {
616 die "starting worker failed: $1\n";
619 if ($readbuf ne $upid) {
620 die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n";
624 $outfh = PVE
::Tools
::upid_open
($upid);
631 POSIX
::write($csync[1], $msg, length ($msg));
632 POSIX
::close($csync[1]);
635 POSIX
::close($csync[1]);
636 kill(-9, $cpid); # make sure it gets killed
640 PVE
::Cluster
::log_msg
('info', $user, "starting task $upid");
642 my $tlist = active_workers
($upid);
643 PVE
::Cluster
::broadcast_tasklist
($tlist);
653 local $SIG{TERM
} = sub { die "got interrupt\n"; };
654 local $SIG{PIPE
} = sub { die "broken pipe\n"; };
656 while (($count = POSIX
::read($psync[0], $readbuf, 4096)) && ($count > 0)) {
658 while ($outbuf =~ s/^(([^\010\r\n]*)(\r|\n|(\010)+|\r\n))//s) {
661 if ($data =~ m/^TASK OK$/) {
663 } elsif ($data =~ m/^TASK ERROR: (.+)$/) {
677 POSIX
::close($psync[0]);
679 if ($outbuf) { # just to be sure
682 print $outfh $outbuf;
688 print STDERR
"$err\n";
690 print $outfh "TASK ERROR: $err\n";
694 &$kill_process_group($cpid, $pstart); # make sure it gets killed
700 &$log_task_result($upid, $user, $res);
703 return wantarray ?
($upid, $res) : $upid;