App-CLI-Plugin-Daemonize

 view release on metacpan or  search on metacpan

inc/Module/Install.pm  view on Meta::CPAN

# is unreliable on some platforms and requires write permissions)
# for now we should catch this and refuse to run.
if ( -f $0 ) {
	my $s = (stat($0))[9];

	# If the modification time is only slightly in the future,
	# sleep briefly to remove the problem.
	my $a = $s - time;
	if ( $a > 0 and $a < 5 ) { sleep 5 }

	# Too far in the future, throw an error.
	my $t = time;
	if ( $s > $t ) { die <<"END_DIE" }

Your installer $0 has a modification time in the future ($s > $t).

This is known to create infinite loops in make.

Please correct this, then run $0 again.

END_DIE

lib/App/CLI/Plugin/Daemonize.pm  view on Meta::CPAN

}

sub daemonize {

	my $self = shift;

	my $devnull = File::Spec->devnull;

	# detach parent process
	$SIG{CHLD} = 'IGNORE';
	defined(my $pid = fork) or $self->throw("can not fork. $!");
	if ($pid < 0) {
		$self->throw("cat not fork. pid:$pid");
	}
	if ($pid) {
		exit;
	}

	# change umask
	umask 0;

	# pgrp and session leader
	my $sid = POSIX::setsid;
	if($sid < 0) {
		$self->throw("can not setsid. sid:$sid");
	}

	# chdir /
	chdir "/" or $self->throw("can not chdir /. $!");

	open STDIN, "<", $devnull  or $self->throw("can not open STDIN");
	open STDOUT, ">", $devnull or $self->throw("can not open STDOUT");
	open STDERR, ">&STDOUT"    or $self->throw("can not open STDERR");
}

1;

__END__

=head1 AUTHOR

Akira Horimoto



( run in 0.353 second using v1.01-cache-2.11-cpan-496ff517765 )