From 9e8599d63f4a120a15b4c1ffe31753ed423b5345 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Fri, 2 Dec 2016 21:00:43 -0800 Subject: [PATCH] taskd.pl: support a --daemon option With the --daemon option fork and become a background daemon. Implies --syslog and --quiet. Also allow a --no-same-pid option (the opposite of --same-pid) and now default --same-pid to on whenever --daemon is NOT used. Signed-off-by: Kyle J. McKay --- taskd/taskd.pl | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/taskd/taskd.pl b/taskd/taskd.pl index d14b064..a676fe0 100755 --- a/taskd/taskd.pl +++ b/taskd/taskd.pl @@ -38,6 +38,7 @@ use Errno; use Fcntl; use POSIX ":sys_wait_h"; use File::Basename; +use File::Spec (); use Cwd qw(realpath); use lib dirname($0); @@ -82,6 +83,7 @@ my $syslog; my $stderr; my $inetd; my $idle_timeout; +my $daemon; my $max_lifetime; my $abbrev = 8; my $showff = 1; @@ -1155,6 +1157,7 @@ my $parse_res = GetOptions( 'progress|P' => \$progress, 'inetd|i' => sub {$inetd = 1; $syslog = 1; $quiet = 1;}, 'idle-timeout|t=i' => \$idle_timeout, + 'daemon' => sub {$daemon = 1; $syslog = 1; $quiet = 1;}, 'max-lifetime=i' => \$max_lifetime, 'syslog|s:s' => \$sfac, 'no-syslog' => sub {$syslog = 0; $sfac = undef;}, @@ -1163,12 +1166,15 @@ my $parse_res = GetOptions( 'show-fast-forward-info' => \$showff, 'no-show-fast-forward-info' => sub {$showff = 0}, 'same-pid' => \$same_pid, + 'no-same-pid' => sub {$same_pid = 0}, 'status-interval=i' => \$stiv, 'idle-status-interval=i' => \$idiv, ) || pod2usage(2); +$same_pid = !$daemon unless defined($same_pid); $syslog = 1 if defined($sfac); $progress = 1 unless $quiet; $abbrev = 128 unless $abbrev > 0; +pod2usage(-msg => "--inetd and --daemon are incompatible") if ($inetd && $daemon); if (defined($idle_timeout)) { die "--idle-timeout must be a whole number\n" unless $idle_timeout =~ /^\d+$/; die "--idle-timeout may not be used without --inetd\n" unless $inetd; @@ -1187,6 +1193,7 @@ if (defined($idiv)) { $idleintv = $idiv * 60; } +open STDIN, '<'.File::Spec->devnull or die "could not redirect STDIN to /dev/null\n" unless $inetd; open STDOUT, '>&STDERR' if $inetd; if ($syslog) { use Sys::Syslog qw(); @@ -1334,6 +1341,7 @@ if ($restart_active) { statmsg "restart file disabled could not unlink \"$restart_file\": $!"; } } +daemon(1, 1) or die "failed to daemonize: $!\n" if $daemon; my $starttime = time; my $endtime = $max_lifetime ? $starttime + $max_lifetime : 0; statmsg "listening on $NAME"; @@ -1520,6 +1528,8 @@ taskd.pl [options] implies --quiet --syslog -t SECONDS | --idle-timeout=SECONDS how long to wait idle before exiting requires --inetd + --daemon become a background daemon + implies --quiet --syslog --max-lifetime=SECONDS how long before graceful restart default is 1 week, 0 disables -s | --syslog[=facility] send messages to syslog instead of @@ -1531,6 +1541,7 @@ taskd.pl [options] --show-fast-forward-info show fast-forward info (default is on) --no-show-fast-forward-info disable showing fast-forward info --same-pid keep same pid during graceful restart + --no-same-pid do not keep same pid on graceful rstrt --status-interval=MINUTES status update interval (default 1) --idle-status-interval=IDLEMINUTES idle status interval (default 60) @@ -1586,6 +1597,16 @@ Only permitted when running in --inetd mode. After SECONDS of inactivity exit normally. The default is no timeout at all (a SECONDS value of 0). Note that it may actually take up to SECONDS+60 for the idle exit to occur. +=item B<--daemon> + +Fork and become a background daemon. Implies B<--syslog> and B<--quiet> (which +can be altered by subsequent B<--no-syslog> and/or B<--no-quiet> options). +Also implies B<--no-same-pid>, but since graceful restarts work by re-exec'ing +taskd.pl with all of its original arguments, using B<--same-pid> won't really +be effective with B<--daemon> since although it will cause the graceful restart +exec to happen from the same pid, when the B<--daemon> option is subsequently +processed it will end up in a new pid anyway. + =item B<--max-lifetime=SECONDS> After taskd has been running for SECONDS of realtime, it will behave as though @@ -1633,8 +1654,14 @@ messages. Instead just show a ' -> ' indicator. =item B<--same-pid> -When performing a graceful restart, keep the same pid rather than switching to -a new one. +When performing a graceful restart, perform the graceful restart exec from +the same pid rather than switching to a new one. This is implied when +I<--daemon> is I used. + +=item B<--no-same-pid> + +When performing a graceful restart, perform the graceful restart exec after +switching to a new pid. This is implied when I<--daemon> I used. =item B<--status-interval=MINUTES> -- 2.11.4.GIT