[libc][sys] add header and functions for sys ipc (#182700)

Split from a larger change. This PR contains the base impl for sys ipc,
sys sem will come after this. @SchrodingerZhu

this PR implements for:
https://github.com/llvm/llvm-project/issues/182161

see the last PR for more detail:
https://github.com/llvm/llvm-project/pull/182683
This commit is contained in:
Pengxiang Huang 2026-03-03 09:48:18 -05:00 committed by GitHub
parent c782e2d405
commit 5b976c9301
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 421 additions and 0 deletions

View File

@ -251,6 +251,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl
# sys/ipc.h entrypoints
libc.src.sys.ipc.ftok
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.mincore

View File

@ -38,6 +38,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_auxv
libc.include.sys_epoll
libc.include.sys_ioctl
libc.include.sys_ipc
libc.include.sys_mman
libc.include.sys_prctl
libc.include.sys_queue

View File

@ -233,6 +233,15 @@ add_proxy_header_library(
libc.include.sys_time
)
add_proxy_header_library(
key_t
HDRS
key_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.key_t
libc.include.sys_ipc
)
add_proxy_header_library(
pid_t
HDRS

22
libc/hdr/types/key_t.h Normal file
View File

@ -0,0 +1,22 @@
//===-- Proxy for key_t ---------------------------------------------------===//
//
// 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_KEY_T_H
#define LLVM_LIBC_HDR_TYPES_KEY_T_H
#ifdef LIBC_FULL_BUILD
#include "include/llvm-libc-types/key_t.h"
#else // Overlay mode
#include <sys/ipc.h>
#endif // LLVM_LIBC_FULL_BUILD
#endif // LLVM_LIBC_HDR_TYPES_KEY_T_H

View File

@ -557,6 +557,20 @@ add_header_macro(
.llvm-libc-macros.sys_ioctl_macros
)
add_header_macro(
sys_ipc
../libc/include/sys/ipc.yaml
sys/ipc.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.sys_ipc_macros
.llvm-libc-types.key_t
.llvm-libc-types.uid_t
.llvm-libc-types.gid_t
.llvm-libc-types.mode_t
.llvm-libc-types.struct_ipc_perm
)
add_header_macro(
sys_mman
../libc/include/sys/mman.yaml
@ -708,6 +722,7 @@ add_header_macro(
.llvm-libc-types.dev_t
.llvm-libc-types.gid_t
.llvm-libc-types.ino_t
.llvm-libc-types.key_t
.llvm-libc-types.mode_t
.llvm-libc-types.nlink_t
.llvm-libc-types.off_t

View File

@ -226,6 +226,12 @@ add_macro_header(
sys-ioctl-macros.h
)
add_macro_header(
sys_ipc_macros
HDR
sys-ipc-macros.h
)
add_macro_header(
sys_stat_macros
HDR

View File

@ -34,6 +34,12 @@ add_header(
sys-ioctl-macros.h
)
add_header(
sys_ipc_macros
HDR
sys-ipc-macros.h
)
add_header(
sys_random_macros
HDR

View File

@ -0,0 +1,24 @@
//===-- Definition of macros from sys/ipc.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_MACROS_LINUX_SYS_IPC_MACROS_H
#define LLVM_LIBC_MACROS_LINUX_SYS_IPC_MACROS_H
#define IPC_PRIVATE 0
// Resource get request flags.
#define IPC_CREAT 01000
#define IPC_EXCL 02000
#define IPC_NOWAIT 04000
// Control commands used with semctl, msgctl, and shmctl.
#define IPC_RMID 0
#define IPC_SET 1
#define IPC_STAT 2
#endif // LLVM_LIBC_MACROS_LINUX_SYS_IPC_MACROS_H

View File

@ -0,0 +1,16 @@
//===-- Macros defined in sys/ipc.h header file ---------------------------===//
//
// 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_MACROS_SYS_IPC_MACROS_H
#define LLVM_LIBC_MACROS_SYS_IPC_MACROS_H
#ifdef __linux__
#include "linux/sys-ipc-macros.h"
#endif
#endif // LLVM_LIBC_MACROS_SYS_IPC_MACROS_H

View File

@ -42,6 +42,7 @@ add_header(imaxdiv_t HDR imaxdiv_t.h)
add_header(in_addr_t HDR in_addr_t.h)
add_header(in_addr HDR in_addr.h DEPENDS .in_addr_t)
add_header(ino_t HDR ino_t.h)
add_header(key_t HDR key_t.h)
add_header(mbstate_t HDR mbstate_t.h)
add_header(mode_t HDR mode_t.h)
add_header(mtx_t HDR mtx_t.h DEPENDS .__futex_word .__mutex_type)
@ -79,6 +80,15 @@ add_header(dl_info HDR Dl_info.h)
add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t)
add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)
add_header(
struct_ipc_perm
HDR struct_ipc_perm.h
DEPENDS
.key_t
.uid_t
.gid_t
.mode_t
)
add_header(struct_pollfd HDR struct_pollfd.h)
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
add_header(struct_sched_param HDR struct_sched_param.h)

View File

@ -0,0 +1,14 @@
//===-- Definition of key_t type ------------------------------------------===//
//
// 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_TYPES_KEY_T_H
#define LLVM_LIBC_TYPES_KEY_T_H
typedef __INT32_TYPE__ key_t;
#endif // LLVM_LIBC_TYPES_KEY_T_H

View File

@ -0,0 +1,34 @@
//===-- Definition of struct ipc_perm -------------------------------------===//
//
// 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_TYPES_STRUCT_IPC_PERM_H
#define LLVM_LIBC_TYPES_STRUCT_IPC_PERM_H
#include "gid_t.h"
#include "key_t.h"
#include "mode_t.h"
#include "uid_t.h"
#ifdef __linux__
struct ipc_perm {
key_t __key;
uid_t uid;
gid_t gid;
uid_t cuid;
gid_t cgid;
mode_t mode;
unsigned short __seq;
unsigned short __padding;
unsigned long __unused_0;
unsigned long __unused_1;
};
#else
#error "ipc_perm not defined for the target platform"
#endif
#endif // LLVM_LIBC_TYPES_STRUCT_IPC_PERM_H

51
libc/include/sys/ipc.yaml Normal file
View File

@ -0,0 +1,51 @@
header: sys/ipc.h
standards:
- posix
- linux
macros:
- macro_name: IPC_PRIVATE
macro_header: sys-ipc-macros.h
standards:
- posix
- macro_name: IPC_CREAT
macro_header: sys-ipc-macros.h
standards:
- posix
- macro_name: IPC_EXCL
macro_header: sys-ipc-macros.h
standards:
- posix
- macro_name: IPC_NOWAIT
macro_header: sys-ipc-macros.h
standards:
- posix
- macro_name: IPC_RMID
macro_header: sys-ipc-macros.h
standards:
- posix
- macro_name: IPC_SET
macro_header: sys-ipc-macros.h
standards:
- posix
- macro_name: IPC_STAT
macro_header: sys-ipc-macros.h
standards:
- posix
- macro_name: IPC_INFO
macro_header: sys-ipc-macros.h
standards:
- linux
types:
- type_name: key_t
- type_name: uid_t
- type_name: gid_t
- type_name: mode_t
- type_name: struct_ipc_perm
functions:
- name: ftok
standards:
- posix
return_type: key_t
arguments:
- type: const char *
- type: int

View File

@ -8,6 +8,7 @@ types:
- type_name: dev_t
- type_name: gid_t
- type_name: ino_t
- type_name: key_t
- type_name: mode_t
- type_name: nlink_t
- type_name: off_t

View File

@ -1,5 +1,6 @@
add_subdirectory(auxv)
add_subdirectory(epoll)
add_subdirectory(ipc)
add_subdirectory(mman)
add_subdirectory(random)
add_subdirectory(resource)

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(
ftok
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.ftok
)

21
libc/src/sys/ipc/ftok.h Normal file
View File

@ -0,0 +1,21 @@
//===-- Implementation header for ftok --------------------------*- C++ -*-===//
//
// 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_IPC_FTOK_H
#define LLVM_LIBC_SRC_SYS_IPC_FTOK_H
#include "hdr/types/key_t.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
key_t ftok(const char *path, int id);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_SYS_IPC_FTOK_H

View File

@ -0,0 +1,23 @@
add_header_library(
kernel_statx
HDRS
kernel_statx.h
DEPENDS
libc.hdr.fcntl_macros
libc.include.sys_syscall
libc.src.__support.error_or
libc.src.__support.OSUtil.osutil
)
add_entrypoint_object(
ftok
SRCS
ftok.cpp
HDRS
../ftok.h
DEPENDS
.kernel_statx
libc.hdr.types.key_t
libc.src.__support.common
libc.src.errno.errno
)

View File

@ -0,0 +1,38 @@
//===-- Linux implementation of ftok --------------------------------------===//
//
// 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/ipc/ftok.h"
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h"
#include "kernel_statx.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(key_t, ftok, (const char *path, int id)) {
struct statx xbuf;
ErrorOr<int> err = statx_for_ftok(path, xbuf);
if (!err.has_value()) {
libc_errno = err.error();
return -1;
}
// key layout based on user input and file stats metadata
// 31 24 16 0
// +-------------+---------------+--------------+
// user input id + minor dev num + file inode num
return static_cast<key_t>(
((id & 0xff) << 24) |
((static_cast<int>(xbuf.stx_dev_minor) & 0xff) << 16) |
(static_cast<int>(xbuf.stx_ino) & 0xffff));
}
} // namespace LIBC_NAMESPACE_DECL

View File

@ -0,0 +1,34 @@
//===-- Wrapper over SYS_statx syscall for ftok ---------------------------===//
//
// 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_IPC_LINUX_KERNEL_STATX_H
#define LLVM_LIBC_SRC_SYS_IPC_LINUX_KERNEL_STATX_H
#include "hdr/fcntl_macros.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/error_or.h"
#include "sys/syscall.h"
#include <linux/stat.h>
namespace LIBC_NAMESPACE_DECL {
LIBC_INLINE ErrorOr<int> statx_for_ftok(const char *path, struct statx &xbuf) {
// store the file stats metadata into xbuf
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_statx, AT_FDCWD, path, 0,
STATX_BASIC_STATS, &xbuf);
if (ret < 0)
return Error(-ret);
return 0;
}
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_SYS_IPC_LINUX_KERNEL_STATX_H

View File

@ -11,6 +11,7 @@ add_subdirectory(wait)
add_subdirectory(prctl)
add_subdirectory(auxv)
add_subdirectory(epoll)
add_subdirectory(ipc)
add_subdirectory(uio)
add_subdirectory(time)
add_subdirectory(ioctl)

View File

@ -0,0 +1,3 @@
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${LIBC_TARGET_OS})
endif()

View File

@ -0,0 +1,19 @@
add_custom_target(libc_sys_ipc_unittests)
add_libc_unittest(
ftok_test
SUITE
libc_sys_ipc_unittests
SRCS
ftok_test.cpp
DEPENDS
libc.hdr.fcntl_macros
libc.include.sys_ipc
libc.src.errno.errno
libc.src.fcntl.open
libc.src.sys.ipc.ftok
libc.src.unistd.close
libc.src.unistd.unlink
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)

View File

@ -0,0 +1,59 @@
//===-- Unittests for ftok ------------------------------------------------===//
//
// 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/ipc/ftok.h"
#include "src/unistd/close.h"
#include "src/unistd/unlink.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "hdr/fcntl_macros.h"
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
using LlvmLibcFtokTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
TEST_F(LlvmLibcFtokTest, InvalidPath) {
ASSERT_THAT(LIBC_NAMESPACE::ftok("no/such/path", 1), Fails(ENOENT));
}
TEST_F(LlvmLibcFtokTest, DeterministicForPathAndId) {
// create a file
constexpr const char *TEST_FILE_NAME = "ftok.test";
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
// we first ensure such file exist and set to readable, writable
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY, 0600);
ASSERT_GT(fd, -1);
ASSERT_ERRNO_SUCCESS();
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
// create keys based on file path and user specified int
key_t key1 = LIBC_NAMESPACE::ftok(TEST_FILE, 'A');
ASSERT_NE(key1, key_t(-1));
ASSERT_ERRNO_SUCCESS();
key_t key2 = LIBC_NAMESPACE::ftok(TEST_FILE, 'A');
ASSERT_NE(key2, key_t(-1));
ASSERT_ERRNO_SUCCESS();
// key should be identical if both inputs are the same
ASSERT_EQ(key1, key2);
// create another key
key_t key3 = LIBC_NAMESPACE::ftok(TEST_FILE, 'B');
ASSERT_NE(key3, key_t(-1));
ASSERT_ERRNO_SUCCESS();
// key should be different if any input is different
ASSERT_NE(key1, key3);
// delete the file
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
}