Module Name:    src
Committed By:   riz
Date:           Sat May 19 15:34:32 UTC 2012

Modified Files:
        src/sys/kern [netbsd-6]: sys_pipe.c
        src/tests/lib/libc/sys [netbsd-6]: t_pipe2.c

Log Message:
Pull up following revision(s) (requested by martin in ticket #270):
        sys/kern/sys_pipe.c: revision 1.136
        tests/lib/libc/sys/t_pipe2.c: revision 1.4
        tests/lib/libc/sys/t_pipe2.c: revision 1.5
        tests/lib/libc/sys/t_pipe2.c: revision 1.6
        tests/lib/libc/sys/t_pipe2.c: revision 1.7
Make sure we can deliver two file descriptors for pipe2() before we set
up anything special (like close on exec).
Fixes PR kern/46457.
Add a case for PR kern/46457. This is skipped for the time being, as it
reproduces the panic described in the PR.
Enable the test for PR kern/46457 now that it does not crash the
kernel any more.
Fix typo in comment.
Simplify the test for PR kern/4645 and make it independend of resource
settings.


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.135.2.1 src/sys/kern/sys_pipe.c
cvs rdiff -u -r1.3 -r1.3.2.1 src/tests/lib/libc/sys/t_pipe2.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/sys_pipe.c
diff -u src/sys/kern/sys_pipe.c:1.135 src/sys/kern/sys_pipe.c:1.135.2.1
--- src/sys/kern/sys_pipe.c:1.135	Wed Jan 25 00:28:36 2012
+++ src/sys/kern/sys_pipe.c	Sat May 19 15:34:32 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_pipe.c,v 1.135 2012/01/25 00:28:36 christos Exp $	*/
+/*	$NetBSD: sys_pipe.c,v 1.135.2.1 2012/05/19 15:34:32 riz Exp $	*/
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.135 2012/01/25 00:28:36 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.135.2.1 2012/05/19 15:34:32 riz Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -267,21 +267,23 @@ pipe1(struct lwp *l, register_t *retval,
 	if (error)
 		goto free2;
 	retval[0] = fd;
-	rf->f_flag = FREAD | flags;
-	rf->f_type = DTYPE_PIPE;
-	rf->f_data = (void *)rpipe;
-	rf->f_ops = &pipeops;
-	fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
 
 	error = fd_allocfile(&wf, &fd);
 	if (error)
 		goto free3;
 	retval[1] = fd;
+
+	rf->f_flag = FREAD | flags;
+	rf->f_type = DTYPE_PIPE;
+	rf->f_data = (void *)rpipe;
+	rf->f_ops = &pipeops;
+	fd_set_exclose(l, (int)retval[0], (flags & O_CLOEXEC) != 0);
+
 	wf->f_flag = FWRITE | flags;
 	wf->f_type = DTYPE_PIPE;
 	wf->f_data = (void *)wpipe;
 	wf->f_ops = &pipeops;
-	fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
+	fd_set_exclose(l, (int)retval[1], (flags & O_CLOEXEC) != 0);
 
 	rpipe->pipe_peer = wpipe;
 	wpipe->pipe_peer = rpipe;

Index: src/tests/lib/libc/sys/t_pipe2.c
diff -u src/tests/lib/libc/sys/t_pipe2.c:1.3 src/tests/lib/libc/sys/t_pipe2.c:1.3.2.1
--- src/tests/lib/libc/sys/t_pipe2.c:1.3	Sat Jan 28 02:47:09 2012
+++ src/tests/lib/libc/sys/t_pipe2.c	Sat May 19 15:34:32 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: t_pipe2.c,v 1.3 2012/01/28 02:47:09 christos Exp $ */
+/* $NetBSD: t_pipe2.c,v 1.3.2.1 2012/05/19 15:34:32 riz Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -36,13 +36,14 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_pipe2.c,v 1.3 2012/01/28 02:47:09 christos Exp $");
+__RCSID("$NetBSD: t_pipe2.c,v 1.3.2.1 2012/05/19 15:34:32 riz Exp $");
 
 #include <atf-c.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <sys/resource.h>
 
 static void
 run(int flags)
@@ -98,6 +99,39 @@ ATF_TC_BODY(pipe2_basic, tc)
 	run(0);
 }
 
+ATF_TC(pipe2_consume);
+ATF_TC_HEAD(pipe2_consume, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test that consuming file descriptors "
+	    "with pipe2(2) does not crash the system (PR kern/46457)");
+}
+
+ATF_TC_BODY(pipe2_consume, tc)
+{
+	struct rlimit rl;
+	int err, filedes[2];
+
+	err = fcntl(4, F_CLOSEM);
+	ATF_REQUIRE(err == 0);
+
+	err = getrlimit(RLIMIT_NOFILE, &rl);
+	ATF_REQUIRE(err == 0);
+	/*
+	 * The heart of this test is to run against the number of open
+	 * file descriptor limit in the middle of a pipe2() call - i.e.
+	 * before the call only a single descriptor may be openend.
+	 */
+	rl.rlim_cur = 4;
+	err = setrlimit(RLIMIT_NOFILE, &rl);
+	ATF_REQUIRE(err == 0);
+
+	/*
+	 * atf_tc_skip("The test case causes a panic (PR kern/46457)");
+	 */
+	err = pipe2(filedes, O_CLOEXEC);
+	ATF_REQUIRE(err == -1);
+}
+
 ATF_TC(pipe2_nonblock);
 ATF_TC_HEAD(pipe2_nonblock, tc)
 {
@@ -147,6 +181,7 @@ ATF_TP_ADD_TCS(tp)
 {
 
 	ATF_TP_ADD_TC(tp, pipe2_basic);
+	ATF_TP_ADD_TC(tp, pipe2_consume);
 	ATF_TP_ADD_TC(tp, pipe2_nonblock);
 	ATF_TP_ADD_TC(tp, pipe2_cloexec);
 	ATF_TP_ADD_TC(tp, pipe2_nosigpipe);

Reply via email to