Hi all,
I'm using Wheel::Run in my script. It works fine in the foreground. But
when I daemonize it,
the last thing executed is the CHLD signal handler. It seems to exit
normally, without core dump or anything abnormal (but there should be
more code after).
That description doesn't tell much, I agree!! any ideas or suggestions
would be appreciated

Thanks
Eddy


This is some code, if it can help:

sub daemonize {
        my $self = shift;

        use POSIX 'setsid';

        chdir('/') or die(Message->m0010($!));
        open(STDIN, '/dev/null') or die(Message->m0011($!));
        open(STDOUT, '>/dev/null') or die(Message->m0012($!));

        # faccio fork se non ho impostato switch di esecuzione in fg
        defined(my $pid = $self->{foreground} ? $$ : fork()) or
die(Message->m0013($!));

        if ($pid) {
                # ESEGUITO NEL PADRE MORITURO
                # cambiare il pid e' necessario, perche' sto facendo
fork
                #$self->pidfile()->move(pid => $pid);

                exit unless $self->{foreground};
        }

        beginCriticalSection($self->lockfile());
        $self->spawnerFile()->append("# PID=$$\n");
        endCriticalSection();
        setsid or die(Message->m0014($!));
        open(STDERR, '>&STDOUT') or die(Message->m0015($!));
}

========================================================

sub got_child_stdout {
        my $stdout = $_[ARG0];
        my $heap = $_[HEAP];
        my $PID = $heap->{pid_figlio};
        $logger->info("STDOUT di $PID: $stdout");

        # filtro lo standard output (usando la modalita' pty in realta'
contiene
        # anche lo standard error) per vedere se il sodo mi sta
chiedendo la password
        # Nel caso uccido il processo
        if ($stdout =~ /$sudo_signature/) {
                $logger->error("sudo misconfigured!!!!");
                kill(9, $PID);
        }

        # filtraggio standard output per valore variabili
        $sieve->feed($stdout);
}


# The child has closed its output filehandles.  It will not be sending
# us any more information, so destroy it.

sub got_child_close {
        my $heap = $_[HEAP];
        $logger->info("child closed");
        $logger->info("child closed exit status $?");
        delete $heap->{child};
}


# eventuali segnalazioni per eventi non gestiti

sub _default {
        $logger->info("L'evento $_[ARG0] non รจ gestito!!! I suoi
parametri sono: @{ $_[ARG1] }");
}


# Gestione del segnale CHLD, che viene ricevuto quando il figlio
termina

sub got_chld_sig {
        my ($kernel, $heap) = @_[KERNEL, HEAP];

        # gestione del codice di ritorno
        # il codice dell'eventuale segnale di terminazione del
proceesso e' nel byte a destra
        $terminationSignal = $_[ARG2] & 255;
        # il codice di ritorno del proceesso e' nel byte a sinstra
        $returnCode = $_[ARG2] >> 8;

        $logger->info("Sengale: $_[ARG0]");
        $logger->info("PID: $_[ARG1]");
        $logger->info("Codice di ritorno non elaborato: $_[ARG2]");
        $logger->info("Codice di ritorno: $returnCode");
        $logger->info("Segnale di terminazione: $terminationSignal");

        # non sono riuscito a far capire che dopo questo evento deve
uscirsene
        # devo usare lo stop del kernel, perche' con modalita' pty si
riesce
        # a far uscire POE dopo la terminazione del programma
        $logger->info("Uscita con stop");
        $kernel->stop();
}


# Dovrebbe arrivare alla fine della sessione.
# Probabilmente non sara' chiamata mai. Comunque loggo

sub _stop {
        my ($kernel, $heap) = @_[ KERNEL, HEAP ];
        $logger->info("segnale di stop");
}


# Condizioni di errore di Wheel::Run
# Non so se sono significative, ma le metto comunque nel log

sub error_state {
        my ($operation, $errnum, $errstr, $wheel_id) = @_[ARG0..ARG3];
        $logger->warn("Wheel $wheel_id generated $operation error
$errnum: $errstr");
}


# Start a session where each event is handled by a function with the
# same name.  Run POE's Kernel (and thus all its sessions) until done.
sub _start {
        my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];

        $heap->{child} = POE::Wheel::Run->new(
                Program => $program,    # Program to run.
                StdioFilter  => POE::Filter::Line->new(),
                StdoutEvent  => "got_child_stdout",
                CloseEvent   => "got_child_close",
                ErrorEvent   => "error_state",
                Conduit      => "pty",
        );

        $kernel->sig(CHLD => "got_chld_sig");
        $heap->{pid_figlio} = $heap->{child}->PID;
}

my @handlers = qw( _start
                   _default
                   _stop
                   got_child_stdout
                   got_child_close
                   got_chld_sig
                   error_state
);

sub new {
        my $class = shift;
        my $href = shift;

        my $self = $href;
        $self->{matchedValue} = $self->{default};

        $logger = Log::Log4perl->get_logger("");

        $logger->error_die("Il parametro di ingresso deve essere un
riferimento ad un hash") unless ref($self) eq 'HASH';

        $logger->error_die("Il parametro cmd deve essere valorizzato")
unless defined $self->{cmd};
        $logger->error_die("Il parametro sieve deve essere
valorizzato") unless defined $self->{sieve};
        $logger->error_die("Il parametro sieve deve essere un oggetto
sieve") unless $self->{sieve}->isa('Sieve');

        $self->{returnCode} = undef;

        $program = $self->{cmd};
        $sieve = $self->{sieve};

        POE::Session->create(package_states => [Performer =>
[EMAIL PROTECTED]);

        bless($self, $class);
        
        return $self;
}

Reply via email to