Hi monks,
I'm trying to act like the (x)inetd daemon and launch an external program after I got a connection from a client and fork()ed. This is my code:

#!/usr/bin/env perl use strict; use warnings; use Socket qw(TCP_NODELAY); use IO::Socket; use IO::Socket::INET; use IPC::Open2; $|++; my @cmd = ('ls', '-l'); my $sock = IO::Socket::INET::->new( Listen => 20, LocalAddr => '0.0.0.0', LocalPort => 10101, Proto => 'tcp', Reuse => 1, ); die "Unable to create socket: $!" unless $sock; $sock->sockopt(TCP_NODELAY, 1); $SIG{CHLD} = 'IGNORE'; while (1) { # Go on forever! my $connection = $sock->accept(); # Blocking my $child = fork(); unless (defined($child)) { $connection->close(); # Not enough resources to fork(); next; } if ($child) { # I'm the parent next; } else { # I'm the child, I serve this connection print "Connection accepted\n"; print "Starting command...\n"; my $cmd_pid = open2('<&'.fileno($connection), '>&'.fileno($con +nection), @cmd); unless ($cmd_pid) { $connection->close(); die "command failed!"; } waitpid($cmd_pid, 0); print "Closing connection.\n"; $connection->close(); exit 0; } }

a simple
telnet localhost 10101
gets the output from ls -l, but on the script's STDOUT i read:

open2: close(4) failed: Bad file descriptor at ./daemon_simple.pl line + 43

and when I try with a command that needs to get input from the client, it seems that the input never arrives.

Does someone known if it's possible to get it working?


Thanks.

In reply to Acting like (x)inetd by mantager

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.