Re: Popen and EVFILT_WRITE question



Mel <fbsd.hackers@xxxxxxxxxxxxxxxxxxxx> writes:

Hi,

from reading the manpage on EVFILT_WRITE I thought it would be an easy to use
interface to detect when a program wants input.
So far, that doesn't seem to be the case. Ultimately what I want to do is pipe
all the popen(3)'d output to a logfile and act on any input it wants.

Could anyone explain to me why I'm never getting the EVFILT_WRITE event in
below testcode?
(It doesn't matter if I open the pipe with w+ or r+).

test.c:
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sysexits.h>
#include <err.h>
#include <stdio.h>

int main(int argc, char **argv)
{
FILE *proc;
int kq;
struct kevent changes[2], events[2];

proc = popen("./test.sh", "w+");
if( -1 == (kq = kqueue()) )
err(EX_OSERR, "Cannot get a kqueue");

EV_SET(&changes[0], fileno(proc), EVFILT_WRITE, EV_ADD|EV_ENABLE, 0,
0, 0);
EV_SET(&changes[1], fileno(proc), EVFILT_READ, EV_ADD|EV_ENABLE, 0,
0, 0);

This is never going to work.

First, the second kevent overrides the first, because they both have the
same ident.

Second, you're going to run into buffering issues.

Third, an EVFILT_WRITE event will trigger as long as there is space in
the pipe buffer. There is no such thing as "when a program wants input"
in Unix; it will either read input or it won't, and what happens when it
reads depends entirely on what the fd it reads from is connected to,
whether it's a slow or fast device, blocking or non-blocking, etc.

DES
--
Dag-Erling Smørgrav - des@xxxxxx
_______________________________________________
freebsd-hackers@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • [PATCH] sys_vmsplice
    ... Attempt to steal a page from a pipe buffer. ... int ret, do_wakeup, i; ... unsigned int this_len; ...
    (Linux-Kernel)
  • vmsplice cant work well
    ... Use vmsplice to fill some user memory into a pipe. ... static inline int xerror ... int do_vmsplice(int fd, void *buffer, int len) ...
    (Linux-Kernel)
  • Re: Data consistency and "other" synchronization methods
    ... int someVariable; ... If the guarantee is not there and cannot be deduced from other ... Alexander Terekhov proposed to implement the pipe on top of strongly ... I don't know much about Unix internals, but I'd start by looking at ...
    (comp.programming.threads)
  • mex program
    ... I need to understand the following mex program ... int *pHandle; ... ifmexErrMsgTxt("PipeIO.c: Could not get pipe ... // Get the read/write flag from matlab ...
    (comp.soft-sys.matlab)
  • [PATCH][RFC] splice support
    ... ->splice_write merely fill the pipe buffers and the splice.c code ... - Add ability to splice to a socket, ... +asmlinkage long sys_splice(int fdin, int fdout, size_t len, unsigned long flags) ...
    (Linux-Kernel)