From 6be064a2b6d920ce698bdb496cedce356f41a35e Mon Sep 17 00:00:00 2001
From: Glauber Costa <glommer@cloudius-systems.com>
Date: Mon, 26 May 2014 16:21:49 +0400
Subject: [PATCH] pipe: implement pipe2

Similar to pipe, but taking flags. We will ignore exec related flags.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
---
 libc/pipe.cc | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/libc/pipe.cc b/libc/pipe.cc
index 40dd061e1..e92ec75ad 100644
--- a/libc/pipe.cc
+++ b/libc/pipe.cc
@@ -92,7 +92,11 @@ int pipe_file::close()
     return 0;
 }
 
-int pipe(int pipefd[2]) {
+int pipe2(int pipefd[2], int flags) {
+    if (flags & ~(O_NONBLOCK | O_CLOEXEC)) {
+        return libc_error(EINVAL);
+    }
+
     auto b = new pipe_buffer;
     std::unique_ptr<pipe_reader> s1{new pipe_reader(b)};
     std::unique_ptr<pipe_writer> s2{new pipe_writer(b)};
@@ -101,6 +105,13 @@ int pipe(int pipefd[2]) {
         fileref f2 = make_file<pipe_file>(move(s2));
         fdesc fd1(f1);
         fdesc fd2(f2);
+
+        // O_CLOEXEC ignored by now
+        if (flags & O_NONBLOCK) {
+            f1->f_flags &= FNONBLOCK;
+            f2->f_flags &= FNONBLOCK;
+        }
+
         // all went well, user owns descriptors now
         pipefd[0] = fd1.release();
         pipefd[1] = fd2.release();
@@ -109,3 +120,8 @@ int pipe(int pipefd[2]) {
         return libc_error(error);
     }
 }
+
+int pipe(int pipefd[2])
+{
+    return pipe2(pipefd, 0);
+}
-- 
GitLab