Module Name:    src
Committed By:   kamil
Date:           Tue Dec 26 08:30:58 UTC 2017

Modified Files:
        src/sys/compat/linux/arch/alpha: linux_pipe.c
        src/sys/compat/linux/common: linux_pipe.c
        src/sys/compat/linux32/common: linux32_unistd.c
        src/sys/compat/netbsd32: netbsd32_netbsd.c
        src/sys/kern: sys_descrip.c sys_pipe.c uipc_syscalls.c
        src/sys/sys: filedesc.h

Log Message:
Refactor pipe1() and correct a bug in sys_pipe2() (SYS_pipe2)

sys_pipe2() returns two integers (values), the 2nd one is a copy of the 2nd
file descriptor that lands in fildes[2]. This is a side effect of reusing
the code for sys_pipe() (SYS_pipe) and not cleaning it up.

The first returned value is (on success) 0.

Introduced a small refactoring in pipe1() that it does not operate over
retval[], but on an array int[2]. A user sets retval[] for pipe() when
desired and needed.

This refactoring touches compat code: netbsd32, linux, linux32.

Before the changes on NetBSD/amd64:

$ ktruss -i ./a.out
[...]
 15131      1 a.out    pipe2(0x7f7fff2e62b8, 0)    = 0, 4
[...]

After the changes:

$ ktruss -i ./a.out
[...]
   782      1 a.out    pipe2(0x7f7fff97e850, 0)    = 0
[...]

There should not be a visible change for current users.

Sponsored by <The NetBSD Foundation>


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/compat/linux/arch/alpha/linux_pipe.c
cvs rdiff -u -r1.67 -r1.68 src/sys/compat/linux/common/linux_pipe.c
cvs rdiff -u -r1.39 -r1.40 src/sys/compat/linux32/common/linux32_unistd.c
cvs rdiff -u -r1.211 -r1.212 src/sys/compat/netbsd32/netbsd32_netbsd.c
cvs rdiff -u -r1.30 -r1.31 src/sys/kern/sys_descrip.c
cvs rdiff -u -r1.142 -r1.143 src/sys/kern/sys_pipe.c
cvs rdiff -u -r1.187 -r1.188 src/sys/kern/uipc_syscalls.c
cvs rdiff -u -r1.63 -r1.64 src/sys/sys/filedesc.h

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

Modified files:

Index: src/sys/compat/linux/arch/alpha/linux_pipe.c
diff -u src/sys/compat/linux/arch/alpha/linux_pipe.c:1.17 src/sys/compat/linux/arch/alpha/linux_pipe.c:1.18
--- src/sys/compat/linux/arch/alpha/linux_pipe.c:1.17	Sun Nov  9 17:48:07 2014
+++ src/sys/compat/linux/arch/alpha/linux_pipe.c	Tue Dec 26 08:30:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_pipe.c,v 1.17 2014/11/09 17:48:07 maxv Exp $	*/
+/*	$NetBSD: linux_pipe.c,v 1.18 2017/12/26 08:30:57 kamil Exp $	*/
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_pipe.c,v 1.17 2014/11/09 17:48:07 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_pipe.c,v 1.18 2017/12/26 08:30:57 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -62,12 +62,13 @@ __KERNEL_RCSID(0, "$NetBSD: linux_pipe.c
 int
 linux_sys_pipe(struct lwp *l, const void *v, register_t *retval)
 {
-	int error;
+	int fd[2], error;
 
-	if ((error = pipe1(l, retval, 0)))
+	if ((error = pipe1(l, fd, 0)))
 		return error;
 
-	(l->l_md.md_tf)->tf_regs[FRAME_A4] = retval[1];
+	retval[0] = fd[0];
+	(l->l_md.md_tf)->tf_regs[FRAME_A4] = fd[1];
 	return 0;
 }
 
@@ -79,16 +80,17 @@ linux_sys_pipe2(struct lwp *l, const str
 		syscallarg(int *) pfds;
 		syscallarg(int) flags;
 	} */
-	int error, flags;
+	int fd[2], error, flags;
 
 	flags = linux_to_bsd_ioflags(SCARG(uap, flags));
 	if ((flags & ~(O_CLOEXEC|O_NONBLOCK)) != 0)
 		return EINVAL;
 
-	if ((error = pipe1(l, retval, flags)))
+	if ((error = pipe1(l, fd, flags)))
 		return error;
 
-	(l->l_md.md_tf)->tf_regs[FRAME_A4] = retval[1];
+	retval[0] = fd[0];
+	(l->l_md.md_tf)->tf_regs[FRAME_A4] = fd[1];
 
 	return 0;
 }

