Skip to content

Commit

Permalink
Double-fork for proper daemonization.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Dec 30, 2010
1 parent db49fa9 commit d8967d9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions IcculusNews_daemon.pl
Expand Up @@ -33,7 +33,7 @@
use HTML::Entities;

# Version of IcculusNews. Change this if you are forking the code.
my $version = "2.0.3";
my $version = "2.0.4";


# Global rights constants.
Expand Down Expand Up @@ -1627,13 +1627,18 @@ sub syslog_and_die {

sub go_to_background {
use POSIX 'setsid';
chdir('/') or syslog_and_die("Can't chdir to '/': $!");
open STDIN,'/dev/null' or syslog_and_die("Can't read '/dev/null': $!");
open STDOUT,'>/dev/null' or syslog_and_die("Can't write '/dev/null': $!");
defined(my $pid=fork()) or syslog_and_die("Can't fork: $!");
# fork once, so launching process regains control.
defined(my $pid=fork) or syslog_and_die("Can't fork: $!");
exit if $pid;
# become session group leader, so we have no controlling terminal.
setsid or syslog_and_die("Can't start new session: $!");
# fork again; group leader (and chance of controlling terminal) vanishes.
defined($pid=fork) or syslog_and_die("Can't fork: $!");
exit if $pid;
open STDERR,'>&STDOUT' or syslog_and_die("Can't duplicate stdout: $!");
chdir('/') or syslog_and_die("Can't chdir to '/': $!");
do_log(syslogDaemon, "Daemon process is now detached");
}

Expand Down

0 comments on commit d8967d9

Please sign in to comment.