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