[libc-commits] [libc] 9015810 - [libc] add kill

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Oct 6 15:26:04 PDT 2022


Author: Michael Jones
Date: 2022-10-06T15:25:56-07:00
New Revision: 9015810648edfecba8203c2231c1aeffbe54bbbc

URL: https://github.com/llvm/llvm-project/commit/9015810648edfecba8203c2231c1aeffbe54bbbc
DIFF: https://github.com/llvm/llvm-project/commit/9015810648edfecba8203c2231c1aeffbe54bbbc.diff

LOG: [libc] add kill

Add the kill syscall wrapper and tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135406

Added: 
    libc/src/signal/kill.h
    libc/src/signal/linux/kill.cpp
    libc/test/src/signal/kill_test.cpp

Modified: 
    libc/config/linux/api.td
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/CMakeLists.txt
    libc/spec/posix.td
    libc/src/signal/CMakeLists.txt
    libc/src/signal/linux/CMakeLists.txt
    libc/test/src/signal/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index bf8dd847f1ab1..9bb7a22ac0f28 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -205,7 +205,13 @@ def SysMManAPI : PublicAPI<"sys/mman.h"> {
 }
 
 def SignalAPI : PublicAPI<"signal.h"> {
-  let Types = ["sigset_t", "struct sigaction", "union sigval", "siginfo_t"];
+  let Types = [
+    "sigset_t",
+    "struct sigaction",
+    "union sigval",
+    "siginfo_t",
+    "pid_t",
+  ];
 }
 
 def ThreadsAPI : PublicAPI<"threads.h"> {

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b8e88e0dae5df..f5f06253aff57 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -26,6 +26,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sched.sched_getaffinity
     libc.src.sched.sched_setaffinity
 
+    # signal.h entrypoints
+    libc.src.signal.kill
+
     # string.h entrypoints
     libc.src.string.bcmp
     libc.src.string.bzero

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 35336d38de8dd..d8b92e2f54735 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -137,6 +137,7 @@ add_gen_header(
     .llvm-libc-types.struct_sigaction
     .llvm-libc-types.__sighandler_t
     .llvm-libc-types.sigset_t
+    .llvm-libc-types.pid_t
 )
 
 add_gen_header(

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index 69b973203816b..ee5a15980edc2 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -258,9 +258,16 @@ def POSIX : StandardSpec<"POSIX"> {
         SigSetType,
         StructSigaction,
         UnionSigVal,
+        PidT,
       ],
       [], // Enumerations
       [
+        FunctionSpec<
+          "kill",
+          RetValSpec<IntType>,
+          [ArgSpec<PidT>,
+            ArgSpec<IntType>]
+        >,
         FunctionSpec<
           "sigaction",
           RetValSpec<IntType>,

diff  --git a/libc/src/signal/CMakeLists.txt b/libc/src/signal/CMakeLists.txt
index 2036be12a1287..8627ecb8cc8ca 100644
--- a/libc/src/signal/CMakeLists.txt
+++ b/libc/src/signal/CMakeLists.txt
@@ -9,6 +9,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.raise
 )
 
+add_entrypoint_object(
+  kill
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.kill
+)
+
 add_entrypoint_object(
   sigaction
   ALIAS

diff  --git a/libc/src/signal/kill.h b/libc/src/signal/kill.h
new file mode 100644
index 0000000000000..52d5b0da6cc14
--- /dev/null
+++ b/libc/src/signal/kill.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for kill function -----------------*- 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_SIGNAL_KILL_H
+#define LLVM_LIBC_SRC_SIGNAL_KILL_H
+
+#include <signal.h>
+
+namespace __llvm_libc {
+
+int kill(pid_t pid, int sig);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SIGNAL_KILL_H

diff  --git a/libc/src/signal/linux/CMakeLists.txt b/libc/src/signal/linux/CMakeLists.txt
index efe32290529ff..9800db8138ca2 100644
--- a/libc/src/signal/linux/CMakeLists.txt
+++ b/libc/src/signal/linux/CMakeLists.txt
@@ -7,6 +7,20 @@ add_header_library(
     libc.src.__support.OSUtil.osutil
 )
 
+add_entrypoint_object(
+  kill
+  SRCS
+    kill.cpp
+  HDRS
+    ../kill.h
+  DEPENDS
+    libc.include.signal
+    libc.include.errno
+    libc.src.errno.errno
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+)
+
 add_entrypoint_object(
   raise
   SRCS

diff  --git a/libc/src/signal/linux/kill.cpp b/libc/src/signal/linux/kill.cpp
new file mode 100644
index 0000000000000..ef957569f6d39
--- /dev/null
+++ b/libc/src/signal/linux/kill.cpp
@@ -0,0 +1,35 @@
+//===-- Linux implementation of kill --------------------------------------===//
+//
+// 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/signal/kill.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/signal/linux/signal_utils.h"
+
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <signal.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, kill, (pid_t pid, int sig)) {
+  int ret = __llvm_libc::syscall_impl(SYS_kill, pid, sig);
+
+  // A negative return value indicates an error with the magnitude of the
+  // value being the error code.
+  if (ret != 0) {
+    errno = (ret > 0 ? ret : -ret);
+    return -1;
+  }
+
+  return ret; // always 0
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/test/src/signal/CMakeLists.txt b/libc/test/src/signal/CMakeLists.txt
index 3274914e48233..885574354db37 100644
--- a/libc/test/src/signal/CMakeLists.txt
+++ b/libc/test/src/signal/CMakeLists.txt
@@ -11,6 +11,22 @@ add_libc_unittest(
     libc.src.signal.raise
 )
 
+add_libc_unittest(
+  kill_test
+  SUITE
+    libc_signal_unittests
+  SRCS
+    kill_test.cpp
+  DEPENDS
+    libc.include.signal
+    libc.include.errno
+    libc.src.errno.errno
+    libc.src.signal.kill
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.test.errno_setter_matcher
+)
+
 add_libc_unittest(
   sigaction_test
   SUITE

diff  --git a/libc/test/src/signal/kill_test.cpp b/libc/test/src/signal/kill_test.cpp
new file mode 100644
index 0000000000000..aaee7b951d8ae
--- /dev/null
+++ b/libc/test/src/signal/kill_test.cpp
@@ -0,0 +1,31 @@
+//===-- Unittests for kill -----------------------------------------------===//
+//
+// 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/signal/kill.h"
+
+#include "include/sys/syscall.h"          // For syscall numbers.
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "test/ErrnoSetterMatcher.h"
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+#include <signal.h>
+
+using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcKillTest, TargetSelf) {
+  pid_t parent_pid = __llvm_libc::syscall_impl(SYS_getpid);
+  ASSERT_THAT(__llvm_libc::kill(parent_pid, 0), Succeeds(0));
+
+  EXPECT_DEATH(
+      [] {
+        pid_t child_pid = __llvm_libc::syscall_impl(SYS_getpid);
+        __llvm_libc::kill(child_pid, SIGKILL);
+      },
+      WITH_SIGNAL(SIGKILL));
+}


        


More information about the libc-commits mailing list