From a0a8ed61d85398bac349459fa11edfb21a63175b Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 15 Jan 2014 19:46:36 +0100 Subject: [PATCH] Add pipe2(2). --- kernel/include/sortix/syscallnum.h | 3 ++- kernel/pipe.cpp | 21 +++++++++++++++--- libc/Makefile | 1 + libc/include/unistd.h | 1 + libc/unistd/pipe.cpp | 8 ++----- libc/unistd/pipe2.cpp | 34 ++++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 libc/unistd/pipe2.cpp diff --git a/kernel/include/sortix/syscallnum.h b/kernel/include/sortix/syscallnum.h index d9301319..eac5b40c 100644 --- a/kernel/include/sortix/syscallnum.h +++ b/kernel/include/sortix/syscallnum.h @@ -146,6 +146,7 @@ #define SYSCALL_DUP3 122 #define SYSCALL_SYMLINKAT 123 #define SYSCALL_TCGETWINCURPOS 124 -#define SYSCALL_MAX_NUM 125 /* index of highest constant + 1 */ +#define SYSCALL_PIPE2 125 +#define SYSCALL_MAX_NUM 126 /* index of highest constant + 1 */ #endif diff --git a/kernel/pipe.cpp b/kernel/pipe.cpp index db0cba35..4d106ee1 100644 --- a/kernel/pipe.cpp +++ b/kernel/pipe.cpp @@ -318,8 +318,16 @@ int PipeNode::poll(ioctx_t* ctx, PollNode* node) namespace Pipe { -static int sys_pipe(int pipefd[2]) +static int sys_pipe2(int pipefd[2], int flags) { + int fdflags = 0; + if ( flags & O_CLOEXEC ) fdflags |= FD_CLOEXEC; + if ( flags & O_CLOFORK ) fdflags |= FD_CLOFORK; + flags &= ~(O_CLOEXEC | O_CLOFORK); + + if ( flags & ~(O_NONBLOCK) ) + return errno = EINVAL, -1; + Process* process = CurrentProcess(); uid_t uid = process->uid; uid_t gid = process->gid; @@ -344,9 +352,9 @@ static int sys_pipe(int pipefd[2]) Ref dtable = process->GetDTable(); int recv_index, send_index; - if ( 0 <= (recv_index = dtable->Allocate(recv_desc, 0)) ) + if ( 0 <= (recv_index = dtable->Allocate(recv_desc, fdflags)) ) { - if ( 0 <= (send_index = dtable->Allocate(send_desc, 0)) ) + if ( 0 <= (send_index = dtable->Allocate(send_desc, fdflags)) ) { int ret[2] = { recv_index, send_index }; if ( CopyToUser(pipefd, ret, sizeof(ret)) ) @@ -360,9 +368,16 @@ static int sys_pipe(int pipefd[2]) return -1; } +// TODO: This system call is replaced by pipe2, will be removed soon. +static int sys_pipe(int pipefd[2]) +{ + return sys_pipe2(pipefd, 0); +} + void Init() { Syscall::Register(SYSCALL_PIPE, (void*) sys_pipe); + Syscall::Register(SYSCALL_PIPE2, (void*) sys_pipe2); } } // namespace Pipe diff --git a/libc/Makefile b/libc/Makefile index 209ae054..95ee10d7 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -507,6 +507,7 @@ unistd/lseek.o \ unistd/memstat.o \ unistd/mkpartition.o \ unistd/pathconf.o \ +unistd/pipe2.o \ unistd/pipe.o \ unistd/pread.o \ unistd/pwrite.o \ diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 5ab4fee3..14257fc6 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -369,6 +369,7 @@ int linkat(int, const char*, int, const char*, int); off_t lseek(int, off_t, int); long pathconf(const char*, int); int pipe(int [2]); +int pipe2(int [2], int); ssize_t pread(int, void*, size_t, off_t); ssize_t pwrite(int, const void*, size_t, off_t); ssize_t readlink(const char* __restrict, char* __restrict, size_t); diff --git a/libc/unistd/pipe.cpp b/libc/unistd/pipe.cpp index 18b65d91..d30bdcfb 100644 --- a/libc/unistd/pipe.cpp +++ b/libc/unistd/pipe.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014. This file is part of the Sortix C Library. @@ -22,13 +22,9 @@ *******************************************************************************/ -#include - #include -DEFN_SYSCALL1(int, sys_pipe, SYSCALL_PIPE, int*); - extern "C" int pipe(int pipefd[2]) { - return sys_pipe(pipefd); + return pipe2(pipefd, 0); } diff --git a/libc/unistd/pipe2.cpp b/libc/unistd/pipe2.cpp new file mode 100644 index 00000000..a428458d --- /dev/null +++ b/libc/unistd/pipe2.cpp @@ -0,0 +1,34 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + unistd/pipe2.cpp + Creates a pair of file descriptors with a reading and writing end. + +*******************************************************************************/ + +#include + +#include + +DEFN_SYSCALL2(int, sys_pipe2, SYSCALL_PIPE2, int*, int); + +extern "C" int pipe2(int pipefd[2], int flags) +{ + return sys_pipe2(pipefd, flags); +}