Module Name:    src
Committed By:   thorpej
Date:           Sat Oct  2 17:32:55 UTC 2021

Modified Files:
        src/sys/kern: uipc_syscalls.c
        src/sys/miscfs/fifofs: fifo_vnops.c
        src/tests/lib/libc/sys: t_poll.c

Log Message:
- Strenghen the poll(2) fifo_inout test to ensure that once the reader
  has read enough that exactly PIPE_BUF space is available that the FIFO
  becomes writable again.
- When creating a FIFO, ensure that the receive low water mark is 1
  (a FIFO must be readable when at least 1 byte is available); this
  was already the case implicitly, but this makes it explicit.
- Similarly, set the send low water mark to PIPE_BUF to ensure that
  the pipe is writable when at least PIPE_BUF bytes of space are available
  in the send buffer.  Without this change, the strengthened test case
  above does not pass (the default send low water mark is larger than
  PIPE_BUF; see soreserve()).
- Make the same low water mark changes to the PIPE_SOCKETPAIR case.


To generate a diff of this commit:
cvs rdiff -u -r1.200 -r1.201 src/sys/kern/uipc_syscalls.c
cvs rdiff -u -r1.87 -r1.88 src/sys/miscfs/fifofs/fifo_vnops.c
cvs rdiff -u -r1.7 -r1.8 src/tests/lib/libc/sys/t_poll.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/uipc_syscalls.c
diff -u src/sys/kern/uipc_syscalls.c:1.200 src/sys/kern/uipc_syscalls.c:1.201
--- src/sys/kern/uipc_syscalls.c:1.200	Sat May 23 23:42:43 2020
+++ src/sys/kern/uipc_syscalls.c	Sat Oct  2 17:32:55 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_syscalls.c,v 1.200 2020/05/23 23:42:43 ad Exp $	*/
+/*	$NetBSD: uipc_syscalls.c,v 1.201 2021/10/02 17:32:55 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.200 2020/05/23 23:42:43 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.201 2021/10/02 17:32:55 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pipe.h"
@@ -1319,6 +1319,21 @@ pipe1(struct lwp *l, int *fildes, int fl
 	wf->f_socket = wso;
 	fildes[1] = fd;
 	solock(wso);
+	/*
+	 * Pipes must be readable when there is at least 1
+	 * byte of data available in the receive buffer.
+	 *
+	 * Pipes must be writable when there is space for
+	 * at least PIPE_BUF bytes in the send buffer.
+	 * If we're increasing the low water mark for the
+	 * send buffer, then mimick how soreserve() would
+	 * have set the high water mark.
+	 */
+	rso->so_rcv.sb_lowat = 1;
+	if (wso->so_snd.sb_lowat < PIPE_BUF) {
+		wso->so_snd.sb_hiwat = PIPE_BUF * 2;
+	}
+	wso->so_snd.sb_lowat = PIPE_BUF;
 	error = unp_connect2(wso, rso);
 	sounlock(wso);
 	if (error != 0)

Index: src/sys/miscfs/fifofs/fifo_vnops.c
diff -u src/sys/miscfs/fifofs/fifo_vnops.c:1.87 src/sys/miscfs/fifofs/fifo_vnops.c:1.88
--- src/sys/miscfs/fifofs/fifo_vnops.c:1.87	Sat Oct  2 02:07:41 2021
+++ src/sys/miscfs/fifofs/fifo_vnops.c	Sat Oct  2 17:32:55 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: fifo_vnops.c,v 1.87 2021/10/02 02:07:41 thorpej Exp $	*/
+/*	$NetBSD: fifo_vnops.c,v 1.88 2021/10/02 17:32:55 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.87 2021/10/02 02:07:41 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.88 2021/10/02 17:32:55 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -156,6 +156,23 @@ fifo_open(void *v)
 			kmem_free(fip, sizeof(*fip));
 			return (error);
 		}
+
+		/*
+		 * FIFOs must be readable when there is at least 1
+		 * byte of data available in the receive buffer.
+		 *
+		 * FIFOs must be writable when there is space for
+		 * at least PIPE_BUF bytes in the send buffer.
+		 * If we're increasing the low water mark for the
+		 * send buffer, then mimick how soreserve() would
+		 * have set the high water mark.
+		 */
+		rso->so_rcv.sb_lowat = 1;
+		if (wso->so_snd.sb_lowat < PIPE_BUF) {
+			wso->so_snd.sb_hiwat = PIPE_BUF * 2;
+		}
+		wso->so_snd.sb_lowat = PIPE_BUF;
+
 		fip->fi_readers = 0;
 		fip->fi_writers = 0;
 		wso->so_state |= SS_CANTRCVMORE;

Index: src/tests/lib/libc/sys/t_poll.c
diff -u src/tests/lib/libc/sys/t_poll.c:1.7 src/tests/lib/libc/sys/t_poll.c:1.8
--- src/tests/lib/libc/sys/t_poll.c:1.7	Sat Oct  2 15:50:06 2021
+++ src/tests/lib/libc/sys/t_poll.c	Sat Oct  2 17:32:55 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_poll.c,v 1.7 2021/10/02 15:50:06 thorpej Exp $	*/
+/*	$NetBSD: t_poll.c,v 1.8 2021/10/02 17:32:55 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -330,6 +330,15 @@ ATF_TC_BODY(fifo_inout, tc)
 	ATF_REQUIRE(pfd[1].revents == 0);
 
 	/*
+	 * Now read enough so that exactly pipe_buf space should
+	 * be available.  The FIFO should be writable after that.
+	 * N.B. we don't care if it's readable at this point.
+	 */
+	ATF_REQUIRE(read(rfd, buf, pipe_buf - 1) == pipe_buf - 1);
+	ATF_REQUIRE(poll(pfd, 2, 0) >= 1);
+	ATF_REQUIRE(pfd[1].revents == (POLLOUT | POLLWRNORM));
+
+	/*
 	 * Now read all of the data out of the FIFO and ensure that
 	 * we get back to the initial state.
 	 */

Reply via email to