[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for semctl (PR #236721)

Connector Switch via libc-commits libc-commits at lists.llvm.org
Thu Oct 29 03:53:17 PDT 2026


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/236721

>From 5c5ca53ae027cf76a3a79f8ca267a97fb9fb9be1 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 29 Oct 2026 18:46:27 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for semctl

This PR move semctl behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
 .../linux/syscall_wrappers/CMakeLists.txt     |  12 ++
 .../OSUtil/linux/syscall_wrappers/semctl.h    |  35 +++++
 libc/src/sys/sem/linux/CMakeLists.txt         |  29 +++--
 libc/src/sys/sem/linux/semctl.cpp             |  13 +-
 .../llvm-project-overlay/libc/BUILD.bazel     | 121 ++++++++++++++++++
 5 files changed, 189 insertions(+), 21 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/semctl.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index e682daff23fb5e..71a0eb7e8d550f 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -183,6 +183,18 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  semctl
+  HDRS
+    semctl.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   close
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/semctl.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/semctl.h
new file mode 100644
index 00000000000000..b6f044fdb0ac47
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/semctl.h
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for semctl.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEMCTL_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEMCTL_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> semctl(int semid, int semnum, int cmd,
+                                unsigned long arg) {
+  return syscall_checked<int>(SYS_semctl, semid, semnum, cmd, arg);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEMCTL_H
diff --git a/libc/src/sys/sem/linux/CMakeLists.txt b/libc/src/sys/sem/linux/CMakeLists.txt
index 0be957ab660d02..624b6bb86bd3e7 100644
--- a/libc/src/sys/sem/linux/CMakeLists.txt
+++ b/libc/src/sys/sem/linux/CMakeLists.txt
@@ -1,17 +1,3 @@
-add_entrypoint_object(
-  semget
-  SRCS
-    semget.cpp
-  HDRS
-    ../semget.h
-  DEPENDS
-    libc.hdr.types.key_t
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
-    libc.src.__support.common
-    libc.src.errno.errno
-)
-
 add_entrypoint_object(
   semctl
   SRCS
@@ -24,6 +10,21 @@ add_entrypoint_object(
     libc.hdr.sys_sem_macros
     libc.hdr.types.struct_semid_ds
     libc.hdr.types.struct_seminfo
+    libc.src.__support.OSUtil.linux.syscall_wrappers.semctl
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  semget
+  SRCS
+    semget.cpp
+  HDRS
+    ../semget.h
+  DEPENDS
+    libc.hdr.types.key_t
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
     libc.src.__support.common
diff --git a/libc/src/sys/sem/linux/semctl.cpp b/libc/src/sys/sem/linux/semctl.cpp
index 1dd38f773004e1..e9ce52dbe8199e 100644
--- a/libc/src/sys/sem/linux/semctl.cpp
+++ b/libc/src/sys/sem/linux/semctl.cpp
@@ -13,11 +13,11 @@
 #include "hdr/sys_sem_macros.h"
 #include "hdr/types/struct_semid_ds.h"
 #include "hdr/types/struct_seminfo.h"
-#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/semctl.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
 #include <stdarg.h>
-#include <sys/syscall.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -89,13 +89,12 @@ LLVM_LIBC_FUNCTION(int, semctl, (int semid, int semnum, int cmd, ...)) {
     return -1;
   }
 
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_semctl, semid, semnum, cmd,
-                                              cmd_arg);
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = linux_syscalls::semctl(semid, semnum, cmd, cmd_arg);
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index f468f19e65edba..86cf1d6341a97e 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -574,6 +574,22 @@ libc_support_library(
     hdrs = ["include/llvm-libc-types/gid_t.h"],
 )
 
+libc_support_library(
+    name = "llvm_libc_types_key_t",
+    hdrs = ["include/llvm-libc-types/key_t.h"],
+)
+
+libc_support_library(
+    name = "llvm_libc_types_struct_ipc_perm",
+    hdrs = ["include/llvm-libc-types/struct_ipc_perm.h"],
+    deps = [
+        ":llvm_libc_types_gid_t",
+        ":llvm_libc_types_key_t",
+        ":llvm_libc_types_mode_t",
+        ":llvm_libc_types_uid_t",
+    ],
+)
+
 libc_support_library(
     name = "llvm_libc_types_pid_t",
     hdrs = ["include/llvm-libc-types/pid_t.h"],
@@ -623,6 +639,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "llvm_libc_macros_sys_ipc_macros",
+    hdrs = [
+        "include/llvm-libc-macros/linux/sys-ipc-macros.h",
+        "include/llvm-libc-macros/sys-ipc-macros.h",
+    ],
+)
+
+libc_support_library(
+    name = "llvm_libc_macros_sys_sem_macros",
+    hdrs = [
+        "include/llvm-libc-macros/linux/sys-sem-macros.h",
+        "include/llvm-libc-macros/sys-sem-macros.h",
+    ],
+)
+
 libc_support_library(
     name = "llvm_libc_macros_sys_socket_macros",
     hdrs = [
@@ -743,6 +775,20 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "llvm_libc_types_struct_semid_ds",
+    hdrs = ["include/llvm-libc-types/struct_semid_ds.h"],
+    deps = [
+        ":llvm_libc_types_struct_ipc_perm",
+        ":llvm_libc_types_time_t",
+    ],
+)
+
+libc_support_library(
+    name = "llvm_libc_types_struct_seminfo",
+    hdrs = ["include/llvm-libc-types/struct_seminfo.h"],
+)
+
 libc_support_library(
     name = "llvm_libc_types_wchar_t",
     hdrs = ["include/llvm-libc-types/wchar_t.h"],
@@ -896,6 +942,24 @@ libc_support_library(
     hdrs = ["hdr/sys_mman_macros.h"],
 )
 
+libc_support_library(
+    name = "hdr_sys_ipc_macros",
+    hdrs = ["hdr/sys_ipc_macros.h"],
+    deps = select({
+        ":full_build": [":llvm_libc_macros_sys_ipc_macros"],
+        "//conditions:default": [],
+    }),
+)
+
+libc_support_library(
+    name = "hdr_sys_sem_macros",
+    hdrs = ["hdr/sys_sem_macros.h"],
+    deps = select({
+        ":full_build": [":llvm_libc_macros_sys_sem_macros"],
+        "//conditions:default": [],
+    }),
+)
+
 libc_support_library(
     name = "hdr_sys_socket_macros",
     hdrs = ["hdr/sys_socket_macros.h"],
@@ -1401,6 +1465,24 @@ libc_support_library(
     hdrs = ["hdr/types/struct_rlimit.h"],
 )
 
+libc_support_library(
+    name = "types_struct_semid_ds",
+    hdrs = ["hdr/types/struct_semid_ds.h"],
+    deps = select({
+        ":full_build": [":llvm_libc_types_struct_semid_ds"],
+        "//conditions:default": [],
+    }),
+)
+
+libc_support_library(
+    name = "types_struct_seminfo",
+    hdrs = ["hdr/types/struct_seminfo.h"],
+    deps = select({
+        ":full_build": [":llvm_libc_types_struct_seminfo"],
+        "//conditions:default": [],
+    }),
+)
+
 libc_support_library(
     name = "types_struct_sysinfo",
     hdrs = ["hdr/types/struct_sysinfo.h"],
@@ -3017,6 +3099,21 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_semctl",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/semctl.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_sendmsg",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h"],
@@ -19329,6 +19426,30 @@ libc_function(
     ],
 )
 
+############################### sys/sem targets ################################
+
+libc_function(
+    name = "semctl",
+    srcs = ["src/sys/sem/linux/semctl.cpp"],
+    hdrs = ["src/sys/sem/semctl.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_libc_errno",
+        ":__support_macros_config",
+        ":__support_osutil_linux_syscall_wrappers_semctl",
+        ":errno",
+        ":hdr_errno_macros",
+        ":hdr_sys_ipc_macros",
+        ":hdr_sys_sem_macros",
+        ":types_struct_semid_ds",
+        ":types_struct_seminfo",
+    ],
+)
+
 ############################### sys/stat targets ###############################
 
 libc_function(



More information about the libc-commits mailing list