X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:date:from:to:subject:message-id:mime-version
	:content-type; q=dns; s=default; b=yp1vDmDs71KYid8la4GdN0RvlGmCQ
	a1wdCJRIWsVrhcXNaq7MUz+FWODCfXl3zCmCQM0667UWrDdnqAjBwzYSG9rDp8YN
	1inJqNmeEM6Le0U0GL/Bsusa2RYiZ5JUSBFEKI0jHh1FCZxOjIRWcLe50h53vGDB
	6h8QwgAjPoVPP4=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:date:from:to:subject:message-id:mime-version
	:content-type; s=default; bh=qgvQaFohh3+esJarAQE2egG2sAo=; b=fEx
	JQx5J+qsntuyNtz4ueDdEKKq1lyFmejlawzBqxf98Y6XCjNnCg8z4n3ySY6OT1WW
	iBvD0ZhdzlI0sIFW8XaWvT8NwOIglPAdC5KJ85jfSFi1nu7CBg0dsuFA+CX7xB+d
	a31Y+mujYv8UTK4No3EgshJrW+TlpB91ldT7GwdM=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 spammy=200000, H*c:HHHH, states, extremely
X-HELO: conssluserg-03.nifty.com
DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-03.nifty.com v2HDIPwv025253
X-Nifty-SrcIP: [175.179.23.201]
Date: Fri, 17 Mar 2017 22:18:28 +0900
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin@cygwin.com
Subject: unable to open fifo multiple times with same access mode
Message-Id: <20170317221828.96bdeb7d71f5e57d6e7e2585@nifty.ne.jp>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="Multipart=_Fri__17_Mar_2017_22_18_28_+0900_dx9Nk+bQQ1dUn9Hl"
X-IsSubscribed: yes

--Multipart=_Fri__17_Mar_2017_22_18_28_+0900_dx9Nk+bQQ1dUn9Hl
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

Hello,

I have noticed that a FIFO cannot be opened multiple times with
same access mode.

POSIX states as follows regarding write() to FIFO:
Write requests of {PIPE_BUF} bytes or less shall not be interleaved
with data from other processes doing writes on the same pipe.

This implicitly says that FIFO can be opened multiple times for write. 

POSIX states as follows regarding read() from FIFO:
The behavior of multiple concurrent reads on the same pipe, FIFO,
or terminal device is unspecified.

This assumes that FIFO can be opened multiple times for read.

Indeed, a FIFO can be opened more than once in Linux and FreeBSD
even with same access mode.


Simple test cases (np1.c, np2.c), attached, provide checks for this.

np1.c trys to open a FIFO twice with O_WRONLY.

Expected result of np1 is:
AAAAAAAAAA
BBBBBBBBBB
AAAAAAAAAA
BBBBBBBBBB
AAAAAAAAAA
BBBBBBBBBB
AAAAAAAAAA
BBBBBBBBBB
AAAAAAAAAA
BBBBBBBBBB

However, the result of np1 in cygwin is:
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA

np2.c trys to open a FIFO twice with O_RDONLY.

Expected result of np2 is:
1:AAAAAAAAAA
2:AAAAAAAAAA
1:AAAAAAAAAA
2:AAAAAAAAAA
1:AAAAAAAAAA
2:AAAAAAAAAA
1:AAAAAAAAAA
2:AAAAAAAAAA
1:AAAAAAAAAA
2:AAAAAAAAAA

However, the result of np2 in cygwin is:
2:open(): Device or resource busy
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA
1:AAAAAAAAAA


Due to this problem, Midnight Commander (mc) fails to start subshell
with following error message, if SHELL is /bin/tcsh.

Cannot open named pipe /tmp/mc-yano/mc.pipe.536
                                               /cygdrive/e/cyg_pub/devel/mc/mc-4.8.19-1.i686/src/mc-4.8.19/src/subshell/common.c: open: Device or resource busy

To reproduce this, try
env SHELL=/bin/tcsh mc


This problem was already discussed in the past as far as I know.
https://cygwin.com/ml/cygwin/2015-03/msg00047.html

How is progress on this issue?

I guess extremely a huge change is nessesary to fix this, but it is
not impossible if DuplicateHandle() is used like pty code.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

--Multipart=_Fri__17_Mar_2017_22_18_28_+0900_dx9Nk+bQQ1dUn9Hl
Content-Type: text/x-csrc;
 name="np1.c"
Content-Disposition: attachment;
 filename="np1.c"
Content-Transfer-Encoding: 7bit

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>

