xref: /llvm-project/libc/src/unistd/linux/pipe.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
15fb82156SMichael Jones //===-- Linux implementation of pipe --------------------------------------===//
25fb82156SMichael Jones //
35fb82156SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45fb82156SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
55fb82156SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65fb82156SMichael Jones //
75fb82156SMichael Jones //===----------------------------------------------------------------------===//
85fb82156SMichael Jones 
95fb82156SMichael Jones #include "src/unistd/pipe.h"
105fb82156SMichael Jones 
115fb82156SMichael Jones #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
125fb82156SMichael Jones #include "src/__support/common.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
14b8de7cf6SMichael Jones #include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
155fb82156SMichael Jones #include "src/errno/libc_errno.h"
165fb82156SMichael Jones #include <sys/syscall.h> // For syscall numbers.
175fb82156SMichael Jones 
18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
195fb82156SMichael Jones 
205fb82156SMichael Jones LLVM_LIBC_FUNCTION(int, pipe, (int pipefd[2])) {
215fb82156SMichael Jones #ifdef SYS_pipe
225fb82156SMichael Jones   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pipe,
235fb82156SMichael Jones                                               reinterpret_cast<long>(pipefd));
245fb82156SMichael Jones #elif defined(SYS_pipe2)
255fb82156SMichael Jones   int ret = LIBC_NAMESPACE::syscall_impl<int>(
265fb82156SMichael Jones       SYS_pipe2, reinterpret_cast<long>(pipefd), 0);
275fb82156SMichael Jones #endif
28b8de7cf6SMichael Jones   MSAN_UNPOISON(pipefd, sizeof(int) * 2);
295fb82156SMichael Jones   if (ret < 0) {
305fb82156SMichael Jones     libc_errno = -ret;
315fb82156SMichael Jones     return -1;
325fb82156SMichael Jones   }
335fb82156SMichael Jones   return ret;
345fb82156SMichael Jones }
355fb82156SMichael Jones 
36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
37