From 2ed08ad6f2484080813ec91728da8854b091caf8 Mon Sep 17 00:00:00 2001 From: Tu Dinh Date: Tue, 19 Jan 2021 22:24:41 +0100 Subject: [PATCH] libpsl: add KillProcess and WaitPid functions These functions are used by powershell-unix to clean up SSH processes after they exit. --- src/libpsl-native/src/CMakeLists.txt | 4 +++- src/libpsl-native/src/killprocess.cpp | 25 ++++++++++++++++++++++ src/libpsl-native/src/killprocess.h | 13 ++++++++++++ src/libpsl-native/src/waitpid.cpp | 30 +++++++++++++++++++++++++++ src/libpsl-native/src/waitpid.h | 13 ++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/libpsl-native/src/killprocess.cpp create mode 100644 src/libpsl-native/src/killprocess.h create mode 100644 src/libpsl-native/src/waitpid.cpp create mode 100644 src/libpsl-native/src/waitpid.h diff --git a/src/libpsl-native/src/CMakeLists.txt b/src/libpsl-native/src/CMakeLists.txt index 4202c96..cc81571 100644 --- a/src/libpsl-native/src/CMakeLists.txt +++ b/src/libpsl-native/src/CMakeLists.txt @@ -27,7 +27,9 @@ add_library(psl-native SHARED createsymlink.cpp followsymlink.cpp createprocess.cpp - nativesyslog.cpp) + nativesyslog.cpp + killprocess.cpp + waitpid.cpp) check_function_exists(sysconf HAVE_SYSCONF) diff --git a/src/libpsl-native/src/killprocess.cpp b/src/libpsl-native/src/killprocess.cpp new file mode 100644 index 0000000..811dd3c --- /dev/null +++ b/src/libpsl-native/src/killprocess.cpp @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! @brief kill a process with SIGKILL + +#include "killprocess.h" + +#include +#include + +//! @brief kill a process with SIGKILL +//! +//! KillProcess +//! +//! @param[in] pid +//! @parblock +//! The target PID to kill. +//! @endparblock +//! +//! @retval true if signal successfully sent, false otherwise +//! +bool KillProcess(pid_t pid) +{ + return kill(pid, SIGKILL) == 0; +} diff --git a/src/libpsl-native/src/killprocess.h b/src/libpsl-native/src/killprocess.h new file mode 100644 index 0000000..d7dee5c --- /dev/null +++ b/src/libpsl-native/src/killprocess.h @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#pragma once + +#include "pal.h" +#include + +PAL_BEGIN_EXTERNC + +bool KillProcess(pid_t pid); + +PAL_END_EXTERNC diff --git a/src/libpsl-native/src/waitpid.cpp b/src/libpsl-native/src/waitpid.cpp new file mode 100644 index 0000000..379dde4 --- /dev/null +++ b/src/libpsl-native/src/waitpid.cpp @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! @brief wait for a child process to stop or terminate + +#include "waitpid.h" + +#include +#include + +//! @brief wait for a child process to stop or terminate +//! +//! WaitPid +//! +//! @param[in] pid +//! @parblock +//! The target PID to wait for. +//! @endparblock +//! +//! @param[in] nohang +//! @parblock +//! Whether to block while waiting for the process. +//! @endparblock +//! +//! @retval PID of exited child, or -1 if error +//! +pid_t WaitPid(pid_t pid, bool nohang) +{ + return waitpid(pid, NULL, nohang ? WNOHANG : 0); +} diff --git a/src/libpsl-native/src/waitpid.h b/src/libpsl-native/src/waitpid.h new file mode 100644 index 0000000..e33baf5 --- /dev/null +++ b/src/libpsl-native/src/waitpid.h @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#pragma once + +#include "pal.h" +#include + +PAL_BEGIN_EXTERNC + +pid_t WaitPid(pid_t pid, bool nohang); + +PAL_END_EXTERNC