int main()
{
	const char fifoname[] = "/tmp/test_fifo";
	pid_t pid1, pid2;
	int pipe;
	int len;
	int i;

	if (mkfifo(fifoname, 0600) == -1) {
		perror("mkfifo()");
		if (errno != EEXIST) exit(-1);
	}

	pid1 = fork();
	if (pid1 == -1) {
		perror("1:fork()");
		goto clean;
	} else if (pid1 == 0) {
		/* Child 1 */
		pipe = open(fifoname, O_WRONLY);
		if ( pipe == -1) {
			perror("1:open()");
			exit(-1);
		}
		for (i=0; i<5; i++) {
			write(pipe, "AAAAAAAAAA\n", 11);
			usleep(200000);
		}
		exit(0);
	}

	/*Parent*/
	pid2 = fork();
	if (pid2 == -1) {
		perror("2:fork()");
		goto clean;
	} else if (pid2 == 0) {
		/* Child 2 */
		usleep(100000);
		pipe = open(fifoname, O_WRONLY);
		if ( pipe == -1) {
			perror("2:open()");
			exit(-1);
		}
		for (i=0; i<5; i++) {
			write(pipe, "BBBBBBBBBB\n", 11);
			usleep(200000);
		}
		exit(0);
	}

	/* Parent */
	pipe = open(fifoname, O_RDONLY);
	if ( pipe == -1) {
		perror("3:open()");
		goto clean;
	}
	do {
		char buf[256];
		len = read(pipe, buf, sizeof(buf));
		if (len > 0) write(STDOUT_FILENO, buf, len);
	} while (len > 0);
	close(pipe);

clean:
	kill(pid1, SIGTERM);
	kill(pid2, SIGTERM);
	if (unlink(fifoname) == -1) {
		perror("unlink()");
	}
	return 0;
}

--Multipart=_Fri__17_Mar_2017_22_18_28_+0900_dx9Nk+bQQ1dUn9Hl
Content-Type: text/x-csrc;
 name="np2.c"
Content-Disposition: attachment;
 filename="np2.c"
Content-Transfer-Encoding: 7bit

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>

int main()
{
	const char fifoname[] = "/tmp/test_fifo";
	pid_t pid1, pid2;
	int pipe;
	int len;
	int i;

	if (mkfifo(fifoname, 0600) == -1) {
		perror("mkfifo()");
		if (errno != EEXIST) exit(-1);
	}

	pid1 = fork();
	if (pid1 == -1) {
		perror("1:fork()");
		goto clean;
	} else if (pid1 == 0) {
		/* Child 1 */
		char buf[256];
		pipe = open(fifoname, O_RDONLY);
		if ( pipe == -1) {
			perror("1:open()");
			exit(-1);
		}
		do {
			char buf[256];
			len = read(pipe, buf, sizeof(buf));
			if (len > 0) {
				write(STDOUT_FILENO, "1:", 2);
				write(STDOUT_FILENO, buf, len);
				usleep(210000);
			}
		} while (len > 0);
		close(pipe);
		exit(0);
	}

	/*Parent*/
	pid2 = fork();
	if (pid2 == -1) {
		perror("2:fork()");
		goto clean;
	} else if (pid2 == 0) {
		/* Child 2 */
		pipe = open(fifoname, O_RDONLY);
		if ( pipe == -1) {
			perror("2:open()");
			exit(-1);
		}
		usleep(200000);
		do {
			char buf[256];
			len = read(pipe, buf, sizeof(buf));
			if (len > 0) {
				write(STDOUT_FILENO, "2:", 2);
				write(STDOUT_FILENO, buf, len);
				usleep(210000);
			}
		} while (len > 0);
		close(pipe);
		exit(0);
	}

	/* Parent */
	pipe = open(fifoname, O_WRONLY);
	if ( pipe == -1) {
		perror("3:open()");
		exit(-1);
	}
	for (i=0; i<10; i++) {
		write(pipe, "AAAAAAAAAA\n", 11);
		usleep(200000);
	}
	close(pipe);

clean:
	kill(pid1, SIGTERM);
	kill(pid2, SIGTERM);
	if (unlink(fifoname) == -1) {
		perror("unlink()");
	}
	return 0;
}


--Multipart=_Fri__17_Mar_2017_22_18_28_+0900_dx9Nk+bQQ1dUn9Hl
Content-Type: text/plain; charset=us-ascii


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
--Multipart=_Fri__17_Mar_2017_22_18_28_+0900_dx9Nk+bQQ1dUn9Hl--
