[llvm-branch-commits] [libc] [llvm] [libc] Use a syscall wrapper for semget (PR #236722)

Connector Switch via llvm-branch-commits llvm-branch-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/236722

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

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

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 71a0eb7e8d550f..d931c83c3a66a1 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -195,6 +195,19 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  semget
+  HDRS
+    semget.h
+  DEPENDS
+    libc.hdr.types.key_t
+    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/semget.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/semget.h
new file mode 100644
index 00000000000000..1d7532aa9b8d19
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/semget.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 semget.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEMGET_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEMGET_H
+
+#include "hdr/types/key_t.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> semget(key_t key, int nsems, int semflg) {
+  return syscall_checked<int>(SYS_semget, key, nsems, semflg);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEMGET_H
diff --git a/libc/src/sys/sem/linux/CMakeLists.txt b/libc/src/sys/sem/linux/CMakeLists.txt
index 624b6bb86bd3e7..8fa3f8db77ec65 100644
--- a/libc/src/sys/sem/linux/CMakeLists.txt
+++ b/libc/src/sys/sem/linux/CMakeLists.txt
@@ -25,9 +25,10 @@ add_entrypoint_object(
     ../semget.h
   DEPENDS
     libc.hdr.types.key_t
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.semget
     libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
     libc.src.errno.errno
 )
 
diff --git a/libc/src/sys/sem/linux/semget.cpp b/libc/src/sys/sem/linux/semget.cpp
index b5a69056ccbd89..c991cee08180c2 100644
--- a/libc/src/sys/sem/linux/semget.cpp
+++ b/libc/src/sys/sem/linux/semget.cpp
@@ -8,20 +8,20 @@
 
 #include "src/sys/sem/semget.h"
 
-#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/semget.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
-#include <sys/syscall.h>
+#include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, semget, (key_t key, int nsems, int semflg)) {
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_semget, key, nsems, semflg);
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = linux_syscalls::semget(key, nsems, semflg);
+  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 86cf1d6341a97e..df577eb35c6780 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1488,6 +1488,15 @@ libc_support_library(
     hdrs = ["hdr/types/struct_sysinfo.h"],
 )
 
+libc_support_library(
+    name = "types_key_t",
+    hdrs = ["hdr/types/key_t.h"],
+    deps = select({
+        ":full_build": [":llvm_libc_types_key_t"],
+        "//conditions:default": [],
+    }),
+)
+
 libc_support_library(
     name = "types_pid_t",
     hdrs = ["hdr/types/pid_t.h"],
@@ -3114,6 +3123,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_semget",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/semget.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":types_key_t",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_sendmsg",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h"],
@@ -19450,6 +19475,24 @@ libc_function(
     ],
 )
 
+libc_function(
+    name = "semget",
+    srcs = ["src/sys/sem/linux/semget.cpp"],
+    hdrs = ["src/sys/sem/semget.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_semget",
+        ":errno",
+        ":types_key_t",
+    ],
+)
+
 ############################### sys/stat targets ###############################
 
 libc_function(



More information about the llvm-branch-commits mailing list