[libc-commits] [libc] [libc] pipe(2) linux syscall wrapper and unittest (PR #85514)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Mon Mar 18 12:01:00 PDT 2024


================
@@ -0,0 +1,36 @@
+//===-- Linux implementation of pipe2
+//---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/pipe2.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pipe2, (int pipefd[2], int flags)) {
+  int ret;
+#ifdef SYS_pipe2
+  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pipe2, pipefd, flags);
+#elif defined(SYS_pipe)
+  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pipe, pipefd);
+#else
+#error "pipe and pipe2 not available."
+#endif
+  if (ret == -1) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return ret;
----------------
michaelrj-google wrote:

the usual design we use is
```
  if (ret < 0) {
    libc_errno = -ret;
    return -1;
  }
  return ret;
```
so I'd recommend that instead. Generally syscalls will return a negative value with a magnitude matching the intended errno value.

https://github.com/llvm/llvm-project/pull/85514


More information about the libc-commits mailing list