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

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Oct 29 03:49:21 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

This PR move semget behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.

---

<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

---
Full diff: https://github.com/llvm/llvm-project/pull/236722.diff


5 Files Affected:

- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+13) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/semget.h (+35) 
- (modified) libc/src/sys/sem/linux/CMakeLists.txt (+3-2) 
- (modified) libc/src/sys/sem/linux/semget.cpp (+6-6) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+43) 


``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 71a0eb7e8d550..d931c83c3a66a 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 0000000000000..1d7532aa9b8d1
--- /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 624b6bb86bd3e..8fa3f8db77ec6 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 b5a69056ccbd8..c991cee08180c 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 d89a926e70ead..85a4947cc3ce7 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(
     }),
 )
 
+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(

``````````

</details>


https://github.com/llvm/llvm-project/pull/236722


More information about the llvm-branch-commits mailing list