]>
Commit | Line | Data |
---|---|---|
390802ab DM |
1 | package PVE::Daemon; |
2 | ||
3 | # Abstract class to implement Daemons | |
4 | # | |
5 | # Features: | |
6 | # * lock and write PID file /var/run/$name.pid to make sure onyl | |
7 | # one instance is running. | |
d7950851 | 8 | # * keep lock open during restart |
390802ab | 9 | # * correctly daemonize (redirect STDIN/STDOUT) |
4fc69176 DM |
10 | # * restart by stop/start, exec, or signal HUP |
11 | # * daemon restart on error (option 'restart_on_error') | |
b2132054 | 12 | # * handle worker processes (option 'max_workers') |
d7950851 DM |
13 | # * allow to restart while workers are still runningl |
14 | # (option 'leave_children_open_on_reload') | |
c4ad45cf | 15 | # * run as different user using setuid/setgid |
d7950851 | 16 | |
390802ab DM |
17 | use strict; |
18 | use warnings; | |
c4ad45cf DM |
19 | use English; |
20 | ||
390802ab DM |
21 | use PVE::SafeSyslog; |
22 | use PVE::INotify; | |
23 | ||
24 | use POSIX ":sys_wait_h"; | |
25 | use Fcntl ':flock'; | |
517c1125 | 26 | use Socket qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN); |
00dc9d0f | 27 | use IO::Socket::IP; |
517c1125 | 28 | |
390802ab DM |
29 | use Getopt::Long; |
30 | use Time::HiRes qw (gettimeofday); | |
31 | ||
32 | use base qw(PVE::CLIHandler); | |
33 | ||
390802ab DM |
34 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; |
35 | ||
36 | my $daemon_initialized = 0; # we only allow one instance | |
9db0800b | 37 | my $daemon_sockets = []; |
390802ab | 38 | |
3206cdf6 DM |
39 | my $close_daemon_lock = sub { |
40 | my ($self) = @_; | |
41 | ||
eead1ad8 DM |
42 | return if !$self->{daemon_lock_fh}; |
43 | ||
3206cdf6 DM |
44 | close $self->{daemon_lock_fh}; |
45 | delete $self->{daemon_lock_fh}; | |
46 | }; | |
47 | ||
c5611195 DM |
48 | my $log_err = sub { |
49 | my ($msg) = @_; | |
50 | chomp $msg; | |
51 | print STDERR "$msg\n"; | |
52 | syslog('err', "%s", $msg); | |
53 | }; | |
54 | ||
eead1ad8 DM |
55 | # call this if you fork() from child |
56 | # Note: we already call this for workers, so it is only required | |
57 | # if you fork inside a simple daemon (max_workers == 0). | |
58 | sub after_fork_cleanup { | |
59 | my ($self) = @_; | |
60 | ||
61 | &$close_daemon_lock($self); | |
62 | ||
63 | PVE::INotify::inotify_close(); | |
64 | ||
65 | for my $sig (qw(CHLD HUP INT TERM QUIT)) { | |
66 | $SIG{$sig} = 'DEFAULT'; # restore default handler | |
67 | # AnyEvent signals only works if $SIG{XX} is | |
68 | # undefined (perl event loop) | |
69 | delete $SIG{$sig}; # so that we can handle events with AnyEvent | |
70 | } | |
71 | } | |
72 | ||
390802ab DM |
73 | my $lockpidfile = sub { |
74 | my ($self) = @_; | |
75 | ||
76 | my $lkfn = $self->{pidfile} . ".lock"; | |
77 | ||
48876459 DM |
78 | my $waittime = 0; |
79 | ||
1a6bc2f3 DM |
80 | if (my $fd = $self->{env_pve_lock_fd}) { |
81 | ||
82 | $self->{daemon_lock_fh} = IO::Handle->new_from_fd($fd, "a"); | |
48876459 | 83 | |
1a6bc2f3 DM |
84 | } else { |
85 | ||
48876459 | 86 | $waittime = 5; |
1a6bc2f3 DM |
87 | $self->{daemon_lock_fh} = IO::File->new(">>$lkfn"); |
88 | } | |
89 | ||
3206cdf6 | 90 | if (!$self->{daemon_lock_fh}) { |
1a6bc2f3 | 91 | die "can't open lock '$lkfn' - $!\n"; |
390802ab DM |
92 | } |
93 | ||
48876459 | 94 | for (my $i = 0; $i < $waittime; $i ++) { |
3206cdf6 DM |
95 | return if flock ($self->{daemon_lock_fh}, LOCK_EX|LOCK_NB); |
96 | sleep(1); | |
97 | } | |
98 | ||
99 | if (!flock ($self->{daemon_lock_fh}, LOCK_EX|LOCK_NB)) { | |
100 | &$close_daemon_lock($self); | |
c5611195 DM |
101 | my $err = $!; |
102 | ||
103 | my ($running, $pid) = $self->running(); | |
104 | if ($running) { | |
105 | die "can't aquire lock '$lkfn' - daemon already started (pid = $pid)\n"; | |
106 | } else { | |
107 | die "can't aquire lock '$lkfn' - $err\n"; | |
108 | } | |
390802ab DM |
109 | } |
110 | }; | |
111 | ||
112 | my $writepidfile = sub { | |
113 | my ($self) = @_; | |
114 | ||
115 | my $pidfile = $self->{pidfile}; | |
116 | ||
c5611195 DM |
117 | die "can't open pid file '$pidfile' - $!\n" if !open (PIDFH, ">$pidfile"); |
118 | ||
390802ab DM |
119 | print PIDFH "$$\n"; |
120 | close (PIDFH); | |
121 | }; | |
122 | ||
123 | my $server_cleanup = sub { | |
124 | my ($self) = @_; | |
125 | ||
126 | unlink $self->{pidfile} . ".lock"; | |
127 | unlink $self->{pidfile}; | |
128 | }; | |
129 | ||
b2132054 DM |
130 | my $finish_workers = sub { |
131 | my ($self) = @_; | |
132 | ||
d7950851 DM |
133 | foreach my $id (qw(workers old_workers)) { |
134 | foreach my $cpid (keys %{$self->{$id}}) { | |
135 | my $waitpid = waitpid($cpid, WNOHANG); | |
136 | if (defined($waitpid) && ($waitpid == $cpid)) { | |
137 | delete ($self->{$id}->{$cpid}); | |
138 | syslog('info', "worker $cpid finished"); | |
139 | } | |
b2132054 DM |
140 | } |
141 | } | |
142 | }; | |
143 | ||
144 | my $start_workers = sub { | |
145 | my ($self) = @_; | |
146 | ||
147 | return if $self->{terminate}; | |
148 | ||
149 | my $count = 0; | |
150 | foreach my $cpid (keys %{$self->{workers}}) { | |
151 | $count++; | |
152 | } | |
153 | ||
154 | my $need = $self->{max_workers} - $count; | |
155 | ||
156 | return if $need <= 0; | |
157 | ||
158 | syslog('info', "starting $need worker(s)"); | |
159 | ||
160 | while ($need > 0) { | |
161 | my $pid = fork; | |
162 | ||
163 | if (!defined ($pid)) { | |
164 | syslog('err', "can't fork worker"); | |
165 | sleep (1); | |
166 | } elsif ($pid) { # parent | |
167 | $self->{workers}->{$pid} = 1; | |
168 | syslog('info', "worker $pid started"); | |
169 | $need--; | |
170 | } else { | |
171 | $0 = "$self->{name} worker"; | |
172 | ||
eead1ad8 | 173 | $self->after_fork_cleanup(); |
b2132054 DM |
174 | |
175 | eval { $self->run(); }; | |
176 | if (my $err = $@) { | |
177 | syslog('err', $err); | |
178 | sleep(5); # avoid fast restarts | |
179 | } | |
180 | ||
181 | syslog('info', "worker exit"); | |
182 | exit (0); | |
183 | } | |
184 | } | |
185 | }; | |
186 | ||
187 | my $terminate_server = sub { | |
9403080a | 188 | my ($self, $allow_open_children) = @_; |
b2132054 DM |
189 | |
190 | $self->{terminate} = 1; # set flag to avoid worker restart | |
191 | ||
192 | if (!$self->{max_workers}) { | |
193 | eval { $self->shutdown(); }; | |
194 | warn $@ if $@; | |
195 | return; | |
196 | } | |
197 | ||
198 | eval { $self->shutdown(); }; | |
199 | warn $@ if $@; | |
200 | ||
f3a33995 | 201 | # we have workers - send TERM signal |
b2132054 DM |
202 | |
203 | foreach my $cpid (keys %{$self->{workers}}) { | |
204 | kill(15, $cpid); # TERM childs | |
205 | } | |
206 | ||
f3a33995 | 207 | # if configured, leave children running on HUP |
9403080a | 208 | return if $allow_open_children && |
d7950851 DM |
209 | $self->{leave_children_open_on_reload}; |
210 | ||
f3a33995 DM |
211 | # else, send TERM to old workers |
212 | foreach my $cpid (keys %{$self->{old_workers}}) { | |
213 | kill(15, $cpid); # TERM childs | |
214 | } | |
215 | ||
b2132054 DM |
216 | # nicely shutdown childs (give them max 10 seconds to shut down) |
217 | my $previous_alarm = alarm(10); | |
218 | eval { | |
219 | local $SIG{ALRM} = sub { die "timeout\n" }; | |
220 | ||
221 | while ((my $pid = waitpid (-1, 0)) > 0) { | |
f3a33995 DM |
222 | foreach my $id (qw(workers old_workers)) { |
223 | if (defined($self->{$id}->{$pid})) { | |
224 | delete($self->{$id}->{$pid}); | |
225 | syslog('info', "worker $pid finished"); | |
226 | } | |
b2132054 DM |
227 | } |
228 | } | |
229 | alarm(0); # avoid race condition | |
230 | }; | |
231 | my $err = $@; | |
232 | ||
233 | alarm ($previous_alarm); | |
234 | ||
235 | if ($err) { | |
236 | syslog('err', "error stopping workers (will kill them now) - $err"); | |
f3a33995 DM |
237 | foreach my $id (qw(workers old_workers)) { |
238 | foreach my $cpid (keys %{$self->{$id}}) { | |
239 | # KILL childs still alive! | |
240 | if (kill (0, $cpid)) { | |
241 | delete($self->{$id}->{$cpid}); | |
242 | syslog("err", "kill worker $cpid"); | |
243 | kill(9, $cpid); | |
244 | # fixme: waitpid? | |
245 | } | |
b2132054 DM |
246 | } |
247 | } | |
248 | } | |
249 | }; | |
250 | ||
8e3e9929 WB |
251 | sub setup { |
252 | my ($self) = @_; | |
253 | ||
254 | initlog($self->{name}); | |
255 | ||
256 | my $restart = $ENV{RESTART_PVE_DAEMON}; | |
257 | delete $ENV{RESTART_PVE_DAEMON}; | |
258 | $self->{env_restart_pve_daemon} = $restart; | |
259 | ||
260 | my $lockfd = $ENV{PVE_DAEMON_LOCK_FD}; | |
261 | delete $ENV{PVE_DAEMON_LOCK_FD}; | |
262 | if (defined($lockfd)) { | |
263 | die "unable to parse lock fd '$lockfd'\n" | |
264 | if $lockfd !~ m/^(\d+)$/; | |
265 | $lockfd = $1; # untaint | |
266 | } | |
267 | $self->{env_pve_lock_fd} = $lockfd; | |
268 | ||
269 | die "please run as root\n" if !$restart && ($> != 0); | |
270 | ||
271 | die "can't create more that one PVE::Daemon" if $daemon_initialized; | |
272 | $daemon_initialized = 1; | |
273 | ||
274 | PVE::INotify::inotify_init(); | |
275 | ||
276 | if (my $gidstr = $self->{setgid}) { | |
277 | my $gid = getgrnam($gidstr) || die "getgrnam failed - $!\n"; | |
278 | POSIX::setgid($gid) || die "setgid $gid failed - $!\n"; | |
279 | $EGID = "$gid $gid"; # this calls setgroups | |
280 | # just to be sure | |
281 | die "detected strange gid\n" if !($GID eq "$gid $gid" && $EGID eq "$gid $gid"); | |
282 | } | |
283 | ||
284 | if (my $uidstr = $self->{setuid}) { | |
285 | my $uid = getpwnam($uidstr) || die "getpwnam failed - $!\n"; | |
286 | POSIX::setuid($uid) || die "setuid $uid failed - $!\n"; | |
287 | # just to be sure | |
288 | die "detected strange uid\n" if !($UID == $uid && $EUID == $uid); | |
289 | } | |
290 | ||
291 | if ($restart && $self->{max_workers}) { | |
292 | if (my $wpids = $ENV{PVE_DAEMON_WORKER_PIDS}) { | |
293 | foreach my $pid (split(':', $wpids)) { | |
294 | if ($pid =~ m/^(\d+)$/) { | |
295 | $self->{old_workers}->{$1} = 1; | |
296 | } | |
297 | } | |
298 | } | |
299 | } | |
300 | ||
301 | $self->{nodename} = PVE::INotify::nodename(); | |
302 | } | |
303 | ||
390802ab DM |
304 | my $server_run = sub { |
305 | my ($self, $debug) = @_; | |
306 | ||
1a6bc2f3 | 307 | # fixme: handle restart lockfd |
390802ab DM |
308 | &$lockpidfile($self); |
309 | ||
1a6bc2f3 DM |
310 | # remove FD_CLOEXEC bit to reuse on exec |
311 | $self->{daemon_lock_fh}->fcntl(Fcntl::F_SETFD(), 0); | |
312 | ||
313 | $ENV{PVE_DAEMON_LOCK_FD} = $self->{daemon_lock_fh}->fileno; | |
314 | ||
390802ab DM |
315 | # run in background |
316 | my $spid; | |
317 | ||
390802ab DM |
318 | $self->{debug} = 1 if $debug; |
319 | ||
320 | $self->init(); | |
321 | ||
322 | if (!$debug) { | |
323 | open STDIN, '</dev/null' || die "can't read /dev/null"; | |
324 | open STDOUT, '>/dev/null' || die "can't write /dev/null"; | |
325 | } | |
326 | ||
a8ba2293 | 327 | if (!$self->{env_restart_pve_daemon} && !$debug) { |
390802ab DM |
328 | PVE::INotify::inotify_close(); |
329 | $spid = fork(); | |
330 | if (!defined ($spid)) { | |
c5611195 | 331 | die "can't put server into background - fork failed"; |
390802ab DM |
332 | } elsif ($spid) { # parent |
333 | exit (0); | |
334 | } | |
335 | PVE::INotify::inotify_init(); | |
336 | } | |
337 | ||
a8ba2293 | 338 | if ($self->{env_restart_pve_daemon}) { |
390802ab DM |
339 | syslog('info' , "restarting server"); |
340 | } else { | |
c5611195 | 341 | &$writepidfile($self); |
390802ab DM |
342 | syslog('info' , "starting server"); |
343 | } | |
344 | ||
c5611195 DM |
345 | POSIX::setsid(); |
346 | ||
390802ab DM |
347 | open STDERR, '>&STDOUT' || die "can't close STDERR\n"; |
348 | ||
b2132054 DM |
349 | my $old_sig_term = $SIG{TERM}; |
350 | local $SIG{TERM} = sub { | |
351 | local ($@, $!, $?); # do not overwrite error vars | |
352 | syslog('info', "received signal TERM"); | |
9403080a | 353 | &$terminate_server($self, 0); |
b2132054 DM |
354 | &$server_cleanup($self); |
355 | &$old_sig_term(@_) if $old_sig_term; | |
356 | }; | |
390802ab | 357 | |
b2132054 DM |
358 | my $old_sig_quit = $SIG{QUIT}; |
359 | local $SIG{QUIT} = sub { | |
360 | local ($@, $!, $?); # do not overwrite error vars | |
361 | syslog('info', "received signal QUIT"); | |
9403080a | 362 | &$terminate_server($self, 0); |
b2132054 DM |
363 | &$server_cleanup($self); |
364 | &$old_sig_quit(@_) if $old_sig_quit; | |
365 | }; | |
390802ab | 366 | |
b2132054 DM |
367 | my $old_sig_int = $SIG{INT}; |
368 | local $SIG{INT} = sub { | |
369 | local ($@, $!, $?); # do not overwrite error vars | |
370 | syslog('info', "received signal INT"); | |
371 | $SIG{INT} = 'DEFAULT'; # allow to terminate now | |
9403080a | 372 | &$terminate_server($self, 0); |
390802ab | 373 | &$server_cleanup($self); |
b2132054 | 374 | &$old_sig_int(@_) if $old_sig_int; |
390802ab DM |
375 | }; |
376 | ||
b2132054 DM |
377 | $SIG{HUP} = sub { |
378 | local ($@, $!, $?); # do not overwrite error vars | |
379 | syslog('info', "received signal HUP"); | |
93710700 | 380 | $self->{got_hup_signal} = 1; |
b2132054 | 381 | if ($self->{max_workers}) { |
9403080a | 382 | &$terminate_server($self, 1); |
b2132054 | 383 | } elsif ($self->can('hup')) { |
bdb5acce DM |
384 | eval { $self->hup() }; |
385 | warn $@ if $@; | |
b2132054 DM |
386 | } |
387 | }; | |
388 | ||
389 | eval { | |
390 | if ($self->{max_workers}) { | |
391 | my $old_sig_chld = $SIG{CHLD}; | |
392 | local $SIG{CHLD} = sub { | |
393 | local ($@, $!, $?); # do not overwrite error vars | |
394 | &$finish_workers($self); | |
395 | &$old_sig_chld(@_) if $old_sig_chld; | |
396 | }; | |
397 | ||
ce0ae1ed DM |
398 | # catch worker finished during restart phase |
399 | &$finish_workers($self); | |
400 | ||
401 | # now loop forever (until we receive terminate signal) | |
402 | for (;;) { | |
b2132054 DM |
403 | &$start_workers($self); |
404 | sleep(5); | |
405 | &$finish_workers($self); | |
406 | last if $self->{terminate}; | |
407 | } | |
bdb5acce | 408 | |
b2132054 DM |
409 | } else { |
410 | $self->run(); | |
411 | } | |
412 | }; | |
390802ab DM |
413 | my $err = $@; |
414 | ||
415 | if ($err) { | |
416 | syslog ('err', "ERROR: $err"); | |
b2132054 | 417 | |
9403080a | 418 | &$terminate_server($self, 1); |
b2132054 | 419 | |
390802ab DM |
420 | if (my $wait_time = $self->{restart_on_error}) { |
421 | $self->restart_daemon($wait_time); | |
422 | } else { | |
423 | $self->exit_daemon(-1); | |
424 | } | |
425 | } | |
426 | ||
b2132054 DM |
427 | if ($self->{got_hup_signal}) { |
428 | $self->restart_daemon(); | |
429 | } else { | |
430 | $self->exit_daemon(0); | |
431 | } | |
390802ab DM |
432 | }; |
433 | ||
434 | sub new { | |
435 | my ($this, $name, $cmdline, %params) = @_; | |
436 | ||
1ea9e6df | 437 | $name = 'daemon' if !$name; # should not happen |
1a6bc2f3 | 438 | |
1ea9e6df | 439 | my $self; |
a8ba2293 | 440 | |
1ea9e6df | 441 | eval { |
1ea9e6df DM |
442 | my $class = ref($this) || $this; |
443 | ||
444 | $self = bless { | |
445 | name => $name, | |
309d99e7 | 446 | pidfile => "/var/run/${name}.pid", |
1ea9e6df | 447 | workers => {}, |
d7950851 | 448 | old_workers => {}, |
1ea9e6df DM |
449 | }, $class; |
450 | ||
309d99e7 | 451 | |
1ea9e6df DM |
452 | foreach my $opt (keys %params) { |
453 | my $value = $params{$opt}; | |
454 | if ($opt eq 'restart_on_error') { | |
455 | $self->{$opt} = $value; | |
456 | } elsif ($opt eq 'stop_wait_time') { | |
457 | $self->{$opt} = $value; | |
309d99e7 | 458 | } elsif ($opt eq 'pidfile') { |
1ea9e6df DM |
459 | $self->{$opt} = $value; |
460 | } elsif ($opt eq 'max_workers') { | |
461 | $self->{$opt} = $value; | |
d7950851 DM |
462 | } elsif ($opt eq 'leave_children_open_on_reload') { |
463 | $self->{$opt} = $value; | |
c4ad45cf DM |
464 | } elsif ($opt eq 'setgid') { |
465 | $self->{$opt} = $value; | |
466 | } elsif ($opt eq 'setuid') { | |
467 | $self->{$opt} = $value; | |
1ea9e6df DM |
468 | } else { |
469 | die "unknown daemon option '$opt'\n"; | |
470 | } | |
390802ab | 471 | } |
d7950851 | 472 | |
c4ad45cf | 473 | |
8e3e9929 WB |
474 | # untaint |
475 | $self->{cmdline} = [map { /^(.*)$/ } @$cmdline]; | |
bdb5acce | 476 | |
1ea9e6df DM |
477 | $0 = $name; |
478 | }; | |
479 | if (my $err = $@) { | |
480 | &$log_err($err); | |
481 | exit(-1); | |
482 | } | |
bdb5acce | 483 | |
390802ab DM |
484 | return $self; |
485 | } | |
486 | ||
487 | sub exit_daemon { | |
488 | my ($self, $status) = @_; | |
489 | ||
490 | syslog("info", "server stopped"); | |
491 | ||
492 | &$server_cleanup($self); | |
493 | ||
494 | exit($status); | |
495 | } | |
496 | ||
497 | sub restart_daemon { | |
498 | my ($self, $waittime) = @_; | |
499 | ||
500 | syslog('info', "server shutdown (restart)"); | |
501 | ||
502 | $ENV{RESTART_PVE_DAEMON} = 1; | |
503 | ||
9db0800b WB |
504 | foreach my $ds (@$daemon_sockets) { |
505 | $ds->fcntl(Fcntl::F_SETFD(), 0); | |
506 | } | |
507 | ||
d7950851 DM |
508 | if ($self->{max_workers}) { |
509 | my @workers = keys %{$self->{workers}}; | |
510 | push @workers, keys %{$self->{old_workers}}; | |
511 | $ENV{PVE_DAEMON_WORKER_PIDS} = join(':', @workers); | |
512 | } | |
513 | ||
390802ab DM |
514 | sleep($waittime) if $waittime; # avoid high server load due to restarts |
515 | ||
516 | PVE::INotify::inotify_close(); | |
517 | ||
518 | exec (@{$self->{cmdline}}); | |
519 | ||
520 | exit (-1); # never reached? | |
521 | } | |
522 | ||
523 | # please overwrite in subclass | |
524 | # this is called at startup - before forking | |
525 | sub init { | |
526 | my ($self) = @_; | |
527 | ||
528 | } | |
529 | ||
530 | # please overwrite in subclass | |
531 | sub shutdown { | |
532 | my ($self) = @_; | |
533 | ||
534 | syslog('info' , "server closing"); | |
535 | ||
b2132054 DM |
536 | if (!$self->{max_workers}) { |
537 | # wait for children | |
538 | 1 while (waitpid(-1, POSIX::WNOHANG()) > 0); | |
539 | } | |
390802ab DM |
540 | } |
541 | ||
bdb5acce DM |
542 | # please define in subclass |
543 | #sub hup { | |
544 | # my ($self) = @_; | |
545 | # | |
546 | # syslog('info' , "received signal HUP (restart)"); | |
547 | #} | |
390802ab DM |
548 | |
549 | # please overwrite in subclass | |
550 | sub run { | |
551 | my ($self) = @_; | |
552 | ||
553 | for (;;) { # forever | |
554 | syslog('info' , "server is running"); | |
555 | sleep(5); | |
556 | } | |
557 | } | |
558 | ||
559 | sub start { | |
560 | my ($self, $debug) = @_; | |
561 | ||
8e3e9929 WB |
562 | eval { |
563 | $self->setup(); | |
564 | &$server_run($self, $debug); | |
565 | }; | |
1a6bc2f3 | 566 | if (my $err = $@) { |
c5611195 DM |
567 | &$log_err("start failed - $err"); |
568 | exit(-1); | |
1a6bc2f3 | 569 | } |
390802ab DM |
570 | } |
571 | ||
bdb5acce DM |
572 | my $read_pid = sub { |
573 | my ($self) = @_; | |
574 | ||
575 | my $pid_str = PVE::Tools::file_read_firstline($self->{pidfile}); | |
576 | ||
577 | return 0 if !$pid_str; | |
578 | ||
579 | return 0 if $pid_str !~ m/^(\d+)$/; # untaint | |
580 | ||
581 | my $pid = int($1); | |
582 | ||
583 | return $pid; | |
584 | }; | |
585 | ||
a526ab1c AG |
586 | # checks if the process was started by systemd |
587 | my $init_ppid = sub { | |
588 | ||
589 | if (getppid() == 1) { | |
590 | return 1; | |
591 | } else { | |
592 | return 0; | |
593 | } | |
594 | }; | |
595 | ||
390802ab DM |
596 | sub running { |
597 | my ($self) = @_; | |
598 | ||
bdb5acce | 599 | my $pid = &$read_pid($self); |
390802ab DM |
600 | |
601 | if ($pid) { | |
602 | my $res = PVE::ProcFSTools::check_process_running($pid) ? 1 : 0; | |
603 | return wantarray ? ($res, $pid) : $res; | |
604 | } | |
605 | ||
606 | return wantarray ? (0, 0) : 0; | |
607 | } | |
608 | ||
609 | sub stop { | |
610 | my ($self) = @_; | |
611 | ||
bdb5acce DM |
612 | my $pid = &$read_pid($self); |
613 | ||
390802ab DM |
614 | return if !$pid; |
615 | ||
616 | if (PVE::ProcFSTools::check_process_running($pid)) { | |
617 | kill(15, $pid); # send TERM signal | |
618 | # give some time | |
619 | my $wait_time = $self->{stop_wait_time} || 5; | |
620 | my $running = 1; | |
621 | for (my $i = 0; $i < $wait_time; $i++) { | |
622 | $running = PVE::ProcFSTools::check_process_running($pid); | |
623 | last if !$running; | |
624 | sleep (1); | |
625 | } | |
626 | ||
627 | syslog('err', "server still running - send KILL") if $running; | |
628 | ||
629 | # to be sure | |
630 | kill(9, $pid); | |
631 | waitpid($pid, 0); | |
632 | } | |
633 | ||
634 | if (-f $self->{pidfile}) { | |
1a6bc2f3 DM |
635 | eval { |
636 | # try to get the lock | |
637 | &$lockpidfile($self); | |
638 | &$server_cleanup($self); | |
639 | }; | |
640 | if (my $err = $@) { | |
c5611195 | 641 | &$log_err("cleanup failed - $err"); |
1a6bc2f3 | 642 | } |
390802ab DM |
643 | } |
644 | } | |
645 | ||
646 | sub register_start_command { | |
da1a7f58 DM |
647 | my ($self, $description) = @_; |
648 | ||
649 | my $class = ref($self); | |
390802ab DM |
650 | |
651 | $class->register_method({ | |
652 | name => 'start', | |
653 | path => 'start', | |
654 | method => 'POST', | |
655 | description => $description || "Start the daemon.", | |
656 | parameters => { | |
657 | additionalProperties => 0, | |
658 | properties => { | |
659 | debug => { | |
660 | description => "Debug mode - stay in foreground", | |
661 | type => "boolean", | |
662 | optional => 1, | |
663 | default => 0, | |
664 | }, | |
665 | }, | |
666 | }, | |
667 | returns => { type => 'null' }, | |
668 | ||
669 | code => sub { | |
670 | my ($param) = @_; | |
671 | ||
25eea093 | 672 | if (&$init_ppid() || $param->{debug}) { |
a526ab1c AG |
673 | $self->start($param->{debug}); |
674 | } else { | |
675 | PVE::Tools::run_command(['systemctl', 'start', $self->{name}]); | |
676 | } | |
390802ab DM |
677 | |
678 | return undef; | |
679 | }}); | |
680 | } | |
681 | ||
bdb5acce DM |
682 | my $reload_daemon = sub { |
683 | my ($self, $use_hup) = @_; | |
684 | ||
a8ba2293 | 685 | if ($self->{env_restart_pve_daemon}) { |
bdb5acce DM |
686 | $self->start(); |
687 | } else { | |
688 | my ($running, $pid) = $self->running(); | |
689 | if (!$running) { | |
690 | $self->start(); | |
691 | } else { | |
692 | if ($use_hup) { | |
b2132054 DM |
693 | syslog('info', "send HUP to $pid"); |
694 | kill 1, $pid; | |
bdb5acce DM |
695 | } else { |
696 | $self->stop(); | |
697 | $self->start(); | |
698 | } | |
699 | } | |
700 | } | |
701 | }; | |
702 | ||
390802ab | 703 | sub register_restart_command { |
da1a7f58 DM |
704 | my ($self, $use_hup, $description) = @_; |
705 | ||
706 | my $class = ref($self); | |
390802ab DM |
707 | |
708 | $class->register_method({ | |
709 | name => 'restart', | |
710 | path => 'restart', | |
711 | method => 'POST', | |
712 | description => $description || "Restart the daemon (or start if not running).", | |
713 | parameters => { | |
714 | additionalProperties => 0, | |
715 | properties => {}, | |
716 | }, | |
717 | returns => { type => 'null' }, | |
718 | ||
719 | code => sub { | |
720 | my ($param) = @_; | |
721 | ||
a526ab1c AG |
722 | if (&$init_ppid()) { |
723 | &$reload_daemon($self, $use_hup); | |
724 | } else { | |
725 | PVE::Tools::run_command(['systemctl', $use_hup ? 'reload-or-restart' : 'restart', $self->{name}]); | |
726 | } | |
bdb5acce DM |
727 | |
728 | return undef; | |
729 | }}); | |
730 | } | |
731 | ||
732 | sub register_reload_command { | |
da1a7f58 DM |
733 | my ($self, $description) = @_; |
734 | ||
735 | my $class = ref($self); | |
bdb5acce DM |
736 | |
737 | $class->register_method({ | |
738 | name => 'reload', | |
739 | path => 'reload', | |
740 | method => 'POST', | |
741 | description => $description || "Reload daemon configuration (or start if not running).", | |
742 | parameters => { | |
743 | additionalProperties => 0, | |
744 | properties => {}, | |
745 | }, | |
746 | returns => { type => 'null' }, | |
747 | ||
748 | code => sub { | |
749 | my ($param) = @_; | |
750 | ||
751 | &$reload_daemon($self, 1); | |
390802ab DM |
752 | |
753 | return undef; | |
754 | }}); | |
755 | } | |
756 | ||
757 | sub register_stop_command { | |
da1a7f58 DM |
758 | my ($self, $description) = @_; |
759 | ||
760 | my $class = ref($self); | |
390802ab DM |
761 | |
762 | $class->register_method({ | |
763 | name => 'stop', | |
764 | path => 'stop', | |
765 | method => 'POST', | |
766 | description => $description || "Stop the daemon.", | |
767 | parameters => { | |
768 | additionalProperties => 0, | |
769 | properties => {}, | |
770 | }, | |
771 | returns => { type => 'null' }, | |
772 | ||
773 | code => sub { | |
774 | my ($param) = @_; | |
775 | ||
a526ab1c AG |
776 | if (&$init_ppid()) { |
777 | $self->stop(); | |
778 | } else { | |
779 | PVE::Tools::run_command(['systemctl', 'stop', $self->{name}]); | |
780 | } | |
390802ab DM |
781 | |
782 | return undef; | |
783 | }}); | |
784 | } | |
785 | ||
786 | sub register_status_command { | |
da1a7f58 DM |
787 | my ($self, $description) = @_; |
788 | ||
789 | my $class = ref($self); | |
390802ab DM |
790 | |
791 | $class->register_method({ | |
792 | name => 'status', | |
793 | path => 'status', | |
794 | method => 'GET', | |
795 | description => "Get daemon status.", | |
796 | parameters => { | |
797 | additionalProperties => 0, | |
798 | properties => {}, | |
799 | }, | |
800 | returns => { | |
801 | type => 'string', | |
802 | enum => ['stopped', 'running'], | |
803 | }, | |
804 | code => sub { | |
805 | my ($param) = @_; | |
806 | ||
807 | return $self->running() ? 'running' : 'stopped'; | |
808 | }}); | |
809 | } | |
810 | ||
517c1125 DM |
811 | # some useful helper |
812 | ||
813 | sub create_reusable_socket { | |
83397d50 | 814 | my ($self, $port, $host, $family) = @_; |
517c1125 DM |
815 | |
816 | die "no port specifed" if !$port; | |
817 | ||
818 | my ($socket, $sockfd); | |
819 | ||
820 | if (defined($sockfd = $ENV{"PVE_DAEMON_SOCKET_$port"}) && | |
821 | $self->{env_restart_pve_daemon}) { | |
822 | ||
823 | die "unable to parse socket fd '$sockfd'\n" | |
824 | if $sockfd !~ m/^(\d+)$/; | |
825 | $sockfd = $1; # untaint | |
826 | ||
00dc9d0f | 827 | $socket = IO::Socket::IP->new; |
517c1125 DM |
828 | $socket->fdopen($sockfd, 'w') || |
829 | die "cannot fdopen file descriptor '$sockfd' - $!\n"; | |
830 | ||
9db0800b | 831 | $socket->fcntl(Fcntl::F_SETFD(), Fcntl::FD_CLOEXEC); |
517c1125 DM |
832 | } else { |
833 | ||
00dc9d0f | 834 | $socket = IO::Socket::IP->new( |
517c1125 DM |
835 | LocalAddr => $host, |
836 | LocalPort => $port, | |
837 | Listen => SOMAXCONN, | |
83397d50 | 838 | Family => $family, |
517c1125 | 839 | Proto => 'tcp', |
e43b3a0f | 840 | GetAddrInfoFlags => 0, |
517c1125 DM |
841 | ReuseAddr => 1) || |
842 | die "unable to create socket - $@\n"; | |
843 | ||
844 | # we often observe delays when using Nagle algorithm, | |
845 | # so we disable that to maximize performance | |
846 | setsockopt($socket, IPPROTO_TCP, TCP_NODELAY, 1); | |
847 | ||
848 | $ENV{"PVE_DAEMON_SOCKET_$port"} = $socket->fileno; | |
849 | } | |
850 | ||
9db0800b WB |
851 | push @$daemon_sockets, $socket; |
852 | ||
517c1125 DM |
853 | return $socket; |
854 | } | |
855 | ||
856 | ||
390802ab DM |
857 | 1; |
858 |