add more privileges, improve docs
[pve-access-control.git] / PVE / RPCEnvironment.pm
CommitLineData
2c3a6c0a
DM
1package PVE::RPCEnvironment;
2
3use strict;
4use warnings;
8d6e045f 5use POSIX qw(:sys_wait_h EINTR);
b28410fc 6use IO::Handle;
2c3a6c0a 7use IO::File;
b28410fc 8use IO::Select;
2c3a6c0a 9use Fcntl qw(:flock);
37d45deb 10use PVE::Exception qw(raise raise_perm_exc);
2c3a6c0a
DM
11use PVE::SafeSyslog;
12use PVE::Tools;
13use PVE::INotify;
14use PVE::Cluster;
15use PVE::ProcFSTools;
16use PVE::AccessControl;
7b24102d 17use CGI;
2c3a6c0a
DM
18
19# we use this singleton class to pass RPC related environment values
20
21my $pve_env;
22
23# save $SIG{CHLD} handler implementation.
24# simply set $SIG{CHLD} = $worker_reaper;
25# and register forked processes with &$register_worker(pid)
26# Note: using $SIG{CHLD} = 'IGNORE' or $SIG{CHLD} = sub { wait (); } or ...
27# has serious side effects, because perls built in system() and open()
28# functions can't get the correct exit status of a child. So we cant use
29# that (also see perlipc)
30
31my $WORKER_PIDS;
32
33my $log_task_result = sub {
34 my ($upid, $user, $status) = @_;
35
36 my $msg = 'successful';
37 my $pri = 'info';
38 if ($status != 0) {
39 my $ec = $status >> 8;
40 my $ic = $status & 255;
41 $msg = $ec ? "failed ($ec)" : "interrupted ($ic)";
42 $pri = 'err';
43 }
44 my $tlist = active_workers($upid);
45 PVE::Cluster::broadcast_tasklist($tlist);
46 my $task;
47 foreach my $t (@$tlist) {
48 if ($t->{upid} eq $upid) {
49 $task = $t;
50 last;
51 }
52 }
53 if ($task && $task->{status}) {
54 $msg = $task->{status};
55 }
56 PVE::Cluster::log_msg($pri, $user, "end task $upid $msg");
57};
58
59my $worker_reaper = sub {
60 local $!; local $?;
61 foreach my $pid (keys %$WORKER_PIDS) {
62 my $waitpid = waitpid ($pid, WNOHANG);
63 if (defined($waitpid) && ($waitpid == $pid)) {
64 my $info = $WORKER_PIDS->{$pid};
65 if ($info && $info->{upid} && $info->{user}) {
66 &$log_task_result($info->{upid}, $info->{user}, $?);
67 }
68 delete ($WORKER_PIDS->{$pid});
69 }
70 }
71};
72
73my $register_worker = sub {
74 my ($pid, $user, $upid) = @_;
75
76 return if !$pid;
77
78 # do not register if already finished
79 my $waitpid = waitpid ($pid, WNOHANG);
80 if (defined($waitpid) && ($waitpid == $pid)) {
81 delete ($WORKER_PIDS->{$pid});
82 return;
83 }
84
85 $WORKER_PIDS->{$pid} = {
86 user => $user,
87 upid => $upid,
88 };
89};
90
91# ACL cache
92
4bc17477
DM
93my $compile_acl_path = sub {
94 my ($self, $user, $path) = @_;
2c3a6c0a 95
2c3a6c0a
DM
96 my $cfg = $self->{user_cfg};
97
98 return undef if !$cfg->{roles};
99
4bc17477 100 die "internal error" if $user eq 'root@pam';
2c3a6c0a 101
4bc17477
DM
102 my $cache = $self->{aclcache};
103 $cache->{$user} = {} if !$cache->{$user};
104 my $data = $cache->{$user};
2c3a6c0a 105
4bc17477
DM
106 if (!$data->{poolroles}) {
107 $data->{poolroles} = {};
108
39c85db8
DM
109 foreach my $pool (keys %{$cfg->{pools}}) {
110 my $d = $cfg->{pools}->{$pool};
111 my @ra = PVE::AccessControl::roles($cfg, $user, "/pool/$pool"); # pool roles
4bc17477
DM
112 next if !scalar(@ra);
113 foreach my $vmid (keys %{$d->{vms}}) {
114 for my $role (@ra) {
115 $data->{poolroles}->{"/vms/$vmid"}->{$role} = 1;
2c3a6c0a
DM
116 }
117 }
4bc17477
DM
118 foreach my $storeid (keys %{$d->{storage}}) {
119 for my $role (@ra) {
120 $data->{poolroles}->{"/storage/$storeid"}->{$role} = 1;
121 }
122 }
123 }
124 }
125
126 my @ra = PVE::AccessControl::roles($cfg, $user, $path);
127
128 # apply roles inherited from pools
129 # Note: assume we do not want to propagate those privs
130 if ($data->{poolroles}->{$path}) {
131 if (!($ra[0] && $ra[0] eq 'NoAccess')) {
8ade28e6
DM
132 if ($data->{poolroles}->{$path}->{NoAccess}) {
133 @ra = ('NoAccess');
134 } else {
135 foreach my $role (keys %{$data->{poolroles}->{$path}}) {
136 push @ra, $role;
137 }
4bc17477 138 }
2c3a6c0a 139 }
4bc17477 140 }
2c3a6c0a 141
4bc17477
DM
142 $data->{roles}->{$path} = [ @ra ];
143
144 my $privs = {};
145 foreach my $role (@ra) {
146 if (my $privset = $cfg->{roles}->{$role}) {
147 foreach my $p (keys %$privset) {
148 $privs->{$p} = 1;
149 }
150 }
2c3a6c0a 151 }
4bc17477 152 $data->{privs}->{$path} = $privs;
2c3a6c0a 153
4bc17477 154 return $privs;
2c3a6c0a
DM
155};
156
4bc17477
DM
157sub roles {
158 my ($self, $user, $path) = @_;
159
160 if ($user eq 'root@pam') { # root can do anything
161 return ('Administrator');
162 }
163
164 $user = PVE::AccessControl::verify_username($user, 1);
165 return () if !$user;
166
167 my $cache = $self->{aclcache};
168 $cache->{$user} = {} if !$cache->{$user};
169
170 my $acl = $cache->{$user};
171
172 my $roles = $acl->{roles}->{$path};
173 return @$roles if $roles;
174
175 &$compile_acl_path($self, $user, $path);
176 $roles = $acl->{roles}->{$path} || [];
177 return @$roles;
178}
179
2c3a6c0a
DM
180sub permissions {
181 my ($self, $user, $path) = @_;
182
4bc17477
DM
183 if ($user eq 'root@pam') { # root can do anything
184 my $cfg = $self->{user_cfg};
185 return $cfg->{roles}->{'Administrator'};
186 }
187
2c3a6c0a
DM
188 $user = PVE::AccessControl::verify_username($user, 1);
189 return {} if !$user;
190
191 my $cache = $self->{aclcache};
4bc17477 192 $cache->{$user} = {} if !$cache->{$user};
2c3a6c0a
DM
193
194 my $acl = $cache->{$user};
195
4bc17477
DM
196 my $perm = $acl->{privs}->{$path};
197 return $perm if $perm;
2c3a6c0a 198
4bc17477 199 return &$compile_acl_path($self, $user, $path);
2c3a6c0a
DM
200}
201
202sub check {
37d45deb 203 my ($self, $user, $path, $privs, $noerr) = @_;
2c3a6c0a
DM
204
205 my $perm = $self->permissions($user, $path);
206
207 foreach my $priv (@$privs) {
37d45deb
DM
208 PVE::AccessControl::verify_privname($priv);
209 if (!$perm->{$priv}) {
210 return undef if $noerr;
211 raise_perm_exc("$path, $priv");
212 }
2c3a6c0a
DM
213 };
214
215 return 1;
216};
217
37d45deb
DM
218sub check_any {
219 my ($self, $user, $path, $privs, $noerr) = @_;
220
221 my $perm = $self->permissions($user, $path);
efce1d57 222
37d45deb
DM
223 my $found = 0;
224 foreach my $priv (@$privs) {
225 PVE::AccessControl::verify_privname($priv);
226 if ($perm->{$priv}) {
227 $found = 1;
228 last;
229 }
230 };
231
232 return 1 if $found;
233
234 return undef if $noerr;
235
236 raise_perm_exc("$path, " . join("|", @$privs));
237};
238
c4a776a6
DM
239sub check_full {
240 my ($self, $username, $path, $privs, $any, $noerr) = @_;
241 if ($any) {
242 return $self->check_any($username, $path, $privs, $noerr);
243 } else {
244 return $self->check($username, $path, $privs, $noerr);
245 }
246}
247
7070c1ae
DM
248sub check_user_enabled {
249 my ($self, $user, $noerr) = @_;
2c3a6c0a
DM
250
251 my $cfg = $self->{user_cfg};
7070c1ae 252 return PVE::AccessControl::check_user_enabled($cfg, $user, $noerr);
2c3a6c0a
DM
253}
254
37d45deb
DM
255sub check_user_exist {
256 my ($self, $user, $noerr) = @_;
257
258 my $cfg = $self->{user_cfg};
259 return PVE::AccessControl::check_user_exist($cfg, $user, $noerr);
260}
261
a23cec1f
DM
262sub check_pool_exist {
263 my ($self, $pool, $noerr) = @_;
264
265 my $cfg = $self->{user_cfg};
266
267 return 1 if $cfg->{pools}->{$pool};
268
269 return undef if $noerr;
270
271 raise_perm_exc("pool '$pool' does not exist");
272}
273
274sub check_vm_perm {
275 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
276
277 my $cfg = $self->{user_cfg};
278
279 if ($pool) {
280 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
281 }
282 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
283};
284
285sub check_storage_perm {
286 my ($self, $user, $vmid, $pool, $storeid, $privs, $any, $noerr) = @_;
287
288 my $cfg = $self->{user_cfg};
289
290 if ($pool && $cfg->{pools}->{$pool} &&
291 $cfg->{pools}->{$pool}->{storage}->{$storeid}) {
292 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
293 }
294 return $self->check_full($user, "/storage/$storeid", $privs, $any, $noerr);
295};
296
37d45deb
DM
297sub is_group_member {
298 my ($self, $group, $user) = @_;
299
300 my $cfg = $self->{user_cfg};
301
302 return 0 if !$cfg->{groups}->{$group};
303
304 return defined($cfg->{groups}->{$group}->{users}->{$user});
305}
306
307sub filter_groups {
b9180ed2 308 my ($self, $user, $privs, $any) = @_;
37d45deb
DM
309
310 my $cfg = $self->{user_cfg};
311
312 my $groups = {};
313 foreach my $group (keys %{$cfg->{groups}}) {
b9180ed2 314 my $path = "/access/groups/$group";
c4a776a6
DM
315 if ($self->check_full($user, $path, $privs, $any, 1)) {
316 $groups->{$group} = $cfg->{groups}->{$group};
37d45deb
DM
317 }
318 }
319
320 return $groups;
321}
322
323sub group_member_join {
324 my ($self, $grouplist) = @_;
325
326 my $users = {};
327
328 my $cfg = $self->{user_cfg};
329 foreach my $group (@$grouplist) {
330 my $data = $cfg->{groups}->{$group};
331 next if !$data;
332 foreach my $user (keys %{$data->{users}}) {
333 $users->{$user} = 1;
334 }
335 }
336
337 return $users;
338}
339
e3a3a0d7
DM
340sub check_perm_modify {
341 my ($self, $username, $path, $noerr) = @_;
342
343 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
344
345 my $testperms = [ 'Permissions.Modify' ];
346 if ($path =~ m|^/storage/.+$|) {
347 push @$testperms, 'Datastore.Allocate';
348 } elsif ($path =~ m|^/vms/.+$|) {
349 push @$testperms, 'VM.Allocate';
7a7a517a
DM
350 } elsif ($path =~ m|^/pool/.+$|) {
351 push @$testperms, 'Pool.Allocate';
e3a3a0d7
DM
352 }
353
354 return $self->check_any($username, $path, $testperms, $noerr);
355}
356
f8cc5a5f
DM
357sub exec_api2_perm_check {
358 my ($self, $check, $username, $param, $noerr) = @_;
359
360 # syslog("info", "CHECK " . join(', ', @$check));
361
362 my $ind = 0;
363 my $test = $check->[$ind++];
364 die "no permission test specified" if !$test;
365
366 if ($test eq 'and') {
367 while (my $subcheck = $check->[$ind++]) {
368 $self->exec_api2_perm_check($subcheck, $username, $param);
369 }
370 return 1;
371 } elsif ($test eq 'or') {
372 while (my $subcheck = $check->[$ind++]) {
373 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
374 }
375 return 0 if $noerr;
376 raise_perm_exc();
377 } elsif ($test eq 'perm') {
378 my ($t, $tmplpath, $privs, %options) = @$check;
379 my $any = $options{any};
380 die "missing parameters" if !($tmplpath && $privs);
c4a776a6
DM
381 my $require_param = $options{require_param};
382 if ($require_param && !defined($param->{$require_param})) {
383 return 0 if $noerr;
384 raise_perm_exc();
385 }
f8cc5a5f 386 my $path = PVE::Tools::template_replace($tmplpath, $param);
e3a3a0d7 387 $path = PVE::AccessControl::normalize_path($path);
c4a776a6 388 return $self->check_full($username, $path, $privs, $any, $noerr);
f8cc5a5f
DM
389 } elsif ($test eq 'userid-group') {
390 my $userid = $param->{userid};
391 my ($t, $privs, %options) = @$check;
82b63965
DM
392 return 0 if !$options{groups_param} && !$self->check_user_exist($userid, $noerr);
393 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
f8cc5a5f
DM
394 my $groups = $self->filter_groups($username, $privs, 1);
395 if ($options{groups_param}) {
396 my @group_param = PVE::Tools::split_list($param->{groups});
82b63965 397 raise_perm_exc("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
f8cc5a5f
DM
398 foreach my $pg (@group_param) {
399 raise_perm_exc("/access/groups/$pg, " . join("|", @$privs))
400 if !$groups->{$pg};
401 }
402 } else {
403 my $allowed_users = $self->group_member_join([keys %$groups]);
404 if (!$allowed_users->{$userid}) {
405 return 0 if $noerr;
406 raise_perm_exc();
407 }
408 }
409 }
410 return 1;
411 } elsif ($test eq 'userid-param') {
82b63965 412 my ($userid, undef, $realm) = verify_username($param->{userid});
f8cc5a5f
DM
413 return if !$self->check_user_exist($userid, $noerr);
414 my ($t, $subtest) = @$check;
415 die "missing parameters" if !$subtest;
416 if ($subtest eq 'self') {
417 return 1 if $username eq 'userid';
418 return 0 if $noerr;
419 raise_perm_exc();
82b63965
DM
420 } elsif ($subtest eq 'Realm.AllocateUser') {
421 my $path = "/access/realm/$realm";
422 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
f8cc5a5f
DM
423 } else {
424 die "unknown userid-param test";
425 }
82b63965 426 } elsif ($test eq 'perm-modify') {
e3a3a0d7
DM
427 my ($t, $tmplpath) = @$check;
428 my $path = PVE::Tools::template_replace($tmplpath, $param);
429 $path = PVE::AccessControl::normalize_path($path);
430 return $self->check_perm_modify($username, $path, $noerr);
431 } else {
f8cc5a5f
DM
432 die "unknown permission test";
433 }
434};
435
436sub check_api2_permissions {
437 my ($self, $perm, $username, $param) = @_;
438
439 return 1 if !$username && $perm->{user} eq 'world';
440
441 raise_perm_exc("user != null") if !$username;
442
443 return 1 if $username eq 'root@pam';
444
445 raise_perm_exc('user != root@pam') if !$perm;
446
447 return 1 if $perm->{user} && $perm->{user} eq 'all';
448
449 return $self->exec_api2_perm_check($perm->{check}, $username, $param)
450 if $perm->{check};
451
452 raise_perm_exc();
453}
454
2c3a6c0a
DM
455# initialize environment - must be called once at program startup
456sub init {
457 my ($class, $type, %params) = @_;
458
459 $class = ref($class) || $class;
460
461 die "already initialized" if $pve_env;
462
e42eedbc 463 die "unknown environment type" if !$type || $type !~ m/^(cli|pub|priv|ha)$/;
2c3a6c0a
DM
464
465 $SIG{CHLD} = $worker_reaper;
466
467 # environment types
468 # cli ... command started fron command line
469 # pub ... access from public server (apache)
470 # priv ... access from private server (pvedaemon)
e42eedbc 471 # ha ... access from HA resource manager agent (rgmanager)
2c3a6c0a
DM
472
473 my $self = {
474 user_cfg => {},
475 aclcache => {},
476 aclversion => undef,
477 type => $type,
478 };
479
480 bless $self, $class;
481
482 foreach my $p (keys %params) {
483 if ($p eq 'atfork') {
484 $self->{$p} = $params{$p};
485 } else {
486 die "unknown option '$p'";
487 }
488 }
489
490 $pve_env = $self;
491
492 my ($sysname, $nodename) = POSIX::uname();
493
494 $nodename =~ s/\..*$//; # strip domain part, if any
495
496 $self->{nodename} = $nodename;
497
498 return $self;
499};
500
501# get the singleton
502sub get {
503
504 die "not initialized" if !$pve_env;
505
506 return $pve_env;
507}
508
7b24102d
DM
509sub parse_params {
510 my ($self, $enable_upload) = @_;
511
512 if ($self->{request_rec}) {
513 my $cgi;
514 if ($enable_upload) {
515 $cgi = CGI->new($self->{request_rec});
516 } else {
517 # disable upload using empty upload_hook
518 $cgi = CGI->new($self->{request_rec}, sub {}, undef, 0);
519 }
520 $self->{cgi} = $cgi;
521 my $params = $cgi->Vars();
76c377c1 522 return PVE::Tools::decode_utf8_parameters($params);
7b24102d
DM
523 } elsif ($self->{params}) {
524 return $self->{params};
525 } else {
526 die "no parameters registered";
527 }
528}
529
530sub get_upload_info {
531 my ($self, $param) = @_;
532
533 my $cgi = $self->{cgi};
534 die "CGI not initialized" if !$cgi;
535
536 my $pd = $cgi->param($param);
537 die "unable to get cgi parameter info\n" if !$pd;
538 my $info = $cgi->uploadInfo($pd);
539 die "unable to get cgi upload info\n" if !$info;
540
541 my $res = { %$info };
542
543 my $tmpfilename = $cgi->tmpFileName($pd);
544 die "unable to get cgi upload file name\n" if !$tmpfilename;
545 $res->{tmpfilename} = $tmpfilename;
546
547 #my $hndl = $cgi->upload($param);
548 #die "unable to get cgi upload handle\n" if !$hndl;
549 #$res->{handle} = $hndl->handle;
550
551 return $res;
552}
553
2c3a6c0a
DM
554# init_request - must be called before each RPC request
555sub init_request {
556 my ($self, %params) = @_;
557
558 PVE::Cluster::cfs_update();
559
be6ea723 560 $self->{result_attributes} = {};
272fe9ff 561
2c3a6c0a
DM
562 my $userconfig; # we use this for regression tests
563 foreach my $p (keys %params) {
564 if ($p eq 'userconfig') {
565 $userconfig = $params{$p};
7b24102d
DM
566 } elsif ($p eq 'request_rec') {
567 # pass Apache2::RequestRec
568 $self->{request_rec} = $params{$p};
569 } elsif ($p eq 'params') {
570 $self->{params} = $params{$p};
2c3a6c0a
DM
571 } else {
572 die "unknown parameter '$p'";
573 }
574 }
575
576 eval {
577 $self->{aclcache} = {};
578 if ($userconfig) {
579 my $ucdata = PVE::Tools::file_get_contents($userconfig);
580 my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
581 $self->{user_cfg} = $cfg;
4bc17477 582 #print Dumper($cfg);
2c3a6c0a
DM
583 } else {
584 my $ucvers = PVE::Cluster::cfs_file_version('user.cfg');
585 if (!$self->{aclcache} || !defined($self->{aclversion}) ||
586 !defined($ucvers) || ($ucvers ne $self->{aclversion})) {
587 $self->{aclversion} = $ucvers;
588 my $cfg = PVE::Cluster::cfs_read_file('user.cfg');
589 $self->{user_cfg} = $cfg;
590 }
591 }
592 };
593 if (my $err = $@) {
594 $self->{user_cfg} = {};
595 die "Unable to load access control list: $err";
596 }
597}
598
599sub set_client_ip {
600 my ($self, $ip) = @_;
601
602 $self->{client_ip} = $ip;
603}
604
605sub get_client_ip {
606 my ($self) = @_;
607
608 return $self->{client_ip};
609}
610
be6ea723
DM
611sub set_result_attrib {
612 my ($self, $key, $value) = @_;
2c3a6c0a 613
be6ea723 614 $self->{result_attributes}->{$key} = $value;
2c3a6c0a
DM
615}
616
be6ea723
DM
617sub get_result_attrib {
618 my ($self, $key) = @_;
272fe9ff 619
be6ea723 620 return $self->{result_attributes}->{$key};
272fe9ff
DM
621}
622
2c3a6c0a
DM
623sub set_language {
624 my ($self, $lang) = @_;
625
626 # fixme: initialize I18N
627
628 $self->{language} = $lang;
629}
630
631sub get_language {
632 my ($self) = @_;
633
634 return $self->{language};
635}
636
637sub set_user {
638 my ($self, $user) = @_;
639
640 # fixme: get ACLs
641
642 $self->{user} = $user;
643}
644
645sub get_user {
646 my ($self) = @_;
647
648 die "user name not set\n" if !$self->{user};
649
650 return $self->{user};
651}
652
653# read/update list of active workers
654# we move all finished tasks to the archive index,
655# but keep aktive and most recent task in the active file.
5bf71a96
DM
656# $nocheck ... consider $new_upid still running (avoid that
657# we try to read the reult to early.
2c3a6c0a 658sub active_workers {
5bf71a96 659 my ($new_upid, $nocheck) = @_;
2c3a6c0a
DM
660
661 my $lkfn = "/var/log/pve/tasks/.active.lock";
662
663 my $timeout = 10;
664
665 my $code = sub {
666
667 my $tasklist = PVE::INotify::read_file('active');
668
669 my @ta;
670 my $tlist = [];
671 my $thash = {}; # only list task once
672
673 my $check_task = sub {
d33d0735 674 my ($task, $running) = @_;
2c3a6c0a 675
d33d0735 676 if ($running || PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart})) {
2c3a6c0a
DM
677 push @$tlist, $task;
678 } else {
679 delete $task->{pid};
680 push @ta, $task;
681 }
682 delete $task->{pstart};
683 };
684
685 foreach my $task (@$tasklist) {
686 my $upid = $task->{upid};
687 next if $thash->{$upid};
688 $thash->{$upid} = $task;
689 &$check_task($task);
690 }
691
692 if ($new_upid && !(my $task = $thash->{$new_upid})) {
693 $task = PVE::Tools::upid_decode($new_upid);
694 $task->{upid} = $new_upid;
695 $thash->{$new_upid} = $task;
d33d0735 696 &$check_task($task, $nocheck);
2c3a6c0a
DM
697 }
698
699
700 @ta = sort { $b->{starttime} cmp $a->{starttime} } @ta;
701
702 my $save = defined($new_upid);
703
704 foreach my $task (@ta) {
705 next if $task->{endtime};
706 $task->{endtime} = time();
707 $task->{status} = PVE::Tools::upid_read_status($task->{upid});
708 $save = 1;
709 }
710
711 my $archive = '';
712 my @arlist = ();
713 foreach my $task (@ta) {
714 if (!$task->{saved}) {
715 $archive .= sprintf("$task->{upid} %08X $task->{status}\n", $task->{endtime});
716 $save = 1;
717 push @arlist, $task;
718 $task->{saved} = 1;
719 }
720 }
721
722 if ($archive) {
723 my $size = 0;
724 my $filename = "/var/log/pve/tasks/index";
725 eval {
726 my $fh = IO::File->new($filename, '>>', 0644) ||
727 die "unable to open file '$filename' - $!\n";
728 PVE::Tools::safe_print($filename, $fh, $archive);
729 $size = -s $fh;
730 close($fh) ||
731 die "unable to close file '$filename' - $!\n";
732 };
733 my $err = $@;
734 if ($err) {
735 syslog('err', $err);
736 foreach my $task (@arlist) { # mark as not saved
737 $task->{saved} = 0;
738 }
739 }
740 my $maxsize = 50000; # about 1000 entries
741 if ($size > $maxsize) {
742 rename($filename, "$filename.1");
743 }
744 }
745
746 # we try to reduce the amount of data
747 # list all running tasks and task and a few others
748 # try to limit to 25 tasks
749 my $ctime = time();
750 my $max = 25 - scalar(@$tlist);
751 foreach my $task (@ta) {
752 last if $max <= 0;
753 push @$tlist, $task;
754 $max--;
755 }
756
757 PVE::INotify::write_file('active', $tlist) if $save;
758
759 return $tlist;
760 };
761
762 my $res = PVE::Tools::lock_file($lkfn, $timeout, $code);
763 die $@ if $@;
764
765 return $res;
766}
767
b9e47e57
DM
768my $kill_process_group = sub {
769 my ($pid, $pstart) = @_;
770
771 # send kill to process group (negative pid)
772 my $kpid = -$pid;
773
774 # always send signal to all pgrp members
775 kill(15, $kpid); # send TERM signal
776
777 # give max 5 seconds to shut down
778 for (my $i = 0; $i < 5; $i++) {
779 return if !PVE::ProcFSTools::check_process_running($pid, $pstart);
780 sleep (1);
781 }
782
783 # to be sure
784 kill(9, $kpid);
785};
786
787sub check_worker {
788 my ($upid, $killit) = @_;
789
790 my $task = PVE::Tools::upid_decode($upid);
791
792 my $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart});
793
794 return 0 if !$running;
795
796 if ($killit) {
797 &$kill_process_group($task->{pid});
798 return 0;
799 }
800
801 return 1;
802}
803
2c3a6c0a
DM
804# start long running workers
805# STDIN is redirected to /dev/null
806# STDOUT,STDERR are redirected to the filename returned by upid_decode
807# NOTE: we simulate running in foreground if ($self->{type} eq 'cli')
808sub fork_worker {
809 my ($self, $dtype, $id, $user, $function) = @_;
810
811 $dtype = 'unknown' if !defined ($dtype);
812 $id = '' if !defined ($id);
813
814 $user = 'root@pve' if !defined ($user);
815
816 my $sync = $self->{type} eq 'cli' ? 1 : 0;
817
818 local $SIG{INT} =
819 local $SIG{QUIT} =
820 local $SIG{PIPE} =
821 local $SIG{TERM} = 'IGNORE';
822
823 my $starttime = time ();
824
825 my @psync = POSIX::pipe();
826 my @csync = POSIX::pipe();
827
828 my $node = $self->{nodename};
829
830 my $cpid = fork();
831 die "unable to fork worker - $!" if !defined($cpid);
832
833 my $workerpuid = $cpid ? $cpid : $$;
834
835 my $pstart = PVE::ProcFSTools::read_proc_starttime($workerpuid) ||
836 die "unable to read process start time";
837
838 my $upid = PVE::Tools::upid_encode ({
839 node => $node, pid => $workerpuid, pstart => $pstart,
840 starttime => $starttime, type => $dtype, id => $id, user => $user });
841
842 my $outfh;
843
844 if (!$cpid) { # child
845
846 $0 = "task $upid";
847
848 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { die "received interrupt\n"; };
849
850 $SIG{CHLD} = $SIG{PIPE} = 'DEFAULT';
851
852 # set sess/process group - we want to be able to kill the
853 # whole process group
854 POSIX::setsid();
855
856 POSIX::close ($psync[0]);
857 POSIX::close ($csync[1]);
858
859 $outfh = $sync ? $psync[1] : undef;
860
861 eval {
862 PVE::INotify::inotify_close();
863
864 if (my $atfork = $self->{atfork}) {
865 &$atfork();
866 }
867
868 # same algorythm as used inside SA
869 # STDIN = /dev/null
870 my $fd = fileno (STDIN);
2c3a6c0a 871
5a941ebe
DM
872 if (!$sync) {
873 close STDIN;
874 POSIX::close(0) if $fd != 0;
875
876 die "unable to redirect STDIN - $!"
877 if !open(STDIN, "</dev/null");
878
879 $outfh = PVE::Tools::upid_open($upid);
880 }
2c3a6c0a 881
2c3a6c0a
DM
882
883 # redirect STDOUT
884 $fd = fileno(STDOUT);
885 close STDOUT;
886 POSIX::close (1) if $fd != 1;
887
888 die "unable to redirect STDOUT - $!"
889 if !open(STDOUT, ">&", $outfh);
890
891 STDOUT->autoflush (1);
892
893 # redirect STDERR to STDOUT
894 $fd = fileno (STDERR);
895 close STDERR;
896 POSIX::close(2) if $fd != 2;
897
898 die "unable to redirect STDERR - $!"
899 if !open(STDERR, ">&1");
900
901 STDERR->autoflush(1);
902 };
903 if (my $err = $@) {
904 my $msg = "ERROR: $err";
905 POSIX::write($psync[1], $msg, length ($msg));
906 POSIX::close($psync[1]);
907 POSIX::_exit(1);
b9e47e57 908 kill(-9, $$);
2c3a6c0a
DM
909 }
910
f6f2d51f 911 # sync with parent (signal that we are ready)
2c3a6c0a
DM
912 if ($sync) {
913 print "$upid\n";
914 } else {
915 POSIX::write($psync[1], $upid, length ($upid));
916 POSIX::close($psync[1]);
917 }
918
919 my $readbuf = '';
920 # sync with parent (wait until parent is ready)
921 POSIX::read($csync[0], $readbuf, 4096);
922 die "parent setup error\n" if $readbuf ne 'OK';
923
e42eedbc
DM
924 if ($self->{type} eq 'ha') {
925 print "task started by HA resource agent\n";
926 }
2c3a6c0a
DM
927 eval { &$function($upid); };
928 my $err = $@;
929 if ($err) {
930 chomp $err;
931 $err =~ s/\n/ /mg;
932 syslog('err', $err);
933 print STDERR "TASK ERROR: $err\n";
934 POSIX::_exit(-1);
935 } else {
936 print STDERR "TASK OK\n";
b9e47e57 937 POSIX::_exit(0);
2c3a6c0a 938 }
b9e47e57 939 kill(-9, $$);
2c3a6c0a
DM
940 }
941
942 # parent
943
944 POSIX::close ($psync[1]);
945 POSIX::close ($csync[0]);
946
947 my $readbuf = '';
948 # sync with child (wait until child starts)
949 POSIX::read($psync[0], $readbuf, 4096);
950
951 if (!$sync) {
952 POSIX::close($psync[0]);
953 &$register_worker($cpid, $user, $upid);
954 } else {
955 chomp $readbuf;
956 }
957
958 eval {
959 die "got no worker upid - start worker failed\n" if !$readbuf;
960
961 if ($readbuf =~ m/^ERROR:\s*(.+)$/m) {
962 die "starting worker failed: $1\n";
963 }
964
965 if ($readbuf ne $upid) {
966 die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n";
967 }
968
969 if ($sync) {
970 $outfh = PVE::Tools::upid_open($upid);
971 }
972 };
973 my $err = $@;
974
975 if (!$err) {
976 my $msg = 'OK';
977 POSIX::write($csync[1], $msg, length ($msg));
978 POSIX::close($csync[1]);
979
980 } else {
981 POSIX::close($csync[1]);
b9e47e57 982 kill(-9, $cpid); # make sure it gets killed
2c3a6c0a
DM
983 die $err;
984 }
985
986 PVE::Cluster::log_msg('info', $user, "starting task $upid");
987
5bf71a96 988 my $tlist = active_workers($upid, $sync);
2c3a6c0a
DM
989 PVE::Cluster::broadcast_tasklist($tlist);
990
991 my $res = 0;
992
993 if ($sync) {
994 my $count;
995 my $outbuf = '';
8d6e045f 996 my $int_count = 0;
2c3a6c0a 997 eval {
8d6e045f 998 local $SIG{INT} = local $SIG{QUIT} = local $SIG{TERM} = sub {
527b2e7a
DM
999 # always send signal to all pgrp members
1000 my $kpid = -$cpid;
8d6e045f 1001 if ($int_count < 3) {
527b2e7a 1002 kill(15, $kpid); # send TERM signal
8d6e045f 1003 } else {
527b2e7a 1004 kill(9, $kpid); # send KILL signal
8d6e045f
DM
1005 }
1006 $int_count++;
1007 };
2c3a6c0a 1008 local $SIG{PIPE} = sub { die "broken pipe\n"; };
b28410fc
DM
1009
1010 my $select = new IO::Select;
1011 my $fh = IO::Handle->new_from_fd($psync[0], 'r');
1012 $select->add($fh);
1013
1014 while ($select->count) {
1015 my @handles = $select->can_read(1);
1016 if (scalar(@handles)) {
1017 my $count = sysread ($handles[0], $readbuf, 4096);
1018 if (!defined ($count)) {
1019 my $err = $!;
1020 die "sync pipe read error: $err\n";
2c3a6c0a 1021 }
b28410fc
DM
1022 last if $count == 0; # eof
1023
1024 $outbuf .= $readbuf;
1025 while ($outbuf =~ s/^(([^\010\r\n]*)(\r|\n|(\010)+|\r\n))//s) {
1026 my $line = $1;
1027 my $data = $2;
1028 if ($data =~ m/^TASK OK$/) {
1029 # skip
1030 } elsif ($data =~ m/^TASK ERROR: (.+)$/) {
1031 print STDERR "$1\n";
1032 } else {
1033 print $line;
1034 }
1035 if ($outfh) {
1036 print $outfh $line;
1037 $outfh->flush();
1038 }
2c3a6c0a 1039 }
b28410fc
DM
1040 } else {
1041 # some commands daemonize without closing stdout
1042 last if !PVE::ProcFSTools::check_process_running($cpid);
2c3a6c0a
DM
1043 }
1044 }
1045 };
1046 my $err = $@;
1047
1048 POSIX::close($psync[0]);
1049
1050 if ($outbuf) { # just to be sure
1051 print $outbuf;
1052 if ($outfh) {
1053 print $outfh $outbuf;
1054 }
1055 }
1056
1057 if ($err) {
1058 $err =~ s/\n/ /mg;
1059 print STDERR "$err\n";
1060 if ($outfh) {
1061 print $outfh "TASK ERROR: $err\n";
1062 }
2c3a6c0a
DM
1063 }
1064
b9e47e57
DM
1065 &$kill_process_group($cpid, $pstart); # make sure it gets killed
1066
2c3a6c0a
DM
1067 close($outfh);
1068
b9e47e57 1069 waitpid($cpid, 0);
2c3a6c0a
DM
1070 $res = $?;
1071 &$log_task_result($upid, $user, $res);
1072 }
1073
1074 return wantarray ? ($upid, $res) : $upid;
1075}
1076
10771;