11.69Sryo/*	$NetBSD: linux_pipe.c,v 1.69 2021/09/23 06:56:27 ryo 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.69Sryo__KERNEL_RCSID(0, "$NetBSD: linux_pipe.c,v 1.69 2021/09/23 06:56:27 ryo 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/mbuf.h>
391.1Sfvdl#include <sys/mman.h>
401.1Sfvdl#include <sys/mount.h>
411.64Schristos#include <sys/fcntl.h>
421.64Schristos#include <sys/filedesc.h>
431.1Sfvdl
441.63Stsutsui#include <sys/sched.h>
451.1Sfvdl#include <sys/syscallargs.h>
461.1Sfvdl
471.48Schristos#include <compat/linux/common/linux_types.h>
481.48Schristos#include <compat/linux/common/linux_mmap.h>
491.48Schristos#include <compat/linux/common/linux_signal.h>
501.59Snjoly#include <compat/linux/common/linux_ipc.h>
511.59Snjoly#include <compat/linux/common/linux_sem.h>
521.64Schristos#include <compat/linux/common/linux_fcntl.h>
531.48Schristos
541.1Sfvdl#include <compat/linux/linux_syscallargs.h>
551.1Sfvdl
561.69Sryo/* Used on: aarch64, arm, i386, m68k, ppc, amd64 */
571.47Serh/* Not used on: alpha, mips, sparc, sparc64 */
581.47Serh/* Alpha, mips, sparc and sparc64 pass one of the fds in a register */
591.1Sfvdl
601.69Sryo#if !defined(__aarch64__)
611.1Sfvdlint
621.64Schristoslinux_sys_pipe(struct lwp *l, const struct linux_sys_pipe_args *uap,
631.64Schristos    register_t *retval)
641.20Sthorpej{
651.61Sdsl	/* {
661.1Sfvdl		syscallarg(int *) pfds;
671.61Sdsl	} */
681.68Skamil	int fd[2], error;
691.1Sfvdl
701.68Skamil	if ((error = pipe1(l, fd, 0)))
711.1Sfvdl		return error;
721.1Sfvdl
731.68Skamil	if ((error = copyout(fd, SCARG(uap, pfds), sizeof(fd))) != 0)
741.68Skamil		return error;
751.68Skamil	retval[0] = 0;
761.68Skamil	return 0;
771.64Schristos}
781.69Sryo#endif
791.64Schristos
801.64Schristosint
811.64Schristoslinux_sys_pipe2(struct lwp *l, const struct linux_sys_pipe2_args *uap,
821.64Schristos    register_t *retval)
831.64Schristos{
841.64Schristos	/* {
851.64Schristos		syscallarg(int *) pfds;
861.64Schristos		syscallarg(int) flags;
871.64Schristos	} */
881.68Skamil	int fd[2], error, flags;
891.64Schristos
901.66Snjoly	flags = linux_to_bsd_ioflags(SCARG(uap, flags));
911.66Snjoly	if ((flags & ~(O_CLOEXEC|O_NONBLOCK)) != 0)
921.64Schristos		return EINVAL;
931.64Schristos
941.68Skamil	if ((error = pipe1(l, fd, flags)))
951.1Sfvdl		return error;
961.54Smanu
971.68Skamil	if ((error = copyout(fd, SCARG(uap, pfds), sizeof(fd))) != 0)
981.68Skamil		return error;
991.68Skamil	retval[0] = 0;
1001.68Skamil	return 0;
1011.64Schristos}
102