]>
Commit | Line | Data |
---|---|---|
d9072797 DM |
1 | package PVE::RESTEnvironment; |
2 | ||
3 | # NOTE: you can/should provide your own specialice class, and | |
4 | # use this a bas class (as example see PVE::RPCEnvironment). | |
5 | ||
6 | # we use this singleton class to pass RPC related environment values | |
7 | ||
8 | use strict; | |
9 | use warnings; | |
10 | use POSIX qw(:sys_wait_h EINTR); | |
11 | use IO::Handle; | |
12 | use IO::File; | |
13 | use IO::Select; | |
14 | use Fcntl qw(:flock); | |
15 | use PVE::Exception qw(raise raise_perm_exc); | |
16 | use PVE::SafeSyslog; | |
17 | use PVE::Tools; | |
18 | use PVE::INotify; | |
19 | use PVE::ProcFSTools; | |
20 | ||
21 | ||
22 | my $rest_env; | |
23 | ||
24 | # save $SIG{CHLD} handler implementation. | |
25 | # simply set $SIG{CHLD} = $worker_reaper; | |
26 | # and register forked processes with &$register_worker(pid) | |
27 | # Note: using $SIG{CHLD} = 'IGNORE' or $SIG{CHLD} = sub { wait (); } or ... | |
28 | # has serious side effects, because perls built in system() and open() | |
fb3a1b29 | 29 | # functions can't get the correct exit status of a child. So we can't use |
d9072797 DM |
30 | # that (also see perlipc) |
31 | ||
32 | my $WORKER_PIDS; | |
33 | my $WORKER_FLAG = 0; | |
34 | ||
35 | my $log_task_result = sub { | |
36 | my ($upid, $user, $status) = @_; | |
37 | ||
38 | return if !$rest_env; | |
39 | ||
40 | my $msg = 'successful'; | |
41 | my $pri = 'info'; | |
42 | if ($status != 0) { | |
43 | my $ec = $status >> 8; | |
44 | my $ic = $status & 255; | |
45 | $msg = $ec ? "failed ($ec)" : "interrupted ($ic)"; | |
46 | $pri = 'err'; | |
47 | } | |
48 | ||
49 | my $tlist = $rest_env->active_workers($upid); | |
9a42d8a2 TL |
50 | eval { $rest_env->broadcast_tasklist($tlist); }; |
51 | syslog('err', $@) if $@; | |
d9072797 DM |
52 | |
53 | my $task; | |
54 | foreach my $t (@$tlist) { | |
55 | if ($t->{upid} eq $upid) { | |
56 | $task = $t; | |
57 | last; | |
58 | } | |
59 | } | |
60 | if ($task && $task->{status}) { | |
61 | $msg = $task->{status}; | |
62 | } | |
63 | ||
ad7962b4 | 64 | $rest_env->log_cluster_msg($pri, $user, "end task $upid $msg"); |
d9072797 DM |
65 | }; |
66 | ||
67 | my $worker_reaper = sub { | |
68 | local $!; local $?; | |
69 | foreach my $pid (keys %$WORKER_PIDS) { | |
70 | my $waitpid = waitpid ($pid, WNOHANG); | |
71 | if (defined($waitpid) && ($waitpid == $pid)) { | |
72 | my $info = $WORKER_PIDS->{$pid}; | |
73 | if ($info && $info->{upid} && $info->{user}) { | |
74 | &$log_task_result($info->{upid}, $info->{user}, $?); | |
75 | } | |
76 | delete ($WORKER_PIDS->{$pid}); | |
77 | } | |
78 | } | |
79 | }; | |
80 | ||
81 | my $register_worker = sub { | |
82 | my ($pid, $user, $upid) = @_; | |
83 | ||
84 | return if !$pid; | |
85 | ||
86 | # do not register if already finished | |
87 | my $waitpid = waitpid ($pid, WNOHANG); | |
88 | if (defined($waitpid) && ($waitpid == $pid)) { | |
89 | delete ($WORKER_PIDS->{$pid}); | |
90 | return; | |
91 | } | |
92 | ||
93 | $WORKER_PIDS->{$pid} = { | |
94 | user => $user, | |
95 | upid => $upid, | |
96 | }; | |
97 | }; | |
98 | ||
99 | # initialize environment - must be called once at program startup | |
100 | sub init { | |
101 | my ($class, $type, %params) = @_; | |
102 | ||
103 | $class = ref($class) || $class; | |
104 | ||
105 | die "already initialized" if $rest_env; | |
106 | ||
107 | die "unknown environment type" | |
108 | if !$type || $type !~ m/^(cli|pub|priv|ha)$/; | |
109 | ||
110 | $SIG{CHLD} = $worker_reaper; | |
111 | ||
112 | # environment types | |
113 | # cli ... command started fron command line | |
096b5f5c | 114 | # pub ... access from public server (pveproxy) |
d9072797 | 115 | # priv ... access from private server (pvedaemon) |
096b5f5c | 116 | # ha ... access from HA resource manager agent (pve-ha-manager) |
d9072797 DM |
117 | |
118 | my $self = { type => $type }; | |
119 | ||
120 | bless $self, $class; | |
121 | ||
122 | foreach my $p (keys %params) { | |
123 | if ($p eq 'atfork') { | |
124 | $self->{$p} = $params{$p}; | |
125 | } else { | |
126 | die "unknown option '$p'"; | |
127 | } | |
128 | } | |
129 | ||
130 | $rest_env = $self; | |
131 | ||
132 | my ($sysname, $nodename) = POSIX::uname(); | |
133 | ||
134 | $nodename =~ s/\..*$//; # strip domain part, if any | |
135 | ||
136 | $self->{nodename} = $nodename; | |
137 | ||
138 | return $self; | |
139 | }; | |
140 | ||
141 | # convenience function for command line tools | |
142 | sub setup_default_cli_env { | |
143 | my ($class, $username) = @_; | |
144 | ||
145 | $class = ref($class) || $class; | |
146 | ||
147 | $username //= 'root@pam'; | |
148 | ||
149 | PVE::INotify::inotify_init(); | |
150 | ||
151 | my $rpcenv = $class->init('cli'); | |
152 | $rpcenv->init_request(); | |
153 | $rpcenv->set_language($ENV{LANG}); | |
154 | $rpcenv->set_user($username); | |
155 | ||
156 | die "please run as root\n" | |
157 | if ($username eq 'root@pam') && ($> != 0); | |
158 | } | |
159 | ||
160 | # get the singleton | |
161 | sub get { | |
162 | ||
163 | die "REST environment not initialized" if !$rest_env; | |
164 | ||
165 | return $rest_env; | |
166 | } | |
167 | ||
168 | sub set_client_ip { | |
169 | my ($self, $ip) = @_; | |
170 | ||
171 | $self->{client_ip} = $ip; | |
172 | } | |
173 | ||
174 | sub get_client_ip { | |
175 | my ($self) = @_; | |
176 | ||
177 | return $self->{client_ip}; | |
178 | } | |
179 | ||
180 | sub set_result_attrib { | |
181 | my ($self, $key, $value) = @_; | |
182 | ||
183 | $self->{result_attributes}->{$key} = $value; | |
184 | } | |
185 | ||
186 | sub get_result_attrib { | |
187 | my ($self, $key) = @_; | |
188 | ||
189 | return $self->{result_attributes}->{$key}; | |
190 | } | |
191 | ||
192 | sub set_language { | |
193 | my ($self, $lang) = @_; | |
194 | ||
195 | # fixme: initialize I18N | |
196 | ||
197 | $self->{language} = $lang; | |
198 | } | |
199 | ||
200 | sub get_language { | |
201 | my ($self) = @_; | |
202 | ||
203 | return $self->{language}; | |
204 | } | |
205 | ||
206 | sub set_user { | |
207 | my ($self, $user) = @_; | |
208 | ||
209 | $self->{user} = $user; | |
210 | } | |
211 | ||
212 | sub get_user { | |
8e6019b1 | 213 | my ($self, $noerr) = @_; |
d9072797 | 214 | |
8e6019b1 | 215 | return $self->{user} if defined($self->{user}) || $noerr; |
d9072797 | 216 | |
8e6019b1 | 217 | die "user name not set\n"; |
d9072797 DM |
218 | } |
219 | ||
61aca93a WB |
220 | sub set_u2f_challenge { |
221 | my ($self, $challenge) = @_; | |
222 | ||
223 | $self->{u2f_challenge} = $challenge; | |
224 | } | |
225 | ||
226 | sub get_u2f_challenge { | |
227 | my ($self, $noerr) = @_; | |
228 | ||
229 | return $self->{u2f_challenge} if defined($self->{u2f_challenge}) || $noerr; | |
230 | ||
231 | die "no active u2f challenge\n"; | |
232 | } | |
233 | ||
c7a7aa4d WB |
234 | sub set_request_host { |
235 | my ($self, $host) = @_; | |
236 | ||
237 | $self->{request_host} = $host; | |
238 | } | |
239 | ||
240 | sub get_request_host { | |
241 | my ($self, $noerr) = @_; | |
242 | ||
243 | return $self->{request_host} if defined($self->{request_host}) || $noerr; | |
244 | ||
245 | die "no hostname available in current environment\n"; | |
246 | } | |
247 | ||
d9072797 DM |
248 | sub is_worker { |
249 | my ($class) = @_; | |
250 | ||
251 | return $WORKER_FLAG; | |
252 | } | |
253 | ||
254 | # read/update list of active workers | |
255 | # we move all finished tasks to the archive index, | |
256 | # but keep aktive and most recent task in the active file. | |
257 | # $nocheck ... consider $new_upid still running (avoid that | |
258 | # we try to read the reult to early. | |
259 | sub active_workers { | |
260 | my ($self, $new_upid, $nocheck) = @_; | |
261 | ||
262 | my $lkfn = "/var/log/pve/tasks/.active.lock"; | |
263 | ||
264 | my $timeout = 10; | |
265 | ||
266 | my $code = sub { | |
267 | ||
268 | my $tasklist = PVE::INotify::read_file('active'); | |
269 | ||
270 | my @ta; | |
271 | my $tlist = []; | |
272 | my $thash = {}; # only list task once | |
273 | ||
274 | my $check_task = sub { | |
275 | my ($task, $running) = @_; | |
276 | ||
277 | if ($running || PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart})) { | |
278 | push @$tlist, $task; | |
279 | } else { | |
280 | delete $task->{pid}; | |
281 | push @ta, $task; | |
282 | } | |
283 | delete $task->{pstart}; | |
284 | }; | |
285 | ||
286 | foreach my $task (@$tasklist) { | |
287 | my $upid = $task->{upid}; | |
288 | next if $thash->{$upid}; | |
289 | $thash->{$upid} = $task; | |
290 | &$check_task($task); | |
291 | } | |
292 | ||
293 | if ($new_upid && !(my $task = $thash->{$new_upid})) { | |
294 | $task = PVE::Tools::upid_decode($new_upid); | |
295 | $task->{upid} = $new_upid; | |
296 | $thash->{$new_upid} = $task; | |
297 | &$check_task($task, $nocheck); | |
298 | } | |
299 | ||
300 | ||
8733bb80 | 301 | @ta = sort { $b->{starttime} <=> $a->{starttime} } @ta; |
d9072797 DM |
302 | |
303 | my $save = defined($new_upid); | |
304 | ||
305 | foreach my $task (@ta) { | |
306 | next if $task->{endtime}; | |
307 | $task->{endtime} = time(); | |
308 | $task->{status} = PVE::Tools::upid_read_status($task->{upid}); | |
309 | $save = 1; | |
310 | } | |
311 | ||
312 | my $archive = ''; | |
313 | my @arlist = (); | |
314 | foreach my $task (@ta) { | |
315 | if (!$task->{saved}) { | |
316 | $archive .= sprintf("%s %08X %s\n", $task->{upid}, $task->{endtime}, $task->{status}); | |
317 | $save = 1; | |
318 | push @arlist, $task; | |
319 | $task->{saved} = 1; | |
320 | } | |
321 | } | |
322 | ||
323 | if ($archive) { | |
324 | my $size = 0; | |
325 | my $filename = "/var/log/pve/tasks/index"; | |
326 | eval { | |
327 | my $fh = IO::File->new($filename, '>>', 0644) || | |
328 | die "unable to open file '$filename' - $!\n"; | |
329 | PVE::Tools::safe_print($filename, $fh, $archive); | |
330 | $size = -s $fh; | |
331 | close($fh) || | |
332 | die "unable to close file '$filename' - $!\n"; | |
333 | }; | |
334 | my $err = $@; | |
335 | if ($err) { | |
336 | syslog('err', $err); | |
337 | foreach my $task (@arlist) { # mark as not saved | |
338 | $task->{saved} = 0; | |
339 | } | |
340 | } | |
341 | my $maxsize = 50000; # about 1000 entries | |
342 | if ($size > $maxsize) { | |
343 | rename($filename, "$filename.1"); | |
344 | } | |
345 | } | |
346 | ||
347 | # we try to reduce the amount of data | |
348 | # list all running tasks and task and a few others | |
349 | # try to limit to 25 tasks | |
d9072797 DM |
350 | my $max = 25 - scalar(@$tlist); |
351 | foreach my $task (@ta) { | |
352 | last if $max <= 0; | |
353 | push @$tlist, $task; | |
354 | $max--; | |
355 | } | |
356 | ||
357 | PVE::INotify::write_file('active', $tlist) if $save; | |
358 | ||
359 | return $tlist; | |
360 | }; | |
361 | ||
362 | my $res = PVE::Tools::lock_file($lkfn, $timeout, $code); | |
363 | die $@ if $@; | |
364 | ||
365 | return $res; | |
366 | } | |
367 | ||
368 | my $kill_process_group = sub { | |
369 | my ($pid, $pstart) = @_; | |
370 | ||
371 | # send kill to process group (negative pid) | |
372 | my $kpid = -$pid; | |
373 | ||
374 | # always send signal to all pgrp members | |
375 | kill(15, $kpid); # send TERM signal | |
376 | ||
377 | # give max 5 seconds to shut down | |
378 | for (my $i = 0; $i < 5; $i++) { | |
379 | return if !PVE::ProcFSTools::check_process_running($pid, $pstart); | |
380 | sleep (1); | |
381 | } | |
382 | ||
383 | # to be sure | |
384 | kill(9, $kpid); | |
385 | }; | |
386 | ||
387 | sub check_worker { | |
a313fe73 | 388 | my ($self, $upid, $killit) = @_; |
d9072797 DM |
389 | |
390 | my $task = PVE::Tools::upid_decode($upid); | |
391 | ||
392 | my $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}); | |
393 | ||
394 | return 0 if !$running; | |
395 | ||
396 | if ($killit) { | |
397 | &$kill_process_group($task->{pid}); | |
398 | return 0; | |
399 | } | |
400 | ||
401 | return 1; | |
402 | } | |
403 | ||
3e2da216 TL |
404 | # acts almost as tee: writes an output both to STDOUT and a task log, |
405 | # we differ as we're worker aware and look also at the childs control pipe, | |
406 | # so we know if the function could be executed successfully or not. | |
407 | my $tee_worker = sub { | |
408 | my ($childfd, $ctrlfd, $taskfh, $cpid) = @_; | |
409 | ||
410 | eval { | |
411 | my $int_count = 0; | |
412 | local $SIG{INT} = local $SIG{QUIT} = local $SIG{TERM} = sub { | |
413 | # always send signal to all pgrp members | |
414 | my $kpid = -$cpid; | |
415 | if ($int_count < 3) { | |
416 | kill(15, $kpid); # send TERM signal | |
417 | } else { | |
418 | kill(9, $kpid); # send KILL signal | |
419 | } | |
420 | $int_count++; | |
421 | }; | |
422 | local $SIG{PIPE} = sub { die "broken pipe\n"; }; | |
423 | ||
424 | my $select = new IO::Select; | |
425 | my $fh = IO::Handle->new_from_fd($childfd, 'r'); | |
426 | $select->add($fh); | |
427 | ||
428 | my $readbuf = ''; | |
429 | my $count; | |
430 | while ($select->count) { | |
431 | my @handles = $select->can_read(1); | |
432 | if (scalar(@handles)) { | |
433 | my $count = sysread ($handles[0], $readbuf, 4096); | |
434 | if (!defined ($count)) { | |
435 | my $err = $!; | |
436 | die "sync pipe read error: $err\n"; | |
437 | } | |
438 | last if $count == 0; # eof | |
439 | ||
440 | print $readbuf; | |
441 | select->flush(); | |
442 | ||
443 | print $taskfh $readbuf; | |
444 | $taskfh->flush(); | |
445 | } else { | |
446 | # some commands daemonize without closing stdout | |
447 | last if !PVE::ProcFSTools::check_process_running($cpid); | |
448 | } | |
449 | } | |
450 | ||
451 | # get status (error or OK) | |
452 | POSIX::read($ctrlfd, $readbuf, 4096); | |
453 | if ($readbuf =~ m/^TASK OK\n?$/) { | |
454 | # skip printing to stdout | |
455 | print $taskfh $readbuf; | |
456 | } elsif ($readbuf =~ m/^TASK ERROR: (.*)\n?$/) { | |
457 | print STDERR "$1\n"; | |
458 | print $taskfh "\n$readbuf"; # ensure start on new line for webUI | |
459 | } else { | |
460 | die "got unexpected control message: $readbuf\n"; | |
461 | } | |
462 | $taskfh->flush(); | |
463 | }; | |
464 | my $err = $@; | |
465 | ||
466 | POSIX::close($childfd); | |
467 | POSIX::close($ctrlfd); | |
468 | ||
469 | if ($err) { | |
470 | $err =~ s/\n/ /mg; | |
471 | print STDERR "$err\n"; | |
472 | print $taskfh "TASK ERROR: $err\n"; | |
473 | } | |
474 | }; | |
475 | ||
d9072797 DM |
476 | # start long running workers |
477 | # STDIN is redirected to /dev/null | |
478 | # STDOUT,STDERR are redirected to the filename returned by upid_decode | |
479 | # NOTE: we simulate running in foreground if ($self->{type} eq 'cli') | |
480 | sub fork_worker { | |
481 | my ($self, $dtype, $id, $user, $function, $background) = @_; | |
482 | ||
483 | $dtype = 'unknown' if !defined ($dtype); | |
484 | $id = '' if !defined ($id); | |
485 | ||
486 | $user = 'root@pve' if !defined ($user); | |
487 | ||
488 | my $sync = ($self->{type} eq 'cli' && !$background) ? 1 : 0; | |
489 | ||
490 | local $SIG{INT} = | |
491 | local $SIG{QUIT} = | |
492 | local $SIG{PIPE} = | |
493 | local $SIG{TERM} = 'IGNORE'; | |
494 | ||
495 | my $starttime = time (); | |
496 | ||
497 | my @psync = POSIX::pipe(); | |
498 | my @csync = POSIX::pipe(); | |
ed52a843 | 499 | my @ctrlfd = POSIX::pipe() if $sync; |
d9072797 DM |
500 | |
501 | my $node = $self->{nodename}; | |
502 | ||
503 | my $cpid = fork(); | |
504 | die "unable to fork worker - $!" if !defined($cpid); | |
505 | ||
506 | my $workerpuid = $cpid ? $cpid : $$; | |
507 | ||
508 | my $pstart = PVE::ProcFSTools::read_proc_starttime($workerpuid) || | |
509 | die "unable to read process start time"; | |
510 | ||
511 | my $upid = PVE::Tools::upid_encode ({ | |
512 | node => $node, pid => $workerpuid, pstart => $pstart, | |
513 | starttime => $starttime, type => $dtype, id => $id, user => $user }); | |
514 | ||
515 | my $outfh; | |
516 | ||
517 | if (!$cpid) { # child | |
518 | ||
519 | $0 = "task $upid"; | |
520 | $WORKER_FLAG = 1; | |
521 | ||
522 | $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { die "received interrupt\n"; }; | |
523 | ||
524 | $SIG{CHLD} = $SIG{PIPE} = 'DEFAULT'; | |
e97f807c | 525 | $SIG{TTOU} = 'IGNORE'; |
d9072797 | 526 | |
6a09f096 | 527 | my $ppgid; |
aea06195 | 528 | # set session/process group allows to kill the process group |
e97f807c | 529 | if ($sync && -t STDIN) { |
aea06195 TL |
530 | # some sync'ed workers operate on the tty but setsid sessions lose |
531 | # the tty, so just create a new pgroup and give it the tty | |
6a09f096 | 532 | $ppgid = POSIX::getpgrp() or die "failed to get old pgid: $!\n"; |
4c99c559 | 533 | POSIX::setpgid(0, 0) or die "failed to setpgid: $!\n"; |
e97f807c SI |
534 | POSIX::tcsetpgrp(fileno(STDIN), $$) or die "failed to tcsetpgrp: $!\n"; |
535 | } else { | |
536 | POSIX::setsid(); | |
537 | } | |
d9072797 DM |
538 | |
539 | POSIX::close ($psync[0]); | |
ed52a843 | 540 | POSIX::close ($ctrlfd[0]) if $sync; |
d9072797 DM |
541 | POSIX::close ($csync[1]); |
542 | ||
543 | $outfh = $sync ? $psync[1] : undef; | |
ed52a843 | 544 | my $resfh = $sync ? $ctrlfd[1] : undef; |
d9072797 DM |
545 | |
546 | eval { | |
547 | PVE::INotify::inotify_close(); | |
548 | ||
549 | if (my $atfork = $self->{atfork}) { | |
550 | &$atfork(); | |
551 | } | |
552 | ||
fb3a1b29 | 553 | # same algorithm as used inside SA |
d9072797 DM |
554 | # STDIN = /dev/null |
555 | my $fd = fileno (STDIN); | |
556 | ||
557 | if (!$sync) { | |
558 | close STDIN; | |
559 | POSIX::close(0) if $fd != 0; | |
560 | ||
561 | die "unable to redirect STDIN - $!" | |
562 | if !open(STDIN, "</dev/null"); | |
563 | ||
564 | $outfh = PVE::Tools::upid_open($upid); | |
4d9f4d62 | 565 | $resfh = fileno($outfh); |
d9072797 DM |
566 | } |
567 | ||
568 | ||
569 | # redirect STDOUT | |
570 | $fd = fileno(STDOUT); | |
571 | close STDOUT; | |
572 | POSIX::close (1) if $fd != 1; | |
573 | ||
574 | die "unable to redirect STDOUT - $!" | |
575 | if !open(STDOUT, ">&", $outfh); | |
576 | ||
577 | STDOUT->autoflush (1); | |
578 | ||
579 | # redirect STDERR to STDOUT | |
580 | $fd = fileno (STDERR); | |
581 | close STDERR; | |
582 | POSIX::close(2) if $fd != 2; | |
583 | ||
584 | die "unable to redirect STDERR - $!" | |
585 | if !open(STDERR, ">&1"); | |
586 | ||
587 | STDERR->autoflush(1); | |
588 | }; | |
589 | if (my $err = $@) { | |
590 | my $msg = "ERROR: $err"; | |
591 | POSIX::write($psync[1], $msg, length ($msg)); | |
592 | POSIX::close($psync[1]); | |
593 | POSIX::_exit(1); | |
594 | kill(-9, $$); | |
595 | } | |
596 | ||
597 | # sync with parent (signal that we are ready) | |
95109cc4 TL |
598 | POSIX::write($psync[1], $upid, length ($upid)); |
599 | POSIX::close($psync[1]) if !$sync; # don't need output pipe if async | |
d9072797 | 600 | |
2311859b WB |
601 | eval { |
602 | my $readbuf = ''; | |
603 | # sync with parent (wait until parent is ready) | |
604 | POSIX::read($csync[0], $readbuf, 4096); | |
605 | die "parent setup error\n" if $readbuf ne 'OK'; | |
d9072797 | 606 | |
2311859b WB |
607 | if ($self->{type} eq 'ha') { |
608 | print "task started by HA resource agent\n"; | |
609 | } | |
610 | &$function($upid); | |
611 | }; | |
edbb302e | 612 | my ($msg, $exitcode); |
d9072797 DM |
613 | my $err = $@; |
614 | if ($err) { | |
615 | chomp $err; | |
616 | $err =~ s/\n/ /mg; | |
617 | syslog('err', $err); | |
edbb302e SI |
618 | $msg = "TASK ERROR: $err\n"; |
619 | $exitcode = -1; | |
d9072797 | 620 | } else { |
edbb302e SI |
621 | $msg = "TASK OK\n"; |
622 | $exitcode = 0; | |
d9072797 | 623 | } |
edbb302e | 624 | POSIX::write($resfh, $msg, length($msg)); |
6a09f096 SI |
625 | |
626 | if ($sync) { | |
627 | POSIX::close($resfh); | |
628 | if ( -t STDIN) { | |
629 | POSIX::tcsetpgrp(fileno(STDIN), $ppgid) or | |
630 | die "failed to tcsetpgrp to parent: $!\n"; | |
631 | } | |
632 | } | |
edbb302e | 633 | POSIX::_exit($exitcode); |
a609b2f7 | 634 | kill(-9, $$); # not really needed, just to be sure |
d9072797 DM |
635 | } |
636 | ||
637 | # parent | |
638 | ||
639 | POSIX::close ($psync[1]); | |
ed52a843 | 640 | POSIX::close ($ctrlfd[1]) if $sync; |
d9072797 DM |
641 | POSIX::close ($csync[0]); |
642 | ||
643 | my $readbuf = ''; | |
644 | # sync with child (wait until child starts) | |
645 | POSIX::read($psync[0], $readbuf, 4096); | |
646 | ||
647 | if (!$sync) { | |
648 | POSIX::close($psync[0]); | |
649 | &$register_worker($cpid, $user, $upid); | |
650 | } else { | |
651 | chomp $readbuf; | |
652 | } | |
653 | ||
654 | eval { | |
655 | die "got no worker upid - start worker failed\n" if !$readbuf; | |
656 | ||
657 | if ($readbuf =~ m/^ERROR:\s*(.+)$/m) { | |
658 | die "starting worker failed: $1\n"; | |
659 | } | |
660 | ||
661 | if ($readbuf ne $upid) { | |
662 | die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n"; | |
663 | } | |
664 | ||
665 | if ($sync) { | |
666 | $outfh = PVE::Tools::upid_open($upid); | |
667 | } | |
668 | }; | |
669 | my $err = $@; | |
670 | ||
671 | if (!$err) { | |
672 | my $msg = 'OK'; | |
673 | POSIX::write($csync[1], $msg, length ($msg)); | |
674 | POSIX::close($csync[1]); | |
675 | ||
676 | } else { | |
677 | POSIX::close($csync[1]); | |
678 | kill(-9, $cpid); # make sure it gets killed | |
679 | die $err; | |
680 | } | |
681 | ||
682 | $self->log_cluster_msg('info', $user, "starting task $upid"); | |
683 | ||
684 | my $tlist = $self->active_workers($upid, $sync); | |
9a42d8a2 TL |
685 | eval { $self->broadcast_tasklist($tlist); }; |
686 | syslog('err', $@) if $@; | |
d9072797 DM |
687 | |
688 | my $res = 0; | |
689 | ||
690 | if ($sync) { | |
ed52a843 | 691 | |
3e2da216 | 692 | $tee_worker->($psync[0], $ctrlfd[0], $outfh, $cpid); |
d9072797 DM |
693 | |
694 | &$kill_process_group($cpid, $pstart); # make sure it gets killed | |
695 | ||
696 | close($outfh); | |
697 | ||
698 | waitpid($cpid, 0); | |
699 | $res = $?; | |
700 | &$log_task_result($upid, $user, $res); | |
701 | } | |
702 | ||
703 | return wantarray ? ($upid, $res) : $upid; | |
704 | } | |
705 | ||
706 | # Abstract function | |
707 | ||
708 | sub log_cluster_msg { | |
709 | my ($self, $pri, $user, $msg) = @_; | |
710 | ||
711 | syslog($pri, "%s", $msg); | |
712 | ||
713 | # PVE::Cluster::log_msg($pri, $user, $msg); | |
714 | } | |
715 | ||
716 | sub broadcast_tasklist { | |
717 | my ($self, $tlist) = @_; | |
718 | ||
719 | # PVE::Cluster::broadcast_tasklist($tlist); | |
720 | } | |
721 | ||
722 | sub check_api2_permissions { | |
723 | my ($self, $perm, $username, $param) = @_; | |
724 | ||
725 | return 1 if !$username && $perm->{user} eq 'world'; | |
726 | ||
727 | raise_perm_exc("user != null") if !$username; | |
728 | ||
729 | return 1 if $username eq 'root@pam'; | |
730 | ||
731 | raise_perm_exc('user != root@pam') if !$perm; | |
732 | ||
733 | return 1 if $perm->{user} && $perm->{user} eq 'all'; | |
734 | ||
735 | ##return $self->exec_api2_perm_check($perm->{check}, $username, $param) | |
736 | ##if $perm->{check}; | |
737 | ||
738 | raise_perm_exc(); | |
739 | } | |
740 | ||
741 | # init_request - should be called before each REST/CLI request | |
742 | sub init_request { | |
743 | my ($self, %params) = @_; | |
744 | ||
dc9c3ffa DM |
745 | $self->{result_attributes} = {} |
746 | ||
747 | # if you nedd more, implement in subclass | |
d9072797 DM |
748 | } |
749 | ||
750 | 1; |