<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:'times new roman', 'new york', times, serif;font-size:12pt"><div>Posso estar falando uma tremenda besteira, mas não seria interessante se trocasse a porta 1 para algo como 50001.&nbsp;</div><div>Se não me engano no linux a porta 1 é alocada para /sbin/init. Basta dar um ps e verás! :)</div><div><br></div><div>Abraços,</div><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><br><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><font size="2" face="Tahoma"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> Andre Carneiro &lt;andregarciacarneiro@gmail.com&gt;<br><b><span style="font-weight: bold;">To:</span></b> saopaulo-pm@mail.pm.org<br><b><span style="font-weight: bold;">Sent:</span></b> Fri, November 27, 2009 9:06:38 AM<br><b><span style="font-weight:
 bold;">Subject:</span></b> [SP-pm] forks + daemon + socket<br></font><br>
Salve pessoal!<br><br><br>Estou tendo um problema utilizando o módulo forks.<br><br>Enquanto eu não faço o 'daemonize' no meu daemon, funciona tudo blz. No entanto, quando o 'daemonize' acontece, tudo que é executado através de forks gera "conexão recusada" no socket. <br>
<br>&lt;code&gt;<br>.<br>.<br>.<br>use App::Daemon qw( daemonize );<br>use POSIX qw(:signal_h );<br>use Getopt::Long;<br>use IO::Socket::UNIX;<br><br>#DAEMON<br>&nbsp;&nbsp;&nbsp; my $SOCKFILE&nbsp;&nbsp;&nbsp; = '/tmp/socktest';<br>&nbsp;&nbsp;&nbsp; unlink $SOCKFILE or die $! if -e $SOCKFILE;<br>
&nbsp;&nbsp;&nbsp; daemonize() if $daemonize == 1;<br>&nbsp;&nbsp;&nbsp; my $usock = IO::Socket::UNIX-&gt;new(&nbsp;&nbsp;&nbsp; Type &nbsp;&nbsp;&nbsp; =&gt; SOCK_STREAM ,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Local&nbsp;&nbsp;&nbsp; =&gt; '/tmp/socktest' ,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Listen&nbsp;&nbsp;&nbsp; =&gt; 1<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ) or die $!;<br>&nbsp;&nbsp;&nbsp; my $r = ();<br>&nbsp;&nbsp;&nbsp; chmod 0777 , $SOCKFILE;<br>&nbsp;&nbsp;&nbsp; my %Process;<br><br>&nbsp;&nbsp;&nbsp; while(1){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(my ($r) = $usock-&gt;accept){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #c<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; die "\nSOCKET NAO FOI ACEITO!!! $! $@\n";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br><br><br>&lt;/code&gt;<br>#cliente<br>.<br>.<br>.<br>use IO::Socket::UNIX;<br>use Getopt::Std;<br>use Config::General;<br>use Data::Dumper;<br>
<br>my $conf_obj&nbsp;&nbsp;&nbsp; = Config::General-&gt;new('config/general.conf');<br>my %config&nbsp;&nbsp;&nbsp; = $conf_obj-&gt;getall();<br>my $SOCKETFILE &nbsp;&nbsp;&nbsp; = '/tmp/socktest';#$config{defaults}-&gt;{scap_socket}; <br><br>@ARGV &lt; 1 and die "Parametros invalidos\n";<br>
<br>my $usock = new IO::Socket::UNIX(<br>&nbsp;&nbsp;&nbsp; Type&nbsp; =&gt; SOCK_STREAM,<br>&nbsp;&nbsp;&nbsp; Peer =&gt; $SOCKETFILE,<br>) or die "Impossivel abrir Socket: $! $@ em " . $SOCKETFILE;<br><br><br><br>my %args;<br>getopts("r:s:p:", \%args);<br>
if($args{r}) {<br>&nbsp;&nbsp;&nbsp; print "Executar Spider $args{r} $args{p}\n";<br>&nbsp;&nbsp;&nbsp; print $usock "run $args{r} $args{p}" or die $!;<br>}<br>elsif($args{s}) {<br>&nbsp;&nbsp;&nbsp; print "Parar Spider $args{s}\n";<br>
&nbsp;&nbsp;&nbsp; print $usock "stop $args{s}";<br>}<br><br><br>O cliente acima instancia uma classe que executa o seguinte trecho de código abaixo...<br><br><br>&lt;code&gt;<br>
.<br>.<br>.<br><br>use forks;<br>use MooseX::Singleton;<br>use Config::General;<br>.<br>.<br>.<br><br>foreach my $t(@{$thread}){ <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $self-&gt;log_spider-&gt;write("debug","Thread $i");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eval{$Threads[$i] = threads-&gt;create('init_thread',$self,$t,$cf)}; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if($@){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $self-&gt;log_spider-&gt;write("error","Erro ao criar Thread $i: $@ $!");<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit(0);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }else {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $self-&gt;stat-&gt;dbic($conns-&gt;[$i]); #setando a conex?o do pool de conex?es.<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $i++;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; foreach my $t (1..$#Threads) {$Threads[$t]-&gt;join;}<br>.<br>.<br>.<br>.<br><br>sub init_thread {<br>&nbsp;&nbsp;&nbsp; my ($self,$thread) = @_;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #várias ações aqui...<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sleep 1;<br>}<br><br><br>
&lt;/code&gt;<br><br clear="all"><br><br>Quando a coisa toda é executada, o que ocorre é isso:<br><br><br>&lt;trecho do log&gt;<br><br><br>[Thu Nov 26 18:57:16 2009] [SCAP2] [debug] Thread 1<br>[Thu Nov 26 18:57:17 2009] [SCAP2] [error] Erro ao criar Thread 1:&nbsp; (13981): Couldn't connect to query server: IO::Socket::INET: connect: Conex�o recusada at /usr/local/lib/perl/5.10.0/<a rel="nofollow" target="_blank" href="http://forks.pm">forks.pm</a> line 3592<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threads::_croak('Couldn\'t connect to query server: IO::Socket::INET: connect:...') called at /usr/local/lib/perl/5.10.0/<a rel="nofollow" target="_blank" href="http://forks.pm">forks.pm</a> line 2113<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threads::_init_thread('HASH(0x98d7f50)', '', undef, undef) called at /usr/local/lib/perl/5.10.0/<a rel="nofollow" target="_blank" href="http://forks.pm">forks.pm</a> line 761<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threads::new('threads', 'init_thread', 'Spider=HASH(0xaa1af10)', 'HASH(0xaa6e980)', 'HASH(0xa8fdc30)') called at /home/andre/git/scap/lib/Spider.pm line 264<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eval {...} called at /home/andre/git/scap/lib/Spider.pm line 264<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Spider::load_sources('Spider=HASH(0xaa1af10)', 'ARRAY(0xaa6e920)', 'HASH(0xa8fdc30)', 'HASH(0xaa6e120)') called at /home/andre/git/scap/lib/Spider.pm line 123<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Spider::start('Spider=HASH(0xaa1af10)') called at ./scap2d line 251<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main::run('ARRAY(0xa9e1bb8)', 'Spider=HASH(0xaa1af10)') called at ./scap2d line 148<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main::start('Getopt::Long::CallBack=HASH(0xa9f0650)', 1) called at /usr/share/perl/5.10/Getopt/Long.pm line 593<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eval {...} called at /usr/share/perl/5.10/Getopt/Long.pm line 592<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Getopt::Long::GetOptionsFromArray('ARRAY(0x8c5c420)', 'start', 'CODE(0xa9de550)', 'stop', 'CODE(0xa9e21b8)', 'restart', 'CODE(0x90efa48)', 'help', 'CODE(0x90efa48)', ...) called at ./scap2d line 72<br>
&nbsp;Conex�o recusada<br>[Thu Nov 26 18:57:17 2009] [SCAP2] [debug] Thread 2<br>[Thu Nov 26 18:57:17 2009] [SCAP2] [debug] Thread 3<br>[Thu Nov 26 18:57:17 2009] [SCAP2] [error] Erro ao criar Thread 2:&nbsp; (13982): Couldn't connect to query server: IO::Socket::INET: connect: Conex�o recusada at /usr/local/lib/perl/5.10.0/<a rel="nofollow" target="_blank" href="http://forks.pm">forks.pm</a> line 3592<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threads::_croak('Couldn\'t connect to query server: IO::Socket::INET: connect:...') called at /usr/local/lib/perl/5.10.0/<a rel="nofollow" target="_blank" href="http://forks.pm">forks.pm</a> line 2113<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threads::_init_thread('HASH(0xad41528)', '', undef, undef) called at /usr/local/lib/perl/5.10.0/<a rel="nofollow" target="_blank" href="http://forks.pm">forks.pm</a> line 761<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threads::new('threads', 'init_thread', 'Spider=HASH(0xaa1af10)', 'HASH(0xaa6e910)', 'HASH(0xa8fdc30)') called at /home/andre/git/scap/lib/Spider.pm line 264<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eval {...} called at /home/andre/git/scap/lib/Spider.pm line 264<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Spider::load_sources('Spider=HASH(0xaa1af10)', 'ARRAY(0xaa6e920)', 'HASH(0xa8fdc30)', 'HASH(0xaa6e120)') called at /home/andre/git/scap/lib/Spider.pm line 123<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Spider::start('Spider=HASH(0xaa1af10)') called at ./scap2d line 251<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main::run('ARRAY(0xa9e1bb8)', 'Spider=HASH(0xaa1af10)') called at ./scap2d line 148<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main::start('Getopt::Long::CallBack=HASH(0xa9f0650)', 1) called at /usr/share/perl/5.10/Getopt/Long.pm line 593<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eval {...} called at /usr/share/perl/5.10/Getopt/Long.pm line 592<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Getopt::Long::GetOptionsFromArray('ARRAY(0x8c5c420)', 'start', 'CODE(0xa9de550)', 'stop', 'CODE(0xa9e21b8)', 'restart', 'CODE(0x90efa48)', 'help', 'CODE(0x90efa48)', ...) called at ./scap2d line 72<br>
&nbsp;Conex�o recusada<br><br><br><br>&lt;/trecho do log&gt;<br><br><br><br>Alguém já passou por isso?<br><br><br><br>Cheers!<br><br><br><br>-- <br>André Garcia Carneiro<br>Analista/Desenvolvedor Perl<br><br><br>
</div></div><div style="position:fixed"></div>


<!-- cg5.c1.mail.mud.yahoo.com compressed/chunked Fri Nov 27 05:03:09 PST 2009 -->
</div><br>



      </body></html>