]> git.proxmox.com Git - pve-access-control.git/blob - PVE/RPCEnvironment.pm
fix bug in fork_worker
[pve-access-control.git] / PVE / RPCEnvironment.pm
1 package PVE::RPCEnvironment;
2
3 use strict;
4 use warnings;
5 use POSIX ":sys_wait_h";
6 use IO::File;
7 use Fcntl qw(:flock);
8 use PVE::SafeSyslog;
9 use PVE::Tools;
10 use PVE::INotify;
11 use PVE::Cluster;
12 use PVE::ProcFSTools;
13 use PVE::AccessControl;
14
15 # we use this singleton class to pass RPC related environment values
16
17 my $pve_env;
18
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)
26
27 my $WORKER_PIDS;
28
29 my $log_task_result = sub {
30 my ($upid, $user, $status) = @_;
31
32 my $msg = 'successful';
33 my $pri = 'info';
34 if ($status != 0) {
35 my $ec = $status >> 8;
36 my $ic = $status & 255;
37 $msg = $ec ? "failed ($ec)" : "interrupted ($ic)";
38 $pri = 'err';
39 }
40 my $tlist = active_workers($upid);
41 PVE::Cluster::broadcast_tasklist($tlist);
42 my $task;
43 foreach my $t (@$tlist) {
44 if ($t->{upid} eq $upid) {
45 $task = $t;
46 last;
47 }
48 }
49 if ($task && $task->{status}) {
50 $msg = $task->{status};
51 }
52 PVE::Cluster::log_msg($pri, $user, "end task $upid $msg");
53 };
54
55 my $worker_reaper = sub {
56 local $!; local $?;
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}, $?);
63 }
64 delete ($WORKER_PIDS->{$pid});
65 }
66 }
67 };
68
69 my $register_worker = sub {
70 my ($pid, $user, $upid) = @_;
71
72 return if !$pid;
73
74 # do not register if already finished
75 my $waitpid = waitpid ($pid, WNOHANG);
76 if (defined($waitpid) && ($waitpid == $pid)) {
77 delete ($WORKER_PIDS->{$pid});
78 return;
79 }
80
81 $WORKER_PIDS->{$pid} = {
82 user => $user,
83 upid => $upid,
84 };
85 };
86
87 # ACL cache
88
89 my $compile_acl = sub {
90 my ($self, $user) = @_;
91
92 my $res = {};
93 my $cfg = $self->{user_cfg};
94
95 return undef if !$cfg->{roles};
96
97 if ($user eq 'root@pam') { # root can do anything
98 return {'/' => $cfg->{roles}->{'Administrator'}};
99 }
100
101 foreach my $path (sort keys %{$cfg->{acl}}) {
102 my @ra = PVE::AccessControl::roles($cfg, $user, $path);
103
104 my $privs = {};
105 foreach my $role (@ra) {
106 if (my $privset = $cfg->{roles}->{$role}) {
107 foreach my $p (keys %$privset) {
108 $privs->{$p} = 1;
109 }
110 }
111 }
112
113 $res->{$path} = $privs;
114 }
115
116 return $res;
117 };
118
119 sub permissions {
120 my ($self, $user, $path) = @_;
121
122 $user = PVE::AccessControl::verify_username($user, 1);
123 return {} if !$user;
124
125 my $cache = $self->{aclcache};
126
127 my $acl = $cache->{$user};
128
129 if (!$acl) {
130 if (!($acl = &$compile_acl($self, $user))) {
131 return {};
132 }
133 $cache->{$user} = $acl;
134 }
135
136 my $perm;
137
138 if (!($perm = $acl->{$path})) {
139 $perm = {};
140 foreach my $p (sort keys %$acl) {
141 my $final = ($path eq $p);
142
143 next if !(($p eq '/') || $final || ($path =~ m|^$p/|));
144
145 $perm = $acl->{$p};
146 }
147 $acl->{$path} = $perm;
148 }
149
150 return $perm;
151 }
152
153 sub check {
154 my ($self, $user, $path, $privs) = @_;
155
156 my $perm = $self->permissions($user, $path);
157
158 foreach my $priv (@$privs) {
159 return undef if !$perm->{$priv};
160 };
161
162 return 1;
163 };
164
165 sub user_enabled {
166 my ($self, $user) = @_;
167
168 my $cfg = $self->{user_cfg};
169 return PVE::AccessControl::user_enabled($cfg, $user);
170 }
171
172 # initialize environment - must be called once at program startup
173 sub init {
174 my ($class, $type, %params) = @_;
175
176 $class = ref($class) || $class;
177
178 die "already initialized" if $pve_env;
179
180 die "unknown environment type" if !$type || $type !~ m/^(cli|pub|priv)$/;
181
182 $SIG{CHLD} = $worker_reaper;
183
184 # environment types
185 # cli ... command started fron command line
186 # pub ... access from public server (apache)
187 # priv ... access from private server (pvedaemon)
188
189 my $self = {
190 user_cfg => {},
191 aclcache => {},
192 aclversion => undef,
193 type => $type,
194 };
195
196 bless $self, $class;
197
198 foreach my $p (keys %params) {
199 if ($p eq 'atfork') {
200 $self->{$p} = $params{$p};
201 } else {
202 die "unknown option '$p'";
203 }
204 }
205
206 $pve_env = $self;
207
208 my ($sysname, $nodename) = POSIX::uname();
209
210 $nodename =~ s/\..*$//; # strip domain part, if any
211
212 $self->{nodename} = $nodename;
213
214 return $self;
215 };
216
217 # get the singleton
218 sub get {
219
220 die "not initialized" if !$pve_env;
221
222 return $pve_env;
223 }
224
225 # init_request - must be called before each RPC request
226 sub init_request {
227 my ($self, %params) = @_;
228
229 PVE::Cluster::cfs_update();
230
231 my $userconfig; # we use this for regression tests
232 foreach my $p (keys %params) {
233 if ($p eq 'userconfig') {
234 $userconfig = $params{$p};
235 } else {
236 die "unknown parameter '$p'";
237 }
238 }
239
240 eval {
241 $self->{aclcache} = {};
242 if ($userconfig) {
243 my $ucdata = PVE::Tools::file_get_contents($userconfig);
244 my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
245 $self->{user_cfg} = $cfg;
246 } else {
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;
253 }
254 }
255 };
256 if (my $err = $@) {
257 $self->{user_cfg} = {};
258 die "Unable to load access control list: $err";
259 }
260 }
261
262 sub set_client_ip {
263 my ($self, $ip) = @_;
264
265 $self->{client_ip} = $ip;
266 }
267
268 sub get_client_ip {
269 my ($self) = @_;
270
271 return $self->{client_ip};
272 }
273
274 sub set_result_count {
275 my ($self, $count) = @_;
276
277 $self->{result_count} = $count;
278 }
279
280 sub get_result_count {
281 my ($self) = @_;
282
283 return $self->{result_count};
284 }
285
286 sub set_language {
287 my ($self, $lang) = @_;
288
289 # fixme: initialize I18N
290
291 $self->{language} = $lang;
292 }
293
294 sub get_language {
295 my ($self) = @_;
296
297 return $self->{language};
298 }
299
300 sub set_user {
301 my ($self, $user) = @_;
302
303 # fixme: get ACLs
304
305 $self->{user} = $user;
306 }
307
308 sub get_user {
309 my ($self) = @_;
310
311 die "user name not set\n" if !$self->{user};
312
313 return $self->{user};
314 }
315
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.
319 # $nocheck ... consider $new_upid still running (avoid that
320 # we try to read the reult to early.
321 sub active_workers {
322 my ($new_upid, $nocheck) = @_;
323
324 my $lkfn = "/var/log/pve/tasks/.active.lock";
325
326 my $timeout = 10;
327
328 my $code = sub {
329
330 my $tasklist = PVE::INotify::read_file('active');
331
332 my @ta;
333 my $tlist = [];
334 my $thash = {}; # only list task once
335
336 my $check_task = sub {
337 my ($task) = @_;
338
339 if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart})) {
340 push @$tlist, $task;
341 } else {
342 delete $task->{pid};
343 push @ta, $task;
344 }
345 delete $task->{pstart};
346 };
347
348 foreach my $task (@$tasklist) {
349 my $upid = $task->{upid};
350 next if $thash->{$upid};
351 $thash->{$upid} = $task;
352 &$check_task($task);
353 }
354
355 if ($new_upid && !(my $task = $thash->{$new_upid})) {
356 $task = PVE::Tools::upid_decode($new_upid);
357 $task->{upid} = $new_upid;
358 $thash->{$new_upid} = $task;
359 &$check_task($task) if !$nocheck;
360 }
361
362
363 @ta = sort { $b->{starttime} cmp $a->{starttime} } @ta;
364
365 my $save = defined($new_upid);
366
367 foreach my $task (@ta) {
368 next if $task->{endtime};
369 $task->{endtime} = time();
370 $task->{status} = PVE::Tools::upid_read_status($task->{upid});
371 $save = 1;
372 }
373
374 my $archive = '';
375 my @arlist = ();
376 foreach my $task (@ta) {
377 if (!$task->{saved}) {
378 $archive .= sprintf("$task->{upid} %08X $task->{status}\n", $task->{endtime});
379 $save = 1;
380 push @arlist, $task;
381 $task->{saved} = 1;
382 }
383 }
384
385 if ($archive) {
386 my $size = 0;
387 my $filename = "/var/log/pve/tasks/index";
388 eval {
389 my $fh = IO::File->new($filename, '>>', 0644) ||
390 die "unable to open file '$filename' - $!\n";
391 PVE::Tools::safe_print($filename, $fh, $archive);
392 $size = -s $fh;
393 close($fh) ||
394 die "unable to close file '$filename' - $!\n";
395 };
396 my $err = $@;
397 if ($err) {
398 syslog('err', $err);
399 foreach my $task (@arlist) { # mark as not saved
400 $task->{saved} = 0;
401 }
402 }
403 my $maxsize = 50000; # about 1000 entries
404 if ($size > $maxsize) {
405 rename($filename, "$filename.1");
406 }
407 }
408
409 # we try to reduce the amount of data
410 # list all running tasks and task and a few others
411 # try to limit to 25 tasks
412 my $ctime = time();
413 my $max = 25 - scalar(@$tlist);
414 foreach my $task (@ta) {
415 last if $max <= 0;
416 push @$tlist, $task;
417 $max--;
418 }
419
420 PVE::INotify::write_file('active', $tlist) if $save;
421
422 return $tlist;
423 };
424
425 my $res = PVE::Tools::lock_file($lkfn, $timeout, $code);
426 die $@ if $@;
427
428 return $res;
429 }
430
431 my $kill_process_group = sub {
432 my ($pid, $pstart) = @_;
433
434 # send kill to process group (negative pid)
435 my $kpid = -$pid;
436
437 # always send signal to all pgrp members
438 kill(15, $kpid); # send TERM signal
439
440 # give max 5 seconds to shut down
441 for (my $i = 0; $i < 5; $i++) {
442 return if !PVE::ProcFSTools::check_process_running($pid, $pstart);
443 sleep (1);
444 }
445
446 # to be sure
447 kill(9, $kpid);
448 };
449
450 sub check_worker {
451 my ($upid, $killit) = @_;
452
453 my $task = PVE::Tools::upid_decode($upid);
454
455 my $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart});
456
457 return 0 if !$running;
458
459 if ($killit) {
460 &$kill_process_group($task->{pid});
461 return 0;
462 }
463
464 return 1;
465 }
466
467 # start long running workers
468 # STDIN is redirected to /dev/null
469 # STDOUT,STDERR are redirected to the filename returned by upid_decode
470 # NOTE: we simulate running in foreground if ($self->{type} eq 'cli')
471 sub fork_worker {
472 my ($self, $dtype, $id, $user, $function) = @_;
473
474 $dtype = 'unknown' if !defined ($dtype);
475 $id = '' if !defined ($id);
476
477 $user = 'root@pve' if !defined ($user);
478
479 my $sync = $self->{type} eq 'cli' ? 1 : 0;
480
481 local $SIG{INT} =
482 local $SIG{QUIT} =
483 local $SIG{PIPE} =
484 local $SIG{TERM} = 'IGNORE';
485
486 my $starttime = time ();
487
488 my @psync = POSIX::pipe();
489 my @csync = POSIX::pipe();
490
491 my $node = $self->{nodename};
492
493 my $cpid = fork();
494 die "unable to fork worker - $!" if !defined($cpid);
495
496 my $workerpuid = $cpid ? $cpid : $$;
497
498 my $pstart = PVE::ProcFSTools::read_proc_starttime($workerpuid) ||
499 die "unable to read process start time";
500
501 my $upid = PVE::Tools::upid_encode ({
502 node => $node, pid => $workerpuid, pstart => $pstart,
503 starttime => $starttime, type => $dtype, id => $id, user => $user });
504
505 my $outfh;
506
507 if (!$cpid) { # child
508
509 $0 = "task $upid";
510
511 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { die "received interrupt\n"; };
512
513 $SIG{CHLD} = $SIG{PIPE} = 'DEFAULT';
514
515 # set sess/process group - we want to be able to kill the
516 # whole process group
517 POSIX::setsid();
518
519 POSIX::close ($psync[0]);
520 POSIX::close ($csync[1]);
521
522 $outfh = $sync ? $psync[1] : undef;
523
524 eval {
525 PVE::INotify::inotify_close();
526
527 if (my $atfork = $self->{atfork}) {
528 &$atfork();
529 }
530
531 # same algorythm as used inside SA
532 # STDIN = /dev/null
533 my $fd = fileno (STDIN);
534 close STDIN;
535 POSIX::close(0) if $fd != 0;
536
537 die "unable to redirect STDIN - $!"
538 if !open(STDIN, "</dev/null");
539
540 $outfh = PVE::Tools::upid_open($upid) if !$sync;
541
542 # redirect STDOUT
543 $fd = fileno(STDOUT);
544 close STDOUT;
545 POSIX::close (1) if $fd != 1;
546
547 die "unable to redirect STDOUT - $!"
548 if !open(STDOUT, ">&", $outfh);
549
550 STDOUT->autoflush (1);
551
552 # redirect STDERR to STDOUT
553 $fd = fileno (STDERR);
554 close STDERR;
555 POSIX::close(2) if $fd != 2;
556
557 die "unable to redirect STDERR - $!"
558 if !open(STDERR, ">&1");
559
560 STDERR->autoflush(1);
561 };
562 if (my $err = $@) {
563 my $msg = "ERROR: $err";
564 POSIX::write($psync[1], $msg, length ($msg));
565 POSIX::close($psync[1]);
566 POSIX::_exit(1);
567 kill(-9, $$);
568 }
569
570 # sync with parent (signal that we are ready)
571 if ($sync) {
572 print "$upid\n";
573 } else {
574 POSIX::write($psync[1], $upid, length ($upid));
575 POSIX::close($psync[1]);
576 }
577
578 my $readbuf = '';
579 # sync with parent (wait until parent is ready)
580 POSIX::read($csync[0], $readbuf, 4096);
581 die "parent setup error\n" if $readbuf ne 'OK';
582
583 eval { &$function($upid); };
584 my $err = $@;
585 if ($err) {
586 chomp $err;
587 $err =~ s/\n/ /mg;
588 syslog('err', $err);
589 print STDERR "TASK ERROR: $err\n";
590 POSIX::_exit(-1);
591 } else {
592 print STDERR "TASK OK\n";
593 POSIX::_exit(0);
594 }
595 kill(-9, $$);
596 }
597
598 # parent
599
600 POSIX::close ($psync[1]);
601 POSIX::close ($csync[0]);
602
603 my $readbuf = '';
604 # sync with child (wait until child starts)
605 POSIX::read($psync[0], $readbuf, 4096);
606
607 if (!$sync) {
608 POSIX::close($psync[0]);
609 &$register_worker($cpid, $user, $upid);
610 } else {
611 chomp $readbuf;
612 }
613
614 eval {
615 die "got no worker upid - start worker failed\n" if !$readbuf;
616
617 if ($readbuf =~ m/^ERROR:\s*(.+)$/m) {
618 die "starting worker failed: $1\n";
619 }
620
621 if ($readbuf ne $upid) {
622 die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n";
623 }
624
625 if ($sync) {
626 $outfh = PVE::Tools::upid_open($upid);
627 }
628 };
629 my $err = $@;
630
631 if (!$err) {
632 my $msg = 'OK';
633 POSIX::write($csync[1], $msg, length ($msg));
634 POSIX::close($csync[1]);
635
636 } else {
637 POSIX::close($csync[1]);
638 kill(-9, $cpid); # make sure it gets killed
639 die $err;
640 }
641
642 PVE::Cluster::log_msg('info', $user, "starting task $upid");
643
644 my $tlist = active_workers($upid, $sync);
645 PVE::Cluster::broadcast_tasklist($tlist);
646
647 my $res = 0;
648
649 if ($sync) {
650 my $count;
651 my $outbuf = '';
652 eval {
653 local $SIG{INT} =
654 local $SIG{QUIT} =
655 local $SIG{TERM} = sub { die "got interrupt\n"; };
656 local $SIG{PIPE} = sub { die "broken pipe\n"; };
657
658 while (($count = POSIX::read($psync[0], $readbuf, 4096)) && ($count > 0)) {
659 $outbuf .= $readbuf;
660 while ($outbuf =~ s/^(([^\010\r\n]*)(\r|\n|(\010)+|\r\n))//s) {
661 my $line = $1;
662 my $data = $2;
663 if ($data =~ m/^TASK OK$/) {
664 # skip
665 } elsif ($data =~ m/^TASK ERROR: (.+)$/) {
666 print STDERR "$1\n";
667 } else {
668 print $line;
669 }
670 if ($outfh) {
671 print $outfh $line;
672 $outfh->flush();
673 }
674 }
675 }
676 };
677 my $err = $@;
678
679 POSIX::close($psync[0]);
680
681 if ($outbuf) { # just to be sure
682 print $outbuf;
683 if ($outfh) {
684 print $outfh $outbuf;
685 }
686 }
687
688 if ($err) {
689 $err =~ s/\n/ /mg;
690 print STDERR "$err\n";
691 if ($outfh) {
692 print $outfh "TASK ERROR: $err\n";
693 }
694 }
695
696 &$kill_process_group($cpid, $pstart); # make sure it gets killed
697
698 close($outfh);
699
700 waitpid($cpid, 0);
701 $res = $?;
702 &$log_task_result($upid, $user, $res);
703 }
704
705 return wantarray ? ($upid, $res) : $upid;
706 }
707
708 1;