Index: src/sys/compat/linux/common/linux_pipe.c
diff -u src/sys/compat/linux/common/linux_pipe.c:1.67 src/sys/compat/linux/common/linux_pipe.c:1.68
--- src/sys/compat/linux/common/linux_pipe.c:1.67	Sun Nov  9 17:48:08 2014
+++ src/sys/compat/linux/common/linux_pipe.c	Tue Dec 26 08:30:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_pipe.c,v 1.67 2014/11/09 17:48:08 maxv Exp $	*/
+/*	$NetBSD: linux_pipe.c,v 1.68 2017/12/26 08:30:57 kamil Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_pipe.c,v 1.67 2014/11/09 17:48:08 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_pipe.c,v 1.68 2017/12/26 08:30:57 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -57,31 +57,6 @@ __KERNEL_RCSID(0, "$NetBSD: linux_pipe.c
 /* Not used on: alpha, mips, sparc, sparc64 */
 /* Alpha, mips, sparc and sparc64 pass one of the fds in a register */
 
-/*
- * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
- * Linux directly passes the pointer.
- */
-static int
-linux_pipe_return(struct lwp *l, int *pfds, register_t *retval)
-{
-	int error;
-
-	if (sizeof(*retval) != sizeof(*pfds)) {
-		/* On amd64, sizeof(register_t) != sizeof(int) */
-		int rpfds[2];
-		rpfds[0] = (int)retval[0];
-		rpfds[1] = (int)retval[1];
-
-		if ((error = copyout(rpfds, pfds, sizeof(rpfds))))
-			return error;
-	} else {
-		if ((error = copyout(retval, pfds, 2 * sizeof(*pfds))))
-			return error;
-	}
-	retval[0] = 0;
-	return 0;
-}
-
 int
 linux_sys_pipe(struct lwp *l, const struct linux_sys_pipe_args *uap,
     register_t *retval)
@@ -89,12 +64,15 @@ linux_sys_pipe(struct lwp *l, const stru
 	/* {
 		syscallarg(int *) pfds;
 	} */
-	int error;
+	int fd[2], error;
 
-	if ((error = pipe1(l, retval, 0)))
+	if ((error = pipe1(l, fd, 0)))
 		return error;
 
-	return linux_pipe_return(l, SCARG(uap, pfds), retval);
+	if ((error = copyout(fd, SCARG(uap, pfds), sizeof(fd))) != 0)
+		return error;
+	retval[0] = 0;
+	return 0;
 }
 
 int
@@ -105,14 +83,17 @@ linux_sys_pipe2(struct lwp *l, const str
 		syscallarg(int *) pfds;
 		syscallarg(int) flags;
 	} */
-	int error, flags;
+	int fd[2], error, flags;
 
 	flags = linux_to_bsd_ioflags(SCARG(uap, flags));
 	if ((flags & ~(O_CLOEXEC|O_NONBLOCK)) != 0)
 		return EINVAL;
 
-	if ((error = pipe1(l, retval, flags)))
+	if ((error = pipe1(l, fd, flags)))
 		return error;
 
-	return linux_pipe_return(l, SCARG(uap, pfds), retval);
+	if ((error = copyout(fd, SCARG(uap, pfds), sizeof(fd))) != 0)
+		return error;
+	retval[0] = 0;
+	return 0;
 }

