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