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