It looks like (to me) PERL is crashing without an ERROR but it also looks like (to me) that I must be doing something wrong. What everything looks like is very strange as PERL exits mid-code without an ERROR and it should not... unless I am wrong... which probably I am... which is why I am asking for HELP!
I've confirmed the code below exhibits the same behavior with a virgin CENTOS 8 (I used Rocky) system and it EXITS... please can someone help me and explain why as really I don't know what things are going on so I am aksing for help!
Two simple PERL programs:
./listen_socket.pl
./send_socket.pl sending_this
Send socket is effectively a simple netcat... nc.
NETCAT works.... if I use:
echo send_this | nc -U /tmp/whydontyouwork.sock
then "listen_socket.pl" does not exit and behaves as expected. If I execute this:
./send_socket.pl send_this
then it fails.
I don't understand why. Please help me and tell me where I am an idiot cos I dunno!
Here are the two perl programs: (listen_socket.pl) and (send_socket.pl)
listen_socket.pl:
use warnings;
use IO::Select;
use IO::Socket::UNIX;
my $SOCK_PATH = "/tmp/whydontyouwork.sock";
my $socket;
my $sel = IO::Select->new();
unlink $SOCK_PATH;
setup();
main();
print STDERR "I know I never get here...see below..\n";
sub setup {
$socket = new IO::Socket::UNIX(
Type => SOCK_STREAM,
Local => $SOCK_PATH,
Listen => 5,
);
$sel->add($socket);
}
sub main {
while ( 1 == 1 ) {
while(my @ready = $sel->can_read(1)) {
foreach my $fh (@ready) {
if($fh == $socket) {
# Create a new socket
my $new = $socket->accept;
$sel->add($new);
}
else {
# Process socket
if ( 1 == 1 ) {
my $data = <$fh>;
if ( defined $data ) {
print STDERR "I am here...I GET HERE!!";
print $fh "\n";
print STDERR "..and all done and I NEVER GET HERE!!??.\n";
$sel->remove($fh);
$fh->close;
}
}
print STDERR "Howdie here? Nope..\n";
}
}
}
}
}
send_socket.pl:
#!/usr/bin/perl
use Socket;
my $SOCK_PATH = "/tmp/whydontyouwork.sock";
if ( $ARGV[0] ) {
socket(CLIENT, PF_UNIX, SOCK_STREAM, 0);
connect(CLIENT, sockaddr_un($SOCK_PATH));
print CLIENT $ARGV[0] ;
close CLIENT;
exit;
}
exit 1;
WORKS ==>
echo send_this | nc -U /tmp/whydontyouwork.sock
NOT WORKS ==>
perl ./send_socket.pl send_this
CodePudding user response:
You should have added the output of each run, at least tell us the listener dies with Broken pipe
which would have made it more obvious what your issue is.
In any case, it looks like your sender is closing the socket and then the listener is trying to write to it.
You can technically ignore the broken pipe and have your output be the same for the perl example by adding $SIG{PIPE} = 'IGNORE';
to the listener, but that of course does not fix the (lack of) logic.
But, really, you should read up a bit more about programming with sockets, as what you were trying to do did not make much sense, and, even more importantly, you should actually pay attention to the error messages for hints to what is going on.