Index: src/sys/compat/linux32/common/linux32_unistd.c
diff -u src/sys/compat/linux32/common/linux32_unistd.c:1.39 src/sys/compat/linux32/common/linux32_unistd.c:1.40
--- src/sys/compat/linux32/common/linux32_unistd.c:1.39	Sun Jun  1 13:42:12 2014
+++ src/sys/compat/linux32/common/linux32_unistd.c	Tue Dec 26 08:30:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux32_unistd.c,v 1.39 2014/06/01 13:42:12 njoly Exp $ */
+/*	$NetBSD: linux32_unistd.c,v 1.40 2017/12/26 08:30:58 kamil Exp $ */
 
 /*-
  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
@@ -33,7 +33,7 @@
 
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(0, "$NetBSD: linux32_unistd.c,v 1.39 2014/06/01 13:42:12 njoly Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux32_unistd.c,v 1.40 2017/12/26 08:30:58 kamil Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -225,51 +225,44 @@ linux32_select1(struct lwp *l, register_
 	return 0;
 }
 
-static int
-linux32_pipe(struct lwp *l, int *fd, register_t *retval)
+int
+linux32_sys_pipe(struct lwp *l, const struct linux32_sys_pipe_args *uap,
+    register_t *retval)
 {
 	/* {
 		syscallarg(netbsd32_intp) fd;
 	} */
-	int error;
-	int pfds[2];
-
-	pfds[0] = (int)retval[0];
-	pfds[1] = (int)retval[1];
+	int f[2], error;
 
-	if ((error = copyout(pfds, fd, 2 * sizeof(*fd))) != 0)
+	if ((error = pipe1(l, f, 0)))
 		return error;
 
+	if ((error = copyout(f, SCARG_P32(uap, fd), sizeof(f))) != 0)
+		return error;
 	retval[0] = 0;
-	retval[1] = 0;
-
 	return 0;
 }
 
 int
-linux32_sys_pipe(struct lwp *l, const struct linux32_sys_pipe_args *uap,
-    register_t *retval)
-{
-	int error;
-	if ((error = pipe1(l, retval, 0)))
-		return error;
-	return linux32_pipe(l, SCARG_P32(uap, fd), retval);
-}
-
-int
 linux32_sys_pipe2(struct lwp *l, const struct linux32_sys_pipe2_args *uap,
     register_t *retval)
 {
-	int flags, error;
+	/* {
+		syscallarg(netbsd32_intp) fd;
+	} */
+	int f[2], flags, error;
 
 	flags = linux_to_bsd_ioflags(SCARG(uap, flags));
 	if ((flags & ~(O_CLOEXEC|O_NONBLOCK)) != 0)
 		return EINVAL;
 
-	if ((error = pipe1(l, retval, flags)))
+	if ((error = pipe1(l, f, flags)))
 		return error;
 
-	return linux32_pipe(l, SCARG_P32(uap, fd), retval);
+	if ((error = copyout(f, SCARG_P32(uap, fd), sizeof(f))) != 0)
+		return error;
+	retval[0] = 0;
+	return 0;
 }
 
 int
@@ -741,4 +734,3 @@ linux32_sys_pwrite(struct lwp *l,
 
 	return sys_pwrite(l, &pra, retval);
 }
-

Index: src/sys/compat/netbsd32/netbsd32_netbsd.c
diff -u src/sys/compat/netbsd32/netbsd32_netbsd.c:1.211 src/sys/compat/netbsd32/netbsd32_netbsd.c:1.212
--- src/sys/compat/netbsd32/netbsd32_netbsd.c:1.211	Tue Dec 19 19:40:03 2017
+++ src/sys/compat/netbsd32/netbsd32_netbsd.c	Tue Dec 26 08:30:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_netbsd.c,v 1.211 2017/12/19 19:40:03 kamil Exp $	*/
+/*	$NetBSD: netbsd32_netbsd.c,v 1.212 2017/12/26 08:30:58 kamil Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001, 2008 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_netbsd.c,v 1.211 2017/12/19 19:40:03 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_netbsd.c,v 1.212 2017/12/26 08:30:58 kamil Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ddb.h"
@@ -2687,13 +2687,10 @@ netbsd32_pipe2(struct lwp *l, const stru
 	} */
 	int fd[2], error;
 
-	error = pipe1(l, retval, SCARG(uap, flags));
+	error = pipe1(l, fd, SCARG(uap, flags));
 	if (error != 0)
 		return error;
 
