]>
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 | |
114 | # pub ... access from public server (apache) | |
115 | # priv ... access from private server (pvedaemon) | |
116 | # ha ... access from HA resource manager agent (rgmanager) | |
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 | ||
220 | sub is_worker { | |
221 | my ($class) = @_; | |
222 | ||
223 | return $WORKER_FLAG; | |
224 | } | |
225 | ||
226 | # read/update list of active workers | |
227 | # we move all finished tasks to the archive index, | |
228 | # but keep aktive and most recent task in the active file. | |
229 | # $nocheck ... consider $new_upid still running (avoid that | |
230 | # we try to read the reult to early. | |
231 | sub active_workers { | |
232 | my ($self, $new_upid, $nocheck) = @_; | |
233 | ||
234 | my $lkfn = "/var/log/pve/tasks/.active.lock"; | |
235 | ||
236 | my $timeout = 10; | |
237 | ||
238 | my $code = sub { | |
239 | ||
240 | my $tasklist = PVE::INotify::read_file('active'); | |
241 | ||
242 | my @ta; | |
243 | my $tlist = []; | |
244 | my $thash = {}; # only list task once | |
245 | ||
246 | my $check_task = sub { | |
247 | my ($task, $running) = @_; | |
248 | ||
249 | if ($running || PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart})) { | |
250 | push @$tlist, $task; | |
251 | } else { | |
252 | delete $task->{pid}; | |
253 | push @ta, $task; | |
254 | } | |
255 | delete $task->{pstart}; | |
256 | }; | |
257 | ||
258 | foreach my $task (@$tasklist) { | |
259 | my $upid = $task->{upid}; | |
260 | next if $thash->{$upid}; | |
261 | $thash->{$upid} = $task; | |
262 | &$check_task($task); | |
263 | } | |
264 | ||
265 | if ($new_upid && !(my $task = $thash->{$new_upid})) { | |
266 | $task = PVE::Tools::upid_decode($new_upid); | |
267 | $task->{upid} = $new_upid; | |
268 | $thash->{$new_upid} = $task; | |
269 | &$check_task($task, $nocheck); | |
270 | } | |
271 | ||
272 | ||
8733bb80 | 273 | @ta = sort { $b->{starttime} <=> $a->{starttime} } @ta; |
d9072797 DM |
274 | |
275 | my $save = defined($new_upid); | |
276 | ||
277 | foreach my $task (@ta) { | |
278 | next if $task->{endtime}; | |
279 | $task->{endtime} = time(); | |
280 | $task->{status} = PVE::Tools::upid_read_status($task->{upid}); | |
281 | $save = 1; | |
282 | } | |
283 | ||
284 | my $archive = ''; | |
285 | my @arlist = (); | |
286 | foreach my $task (@ta) { | |
287 | if (!$task->{saved}) { | |
288 | $archive .= sprintf("%s %08X %s\n", $task->{upid}, $task->{endtime}, $task->{status}); | |
289 | $save = 1; | |
290 | push @arlist, $task; | |
291 | $task->{saved} = 1; | |
292 | } | |
293 | } | |
294 | ||
295 | if ($archive) { | |
296 | my $size = 0; | |
297 | my $filename = "/var/log/pve/tasks/index"; | |
298 | eval { | |
299 | my $fh = IO::File->new($filename, '>>', 0644) || | |
300 | die "unable to open file '$filename' - $!\n"; | |
301 | PVE::Tools::safe_print($filename, $fh, $archive); | |
302 | $size = -s $fh; | |
303 | close($fh) || | |
304 | die "unable to close file '$filename' - $!\n"; | |
305 | }; | |
306 | my $err = $@; | |
307 | if ($err) { | |
308 | syslog('err', $err); | |
309 | foreach my $task (@arlist) { # mark as not saved | |
310 | $task->{saved} = 0; | |
311 | } | |
312 | } | |
313 | my $maxsize = 50000; # about 1000 entries | |
314 | if ($size > $maxsize) { | |
315 | rename($filename, "$filename.1"); | |
316 | } | |
317 | } | |
318 | ||
319 | # we try to reduce the amount of data | |
320 | # list all running tasks and task and a few others | |
321 | # try to limit to 25 tasks | |
d9072797 DM |
322 | my $max = 25 - scalar(@$tlist); |
323 | foreach my $task (@ta) { | |
324 | last if $max <= 0; | |
325 | push @$tlist, $task; | |
326 | $max--; | |
327 | } | |
328 | ||
329 | PVE::INotify::write_file('active', $tlist) if $save; | |
330 | ||
331 | return $tlist; | |
332 | }; | |
333 | ||
334 | my $res = PVE::Tools::lock_file($lkfn, $timeout, $code); | |
335 | die $@ if $@; | |
336 | ||
337 | return $res; | |
338 | } | |
339 | ||
340 | my $kill_process_group = sub { | |
341 | my ($pid, $pstart) = @_; | |
342 | ||
343 | # send kill to process group (negative pid) | |
344 | my $kpid = -$pid; | |
345 | ||
346 | # always send signal to all pgrp members | |
347 | kill(15, $kpid); # send TERM signal | |
348 | ||
349 | # give max 5 seconds to shut down | |
350 | for (my $i = 0; $i < 5; $i++) { | |
351 | return if !PVE::ProcFSTools::check_process_running($pid, $pstart); | |
352 | sleep (1); | |
353 | } | |
354 | ||
355 | # to be sure | |
356 | kill(9, $kpid); | |
357 | }; | |
358 | ||
359 | sub check_worker { | |
a313fe73 | 360 | my ($self, $upid, $killit) = @_; |
d9072797 DM |
361 | |
362 | my $task = PVE::Tools::upid_decode($upid); | |
363 | ||
364 | my $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}); | |
365 | ||
366 | return 0 if !$running; | |
367 | ||
368 | if ($killit) { | |
369 | &$kill_process_group($task->{pid}); | |
370 | return 0; | |
371 | } | |
372 | ||
373 | return 1; | |
374 | } | |
375 | ||
3e2da216 TL |
376 | # acts almost as tee: writes an output both to STDOUT and a task log, |
377 | # we differ as we're worker aware and look also at the childs control pipe, | |
378 | # so we know if the function could be executed successfully or not. | |
379 | my $tee_worker = sub { | |
380 | my ($childfd, $ctrlfd, $taskfh, $cpid) = @_; | |
381 | ||
382 | eval { | |
383 | my $int_count = 0; | |
384 | local $SIG{INT} = local $SIG{QUIT} = local $SIG{TERM} = sub { | |
385 | # always send signal to all pgrp members | |
386 | my $kpid = -$cpid; | |
387 | if ($int_count < 3) { | |
388 | kill(15, $kpid); # send TERM signal | |
389 | } else { | |
390 | kill(9, $kpid); # send KILL signal | |
391 | } | |
392 | $int_count++; | |
393 | }; | |
394 | local $SIG{PIPE} = sub { die "broken pipe\n"; }; | |
395 | ||
396 | my $select = new IO::Select; | |
397 | my $fh = IO::Handle->new_from_fd($childfd, 'r'); | |
398 | $select->add($fh); | |
399 | ||
400 | my $readbuf = ''; | |
401 | my $count; | |
402 | while ($select->count) { | |
403 | my @handles = $select->can_read(1); | |
404 | if (scalar(@handles)) { | |
405 | my $count = sysread ($handles[0], $readbuf, 4096); | |
406 | if (!defined ($count)) { | |
407 | my $err = $!; | |
408 | die "sync pipe read error: $err\n"; | |
409 | } | |
410 | last if $count == 0; # eof | |
411 | ||
412 | print $readbuf; | |
413 | select->flush(); | |
414 | ||
415 | print $taskfh $readbuf; | |
416 | $taskfh->flush(); | |
417 | } else { | |
418 | # some commands daemonize without closing stdout | |
419 | last if !PVE::ProcFSTools::check_process_running($cpid); | |
420 | } | |
421 | } | |
422 | ||
423 | # get status (error or OK) | |
424 | POSIX::read($ctrlfd, $readbuf, 4096); | |
425 | if ($readbuf =~ m/^TASK OK\n?$/) { | |
426 | # skip printing to stdout | |
427 | print $taskfh $readbuf; | |
428 | } elsif ($readbuf =~ m/^TASK ERROR: (.*)\n?$/) { | |
429 | print STDERR "$1\n"; | |
430 | print $taskfh "\n$readbuf"; # ensure start on new line for webUI | |
431 | } else { | |
432 | die "got unexpected control message: $readbuf\n"; | |
433 | } | |
434 | $taskfh->flush(); | |
435 | }; | |
436 | my $err = $@; | |
437 | ||
438 | POSIX::close($childfd); | |
439 | POSIX::close($ctrlfd); | |
440 | ||
441 | if ($err) { | |
442 | $err =~ s/\n/ /mg; | |
443 | print STDERR "$err\n"; | |
444 | print $taskfh "TASK ERROR: $err\n"; | |
445 | } | |
446 | }; | |
447 | ||
d9072797 DM |
448 | # start long running workers |
449 | # STDIN is redirected to /dev/null | |
450 | # STDOUT,STDERR are redirected to the filename returned by upid_decode | |
451 | # NOTE: we simulate running in foreground if ($self->{type} eq 'cli') | |
452 | sub fork_worker { | |
453 | my ($self, $dtype, $id, $user, $function, $background) = @_; | |
454 | ||
455 | $dtype = 'unknown' if !defined ($dtype); | |
456 | $id = '' if !defined ($id); | |
457 | ||
458 | $user = 'root@pve' if !defined ($user); | |
459 | ||
460 | my $sync = ($self->{type} eq 'cli' && !$background) ? 1 : 0; | |
461 | ||
462 | local $SIG{INT} = | |
463 | local $SIG{QUIT} = | |
464 | local $SIG{PIPE} = | |
465 | local $SIG{TERM} = 'IGNORE'; | |
466 | ||
467 | my $starttime = time (); | |
468 | ||
469 | my @psync = POSIX::pipe(); | |
470 | my @csync = POSIX::pipe(); | |
ed52a843 | 471 | my @ctrlfd = POSIX::pipe() if $sync; |
d9072797 DM |
472 | |
473 | my $node = $self->{nodename}; | |
474 | ||
475 | my $cpid = fork(); | |
476 | die "unable to fork worker - $!" if !defined($cpid); | |
477 | ||
478 | my $workerpuid = $cpid ? $cpid : $$; | |
479 | ||
480 | my $pstart = PVE::ProcFSTools::read_proc_starttime($workerpuid) || | |
481 | die "unable to read process start time"; | |
482 | ||
483 | my $upid = PVE::Tools::upid_encode ({ | |
484 | node => $node, pid => $workerpuid, pstart => $pstart, | |
485 | starttime => $starttime, type => $dtype, id => $id, user => $user }); | |
486 | ||
487 | my $outfh; | |
488 | ||
489 | if (!$cpid) { # child | |
490 | ||
491 | $0 = "task $upid"; | |
492 | $WORKER_FLAG = 1; | |
493 | ||
494 | $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { die "received interrupt\n"; }; | |
495 | ||
496 | $SIG{CHLD} = $SIG{PIPE} = 'DEFAULT'; | |
e97f807c | 497 | $SIG{TTOU} = 'IGNORE'; |
d9072797 | 498 | |
6a09f096 | 499 | my $ppgid; |
aea06195 | 500 | # set session/process group allows to kill the process group |
e97f807c | 501 | if ($sync && -t STDIN) { |
aea06195 TL |
502 | # some sync'ed workers operate on the tty but setsid sessions lose |
503 | # the tty, so just create a new pgroup and give it the tty | |
6a09f096 | 504 | $ppgid = POSIX::getpgrp() or die "failed to get old pgid: $!\n"; |
4c99c559 | 505 | POSIX::setpgid(0, 0) or die "failed to setpgid: $!\n"; |
e97f807c SI |
506 | POSIX::tcsetpgrp(fileno(STDIN), $$) or die "failed to tcsetpgrp: $!\n"; |
507 | } else { | |
508 | POSIX::setsid(); | |
509 | } | |
d9072797 DM |
510 | |
511 | POSIX::close ($psync[0]); | |
ed52a843 | 512 | POSIX::close ($ctrlfd[0]) if $sync; |
d9072797 DM |
513 | POSIX::close ($csync[1]); |
514 | ||
515 | $outfh = $sync ? $psync[1] : undef; | |
ed52a843 | 516 | my $resfh = $sync ? $ctrlfd[1] : undef; |
d9072797 DM |
517 | |
518 | eval { | |
519 | PVE::INotify::inotify_close(); | |
520 | ||
521 | if (my $atfork = $self->{atfork}) { | |
522 | &$atfork(); | |
523 | } | |
524 | ||
fb3a1b29 | 525 | # same algorithm as used inside SA |
d9072797 DM |
526 | # STDIN = /dev/null |
527 | my $fd = fileno (STDIN); | |
528 | ||
529 | if (!$sync) { | |
530 | close STDIN; | |
531 | POSIX::close(0) if $fd != 0; | |
532 | ||
533 | die "unable to redirect STDIN - $!" | |
534 | if !open(STDIN, "</dev/null"); | |
535 | ||
536 | $outfh = PVE::Tools::upid_open($upid); | |
4d9f4d62 | 537 | $resfh = fileno($outfh); |
d9072797 DM |
538 | } |
539 | ||
540 | ||
541 | # redirect STDOUT | |
542 | $fd = fileno(STDOUT); | |
543 | close STDOUT; | |
544 | POSIX::close (1) if $fd != 1; | |
545 | ||
546 | die "unable to redirect STDOUT - $!" | |
547 | if !open(STDOUT, ">&", $outfh); | |
548 | ||
549 | STDOUT->autoflush (1); | |
550 | ||
551 | # redirect STDERR to STDOUT | |
552 | $fd = fileno (STDERR); | |
553 | close STDERR; | |
554 | POSIX::close(2) if $fd != 2; | |
555 | ||
556 | die "unable to redirect STDERR - $!" | |
557 | if !open(STDERR, ">&1"); | |
558 | ||
559 | STDERR->autoflush(1); | |
560 | }; | |
561 | if (my $err = $@) { | |
562 | my $msg = "ERROR: $err"; | |
563 | POSIX::write($psync[1], $msg, length ($msg)); | |
564 | POSIX::close($psync[1]); | |
565 | POSIX::_exit(1); | |
566 | kill(-9, $$); | |
567 | } | |
568 | ||
569 | # sync with parent (signal that we are ready) | |
95109cc4 TL |
570 | POSIX::write($psync[1], $upid, length ($upid)); |
571 | POSIX::close($psync[1]) if !$sync; # don't need output pipe if async | |
d9072797 | 572 | |
2311859b WB |
573 | eval { |
574 | my $readbuf = ''; | |
575 | # sync with parent (wait until parent is ready) | |
576 | POSIX::read($csync[0], $readbuf, 4096); | |
577 | die "parent setup error\n" if $readbuf ne 'OK'; | |
d9072797 | 578 | |
2311859b WB |
579 | if ($self->{type} eq 'ha') { |
580 | print "task started by HA resource agent\n"; | |
581 | } | |
582 | &$function($upid); | |
583 | }; | |
edbb302e | 584 | my ($msg, $exitcode); |
d9072797 DM |
585 | my $err = $@; |
586 | if ($err) { | |
587 | chomp $err; | |
588 | $err =~ s/\n/ /mg; | |
589 | syslog('err', $err); | |
edbb302e SI |
590 | $msg = "TASK ERROR: $err\n"; |
591 | $exitcode = -1; | |
d9072797 | 592 | } else { |
edbb302e SI |
593 | $msg = "TASK OK\n"; |
594 | $exitcode = 0; | |
d9072797 | 595 | } |
edbb302e | 596 | POSIX::write($resfh, $msg, length($msg)); |
6a09f096 SI |
597 | |
598 | if ($sync) { | |
599 | POSIX::close($resfh); | |
600 | if ( -t STDIN) { | |
601 | POSIX::tcsetpgrp(fileno(STDIN), $ppgid) or | |
602 | die "failed to tcsetpgrp to parent: $!\n"; | |
603 | } | |
604 | } | |
edbb302e | 605 | POSIX::_exit($exitcode); |
a609b2f7 | 606 | kill(-9, $$); # not really needed, just to be sure |
d9072797 DM |
607 | } |
608 | ||
609 | # parent | |
610 | ||
611 | POSIX::close ($psync[1]); | |
ed52a843 | 612 | POSIX::close ($ctrlfd[1]) if $sync; |
d9072797 DM |
613 | POSIX::close ($csync[0]); |
614 | ||
615 | my $readbuf = ''; | |
616 | # sync with child (wait until child starts) | |
617 | POSIX::read($psync[0], $readbuf, 4096); | |
618 | ||
619 | if (!$sync) { | |
620 | POSIX::close($psync[0]); | |
621 | &$register_worker($cpid, $user, $upid); | |
622 | } else { | |
623 | chomp $readbuf; | |
624 | } | |
625 | ||
626 | eval { | |
627 | die "got no worker upid - start worker failed\n" if !$readbuf; | |
628 | ||
629 | if ($readbuf =~ m/^ERROR:\s*(.+)$/m) { | |
630 | die "starting worker failed: $1\n"; | |
631 | } | |
632 | ||
633 | if ($readbuf ne $upid) { | |
634 | die "got strange worker upid ('$readbuf' != '$upid') - start worker failed\n"; | |
635 | } | |
636 | ||
637 | if ($sync) { | |
638 | $outfh = PVE::Tools::upid_open($upid); | |
639 | } | |
640 | }; | |
641 | my $err = $@; | |
642 | ||
643 | if (!$err) { | |
644 | my $msg = 'OK'; | |
645 | POSIX::write($csync[1], $msg, length ($msg)); | |
646 | POSIX::close($csync[1]); | |
647 | ||
648 | } else { | |
649 | POSIX::close($csync[1]); | |
650 | kill(-9, $cpid); # make sure it gets killed | |
651 | die $err; | |
652 | } | |
653 | ||
654 | $self->log_cluster_msg('info', $user, "starting task $upid"); | |
655 | ||
656 | my $tlist = $self->active_workers($upid, $sync); | |
9a42d8a2 TL |
657 | eval { $self->broadcast_tasklist($tlist); }; |
658 | syslog('err', $@) if $@; | |
d9072797 DM |
659 | |
660 | my $res = 0; | |
661 | ||
662 | if ($sync) { | |
ed52a843 | 663 | |
3e2da216 | 664 | $tee_worker->($psync[0], $ctrlfd[0], $outfh, $cpid); |
d9072797 DM |
665 | |
666 | &$kill_process_group($cpid, $pstart); # make sure it gets killed | |
667 | ||
668 | close($outfh); | |
669 | ||
670 | waitpid($cpid, 0); | |
671 | $res = $?; | |
672 | &$log_task_result($upid, $user, $res); | |
673 | } | |
674 | ||
675 | return wantarray ? ($upid, $res) : $upid; | |
676 | } | |
677 | ||
678 | # Abstract function | |
679 | ||
680 | sub log_cluster_msg { | |
681 | my ($self, $pri, $user, $msg) = @_; | |
682 | ||
683 | syslog($pri, "%s", $msg); | |
684 | ||
685 | # PVE::Cluster::log_msg($pri, $user, $msg); | |
686 | } | |
687 | ||
688 | sub broadcast_tasklist { | |
689 | my ($self, $tlist) = @_; | |
690 | ||
691 | # PVE::Cluster::broadcast_tasklist($tlist); | |
692 | } | |
693 | ||
694 | sub check_api2_permissions { | |
695 | my ($self, $perm, $username, $param) = @_; | |
696 | ||
697 | return 1 if !$username && $perm->{user} eq 'world'; | |
698 | ||
699 | raise_perm_exc("user != null") if !$username; | |
700 | ||
701 | return 1 if $username eq 'root@pam'; | |
702 | ||
703 | raise_perm_exc('user != root@pam') if !$perm; | |
704 | ||
705 | return 1 if $perm->{user} && $perm->{user} eq 'all'; | |
706 | ||
707 | ##return $self->exec_api2_perm_check($perm->{check}, $username, $param) | |
708 | ##if $perm->{check}; | |
709 | ||
710 | raise_perm_exc(); | |
711 | } | |
712 | ||
713 | # init_request - should be called before each REST/CLI request | |
714 | sub init_request { | |
715 | my ($self, %params) = @_; | |
716 | ||
dc9c3ffa DM |
717 | $self->{result_attributes} = {} |
718 | ||
719 | # if you nedd more, implement in subclass | |
d9072797 DM |
720 | } |
721 | ||
722 | 1; |