Commit 6c99d508 authored by sikeda's avatar sikeda
Browse files

[dev] Introducing daemonize() method and {detached} attribute to Sympa::Process.


git-svn-id: https://subversion.renater.fr/sympa/branches/sympa-6.2-branch@12518 05aa8bb8-cd2b-0410-b1d7-8918dfa770ce
parent 2002f42d
......@@ -47,6 +47,7 @@ sub _new_instance {
bless {
children => {},
detached => 0,
generation => 0,
} => $class;
}
......@@ -58,9 +59,38 @@ sub init {
foreach my $key (sort keys %options) {
$self->{$key} = $options{$key};
}
$self->{name} ||= $self->{pidname} || [split /\//, $PROGRAM_NAME]->[-1];
$self;
}
# Put ourselves in background. That method works on many systems, although,
# it seems that Unix conceptors have decided that there won't be a single and
# easy way to detach a process from its controlling TTY.
sub daemonize {
my $self = shift;
if (open my $tty, '/dev/tty') {
ioctl $tty, 0x20007471, 0; # XXX s/b TIOCNOTTY()
close $tty;
}
open STDIN, '<', '/dev/null';
open STDOUT, '>>', '/dev/null';
open STDERR, '>>', '/dev/null';
setpgrp 0, 0;
my $child_pid = CORE::fork();
if ($child_pid) {
$log->syslog('notice', 'Starting %s daemon, PID %d',
$self->{name}, $child_pid);
exit 0;
} elsif (not defined $child_pid) {
die sprintf 'Cannot fork %s daemon: %s', $self->{name}, $ERRNO;
} else {
$self->{detached} = 1;
}
}
sub fork {
my $self = shift;
my $tag = shift || [split /\//, $PROGRAM_NAME]->[-1];
......@@ -435,6 +465,19 @@ A new L<Sympa::Process> instance, or I<undef> for failure.
I<Instance method>.
TBD.
=item daemonize ( )
I<Instance method>.
Puts ourselves in background.
Parameters:
None.
Returns:
None.
=item fork ( [ $tag ] )
I<Instance method>.
......@@ -533,6 +576,11 @@ L<Sympa::Process> instance may have following attributes:
Hashref with child PIDs forked by fork() method as keys.
=item {detached}
True value is set if daemonize() method was called and the process has been
detached from TTY.
=item {generation}
Generation of process.
......@@ -547,4 +595,6 @@ L<Sympa::Tools::Daemon> appeared on Sympa 6.2a.41.
Renamed L<Sympa::Process> appeared on Sympa 6.2.12
and began to provide OO interface.
Sympa 6.2.13 introduced daemonize() method and {detached} attribute.
=cut
......@@ -83,23 +83,9 @@ unless (Sympa::DatabaseManager->instance) {
$Conf::Conf{'db_name'};
}
## Put ourselves in background if not in debug mode.
unless ($main::options{'debug'} || $main::options{'foreground'}) {
open(STDERR, ">> /dev/null");
open(STDOUT, ">> /dev/null");
if (open(TTY, "/dev/tty")) {
# ioctl(TTY, TIOCNOTTY(), 0);
ioctl(TTY, 0x20007471, 0); # XXX s/b TIOCNOTTY
close(TTY);
}
setpgrp 0, 0;
my $child_pid = fork;
if ($child_pid) {
$log->syslog('info', 'Starting archived daemon, PID %s', $child_pid);
exit 0;
} elsif (not defined $child_pid) {
die sprintf 'Cannot fork: %s', $ERRNO;
}
# Put ourselves in background if not in debug mode.
unless ($main::options{'foreground'}) {
$process->daemonize;
}
# Create and write the PID file.
......
......@@ -88,22 +88,9 @@ unless (Sympa::DatabaseManager->instance) {
$Conf::Conf{'db_name'};
}
## Put ourselves in background if not in debug mode.
unless ($main::options{'debug'} || $main::options{'foreground'}) {
open(STDERR, ">> /dev/null");
open(STDOUT, ">> /dev/null");
if (open(TTY, "/dev/tty")) {
ioctl(TTY, 0x20007471, 0); # XXX s/b TIOCNOTTY()
close(TTY);
}
setpgrp 0, 0;
my $child_pid = fork;
if ($child_pid) {
$log->syslog('info', 'Starting bounced daemon, PID %s', $child_pid);
exit 0;
} elsif (not defined $child_pid) {
die sprintf 'Cannot fork: %s', $ERRNO;
}
# Put ourselves in background if not in debug mode.
unless ($main::options{'foreground'}) {
$process->daemonize;
}
# Create and write the PID file.
......
......@@ -117,22 +117,9 @@ if ($main::options{'log_level'}) {
);
}
## Put ourselves in background if not in debug mode.
unless ($main::options{'debug'} || $main::options{'foreground'}) {
open(STDERR, ">> /dev/null");
open(STDOUT, ">> /dev/null");
if (open(TTY, "/dev/tty")) {
close(TTY);
}
setpgrp 0, 0;
my $child_pid = fork;
if ($child_pid) {
$log->syslog('info', 'Starting bulk master daemon, PID %s',
$child_pid);
exit 0;
} elsif (not defined $child_pid) {
die sprintf 'Cannot fork: %s', $ERRNO;
}
# Put ourselves in background if not in debug mode.
unless ($main::options{'foreground'}) {
$process->daemonize;
}
$log->openlog($Conf::Conf{'syslog'}, $Conf::Conf{'log_socket_type'});
......@@ -195,7 +182,7 @@ while (!$end) {
my ($message, $handle);
unless ($main::options{'foreground'}) {
if ($process->{detached}) {
# Create child bulks if too much packets are waiting to be sent in
# the bulk_mailer table.
# Only the main bulk process ({generation} is 0) can create child
......
......@@ -43,7 +43,7 @@ use Sympa::Spindle::ProcessAutomatic;
use Sympa::Tools::Data;
my $process = Sympa::Process->instance;
$process->init(pidname => 'sympa_automatic');
$process->init(pidname => 'sympa_automatic', name => 'sympa/automatic');
## Init random engine
srand(time());
......@@ -78,30 +78,9 @@ my $mailer = Sympa::Mailer->instance;
_load();
## Put ourselves in background if we're not in debug mode. That method
## works on many systems, although, it seems that Unix conceptors have
## decided that there won't be a single and easy way to detach a
## process from its controlling TTY.
# Put ourselves in background if we're not in debug mode.
unless ($main::options{'foreground'}) {
if (open(TTY, "/dev/tty")) {
ioctl(TTY, 0x20007471, 0); # XXX s/b TIOCNOTTY()
close(TTY);
}
open(STDIN, ">> /dev/null");
open(STDOUT, ">> /dev/null");
open(STDERR, ">> /dev/null");
setpgrp 0, 0;
my $child_pid = fork;
if ($child_pid) {
$log->syslog('notice', 'Starting sympa/automatic daemon, PID %d',
$child_pid);
exit 0;
} elsif (not defined $child_pid) {
die sprintf 'Cannot fork: %s', $ERRNO;
}
$process->daemonize;
}
$log->openlog($Conf::Conf{'syslog'}, $Conf::Conf{'log_socket_type'});
......
......@@ -53,7 +53,7 @@ use Sympa::Tools::Data;
use Sympa::Topic;
my $process = Sympa::Process->instance;
$process->init(pidname => 'sympa_msg');
$process->init(pidname => 'sympa_msg', name => 'sympa/msg');
## Internal tuning
# delay between each read of the digestqueue
......@@ -95,29 +95,9 @@ my $mailer = Sympa::Mailer->instance;
_load();
## Put ourselves in background if we're not in debug mode. That method
## works on many systems, although, it seems that Unix conceptors have
## decided that there won't be a single and easy way to detach a
## process from its controlling TTY.
# Put ourselves in background if we're not in debug mode.
unless ($main::options{'foreground'}) {
if (open(TTY, "/dev/tty")) {
ioctl(TTY, 0x20007471, 0); # XXX s/b TIOCNOTTY()
close(TTY);
}
open(STDIN, ">> /dev/null");
open(STDOUT, ">> /dev/null");
open(STDERR, ">> /dev/null");
setpgrp 0, 0;
my $child_pid = fork;
if ($child_pid) {
$log->syslog('notice', 'Starting sympa/msg daemon, PID %d',
$child_pid);
exit 0;
} elsif (not defined $child_pid) {
die sprintf 'Cannot fork: %s', $ERRNO;
}
$process->daemonize;
# Fork a new process dedicated to automatic list creation, if required.
if ($Conf::Conf{'automatic_list_feature'} eq 'on') {
......
......@@ -108,25 +108,9 @@ if ($main::options{'log_level'}) {
);
}
## Put ourselves in background if not in debug mode.
unless ($main::options{'debug'} || $main::options{'foreground'}) {
open(STDERR, ">> /dev/null");
open(STDOUT, ">> /dev/null");
if (open(TTY, "/dev/tty")) {
ioctl(TTY, 0x20007471, 0); # s/b TIOCNOTTY()
close(TTY);
}
setpgrp 0, 0;
my $child_pid = fork;
if ($child_pid) {
$log->syslog('info', 'Starting task_manager daemon, PID %s',
$child_pid);
exit 0;
} elsif (not defined $child_pid) {
die sprintf 'Cannot fork: %s', $ERRNO;
}
# Put ourselves in background if not in debug mode.
unless ($main::options{'foreground'}) {
$process->daemonize;
}
# Create and write the PID file.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment