NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: lib/55836: popen(3)'s type "r+" doesn't work correctly

Hi, Wolfgang!

On Wed, 2 Dec 2020, Wolfgang Solfrank wrote:

The following reply was made to PR lib/55836; it has been noted by GNATS.

From: Wolfgang Solfrank <>
Subject: Re: lib/55836: popen(3)'s type "r+" doesn't work correctly
Date: Wed, 2 Dec 2020 16:55:44 +0100


> While I was playing with read/write pipes with popen(command, "r+") I
> realized that this doesn't work as expected.  The plus triggers creating
> the pipe object with socketpair(2) instead of pipe2(2) in
> src/lib/libc/gen/popen.c, but socketpair doesn't seem to work properly.

This seems to be a problem with your expectations.  The "r+" tells
popen to open the pipe "for reading and writing" (see popen(3)).
I.e. with your test program, the forked "cat" gets its stdin *and*
stdout connected to the one end of the socketpair.  So the cat
doesn't read the output from the ls command, but sits there waiting
for your program to write something into fp.

So this works exactly as designed.

--				Wolfgang Solfrank

Thank you for your input. According to the man pages "the command's standard input is the same as that of the process that called popen()".

Okay, if you don't trust this, try that after the pipe is opened:

	fputs("abcdefg\n", fp);

fgets(buf, sizeof(buf), fp) returns this string but after that it hangs, no NULL will be returned.


Home | Main Index | Thread Index | Old Index