Skip to content

Commit 34e161c

Browse files
committed
impl readv
1 parent aab25f2 commit 34e161c

File tree

9 files changed

+128
-1
lines changed

9 files changed

+128
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ set(TARGET_LIBC_ENTRYPOINTS
355355

356356
# sys/uio.h entrypoints
357357
libc.src.sys.uio.writev
358+
libc.src.sys.uio.readv
358359
)
359360

360361
if(LLVM_LIBC_INCLUDE_SCUDO)

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ set(TARGET_LIBC_ENTRYPOINTS
355355

356356
# sys/uio.h entrypoints
357357
libc.src.sys.uio.writev
358+
libc.src.sys.uio.readv
358359
)
359360

360361
if(LLVM_LIBC_INCLUDE_SCUDO)

libc/include/sys/uio.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ functions:
1515
- type: int
1616
- type: const struct iovec *
1717
- type: int
18+
- name: readv
19+
standards:
20+
- POSIX
21+
return_type: ssize_t
22+
arguments:
23+
- type: int
24+
- type: const struct iovec *
25+
- type: int

libc/src/sys/uio/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ add_entrypoint_object(
88
DEPENDS
99
.${LIBC_TARGET_OS}.writev
1010
)
11+
12+
add_entrypoint_object(
13+
readv
14+
ALIAS
15+
DEPENDS
16+
.${LIBC_TARGET_OS}.readv
17+
)

libc/src/sys/uio/linux/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ add_entrypoint_object(
1212
libc.hdr.types.ssize_t
1313
libc.hdr.types.struct_iovec
1414
)
15+
16+
add_entrypoint_object(
17+
readv
18+
SRCS
19+
readv.cpp
20+
HDRS
21+
../readv.h
22+
DEPENDS
23+
libc.include.sys_syscall
24+
libc.src.__support.OSUtil.osutil
25+
libc.src.__support.common
26+
libc.src.errno.errno
27+
libc.hdr.types.ssize_t
28+
libc.hdr.types.struct_iovec
29+
)

libc/src/sys/uio/linux/readv.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation file for readv -------------------------------------===//
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+
#include "src/sys/uio/readv.h"
9+
#include "src/__support/OSUtil/syscall.h"
10+
#include "src/__support/common.h"
11+
#include "src/errno/libc_errno.h"
12+
#include <sys/syscall.h>
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(ssize_t, readv, (int fd, const iovec *iov, int iovcnt)) {
17+
long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_readv, fd, iov, iovcnt);
18+
// On failure, return -1 and set errno.
19+
if (ret < 0) {
20+
libc_errno = static_cast<int>(-ret);
21+
return -1;
22+
}
23+
// On success, return number of bytes read.
24+
return static_cast<ssize_t>(ret);
25+
}
26+
27+
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/uio/readv.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for readv -----------------------------------===//
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_SYS_UIO_READV_H
10+
#define LLVM_LIBC_SRC_SYS_UIO_READV_H
11+
12+
#include "hdr/types/ssize_t.h"
13+
#include "hdr/types/struct_iovec.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
ssize_t readv(int fd, const iovec *iov, int iovcnt);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_SYS_UIO_READV_H

libc/test/src/sys/uio/CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
add_custom_target(libc_sys_uio_unittests)
2+
23
add_libc_unittest(
34
writev_test
45
SUITE
5-
libc_sys_uio_unittests
6+
libc_sys_uio_unittests
67
SRCS
78
writev_test.cpp
89
DEPENDS
@@ -13,3 +14,18 @@ add_libc_unittest(
1314
libc.src.fcntl.open
1415
libc.test.UnitTest.ErrnoSetterMatcher
1516
)
17+
18+
add_libc_unittest(
19+
readv_test
20+
SUITE
21+
libc_sys_uio_unittests
22+
SRCS
23+
readv_test.cpp
24+
DEPENDS
25+
libc.src.errno.errno
26+
libc.src.__support.common
27+
libc.src.sys.uio.readv
28+
libc.src.unistd.close
29+
libc.src.fcntl.open
30+
libc.test.UnitTest.ErrnoSetterMatcher
31+
)

libc/test/src/sys/uio/readv_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Unittests for readv -----------------------------------------------===//
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/fcntl/open.h"
10+
#include "src/sys/uio/readv.h"
11+
#include "src/unistd/close.h"
12+
#include "test/UnitTest/ErrnoSetterMatcher.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
16+
17+
TEST(LlvmLibcSysUioReadvTest, SmokeTest) {
18+
int fd = LIBC_NAMESPACE::open("/dev/urandom", O_RDONLY);
19+
ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
20+
char buf0[2];
21+
char buf1[3];
22+
struct iovec iov[2];
23+
iov[0].iov_base = buf0;
24+
iov[0].iov_len = 1;
25+
iov[1].iov_base = buf1;
26+
iov[1].iov_len = 2;
27+
ASSERT_THAT(LIBC_NAMESPACE::readv(fd, iov, 2),
28+
returns(EQ(3)).with_errno(EQ(0)));
29+
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
30+
}

0 commit comments

Comments
 (0)