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