linux_pipe.c revision 1.64
11.64Schristos/*	$NetBSD: linux_pipe.c,v 1.64 2011/04/10 15:50:34 christos Exp $	*/
21.47Serh
31.47Serh/*-
41.49Sfvdl * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
51.47Serh * All rights reserved.
61.47Serh *
71.47Serh * This code is derived from software contributed to The NetBSD Foundation
81.49Sfvdl * by Frank van der Linden and Eric Haszlakiewicz.
91.47Serh *
101.47Serh * Redistribution and use in source and binary forms, with or without
111.47Serh * modification, are permitted provided that the following conditions
121.47Serh * are met:
131.47Serh * 1. Redistributions of source code must retain the above copyright
141.47Serh *    notice, this list of conditions and the following disclaimer.
151.47Serh * 2. Redistributions in binary form must reproduce the above copyright
161.47Serh *    notice, this list of conditions and the following disclaimer in the
171.47Serh *    documentation and/or other materials provided with the distribution.
181.47Serh *
191.47Serh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.47Serh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.47Serh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.47Serh * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.47Serh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.47Serh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.47Serh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.47Serh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.47Serh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.47Serh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.47Serh * POSSIBILITY OF SUCH DAMAGE.
301.1Sfvdl */
311.52Slukem
321.52Slukem#include <sys/cdefs.h>
331.64Schristos__KERNEL_RCSID(0, "$NetBSD: linux_pipe.c,v 1.64 2011/04/10 15:50:34 christos Exp $");
341.1Sfvdl
351.1Sfvdl#include <sys/param.h>
361.1Sfvdl#include <sys/systm.h>
371.1Sfvdl#include <sys/kernel.h>
381.1Sfvdl#include <sys/malloc.h>
391.1Sfvdl#include <sys/mbuf.h>
401.1Sfvdl#include <sys/mman.h>
411.1Sfvdl#include <sys/mount.h>
421.64Schristos#include <sys/fcntl.h>
431.64Schristos#include <sys/filedesc.h>
441.1Sfvdl
451.63Stsutsui#include <sys/sched.h>
461.1Sfvdl#include <sys/syscallargs.h>
471.1Sfvdl
481.48Schristos#include <compat/linux/common/linux_types.h>
491.48Schristos#include <compat/linux/common/linux_mmap.h>
501.48Schristos#include <compat/linux/common/linux_signal.h>
511.59Snjoly#include <compat/linux/common/linux_ipc.h>
521.59Snjoly#include <compat/linux/common/linux_sem.h>
531.64Schristos#include <compat/linux/common/linux_fcntl.h>
541.48Schristos
551.1Sfvdl#include <compat/linux/linux_syscallargs.h>
561.1Sfvdl
571.54Smanu/* Used on: arm, i386, m68k, ppc, amd64 */
581.47Serh/* Not used on: alpha, mips, sparc, sparc64 */
591.47Serh/* Alpha, mips, sparc and sparc64 pass one of the fds in a register */
601.1Sfvdl
611.1Sfvdl/*
621.1Sfvdl * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
631.1Sfvdl * Linux directly passes the pointer.
641.1Sfvdl */
651.64Schristosstatic int
661.64Schristoslinux_pipe_return(struct lwp *l, int *pfds, register_t *retval, int flags)
671.64Schristos{
681.64Schristos	int error;
691.64Schristos
701.64Schristos	if (sizeof(*retval) != sizeof(*pfds)) {
711.64Schristos		/* On amd64, sizeof(register_t) != sizeof(int) */
721.64Schristos		int rpfds[2];
731.64Schristos		rpfds[0] = (int)retval[0];
741.64Schristos		rpfds[1] = (int)retval[1];
751.64Schristos
761.64Schristos		if ((error = copyout(rpfds, pfds, sizeof(rpfds))))
771.64Schristos			return error;
781.64Schristos	} else {
791.64Schristos		if ((error = copyout(retval, pfds, 2 * sizeof(*pfds))))
801.64Schristos			return error;
811.64Schristos	}
821.64Schristos	if (flags & LINUX_O_CLOEXEC) {
831.64Schristos		fd_set_exclose(l, retval[0], true);
841.64Schristos		fd_set_exclose(l, retval[1], true);
851.64Schristos	}
861.64Schristos	retval[0] = 0;
871.64Schristos	return 0;
881.64Schristos}
891.64Schristos
901.1Sfvdlint
911.64Schristoslinux_sys_pipe(struct lwp *l, const struct linux_sys_pipe_args *uap,
921.64Schristos    register_t *retval)
931.20Sthorpej{
941.61Sdsl	/* {
951.1Sfvdl		syscallarg(int *) pfds;
961.61Sdsl	} */
971.1Sfvdl	int error;
981.1Sfvdl
991.64Schristos	if ((error = pipe1(l, retval, 0)))
1001.1Sfvdl		return error;
1011.1Sfvdl
1021.64Schristos	return linux_pipe_return(l, SCARG(uap, pfds), retval, 0);
1031.64Schristos}
1041.64Schristos
1051.64Schristosint
1061.64Schristoslinux_sys_pipe2(struct lwp *l, const struct linux_sys_pipe2_args *uap,
1071.64Schristos    register_t *retval)
1081.64Schristos{
1091.64Schristos	/* {
1101.64Schristos		syscallarg(int *) pfds;
1111.64Schristos		syscallarg(int) flags;
1121.64Schristos	} */
1131.64Schristos	int error;
1141.64Schristos	int flag = 0;
1151.64Schristos
1161.64Schristos	switch (SCARG(uap, flags)) {
1171.64Schristos	case LINUX_O_CLOEXEC:
1181.64Schristos		break;
1191.64Schristos	case LINUX_O_NONBLOCK:
1201.64Schristos	case LINUX_O_NONBLOCK|LINUX_O_CLOEXEC:
1211.64Schristos		flag = O_NONBLOCK;
1221.64Schristos		break;
1231.64Schristos	default:
1241.64Schristos		return EINVAL;
1251.64Schristos	}
1261.64Schristos
1271.64Schristos	if ((error = pipe1(l, retval, flag)))
1281.1Sfvdl		return error;
1291.54Smanu
1301.64Schristos	return linux_pipe_return(l, SCARG(uap, pfds), retval,
1311.64Schristos	    SCARG(uap, flags));
1321.64Schristos}
1331.64Schristos
1341.64Schristosint
1351.64Schristoslinux_sys_dup3(struct lwp *l, const struct linux_sys_dup3_args *uap,
1361.64Schristos    register_t *retval)
1371.64Schristos{
1381.64Schristos	/* {
1391.64Schristos		syscallarg(int) from;
1401.64Schristos		syscallarg(int) to;
1411.64Schristos		syscallarg(int) flags;
1421.64Schristos	} */
1431.64Schristos	int error;
1441.64Schristos	if ((error = sys_dup2(l, (const struct sys_dup2_args *)uap, retval)))
1451.54Smanu		return error;
1461.1Sfvdl
1471.64Schristos	if (SCARG(uap, flags) & LINUX_O_CLOEXEC)
1481.64Schristos		fd_set_exclose(l, SCARG(uap, to), true);
1491.64Schristos
1501.1Sfvdl	return 0;
1511.1Sfvdl}
152