| 1 | package Script::Daemonizer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use POSIX qw(:signal_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Fcntl qw/:DEFAULT :flock/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use FindBin (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use File::Basename (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | $Script::Daemonizer::VERSION = '1.01.01'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 16 | # 'Private' vars | ||||||||||||||||||||||||||||
| 17 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 18 | 4 | my @argv_copy; | |||||||||||||||||||||||||||
| 19 | 4 | my $devnull = File::Spec->devnull; | |||||||||||||||||||||||||||
| 20 | 4 | my @daemon_options = ( qw{ | |||||||||||||||||||||||||||
| 21 | chdir | ||||||||||||||||||||||||||||
| 22 | do_not_tie_stdhandles | ||||||||||||||||||||||||||||
| 23 | drop_privileges | ||||||||||||||||||||||||||||
| 24 | output_file | ||||||||||||||||||||||||||||
| 25 | pidfile | ||||||||||||||||||||||||||||
| 26 | restart_on | ||||||||||||||||||||||||||||
| 27 | setsid | ||||||||||||||||||||||||||||
| 28 | sigunmask | ||||||||||||||||||||||||||||
| 29 | stdout_file | ||||||||||||||||||||||||||||
| 30 | stderr_file | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | _DEBUG | ||||||||||||||||||||||||||||
| 33 | } ); | ||||||||||||||||||||||||||||
| 34 | 4 | my %id_map = ( | |||||||||||||||||||||||||||
| 35 | user => 'uid', | ||||||||||||||||||||||||||||
| 36 | group => 'gid', | ||||||||||||||||||||||||||||
| 37 | euser => 'euid', | ||||||||||||||||||||||||||||
| 38 | egroup => 'egid', | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | 4 | my $global_pidfh; | |||||||||||||||||||||||||||
| 41 | 4 | my %defaults = ( | |||||||||||||||||||||||||||
| 42 | working_dir => File::Spec->rootdir(), | ||||||||||||||||||||||||||||
| 43 | umask => 0, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | ################################################################################ | ||||||||||||||||||||||||||||
| 49 | # SAVING @ARGV for restart() | ||||||||||||||||||||||||||||
| 50 | ################################################################################ | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | # restart() needs the exact list of arguments in order to relaunch the script, | ||||||||||||||||||||||||||||
| 53 | # if requested. | ||||||||||||||||||||||||||||
| 54 | # User is free to shift(@ARGV) and/or modify it in any way, we ensure we always | ||||||||||||||||||||||||||||
| 55 | # get the "real" args (unless someone takes some extra effort to modify them | ||||||||||||||||||||||||||||
| 56 | # before we get here). | ||||||||||||||||||||||||||||
| 57 | # restart() gets an array of args, thoug, so there is no need to tamper with | ||||||||||||||||||||||||||||
| 58 | # this: | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 4 | @argv_copy = @ARGV; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ################################################################################ | ||||||||||||||||||||||||||||
| 65 | # HANDLING SIGHUP | ||||||||||||||||||||||||||||
| 66 | ################################################################################ | ||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | # When the script restarts itself upon receiving SIGHUP, that signal is masked. | ||||||||||||||||||||||||||||
| 69 | # When starting, we unmask the signals so that they do not stop working for us. | ||||||||||||||||||||||||||||
| 70 | # We do this regardless of how we were launched. | ||||||||||||||||||||||||||||
| 71 | # | ||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 4 | my $sigset = POSIX::SigSet->new( SIGHUP ); # Just handle HUP | |||||||||||||||||||||||||||
| 74 | 4 | sigprocmask(SIG_UNBLOCK, $sigset); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | ################################################################################ | ||||||||||||||||||||||||||||
| 80 | # HANDLING IMPORT TAGS | ||||||||||||||||||||||||||||
| 81 | ################################################################################ | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 4 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 84 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 85 | 4 | for my $opt (@_) { | |||||||||||||||||||||||||||
| 86 | 2 | ▶ | if ($opt eq ':NOCHDIR') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 87 | 1 | delete $defaults{working_dir}; | |||||||||||||||||||||||||||
| 88 | } elsif ($opt eq ':NOUMASK') { | ||||||||||||||||||||||||||||
| 89 | 1 | delete $defaults{umask}; | |||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 0 | croak "Unknown tag: $opt"; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 99 | # 'Private' functions | ||||||||||||||||||||||||||||
| 100 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | ################ | ||||||||||||||||||||||||||||
| 103 | # sub _debug() # | ||||||||||||||||||||||||||||
| 104 | ################ | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 5 | ▶ | sub _debug { | ||||||||||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 107 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | print @_, "\n" | ||||||||||||||||||||||||||||
| 109 | 5 | ▶ | if $self->{_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ################## | ||||||||||||||||||||||||||||
| 114 | # sub _set_umask # | ||||||||||||||||||||||||||||
| 115 | ################## | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | sub _set_umask { | ||||||||||||||||||||||||||
|
Subroutine
_set_umask: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 118 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | defined(umask($self->{umask})) or | ||||||||||||||||||||||||||||
| 120 | 2 | ▶ | croak qq(Cannot set umask to "), $self->{umask}, qq(": $!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | ############### | ||||||||||||||||||||||||||||
| 124 | # sub _fork # | ||||||||||||||||||||||||||||
| 125 | ############### | ||||||||||||||||||||||||||||
| 126 | # fork() a child | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub _fork { | ||||||||||||||||||||||||||
|
Subroutine
_fork: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 128 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return unless $self->{fork}; # Just in case, but already checked when | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | # _fork() is called | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # See http://code.activestate.com/recipes/278731/ or the source of | ||||||||||||||||||||||||||||
| 134 | # Proc::Daemon for a discussion on ignoring SIGHUP. | ||||||||||||||||||||||||||||
| 135 | # Since ignoring it across the fork() should not be harmful, I prefer to set | ||||||||||||||||||||||||||||
| 136 | # this to IGNORE anyway. | ||||||||||||||||||||||||||||
| 137 | 0 | local $SIG{'HUP'} = 'IGNORE'; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | defined(my $pid = fork()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | or croak "Cannot fork: $!"; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | exit 0 if $pid; # parent exits here | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | $self->{fork}--; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | $self->_debug("Forked, remaining forks: ", $self->{fork}); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ############### | ||||||||||||||||||||||||||||
| 151 | # sub _setsid # | ||||||||||||||||||||||||||||
| 152 | ############### | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | ▶ | sub _setsid { | ||||||||||||||||||||||||||
|
Subroutine
_setsid: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||
| 155 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | return if | ||||||||||||||||||||||||||||
| 157 | 1 | ▶ | ( exists $self->{ setsid } && $self->{ setsid } eq 'SKIP' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"setsid"} and $self->{"setsid"} eq "SKIP"
MC/DC0%0/2
exists $self->{"setsid"} and $self->{"setsid"} eq "SKIP"
exists $self->{"setsid"}
$self->{"setsid"} eq "SKIP"
Truth table33%1/3
A and B
A= exists $self->{"setsid"}B= $self->{"setsid"} eq "SKIP"
| |||||||||||||||||||||||||||||
| 158 | 1 | ▶ | POSIX::setsid() or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | croak "Unable to set session id: $!"; | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | ######################### | ||||||||||||||||||||||||||||
| 163 | # sub _write_pidfile # | ||||||||||||||||||||||||||||
| 164 | ######################### | ||||||||||||||||||||||||||||
| 165 | # Open the pidfile (creating it if necessary), then lock it, then truncate it, | ||||||||||||||||||||||||||||
| 166 | # then write pid into it. Then retun filehandle. | ||||||||||||||||||||||||||||
| 167 | # If environment variable $_pidfile_fileno is set, then we assume we're product | ||||||||||||||||||||||||||||
| 168 | # of an exec() and take that file descriptor as the (already opened) pidfile. | ||||||||||||||||||||||||||||
| 169 | 2 | ▶ | sub _write_pidfile { | ||||||||||||||||||||||||||
|
Subroutine
_write_pidfile: called CC=8 SCAR=25.8
| |||||||||||||||||||||||||||||
| 170 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 171 | 2 | my $pidfile = $self->{pidfile}; | |||||||||||||||||||||||||||
| 172 | 2 | my $fh; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # First we must see if there is a _pidfile_fileno variable in environment; | ||||||||||||||||||||||||||||
| 175 | # that means that we were started by an exec() and we must keep the same | ||||||||||||||||||||||||||||
| 176 | # pidfile as before | ||||||||||||||||||||||||||||
| 177 | 2 | my $pidfd = delete $ENV{_pidfile_fileno}; | |||||||||||||||||||||||||||
| 178 | 2 | ▶ | if (defined $pidfd && $pidfd =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $pidfd and $pidfd =~ /^\d+$/
MC/DC0%0/2
defined $pidfd and $pidfd =~ /^\d+$/
defined $pidfd
$pidfd =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $pidfdB= $pidfd =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 179 | 0 | $self->_debug("Reopening pidfile from file descriptor"); | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | open($fh, ">&=$pidfd") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | or croak "can't open fd $pidfd: $!"; | ||||||||||||||||||||||||||||
| 182 | # Re-set close-on-exec bit for pidfile filehandle | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | fcntl($fh, F_SETFD, 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | or die "Can't set close-on-exec flag on pidfile filehandle: $!\n"; | ||||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||
| 186 | 2 | $self->_debug("Opening a new pid file"); | |||||||||||||||||||||||||||
| 187 | # Open configured pidfile | ||||||||||||||||||||||||||||
| 188 | 2 | ▶ | sysopen($fh, $pidfile, O_RDWR | O_CREAT) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | or croak "can't open $pidfile: $!"; | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 2 | ▶ | flock($fh, LOCK_EX|LOCK_NB) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | or croak "can't lock $pidfile: $! - is another instance running?"; | ||||||||||||||||||||||||||||
| 193 | 2 | ▶ | truncate($fh, 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | or croak "can't truncate $pidfile: $!"; | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | select((select( $fh ), ++$|)[0]); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 2 | print $fh $$; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # Save it as a global so that in short init syntax | ||||||||||||||||||||||||||||
| 201 | # Script::Daemonizer->new( pidfile => $pfile )->daemonize; | ||||||||||||||||||||||||||||
| 202 | # it stays in scope | ||||||||||||||||||||||||||||
| 203 | 2 | return $global_pidfh = $self->{pidfh} = $fh; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | ############## | ||||||||||||||||||||||||||||
| 208 | # sub _chdir # | ||||||||||||||||||||||||||||
| 209 | ############## | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 1 | ▶ | sub _chdir { | ||||||||||||||||||||||||||
|
Subroutine
_chdir: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 212 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 213 | chdir($self->{'working_dir'}) or | ||||||||||||||||||||||||||||
| 214 | 1 | ▶ | croak "Cannot change directory to ", $self->{'working_dir'}, ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | ################# | ||||||||||||||||||||||||||||
| 219 | # sub _close # | ||||||||||||||||||||||||||||
| 220 | ################# | ||||||||||||||||||||||||||||
| 221 | # Handle closing of STDOUT/STDERR | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub _close { | ||||||||||||||||||||||||||
|
Subroutine
_close: not called CC=2 SCAR=11.6
| |||||||||||||||||||||||||||||
| 223 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 224 | 0 | my $fh = shift; | |||||||||||||||||||||||||||
| 225 | # Have to lookup handles by name | ||||||||||||||||||||||||||||
| 226 | 0 | $self->_debug("Closing $fh"); | |||||||||||||||||||||||||||
| 227 | 4 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | open *$fh, '>', $devnull | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | or croak "Unable to open $fh on $devnull: $!"; | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | ################# | ||||||||||||||||||||||||||||
| 234 | # sub _redirect # | ||||||||||||||||||||||||||||
| 235 | ################# | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 2 | ▶ | sub _redirect { | ||||||||||||||||||||||||||
|
Subroutine
_redirect: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 238 | 2 | my ( $self, $fh, $destination ) = @_; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 2 | ▶ | $destination = $devnull | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | if $destination eq '/dev/null'; | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 2 | $self->_debug("Redirecting $fh on: $destination ", $destination); | |||||||||||||||||||||||||||
| 244 | 4 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 245 | 2 | ▶ | open *$fh, '>>', $destination | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | or croak "Unable to open $fh on $destination: $!"; | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | ########################## | ||||||||||||||||||||||||||||
| 251 | # sub _manage_stdhandles # | ||||||||||||||||||||||||||||
| 252 | ########################## | ||||||||||||||||||||||||||||
| 253 | 1 | ▶ | sub _manage_stdhandles { | ||||||||||||||||||||||||||
|
Subroutine
_manage_stdhandles: called CC=11 SCAR=43.3
| |||||||||||||||||||||||||||||
| 254 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 1 | ▶ | open STDIN, '<', $devnull | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | or croak "Cannot reopen STDIN on $devnull: $!"; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # If we were requested to redirect output on a file, do it now and return | ||||||||||||||||||||||||||||
| 260 | 1 | ▶ | if ($self->{output_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 1 | $self->_debug("Using output file"); | |||||||||||||||||||||||||||
| 262 | 1 | $self->_redirect( $_, $self->{output_file}) for (qw{STDOUT STDERR}); | |||||||||||||||||||||||||||
| 263 | 1 | return 1; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # Use Tie::Syslog unless both stdout/stderr redirected to file | ||||||||||||||||||||||||||||
| 267 | 0 | ▶ | unless ($self->{stdout_file} && $self->{stderr_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"stdout_file"} and $self->{"stderr_file"}
MC/DC0%0/2
$self->{"stdout_file"} and $self->{"stderr_file"}
$self->{"stdout_file"}
$self->{"stderr_file"}
Truth table0%0/3
A and B
A= $self->{"stdout_file"}B= $self->{"stderr_file"}
| |||||||||||||||||||||||||||||
| 268 | 0 | $self->_debug("Using Tie::Syslog"); | |||||||||||||||||||||||||||
| 269 | 0 | eval { | |||||||||||||||||||||||||||
| 270 | 0 | require Tie::Syslog; | |||||||||||||||||||||||||||
| 271 | }; | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | carp "Unable to load Tie::Syslog module. Error is:\n----\n$@----\nI will continue without output" | ||||||||||||||||||||||||||||
| 275 | 0 | ▶ | if $self->{_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 0 | $self->_close( $_ ) for (qw{STDOUT STDERR}); | |||||||||||||||||||||||||||
| 277 | 0 | return 0; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 0 | $Tie::Syslog::ident = $self->{name}; | |||||||||||||||||||||||||||
| 281 | 0 | $Tie::Syslog::logopt = 'ndelay,pid'; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | # STDOUT | ||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ($self->{stdout_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | $self->_redirect( 'STDOUT', $self->{stdout_file} ); | |||||||||||||||||||||||||||
| 287 | } else { | ||||||||||||||||||||||||||||
| 288 | 0 | $self->_close( 'STDOUT' ); | |||||||||||||||||||||||||||
| 289 | 0 | $self->_debug("Tying STDOUT to Tie::Syslog"); | |||||||||||||||||||||||||||
| 290 | 0 | tie *STDOUT, 'Tie::Syslog', { | |||||||||||||||||||||||||||
| 291 | facility => 'LOG_DAEMON', | ||||||||||||||||||||||||||||
| 292 | priority => 'LOG_INFO', | ||||||||||||||||||||||||||||
| 293 | }; | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # STDERR | ||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if ($self->{stderr_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | $self->_redirect( 'STDERR', $self->{stderr_file} ); | |||||||||||||||||||||||||||
| 299 | } else { | ||||||||||||||||||||||||||||
| 300 | 0 | $self->_close( 'STDERR' ); | |||||||||||||||||||||||||||
| 301 | 0 | $self->_debug("Tying STDERR to Tie::Syslog"); | |||||||||||||||||||||||||||
| 302 | 0 | tie *STDERR, 'Tie::Syslog', { | |||||||||||||||||||||||||||
| 303 | facility => 'LOG_DAEMON', | ||||||||||||||||||||||||||||
| 304 | priority => 'LOG_ERR', | ||||||||||||||||||||||||||||
| 305 | }; | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | ######################## | ||||||||||||||||||||||||||||
| 311 | # sub _get_signal_list # | ||||||||||||||||||||||||||||
| 312 | ######################## | ||||||||||||||||||||||||||||
| 313 | 0 | ▶ | sub _get_signal_list { | ||||||||||||||||||||||||||
|
Subroutine
_get_signal_list: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 314 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 319 | # 'Public' functions | ||||||||||||||||||||||||||||
| 320 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 6 | ▶ | sub drop_privileges { | ||||||||||||||||||||||||||
|
Subroutine
drop_privileges: called CC=17 SCAR=35.5
Pod
documented
| |||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # Check parameters: | ||||||||||||||||||||||||||||
| 327 | 6 | ▶ | croak "Odd number of arguments in drop_privileges() call!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | if @_ % 2; | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | # Get parameters | ||||||||||||||||||||||||||||
| 331 | 6 | ▶ | my %ids = @_ ? @_ : %{ $self->{drop_privileges} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # Resolve user name to user id if given | ||||||||||||||||||||||||||||
| 334 | 6 | for (qw{ user euser }) { | |||||||||||||||||||||||||||
| 335 | 12 | ▶ | defined( my $us = delete $ids{ $_ } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | or next; | ||||||||||||||||||||||||||||
| 337 | 4 | ▶ | defined ( $ids{ $id_map{ $_ } } = getpwnam( $us ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | or croak "No such user: $us"; | ||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | # Resolve group name to group id if given | ||||||||||||||||||||||||||||
| 342 | 6 | for (qw{ group egroup }) { | |||||||||||||||||||||||||||
| 343 | 12 | ▶ | defined( my $gr = delete $ids{ $_ } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | or next; | ||||||||||||||||||||||||||||
| 345 | 4 | ▶ | defined ( $ids{ $id_map{ $_ } } = getgrnam( $gr ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | or croak "No such group: $gr"; | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # Get ids | ||||||||||||||||||||||||||||
| 350 | 6 | my ($euid, $egid, $uid, $gid) = @ids{qw(euid egid uid gid)}; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | # Drop GROUP ID | ||||||||||||||||||||||||||||
| 353 | 6 | ▶ | if (defined $gid) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 354 | 6 | ▶ | POSIX::setgid((split " ", $gid)[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | or croak "POSIX::setgid() failed: $!"; | ||||||||||||||||||||||||||||
| 356 | } elsif (defined $egid) { | ||||||||||||||||||||||||||||
| 357 | # $egid might be a list | ||||||||||||||||||||||||||||
| 358 | 0 | $) = $egid; | |||||||||||||||||||||||||||
| 359 | 0 | ▶ | croak "Cannot drop effective group id to $egid: $!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | if $!; | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # Drop USER ID | ||||||||||||||||||||||||||||
| 364 | 6 | ▶ | if (defined $uid) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 365 | 6 | ▶ | POSIX::setuid($uid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | or croak "POSIX::setuid() failed: $!"; | ||||||||||||||||||||||||||||
| 367 | } elsif (defined $euid) { | ||||||||||||||||||||||||||||
| 368 | # Drop EUID too, unless explicitly forced to something else | ||||||||||||||||||||||||||||
| 369 | 0 | $> = $euid; | |||||||||||||||||||||||||||
| 370 | 0 | ▶ | croak "Cannot drop effective user id to $uid: $!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | if $!; | ||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 6 | return 1; | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 15 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=17 SCAR=36.1
Pod
documented
| |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 15 | my $pkg = shift; | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 15 | ▶ | croak ("This is a class method!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | if ref($pkg); | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 15 | ▶ | croak "Odd number of arguments in configuration!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | if @_ %2; | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 14 | my $self = { | |||||||||||||||||||||||||||
| 389 | %defaults, | ||||||||||||||||||||||||||||
| 390 | }; | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | # Get the configuration | ||||||||||||||||||||||||||||
| 393 | 14 | my %params = @_; | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # Set useful defaults | ||||||||||||||||||||||||||||
| 396 | 14 | ▶ | $self->{name} = delete $params{name} || (File::Spec->splitpath($0))[-1]; | ||||||||||||||||||||||||||
|
Condition66%2/3
delete $params{'name'} || ('File::Spec'->splitpath($0))[-1]
MC/DC0%0/2
delete $params{'name'} || ('File::Spec'->splitpath($0))[-1]
delete $params{'name'}
('File::Spec'->splitpath($0))[-1]
Truth table66%2/3
A || B
A= delete $params{'name'}B= ('File::Spec'->splitpath($0))[-1]
| |||||||||||||||||||||||||||||
| 397 | $self->{fork} = (exists $params{fork} && $params{fork} =~ /^[012]$/) | ||||||||||||||||||||||||||||
| 398 | ? delete $params{fork} | ||||||||||||||||||||||||||||
| 399 | 14 | ▶ | : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $params{"fork"} and $params{"fork"} =~ /^[012]$/
MC/DC0%0/2
exists $params{"fork"} and $params{"fork"} =~ /^[012]$/
exists $params{"fork"}
$params{"fork"} =~ /^[012]$/
Truth table33%1/3
A and B
A= exists $params{"fork"}B= $params{"fork"} =~ /^[012]$/
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 14 | ▶ | $self->{working_dir} = delete $params{working_dir} if $params{working_dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 14 | ▶ | if (exists $params{umask}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | croak "Invalid umask specified: ", $params{umask} | ||||||||||||||||||||||||||||
| 405 | 0 | ▶ | unless $params{umask} =~ /^[0-7]{1,3}$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 0 | $self->{umask} = delete $params{umask}; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | # Get other options as they are: | ||||||||||||||||||||||||||||
| 410 | 14 | for (@daemon_options) { | |||||||||||||||||||||||||||
| 411 | $self->{ $_ } = delete $params{ $_ } | ||||||||||||||||||||||||||||
| 412 | 154 | ▶ | if exists $params{ $_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 14 | my @extra_args = keys %params; | |||||||||||||||||||||||||||
| 416 | { | ||||||||||||||||||||||||||||
| 417 | 14 | local $" = ", "; | |||||||||||||||||||||||||||
| 418 | 14 | ▶ | croak sprintf "Invalid argument(s) passed: @extra_args" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | if @extra_args; | ||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 13 | bless $self, $pkg; | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | # Set up signal handlers | ||||||||||||||||||||||||||||
| 425 | 13 | ▶ | if ($self->{restart_on} && ref $self->{restart_on} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"restart_on"} and ref $self->{"restart_on"} eq "ARRAY"
MC/DC0%0/2
$self->{"restart_on"} and ref $self->{"restart_on"} eq "ARRAY"
$self->{"restart_on"}
ref $self->{"restart_on"} eq "ARRAY"
Truth table33%1/3
A and B
A= $self->{"restart_on"}B= ref $self->{"restart_on"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 426 | 0 | my @sigs = @{ $self->{restart_on} }; | |||||||||||||||||||||||||||
| 427 | 0 | for (@sigs) { | |||||||||||||||||||||||||||
| 428 | 0 | ▶ | $SIG{ $_ } = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 429 | 0 | $self->restart(); | |||||||||||||||||||||||||||
| 430 | 0 | }; | |||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | 0 | $self->sigunmask( @sigs ); | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | # Unmask signals if requested | ||||||||||||||||||||||||||||
| 436 | 13 | ▶ | if ($self->{sigunmask} && ref $self->{sigunmask} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"sigunmask"} and ref $self->{"sigunmask"} eq "ARRAY"
MC/DC0%0/2
$self->{"sigunmask"} and ref $self->{"sigunmask"} eq "ARRAY"
$self->{"sigunmask"}
ref $self->{"sigunmask"} eq "ARRAY"
Truth table33%1/3
A and B
A= $self->{"sigunmask"}B= ref $self->{"sigunmask"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 437 | 0 | $self->sigunmask(@{ $self->{sigunmask} }); | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 13 | return $self; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 0 | ▶ | sub daemonize { | ||||||||||||||||||||||||||
|
Subroutine
daemonize: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 445 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | # Step 0.0 - OPTIONAL: drop privileges | ||||||||||||||||||||||||||||
| 448 | $self->drop_privileges | ||||||||||||||||||||||||||||
| 449 | 0 | ▶ | if $self->{drop_privileges}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | # Step 1. | ||||||||||||||||||||||||||||
| 452 | $self->_set_umask | ||||||||||||||||||||||||||||
| 453 | 0 | ▶ | if exists $self->{umask}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | # Step 2. | ||||||||||||||||||||||||||||
| 456 | $self->_fork() | ||||||||||||||||||||||||||||
| 457 | 0 | ▶ | if $self->{fork}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | # Step 3. | ||||||||||||||||||||||||||||
| 460 | 0 | $self->_setsid(); | |||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | # Step 4. | ||||||||||||||||||||||||||||
| 463 | $self->_fork() | ||||||||||||||||||||||||||||
| 464 | 0 | ▶ | if $self->{fork}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | # Step 4.5 - OPTIONAL: take a lock on pidfile | ||||||||||||||||||||||||||||
| 467 | # (and write pid into it) | ||||||||||||||||||||||||||||
| 468 | $self->_write_pidfile() | ||||||||||||||||||||||||||||
| 469 | 0 | ▶ | if $self->{pidfile}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | # Step 5. | ||||||||||||||||||||||||||||
| 472 | $self->_chdir() | ||||||||||||||||||||||||||||
| 473 | 0 | ▶ | if $self->{working_dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | # Step 6. | ||||||||||||||||||||||||||||
| 477 | # REMOVED! | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | # Step 7. | ||||||||||||||||||||||||||||
| 481 | 0 | $self->_manage_stdhandles(); | |||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 0 | return 1; | |||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 0 | ▶ | sub restart { | ||||||||||||||||||||||||||
|
Subroutine
restart: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | 0 | ▶ | my @args = @_ ? @_ : @argv_copy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | # See perlipc | ||||||||||||||||||||||||||||
| 494 | # make the daemon cross-platform, so exec always calls the script | ||||||||||||||||||||||||||||
| 495 | # itself with the right path, no matter how the script was invoked. | ||||||||||||||||||||||||||||
| 496 | 0 | my $script = File::Basename::basename($0); | |||||||||||||||||||||||||||
| 497 | 0 | my $SELF = File::Spec->catfile($FindBin::Bin, $script); | |||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | # $pidf must be kept open across exec() if we don't want race conditions: | ||||||||||||||||||||||||||||
| 500 | 0 | ▶ | if (my $pidfh = $self->{pidfh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | 0 | $self->_debug("Keeping current pidfile open"); | |||||||||||||||||||||||||||
| 502 | # Clear close-on-exec bit for pidfile filehandle | ||||||||||||||||||||||||||||
| 503 | 0 | ▶ | fcntl($pidfh, F_SETFD, 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | or die "Can't clear close-on-exec flag on pidfile filehandle: $!\n"; | ||||||||||||||||||||||||||||
| 505 | # Now we must notify ourseves that pidfile is already open | ||||||||||||||||||||||||||||
| 506 | 0 | $ENV{_pidfile_fileno} = fileno( $pidfh ); | |||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | 0 | ▶ | exec($SELF, @args) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | or croak "$0: couldn't restart: $!"; | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | # Bye default, we unmask SIGHUP but, if other signals must be unmasked too, | ||||||||||||||||||||||||||||
| 515 | # then use this and pass in a list of signals to be unmasked. | ||||||||||||||||||||||||||||
| 516 | 0 | ▶ | sub sigunmask { | ||||||||||||||||||||||||||
|
Subroutine
sigunmask: not called CC=2 SCAR=13.7
Pod
documented
| |||||||||||||||||||||||||||||
| 517 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 518 | 0 | ▶ | croak "sigunmask called without arguments" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 519 | unless @_; | ||||||||||||||||||||||||||||
| 520 | 4 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 521 | # Have to convert manually signal names into numbers. I remove the prefix | ||||||||||||||||||||||||||||
| 522 | # POSIX::[SIG] from signal name and add it back again, this allows user to | ||||||||||||||||||||||||||||
| 523 | # refer to signals in any way, for example: | ||||||||||||||||||||||||||||
| 524 | # QUIT | ||||||||||||||||||||||||||||
| 525 | # SIGQUIT | ||||||||||||||||||||||||||||
| 526 | # POSIX::QUIT | ||||||||||||||||||||||||||||
| 527 | # POSIX::SIGQUIT | ||||||||||||||||||||||||||||
| 528 | my @sigs = map { | ||||||||||||||||||||||||||||
| 529 | 0 | ( my $signal = $_ ) =~ s/^POSIX:://; | |||||||||||||||||||||||||||
| 530 | 0 | $signal =~ s/^SIG//; | |||||||||||||||||||||||||||
| 531 | 0 | $signal = "POSIX::SIG".$signal; | |||||||||||||||||||||||||||
| 532 | 0 | &$signal | |||||||||||||||||||||||||||
| 533 | } @_; | ||||||||||||||||||||||||||||
| 534 | 0 | my $sigset = POSIX::SigSet->new( @sigs ); # Handle all given signals | |||||||||||||||||||||||||||
| 535 | 0 | sigprocmask(SIG_UNBLOCK, $sigset); | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 4 | 'End of Script::Daemonizer' | |||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | __END__ | ||||||||||||||||||||||||||||