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