-	fd[0] = retval[0];
-	fd[1] = retval[1];
-
 	error = copyout(fd, SCARG_P32(uap, fildes), sizeof(fd));
 	if (error != 0)
 		return error;

Index: src/sys/kern/sys_descrip.c
diff -u src/sys/kern/sys_descrip.c:1.30 src/sys/kern/sys_descrip.c:1.31
--- src/sys/kern/sys_descrip.c:1.30	Fri Sep  5 09:20:59 2014
+++ src/sys/kern/sys_descrip.c	Tue Dec 26 08:30:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_descrip.c,v 1.30 2014/09/05 09:20:59 matt Exp $	*/
+/*	$NetBSD: sys_descrip.c,v 1.31 2017/12/26 08:30:58 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.30 2014/09/05 09:20:59 matt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.31 2017/12/26 08:30:58 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -764,7 +764,15 @@ sys___posix_fadvise50(struct lwp *l,
 int
 sys_pipe(struct lwp *l, const void *v, register_t *retval)
 {
-	return pipe1(l, retval, 0);
+	int fd[2], error;
+
+	if ((error = pipe1(l, fd, 0)) != 0)
+		return error;
+
+	retval[0] = fd[0];
+	retval[1] = fd[1];
+
+	return 0;
 }
 
 int
@@ -776,10 +784,9 @@ sys_pipe2(struct lwp *l, const struct sy
 	} */
 	int fd[2], error;
 
-	if ((error = pipe1(l, retval, SCARG(uap, flags))) != 0)
+	if ((error = pipe1(l, fd, SCARG(uap, flags))) != 0)
 		return error;
-	fd[0] = retval[0];
-	fd[1] = retval[1];
+
 	if ((error = copyout(fd, SCARG(uap, fildes), sizeof(fd))) != 0)
 		return error;
 	retval[0] = 0;

Index: src/sys/kern/sys_pipe.c
diff -u src/sys/kern/sys_pipe.c:1.142 src/sys/kern/sys_pipe.c:1.143
--- src/sys/kern/sys_pipe.c:1.142	Thu Nov 30 20:25:55 2017
+++ src/sys/kern/sys_pipe.c	Tue Dec 26 08:30:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_pipe.c,v 1.142 2017/11/30 20:25:55 christos Exp $	*/
+/*	$NetBSD: sys_pipe.c,v 1.143 2017/12/26 08:30:58 kamil 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.142 2017/11/30 20:25:55 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.143 2017/12/26 08:30:58 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -245,7 +245,7 @@ pipe_dtor(void *arg, void *obj)
  * The pipe system call for the DTYPE_PIPE type of pipes
  */
 int
