]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Daemon.pm
run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / Daemon.pm
1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # contains common daemon code for the nntpd and httpd servers.
4 # This may be used for read-only IMAP server if we decide to implement it.
5 package PublicInbox::Daemon;
6 use strict;
7 use warnings;
8 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
9 use IO::Handle;
10 use IO::Socket;
11 use POSIX qw(WNOHANG);
12 use Socket qw(IPPROTO_TCP SOL_SOCKET);
13 sub SO_ACCEPTFILTER () { 0x1000 }
14 use Cwd qw/abs_path/;
15 STDOUT->autoflush(1);
16 STDERR->autoflush(1);
17 use PublicInbox::DS qw(now);
18 require PublicInbox::EvCleanup;
19 require PublicInbox::Listener;
20 require PublicInbox::ParentPipe;
21 my @CMD;
22 my $set_user;
23 my (@cfg_listen, $stdout, $stderr, $group, $user, $pid_file, $daemonize);
24 my $worker_processes = 1;
25 my @listeners;
26 my %pids;
27 my %listener_names; # sockname => IO::Handle
28 my %tls_opt; # scheme://sockname => args for IO::Socket::SSL->start_SSL
29 my $reexec_pid;
30 my $cleanup;
31 my ($uid, $gid);
32 my ($default_cert, $default_key);
33 END { $cleanup->() if $cleanup };
34 my %KNOWN_TLS = ( 443 => 'https', 563 => 'nntps' );
35 my %KNOWN_STARTTLS = ( 119 => 'nntp' );
36
37 sub accept_tls_opt ($) {
38         my ($opt_str) = @_;
39         # opt_str: opt1=val1,opt2=val2 (opt may repeat for multi-value)
40         require PublicInbox::TLS;
41         my $o = {};
42         # allow ',' as delimiter since '&' is shell-unfriendly
43         foreach (split(/[,&]/, $opt_str)) {
44                 my ($k, $v) = split(/=/, $_, 2);
45                 push @{$o->{$k} ||= []}, $v;
46         }
47
48         # key may be a part of cert.  At least
49         # p5-io-socket-ssl/example/ssl_server.pl has this fallback:
50         $o->{cert} //= [ $default_cert ];
51         $o->{key} //= defined($default_key) ? [ $default_key ] : $o->{cert};
52         my %ctx_opt = (SSL_server => 1);
53         # parse out hostname:/path/to/ mappings:
54         foreach my $k (qw(cert key)) {
55                 my $x = $ctx_opt{'SSL_'.$k.'_file'} = {};
56                 foreach my $path (@{$o->{$k}}) {
57                         my $host = '';
58                         $path =~ s/\A([^:]+):// and $host = $1;
59                         $x->{$host} = $path;
60                 }
61         }
62         my $ctx = IO::Socket::SSL::SSL_Context->new(%ctx_opt) or
63                 die 'SSL_Context->new: '.PublicInbox::TLS::err();
64
65         # save ~34K per idle connection (cf. SSL_CTX_set_mode(3ssl))
66         # RSS goes from 346MB to 171MB with 10K idle NNTPS clients on amd64
67         # cf. https://rt.cpan.org/Ticket/Display.html?id=129463
68         my $mode = eval { Net::SSLeay::MODE_RELEASE_BUFFERS() };
69         if ($mode && $ctx->{context}) {
70                 eval { Net::SSLeay::CTX_set_mode($ctx->{context}, $mode) };
71                 warn "W: $@ (setting SSL_MODE_RELEASE_BUFFERS)\n" if $@;
72         }
73
74         { SSL_server => 1, SSL_startHandshake => 0, SSL_reuse_ctx => $ctx };
75 }
76
77 sub daemon_prepare ($) {
78         my ($default_listen) = @_;
79         @CMD = ($0, @ARGV);
80         $SIG{HUP} = $SIG{USR1} = $SIG{USR2} = $SIG{PIPE} =
81                 $SIG{TTIN} = $SIG{TTOU} = $SIG{WINCH} = 'IGNORE';
82         my %opts = (
83                 'l|listen=s' => \@cfg_listen,
84                 '1|stdout=s' => \$stdout,
85                 '2|stderr=s' => \$stderr,
86                 'W|worker-processes=i' => \$worker_processes,
87                 'P|pid-file=s' => \$pid_file,
88                 'u|user=s' => \$user,
89                 'g|group=s' => \$group,
90                 'D|daemonize' => \$daemonize,
91                 'cert=s' => \$default_cert,
92                 'key=s' => \$default_key,
93         );
94         GetOptions(%opts) or die "bad command-line args\n";
95
96         if (defined $pid_file && $pid_file =~ /\.oldbin\z/) {
97                 die "--pid-file cannot end with '.oldbin'\n";
98         }
99         @listeners = inherit();
100
101         # allow socket-activation users to set certs once and not
102         # have to configure each socket:
103         my @inherited_names = keys(%listener_names) if defined($default_cert);
104
105         # ignore daemonize when inheriting
106         $daemonize = undef if scalar @listeners;
107
108         push @cfg_listen, $default_listen unless (@listeners || @cfg_listen);
109
110         foreach my $l (@cfg_listen) {
111                 my $orig = $l;
112                 my $scheme = '';
113                 if ($l =~ s!\A([^:]+)://!!) {
114                         $scheme = $1;
115                 } elsif ($l =~ /\A(?:\[[^\]]+\]|[^:]+):([0-9])+/) {
116                         my $s = $KNOWN_TLS{$1} // $KNOWN_STARTTLS{$1};
117                         $scheme = $s if defined $s;
118                 }
119                 if ($l =~ s!/?\?(.+)\z!!) {
120                         $tls_opt{"$scheme://$l"} = accept_tls_opt($1);
121                 } elsif (defined($default_cert)) {
122                         $tls_opt{"$scheme://$l"} = accept_tls_opt('');
123                 } elsif ($scheme =~ /\A(?:nntps|https)\z/) {
124                         die "$orig specified w/o cert=\n";
125                 }
126                 # TODO: use scheme to load either NNTP.pm or HTTP.pm
127
128                 next if $listener_names{$l}; # already inherited
129                 my (%o, $sock_pkg);
130                 if (index($l, '/') == 0) {
131                         $sock_pkg = 'IO::Socket::UNIX';
132                         eval "use $sock_pkg";
133                         die $@ if $@;
134                         %o = (Type => SOCK_STREAM, Peer => $l);
135                         if (-S $l) {
136                                 my $c = $sock_pkg->new(%o);
137                                 if (!defined($c) && $!{ECONNREFUSED}) {
138                                         unlink $l or die
139 "failed to unlink stale socket=$l: $!\n";
140                                 } # else: let the bind fail
141                         }
142                         $o{Local} = delete $o{Peer};
143                 } else {
144                         # both work for IPv4, too
145                         for (qw(IO::Socket::IP IO::Socket::INET6)) {
146                                 $sock_pkg = $_;
147                                 eval "use $sock_pkg";
148                                 $@ or last;
149                         }
150                         die $@ if $@;
151                         %o = (LocalAddr => $l, ReuseAddr => 1, Proto => 'tcp');
152                 }
153                 $o{Listen} = 1024;
154                 my $prev = umask 0000;
155                 my $s = eval { $sock_pkg->new(%o) };
156                 warn "error binding $l: $! ($@)\n" unless $s;
157                 umask $prev;
158                 if ($s) {
159                         $listener_names{sockname($s)} = $s;
160                         $s->blocking(0);
161                         push @listeners, $s;
162                 }
163         }
164
165         # cert/key options in @cfg_listen takes precedence when inheriting,
166         # but map well-known inherited ports if --listen isn't specified
167         # at all
168         for my $sockname (@inherited_names) {
169                 $sockname =~ /:([0-9]+)\z/ or next;
170                 if (my $scheme = $KNOWN_TLS{$1}) {
171                         $tls_opt{"$scheme://$sockname"} ||= accept_tls_opt('');
172                 } elsif (($scheme = $KNOWN_STARTTLS{$1})) {
173                         next if $tls_opt{"$scheme://$sockname"};
174                         $tls_opt{''} ||= accept_tls_opt('');
175                 }
176         }
177
178         die "No listeners bound\n" unless @listeners;
179 }
180
181 sub check_absolute ($$) {
182         my ($var, $val) = @_;
183         if (defined $val && index($val, '/') != 0) {
184                 die
185 "--$var must be an absolute path when using --daemonize: $val\n";
186         }
187 }
188
189 sub daemonize () {
190         if ($daemonize) {
191                 foreach my $i (0..$#ARGV) {
192                         my $arg = $ARGV[$i];
193                         next unless -e $arg;
194                         $ARGV[$i] = abs_path($arg);
195                 }
196                 check_absolute('stdout', $stdout);
197                 check_absolute('stderr', $stderr);
198                 check_absolute('pid-file', $pid_file);
199
200                 chdir '/' or die "chdir failed: $!";
201         }
202
203         return unless (defined $pid_file || defined $group || defined $user
204                         || $daemonize);
205
206         eval { require Net::Server::Daemonize };
207         if ($@) {
208                 die
209 "Net::Server required for --pid-file, --group, --user, and --daemonize\n$@\n";
210         }
211
212         Net::Server::Daemonize::check_pid_file($pid_file) if defined $pid_file;
213         $uid = Net::Server::Daemonize::get_uid($user) if defined $user;
214         if (defined $group) {
215                 $gid = Net::Server::Daemonize::get_gid($group);
216                 $gid = (split /\s+/, $gid)[0];
217         } elsif (defined $uid) {
218                 $gid = (getpwuid($uid))[3];
219         }
220
221         # We change users in the worker to ensure upgradability,
222         # The upgrade will create the ".oldbin" pid file in the
223         # same directory as the given pid file.
224         $uid and $set_user = sub {
225                 $set_user = undef;
226                 Net::Server::Daemonize::set_user($uid, $gid);
227         };
228
229         if ($daemonize) {
230                 my $pid = fork;
231                 die "could not fork: $!\n" unless defined $pid;
232                 exit if $pid;
233
234                 open(STDIN, '+<', '/dev/null') or
235                                         die "redirect stdin failed: $!\n";
236                 open STDOUT, '>&STDIN' or die "redirect stdout failed: $!\n";
237                 open STDERR, '>&STDIN' or die "redirect stderr failed: $!\n";
238                 POSIX::setsid();
239                 $pid = fork;
240                 die "could not fork: $!\n" unless defined $pid;
241                 exit if $pid;
242         }
243         if (defined $pid_file) {
244                 write_pid($pid_file);
245                 my $unlink_pid = $$;
246                 $cleanup = sub {
247                         $cleanup = undef; # avoid cyclic reference
248                         unlink_pid_file_safe_ish($unlink_pid, $pid_file);
249                 };
250         }
251 }
252
253
254 sub worker_quit {
255         # killing again terminates immediately:
256         exit unless @listeners;
257
258         $_->close foreach @listeners; # call PublicInbox::DS::close
259         @listeners = ();
260
261         my $proc_name;
262         my $warn = 0;
263         # drop idle connections and try to quit gracefully
264         PublicInbox::DS->SetPostLoopCallback(sub {
265                 my ($dmap, undef) = @_;
266                 my $n = 0;
267                 my $now = now();
268
269                 foreach my $s (values %$dmap) {
270                         $s->can('busy') or next;
271                         if ($s->busy($now)) {
272                                 ++$n;
273                         } else {
274                                 # close as much as possible, early as possible
275                                 $s->close;
276                         }
277                 }
278                 if ($n) {
279                         if (($warn + 5) < now()) {
280                                 warn "$$ quitting, $n client(s) left\n";
281                                 $warn = now();
282                         }
283                         unless (defined $proc_name) {
284                                 $proc_name = (split(/\s+/, $0))[0];
285                                 $proc_name =~ s!\A.*?([^/]+)\z!$1!;
286                         }
287                         $0 = "$proc_name quitting, $n client(s) left";
288                 }
289                 $n; # true: loop continues, false: loop breaks
290         });
291 }
292
293 sub reopen_logs {
294         if ($stdout) {
295                 open STDOUT, '>>', $stdout or
296                         warn "failed to redirect stdout to $stdout: $!\n";
297                 STDOUT->autoflush(1);
298                 do_chown($stdout);
299         }
300         if ($stderr) {
301                 open STDERR, '>>', $stderr or
302                         warn "failed to redirect stderr to $stderr: $!\n";
303                 STDERR->autoflush(1);
304                 do_chown($stderr);
305         }
306 }
307
308 sub sockname ($) {
309         my ($s) = @_;
310         my $addr = getsockname($s) or return;
311         my ($host, $port) = host_with_port($addr);
312         if ($port == 0 && $host eq '127.0.0.1') {
313                 my ($path) = Socket::sockaddr_un($addr);
314                 return $path;
315         }
316         "$host:$port";
317 }
318
319 sub unpack_ipv6 ($) {
320         my ($addr) = @_;
321         my ($port, $host);
322
323         # Socket.pm in Perl 5.14+ supports IPv6:
324         eval {
325                 ($port, $host) = Socket::unpack_sockaddr_in6($addr);
326                 $host = Socket::inet_ntop(Socket::AF_INET6(), $host);
327         };
328
329         if ($@) {
330                 # Perl 5.12 or earlier?  SpamAssassin and Net::Server use
331                 # Socket6, so it may be installed on our system, already
332                 # (otherwise die here):
333                 require Socket6;
334
335                 ($port, $host) = Socket6::unpack_sockaddr_in6($addr);
336                 $host = Socket6::inet_ntop(Socket6::AF_INET6(), $host);
337         }
338         ($host, $port);
339 }
340
341 sub host_with_port ($) {
342         my ($addr) = @_;
343         my ($port, $host);
344
345         # this eval will die on Unix sockets:
346         eval {
347                 if (length($addr) >= 28) {
348                         ($host, $port) = unpack_ipv6($addr);
349                         $host = "[$host]";
350                 } else {
351                         ($port, $host) = Socket::sockaddr_in($addr);
352                         $host = Socket::inet_ntoa($host);
353                 }
354         };
355         $@ ? ('127.0.0.1', 0) : ($host, $port);
356 }
357
358 sub inherit () {
359         return () if ($ENV{LISTEN_PID} || 0) != $$;
360         my $fds = $ENV{LISTEN_FDS} or return ();
361         my $end = $fds + 2; # LISTEN_FDS_START - 1
362         my @rv = ();
363         foreach my $fd (3..$end) {
364                 my $s = IO::Handle->new_from_fd($fd, 'r');
365                 if (my $k = sockname($s)) {
366                         if ($s->blocking) {
367                                 $s->blocking(0);
368                                 warn <<"";
369 Inherited socket (fd=$fd) is blocking, making it non-blocking.
370 Set 'NonBlocking = true' in the systemd.service unit to avoid stalled
371 processes when multiple service instances start.
372
373                         }
374                         $listener_names{$k} = $s;
375                         push @rv, $s;
376                 } else {
377                         warn "failed to inherit fd=$fd (LISTEN_FDS=$fds)";
378                 }
379         }
380         @rv
381 }
382
383 sub upgrade () {
384         if ($reexec_pid) {
385                 warn "upgrade in-progress: $reexec_pid\n";
386                 return;
387         }
388         if (defined $pid_file) {
389                 if ($pid_file =~ /\.oldbin\z/) {
390                         warn "BUG: .oldbin suffix exists: $pid_file\n";
391                         return;
392                 }
393                 unlink_pid_file_safe_ish($$, $pid_file);
394                 $pid_file .= '.oldbin';
395                 write_pid($pid_file);
396         }
397         my $pid = fork;
398         unless (defined $pid) {
399                 warn "fork failed: $!\n";
400                 return;
401         }
402         if ($pid == 0) {
403                 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
404                 $ENV{LISTEN_FDS} = scalar @listeners;
405                 $ENV{LISTEN_PID} = $$;
406                 foreach my $s (@listeners) {
407                         my $fl = fcntl($s, F_GETFD, 0);
408                         fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
409                 }
410                 exec @CMD;
411                 die "Failed to exec: $!\n";
412         }
413         $reexec_pid = $pid;
414 }
415
416 sub kill_workers ($) {
417         my ($s) = @_;
418
419         while (my ($pid, $id) = each %pids) {
420                 kill $s, $pid;
421         }
422 }
423
424 sub upgrade_aborted ($) {
425         my ($p) = @_;
426         warn "reexec PID($p) died with: $?\n";
427         $reexec_pid = undef;
428         return unless $pid_file;
429
430         my $file = $pid_file;
431         $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file";
432         unlink_pid_file_safe_ish($$, $pid_file);
433         $pid_file = $file;
434         eval { write_pid($pid_file) };
435         warn $@, "\n" if $@;
436 }
437
438 sub reap_children () {
439         while (1) {
440                 my $p = waitpid(-1, WNOHANG) or return;
441                 if (defined $reexec_pid && $p == $reexec_pid) {
442                         upgrade_aborted($p);
443                 } elsif (defined(my $id = delete $pids{$p})) {
444                         warn "worker[$id] PID($p) died with: $?\n";
445                 } elsif ($p > 0) {
446                         warn "unknown PID($p) reaped: $?\n";
447                 } else {
448                         return;
449                 }
450         }
451 }
452
453 sub unlink_pid_file_safe_ish ($$) {
454         my ($unlink_pid, $file) = @_;
455         return unless defined $unlink_pid && $unlink_pid == $$;
456
457         open my $fh, '<', $file or return;
458         local $/ = "\n";
459         defined(my $read_pid = <$fh>) or return;
460         chomp $read_pid;
461         if ($read_pid == $unlink_pid) {
462                 Net::Server::Daemonize::unlink_pid_file($file);
463         }
464 }
465
466 sub master_loop {
467         pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!";
468         pipe(my ($r, $w)) or die "failed to create self-pipe: $!";
469
470         if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ = 1031
471                 fcntl($_, 1031, 4096) for ($w, $p1);
472         }
473
474         IO::Handle::blocking($w, 0);
475         my $set_workers = $worker_processes;
476         my @caught;
477         my $master_pid = $$;
478         foreach my $s (qw(HUP CHLD QUIT INT TERM USR1 USR2 TTIN TTOU WINCH)) {
479                 $SIG{$s} = sub {
480                         return if $$ != $master_pid;
481                         push @caught, $s;
482                         syswrite($w, '.');
483                 };
484         }
485         reopen_logs();
486         # main loop
487         my $quit = 0;
488         while (1) {
489                 while (my $s = shift @caught) {
490                         if ($s eq 'USR1') {
491                                 reopen_logs();
492                                 kill_workers($s);
493                         } elsif ($s eq 'USR2') {
494                                 upgrade();
495                         } elsif ($s =~ /\A(?:QUIT|TERM|INT)\z/) {
496                                 exit if $quit++;
497                                 kill_workers($s);
498                         } elsif ($s eq 'WINCH') {
499                                 if (-t STDIN || -t STDOUT || -t STDERR) {
500                                         warn
501 "ignoring SIGWINCH since we are not daemonized\n";
502                                         $SIG{WINCH} = 'IGNORE';
503                                 } else {
504                                         $worker_processes = 0;
505                                 }
506                         } elsif ($s eq 'HUP') {
507                                 $worker_processes = $set_workers;
508                                 kill_workers($s);
509                         } elsif ($s eq 'TTIN') {
510                                 if ($set_workers > $worker_processes) {
511                                         ++$worker_processes;
512                                 } else {
513                                         $worker_processes = ++$set_workers;
514                                 }
515                         } elsif ($s eq 'TTOU') {
516                                 if ($set_workers > 0) {
517                                         $worker_processes = --$set_workers;
518                                 }
519                         } elsif ($s eq 'CHLD') {
520                                 reap_children();
521                         }
522                 }
523
524                 my $n = scalar keys %pids;
525                 if ($quit) {
526                         exit if $n == 0;
527                         $set_workers = $worker_processes = $n = 0;
528                 }
529
530                 if ($n > $worker_processes) {
531                         while (my ($k, $v) = each %pids) {
532                                 kill('TERM', $k) if $v >= $worker_processes;
533                         }
534                         $n = $worker_processes;
535                 }
536                 foreach my $i ($n..($worker_processes - 1)) {
537                         my $pid = fork;
538                         if (!defined $pid) {
539                                 warn "failed to fork worker[$i]: $!\n";
540                         } elsif ($pid == 0) {
541                                 $set_user->() if $set_user;
542                                 return $p0; # run normal work code
543                         } else {
544                                 warn "PID=$pid is worker[$i]\n";
545                                 $pids{$pid} = $i;
546                         }
547                 }
548                 # just wait on signal events here:
549                 sysread($r, my $buf, 8);
550         }
551         exit # never gets here, just for documentation
552 }
553
554 sub tls_start_cb ($$) {
555         my ($opt, $orig_post_accept) = @_;
556         sub {
557                 my ($io, $addr, $srv) = @_;
558                 my $ssl = IO::Socket::SSL->start_SSL($io, %$opt);
559                 $orig_post_accept->($ssl, $addr, $srv);
560         }
561 }
562
563 sub defer_accept ($$) {
564         my ($s, $af_name) = @_;
565         return unless defined $af_name;
566         if ($^O eq 'linux') {
567                 my $x = getsockopt($s, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT());
568                 return unless defined $x; # may be Unix socket
569                 my $sec = unpack('i', $x);
570                 return if $sec > 0; # systemd users may set a higher value
571                 setsockopt($s, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT(), 1);
572         } elsif ($^O eq 'freebsd') {
573                 my $x = getsockopt($s, SOL_SOCKET, SO_ACCEPTFILTER);
574                 return if defined $x; # don't change if set
575                 my $accf_arg = pack('a16a240', $af_name, '');
576                 setsockopt($s, SOL_SOCKET, SO_ACCEPTFILTER, $accf_arg);
577         }
578 }
579
580 sub daemon_loop ($$$$) {
581         my ($refresh, $post_accept, $nntpd, $af_default) = @_;
582         PublicInbox::EvCleanup::enable(); # early for $refresh
583         my %post_accept;
584         while (my ($k, $v) = each %tls_opt) {
585                 if ($k =~ s!\A(?:nntps|https)://!!) {
586                         $post_accept{$k} = tls_start_cb($v, $post_accept);
587                 } elsif ($nntpd) { # STARTTLS, $k eq '' is OK
588                         $nntpd->{accept_tls} = $v;
589                 }
590         }
591         my $parent_pipe;
592         if ($worker_processes > 0) {
593                 $refresh->(); # preload by default
594                 my $fh = master_loop(); # returns if in child process
595                 $parent_pipe = PublicInbox::ParentPipe->new($fh, *worker_quit);
596         } else {
597                 reopen_logs();
598                 $set_user->() if $set_user;
599                 $SIG{USR2} = sub { worker_quit() if upgrade() };
600                 $refresh->();
601         }
602         $uid = $gid = undef;
603         reopen_logs();
604         $SIG{QUIT} = $SIG{INT} = $SIG{TERM} = *worker_quit;
605         $SIG{USR1} = *reopen_logs;
606         $SIG{HUP} = $refresh;
607         $SIG{CHLD} = 'DEFAULT';
608         $SIG{$_} = 'IGNORE' for qw(USR2 TTIN TTOU WINCH);
609         @listeners = map {;
610                 my $tls_cb = $post_accept{sockname($_)};
611
612                 # NNTPS, HTTPS, HTTP, and POP3S are client-first traffic
613                 # NNTP and POP3 are server-first
614                 defer_accept($_, $tls_cb ? 'dataready' : $af_default);
615
616                 # this calls epoll_create:
617                 PublicInbox::Listener->new($_, $tls_cb || $post_accept)
618         } @listeners;
619         PublicInbox::DS->EventLoop;
620         $parent_pipe = undef;
621 }
622
623
624 sub run ($$$;$) {
625         my ($default, $refresh, $post_accept, $nntpd) = @_;
626         daemon_prepare($default);
627         my $af_default = $default =~ /:8080\z/ ? 'httpready' : undef;
628         daemonize();
629         daemon_loop($refresh, $post_accept, $nntpd, $af_default);
630 }
631
632 sub do_chown ($) {
633         my ($path) = @_;
634         if (defined $uid and !chown($uid, $gid, $path)) {
635                 warn "could not chown $path: $!\n";
636         }
637 }
638
639 sub write_pid ($) {
640         my ($path) = @_;
641         Net::Server::Daemonize::create_pid_file($path);
642         do_chown($path);
643 }
644
645 1;