Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct check for failed forks #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/Daemon/Control.pm
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,14 @@ sub _double_fork {
my $pid = fork();

$self->trace( "_double_fork()" );
if ( $pid == 0 ) { # Child, launch the process here.
if ( not defined $pid ) { # We couldn't fork. =(
die "Cannot fork: $!";
} elsif ( $pid == 0 ) { # Child, launch the process here.
setsid(); # Become the process leader.
my $new_pid = fork();
if ( $new_pid == 0 ) { # Our double fork.
if ( not defined $new_pid ) {
die "Cannot fork: $!";
} elsif ( $new_pid == 0 ) { # Our double fork.

if ( $self->gid ) {
setgid( $self->gid );
Expand Down Expand Up @@ -221,16 +225,12 @@ sub _double_fork {
}

$self->_launch_program;
} elsif ( not defined $new_pid ) {
warn "Cannot fork: $!";
} else {
$self->pid( $new_pid );
$self->trace("Set PID => $new_pid" );
$self->write_pid;
_exit 0;
}
} elsif ( not defined $pid ) { # We couldn't fork. =(
warn "Cannot fork: $!";
} else { # In the parent, $pid = child's PID, return it.
waitpid( $pid, 0 );
}
Expand All @@ -244,10 +244,10 @@ sub _fork {
my $pid = fork();

$self->trace( "_fork()" );
if ( $pid == 0 ) { # Child, launch the process here.
if ( not defined $pid ) {
die "Cannot fork: $!";
} elsif ( $pid == 0 ) { # Child, launch the process here.
$self->_launch_program;
} elsif ( not defined $pid ) {
warn "Cannot fork: $!";
} else { # In the parent, $pid = child's PID, return it.
$self->pid( $pid );
$self->trace("Set PID => $pid" );
Expand Down
40 changes: 40 additions & 0 deletions t/05_fork_fail.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/perl
use warnings;
use strict;

use Errno qw(EAGAIN);
use File::Temp qw(tempfile);
use Test::More;

# Simulate failing forks in the Daemon::Control package only
BEGIN {
*CORE::GLOBAL::fork = sub {
if ((caller)[0] eq 'Daemon::Control') {
$! = EAGAIN;
undef;
} else {
CORE::fork;
}
};
}

my(undef,$pidfile) = tempfile(SUFFIX => '_Daemon_Control.pid', UNLINK => 1);
my(undef,$outfile) = tempfile(SUFFIX => '_Daemon_Control.txt', UNLINK => 1);
unlink $outfile;

use_ok 'Daemon::Control';

eval {
Daemon::Control->new(
name => "failing_fork_test",
program => "/bin/sh",
program_args => ['-c', "echo this should not happen > $outfile"],
pid_file => $pidfile,
fork => 1,
)->run_command('start');
};
like $@, qr{Cannot fork};

ok !-e $outfile, 'daemon was not called';

done_testing;