This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

unable to open fifo multiple times with same access mode


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>
#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;
}
#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;
}

--
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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]