Skip to content

Commit 6219c80

Browse files
[libc] [unistd] implement pipe2 syscall wrapper (#114474)
Closes #85289 Co-authored-by: Michael Jones <michaelrj@google.com>
1 parent 9f3b6ad commit 6219c80

File tree

11 files changed

+140
-0
lines changed

11 files changed

+140
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ set(TARGET_LIBC_ENTRYPOINTS
330330
libc.src.unistd.lseek
331331
libc.src.unistd.pathconf
332332
libc.src.unistd.pipe
333+
libc.src.unistd.pipe2
333334
libc.src.unistd.pread
334335
libc.src.unistd.pwrite
335336
libc.src.unistd.read

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ set(TARGET_LIBC_ENTRYPOINTS
329329
libc.src.unistd.lseek
330330
libc.src.unistd.pathconf
331331
libc.src.unistd.pipe
332+
libc.src.unistd.pipe2
332333
libc.src.unistd.pread
333334
libc.src.unistd.pwrite
334335
libc.src.unistd.read

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ set(TARGET_LIBC_ENTRYPOINTS
329329
libc.src.unistd.lseek
330330
libc.src.unistd.pathconf
331331
libc.src.unistd.pipe
332+
libc.src.unistd.pipe2
332333
libc.src.unistd.pread
333334
libc.src.unistd.pwrite
334335
libc.src.unistd.read

libc/newhdrgen/yaml/unistd.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ functions:
204204
return_type: int
205205
arguments:
206206
- type: int *
207+
- name: pipe2
208+
standards:
209+
- Linux
210+
return_type: int
211+
arguments:
212+
- type: int *
213+
- type: int
207214
- name: pread
208215
standards:
209216
- POSIX

libc/spec/linux.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,23 @@ def Linux : StandardSpec<"Linux"> {
287287
]
288288
>;
289289

290+
291+
HeaderSpec UniStd = HeaderSpec<
292+
"unistd.h",
293+
[], // Macros
294+
[],
295+
[], // Enumerations
296+
[
297+
FunctionSpec<
298+
"pipe2",
299+
RetValSpec<IntType>,
300+
[ArgSpec<IntPtr>, ArgSpec<IntType>] //TODO: make this int[2]
301+
>,
302+
],
303+
[]
304+
>;
305+
306+
290307
let Headers = [
291308
Errno,
292309
SysEpoll,
@@ -295,5 +312,6 @@ def Linux : StandardSpec<"Linux"> {
295312
SysRandom,
296313
SysTime,
297314
Signal,
315+
UniStd,
298316
];
299317
}

libc/src/unistd/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ add_entrypoint_object(
182182
.${LIBC_TARGET_OS}.pipe
183183
)
184184

185+
add_entrypoint_object(
186+
pipe2
187+
ALIAS
188+
DEPENDS
189+
.${LIBC_TARGET_OS}.pipe2
190+
)
191+
185192
add_entrypoint_object(
186193
pread
187194
ALIAS

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,19 @@ add_entrypoint_object(
329329
libc.src.errno.errno
330330
)
331331

332+
add_entrypoint_object(
333+
pipe2
334+
SRCS
335+
pipe2.cpp
336+
HDRS
337+
../pipe2.h
338+
DEPENDS
339+
libc.include.unistd
340+
libc.include.sys_syscall
341+
libc.src.__support.OSUtil.osutil
342+
libc.src.errno.errno
343+
)
344+
332345
add_entrypoint_object(
333346
pread
334347
SRCS

libc/src/unistd/linux/pipe2.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Linux implementation of pipe --------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/pipe2.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
#include "src/errno/libc_errno.h"
15+
#include <sys/syscall.h> // For syscall numbers.
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
LLVM_LIBC_FUNCTION(int, pipe2, (int pipefd[2], int flags)) {
20+
int ret = LIBC_NAMESPACE::syscall_impl<int>(
21+
SYS_pipe2, reinterpret_cast<long>(pipefd), flags);
22+
if (ret < 0) {
23+
libc_errno = -ret;
24+
return -1;
25+
}
26+
MSAN_UNPOISON(pipefd, sizeof(int) * 2);
27+
return ret;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/unistd/pipe2.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for pipe2 -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_UNISTD_PIPE2_H
10+
#define LLVM_LIBC_SRC_UNISTD_PIPE2_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int pipe2(int pipefd[2], int flags);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_UNISTD_PIPE2_H

libc/test/src/unistd/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ add_libc_unittest(
217217
libc.test.UnitTest.ErrnoSetterMatcher
218218
)
219219

220+
add_libc_unittest(
221+
pipe2_test
222+
SUITE
223+
libc_unistd_unittests
224+
SRCS
225+
pipe2_test.cpp
226+
DEPENDS
227+
libc.include.unistd
228+
libc.src.errno.errno
229+
libc.src.unistd.close
230+
libc.src.unistd.pipe2
231+
libc.test.UnitTest.ErrnoSetterMatcher
232+
)
233+
220234
add_libc_unittest(
221235
rmdir_test
222236
SUITE

0 commit comments

Comments
 (0)