Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Matts (1087)

Matts
  (email not shown publicly)

I work for MessageLabs [messagelabs.com] in Toronto, ON, Canada. I write spam filters, MTA software, high performance network software, string matching algorithms, and other cool stuff mostly in Perl and C.

Journal of Matts (1087)

Wednesday February 23, 2005
03:08 PM

Unixy Question

[ #23336 ]

So my question is this:

    Given stdin (read only) and stdout (write only) [*] is there a way to create a single read/write filehandle that reads from stdin and writes to stdout? And tie() doesn't count as I need this to pass to poll().

[*] Unless you're djb, but that's just freaky

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • This would be without using Inline::C to compile a new kernel module, I take it?

    a.k.a. no idea, and I'm not convinced that it's possible. (so someone prove me wrong, please)

  • If it's a terminal you're attached to, I seem to recall that fd 0 can be written to, and fd 1 can be read from, just fine. Not sure if that's linux-specific or a common UNIX idiom though.

    However, if you can't make that assumption -- ie if fd 0/1 may have been redirected to a non-tty source/sink, no, I don't think it's possible. :(
  • I think that some systems make socketpair(2) [freebsd.org] bidirectional fd's. It appears that way on my FreeBSD box, but I don't know about others. Hmmm, even pipe(2) [freebsd.org] appears bidirectional on FreeBSD actually.

    -Dom

  • As near as I can tell, yes, but only if STDIN and STDOUT are opened on the same thing (ie the only difference between them is the mode). Don't ask me how to get such a filehandle.

    What do you need this for?

    • I don't *need* it, but I'm writing an app that works as either a poll() server, a forking server, or an inetd style server (where stdin is the read half of the socket, and stdout is the write half).

      In fork/inetd mode the app still uses poll() internally (for various reasons) so it'd be nice to be able to just use the same code for each. Instead in inetd mode I have to pull a line off the read handle, and ask the write handle to process it and create the output.

      This is all rather hard to explain in english
  • #! /usr/bin/perl -wT

    use FileHandle();
    use strict;

    my ($fileHandle) = new FileHandle();
    open($fileHandle, "+>/dev/fd/0") || die("Whoops:$!\n");
    unless ($fileHandle->print("Hello\n")) {
            die("Failed to write:$!\n");
    }
    my ($buffer);
    unless ($fileHandle->read($buffer, 1)) {
            die("Failed to read:$!\n");
    }
    • No, because although it opens fd0 for reading, the data isn't coming in on fd0 - it's coming in on fd1.

      Matt.
      • interesting. cos it worked (wrote to stdout and read from stdin) on the terminals that i was working on in solaris and linux. and on linux /dev/fd/0 and /dev/fd/1 are symlinks that point to the same file.
        • I might give it a go. I'm remembering that the stdin/stdout in an inetd/tcpserver situation are just built from dup2()ing the same filehandle twice onto those two separate file descriptors, so it might just work.

          Of course I've now spent a few days building a work around :-)