-pipe1(struct lwp *l, register_t *retval, int flags)
+pipe1(struct lwp *l, int *fildes, int flags)
 {
 	struct pipe *rpipe, *wpipe;
 	file_t *rf, *wf;
@@ -267,33 +267,33 @@ pipe1(struct lwp *l, register_t *retval,
 	error = fd_allocfile(&rf, &fd);
 	if (error)
 		goto free2;
-	retval[0] = fd;
+	fildes[0] = fd;
 
 	error = fd_allocfile(&wf, &fd);
 	if (error)
 		goto free3;
-	retval[1] = fd;
+	fildes[1] = fd;
 
 	rf->f_flag = FREAD | flags;
 	rf->f_type = DTYPE_PIPE;
 	rf->f_pipe = rpipe;
 	rf->f_ops = &pipeops;
-	fd_set_exclose(l, (int)retval[0], (flags & O_CLOEXEC) != 0);
+	fd_set_exclose(l, fildes[0], (flags & O_CLOEXEC) != 0);
 
 	wf->f_flag = FWRITE | flags;
 	wf->f_type = DTYPE_PIPE;
 	wf->f_pipe = wpipe;
 	wf->f_ops = &pipeops;
-	fd_set_exclose(l, (int)retval[1], (flags & O_CLOEXEC) != 0);
+	fd_set_exclose(l, fildes[1], (flags & O_CLOEXEC) != 0);
 
 	rpipe->pipe_peer = wpipe;
 	wpipe->pipe_peer = rpipe;
 
-	fd_affix(p, rf, (int)retval[0]);
-	fd_affix(p, wf, (int)retval[1]);
+	fd_affix(p, rf, fildes[0]);
+	fd_affix(p, wf, fildes[1]);
 	return (0);
 free3:
-	fd_abort(p, rf, (int)retval[0]);
+	fd_abort(p, rf, fildes[0]);
 free2:
 	pipeclose(wpipe);
 	pipeclose(rpipe);

Index: src/sys/kern/uipc_syscalls.c
diff -u src/sys/kern/uipc_syscalls.c:1.187 src/sys/kern/uipc_syscalls.c:1.188
--- src/sys/kern/uipc_syscalls.c:1.187	Tue Jun 20 20:34:49 2017
+++ src/sys/kern/uipc_syscalls.c	Tue Dec 26 08:30:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_syscalls.c,v 1.187 2017/06/20 20:34:49 christos Exp $	*/
+/*	$NetBSD: uipc_syscalls.c,v 1.188 2017/12/26 08:30:58 kamil 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.187 2017/06/20 20:34:49 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.188 2017/12/26 08:30:58 kamil Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pipe.h"
@@ -1265,7 +1265,7 @@ sys_getsockopt(struct lwp *l, const stru
 #ifdef PIPE_SOCKETPAIR
 
 int
-pipe1(struct lwp *l, register_t *retval, int flags)
+pipe1(struct lwp *l, int *fildes, int flags)
 {
 	file_t		*rf, *wf;
 	struct socket	*rso, *wso;
@@ -1284,7 +1284,7 @@ pipe1(struct lwp *l, register_t *retval,
 	rso->so_state |= SS_ISAPIPE;
 	if ((error = fd_allocfile(&rf, &fd)) != 0)
 		goto free2;
-	retval[0] = fd;
+	fildes[0] = fd;
 	rf->f_flag = FREAD | flags;
 	rf->f_type = DTYPE_SOCKET;
 	rf->f_ops = &socketops;
@@ -1295,19 +1295,19 @@ pipe1(struct lwp *l, register_t *retval,
 	wf->f_type = DTYPE_SOCKET;
 	wf->f_ops = &socketops;
 	wf->f_socket = wso;
-	retval[1] = fd;
+	fildes[1] = fd;
 	solock(wso);
 	error = unp_connect2(wso, rso);
 	sounlock(wso);
 	if (error != 0)
 		goto free4;
-	fd_affix(p, wf, (int)retval[1]);
-	fd_affix(p, rf, (int)retval[0]);
+	fd_affix(p, wf, fildes[1]);
+	fd_affix(p, rf, fildes[0]);
 	return (0);
  free4:
-	fd_abort(p, wf, (int)retval[1]);
+	fd_abort(p, wf, fildes[1]);
  free3:
-	fd_abort(p, rf, (int)retval[0]);
+	fd_abort(p, rf, fildes[0]);
  free2:
 	(void)soclose(wso);
  free1:

Index: src/sys/sys/filedesc.h
diff -u src/sys/sys/filedesc.h:1.63 src/sys/sys/filedesc.h:1.64
--- src/sys/sys/filedesc.h:1.63	Sat Feb 11 23:16:18 2012
+++ src/sys/sys/filedesc.h	Tue Dec 26 08:30:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: filedesc.h,v 1.63 2012/02/11 23:16:18 martin Exp $	*/
+/*	$NetBSD: filedesc.h,v 1.64 2017/12/26 08:30:58 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -209,7 +209,7 @@ int	fd_dup(file_t *, int, int *, bool);
 int	fd_dup2(file_t *, unsigned, int);
 int	fd_clone(file_t *, unsigned, int, const struct fileops *, void *);
 void	fd_set_exclose(struct lwp *, int, bool);
-int	pipe1(struct lwp *, register_t *, int);
+int	pipe1(struct lwp *, int *, int);
 int	dodup(struct lwp *, int, int, int, register_t *);
 
 void	cwd_sys_init(void);

Reply via email to