[libc] implement sys/uio/writev (#122233)

implement sys/uio/writev according to POSIX standard. This vectorized IO
API is needed by many logging libraries to achieve atomic logging
multiple strings.
This commit is contained in:
Schrodinger ZHU Yifan 2025-01-10 12:49:00 +08:00 committed by GitHub
parent 4c8fdc2954
commit 73dd730fb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 198 additions and 0 deletions

View File

@ -350,6 +350,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# wchar.h entrypoints
libc.src.wchar.wctob
# sys/uio.h entrypoints
libc.src.sys.uio.writev
)
if(LLVM_LIBC_INCLUDE_SCUDO)

View File

@ -350,6 +350,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# wchar.h entrypoints
libc.src.wchar.wctob
libc.src.wchar.btowc
# sys/uio.h entrypoints
libc.src.sys.uio.writev
)
if(LLVM_LIBC_INCLUDE_SCUDO)

View File

@ -333,3 +333,12 @@ add_proxy_header_library(
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.uid_t
)
add_proxy_header_library(
struct_iovec
HDRS
struct_iovec.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_iovec
libc.include.sys_uio
)

View File

@ -0,0 +1,21 @@
//===-- Proxy for struct iovec -------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_IOVEC_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_IOVEC_H
#ifdef LIBC_FULL_BUILD
#include "include/llvm-libc-types/struct_iovec.h"
#else
#include <sys/uio.h>
#endif // LIBC_FULL_BUILD
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_IOVEC_H

View File

@ -632,6 +632,16 @@ add_header_macro(
.llvm-libc-types.struct_utsname
)
add_header_macro(
sys_uio
../libc/include/sys/uio.yaml
sys/uio.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-types.struct_iovec
.llvm-libc-types.ssize_t
)
add_header_macro(
sys_wait
../libc/include/sys/wait.yaml

View File

@ -0,0 +1,16 @@
//===-- POSIX header uio.h ------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SYS_UIO_H
#define LLVM_LIBC_SYS_UIO_H
#include "__llvm-libc-common.h"
%%public_api()
#endif // LLVM_LIBC_SYS_UIO_H

17
libc/include/sys/uio.yaml Normal file
View File

@ -0,0 +1,17 @@
header: sys/uio.h
header_template: uio.h.def
macros: []
types:
- type_name: struct_iovec
- type_name: ssize_t
enums: []
objects: []
functions:
- name: writev
standards:
- POSIX
return_type: ssize_t
arguments:
- type: int
- type: const struct iovec *
- type: int

View File

@ -11,3 +11,4 @@ add_subdirectory(statvfs)
add_subdirectory(utsname)
add_subdirectory(wait)
add_subdirectory(prctl)
add_subdirectory(uio)

View File

@ -0,0 +1,10 @@
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()
add_entrypoint_object(
writev
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.writev
)

View File

@ -0,0 +1,14 @@
add_entrypoint_object(
writev
SRCS
writev.cpp
HDRS
../writev.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.errno.errno
libc.hdr.types.ssize_t
libc.hdr.types.struct_iovec
)

View File

@ -0,0 +1,27 @@
//===-- Implementation file for writev ------------------------------------===//
//
// 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/sys/uio/writev.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
#include <sys/syscall.h>
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(ssize_t, writev, (int fd, const iovec *iov, int iovcnt)) {
long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_writev, fd, iov, iovcnt);
// On failure, return -1 and set errno.
if (ret < 0) {
libc_errno = static_cast<int>(-ret);
return -1;
}
// On success, return number of bytes written.
return static_cast<ssize_t>(ret);
}
} // namespace LIBC_NAMESPACE_DECL

22
libc/src/sys/uio/writev.h Normal file
View File

@ -0,0 +1,22 @@
//===-- Implementation header for writev ----------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_SYS_UIO_WRITEV_H
#define LLVM_LIBC_SRC_SYS_UIO_WRITEV_H
#include "hdr/types/ssize_t.h"
#include "hdr/types/struct_iovec.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
ssize_t writev(int fd, const iovec *iov, int iovcnt);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_SYS_UIO_WRITEV_H

View File

@ -11,3 +11,4 @@ add_subdirectory(wait)
add_subdirectory(prctl)
add_subdirectory(auxv)
add_subdirectory(epoll)
add_subdirectory(uio)

View File

@ -0,0 +1,15 @@
add_custom_target(libc_sys_uio_unittests)
add_libc_unittest(
writev_test
SUITE
libc_sys_uio_unittests
SRCS
writev_test.cpp
DEPENDS
libc.src.errno.errno
libc.src.__support.common
libc.src.sys.uio.writev
libc.src.unistd.close
libc.src.fcntl.open
libc.test.UnitTest.ErrnoSetterMatcher
)

View File

@ -0,0 +1,29 @@
//===-- Unittests for writev ----------------------------------------------===//
//
// 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/fcntl/open.h"
#include "src/sys/uio/writev.h"
#include "src/unistd/close.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
TEST(LlvmLibcSysUioWritevTest, SmokeTest) {
int fd = LIBC_NAMESPACE::open("/dev/null", O_WRONLY);
ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
const char *data = "Hello, World!\n";
struct iovec iov[2];
iov[0].iov_base = const_cast<char *>(data);
iov[0].iov_len = 7;
iov[1].iov_base = const_cast<char *>(data + 7);
iov[1].iov_len = 8;
ASSERT_THAT(LIBC_NAMESPACE::writev(fd, iov, 2),
returns(EQ(15)).with_errno(EQ(0)));
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
}