Skip to content

Commit deb01d6

Browse files
authored
Add KillProcess and WaitPid functions to libpsl (#63)
1 parent e1cff37 commit deb01d6

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

src/libpsl-native/src/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ add_library(psl-native SHARED
2727
createsymlink.cpp
2828
followsymlink.cpp
2929
createprocess.cpp
30-
nativesyslog.cpp)
30+
nativesyslog.cpp
31+
killprocess.cpp
32+
waitpid.cpp)
3133

3234
check_function_exists(sysconf HAVE_SYSCONF)
3335

src/libpsl-native/src/killprocess.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
//! @brief kill a process with SIGKILL
5+
6+
#include "killprocess.h"
7+
8+
#include <sys/types.h>
9+
#include <signal.h>
10+
11+
//! @brief kill a process with SIGKILL
12+
//!
13+
//! KillProcess
14+
//!
15+
//! @param[in] pid
16+
//! @parblock
17+
//! The target PID to kill.
18+
//! @endparblock
19+
//!
20+
//! @retval true if signal successfully sent, false otherwise
21+
//!
22+
bool KillProcess(pid_t pid)
23+
{
24+
return kill(pid, SIGKILL) == 0;
25+
}

src/libpsl-native/src/killprocess.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#include "pal.h"
7+
#include <sys/types.h>
8+
9+
PAL_BEGIN_EXTERNC
10+
11+
bool KillProcess(pid_t pid);
12+
13+
PAL_END_EXTERNC

src/libpsl-native/src/waitpid.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
//! @brief wait for a child process to stop or terminate
5+
6+
#include "waitpid.h"
7+
8+
#include <sys/types.h>
9+
#include <sys/wait.h>
10+
11+
//! @brief wait for a child process to stop or terminate
12+
//!
13+
//! WaitPid
14+
//!
15+
//! @param[in] pid
16+
//! @parblock
17+
//! The target PID to wait for.
18+
//! @endparblock
19+
//!
20+
//! @param[in] nohang
21+
//! @parblock
22+
//! Whether to block while waiting for the process.
23+
//! @endparblock
24+
//!
25+
//! @retval PID of exited child, or -1 if error
26+
//!
27+
pid_t WaitPid(pid_t pid, bool nohang)
28+
{
29+
return waitpid(pid, NULL, nohang ? WNOHANG : 0);
30+
}

src/libpsl-native/src/waitpid.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#include "pal.h"
7+
#include <sys/types.h>
8+
9+
PAL_BEGIN_EXTERNC
10+
11+
pid_t WaitPid(pid_t pid, bool nohang);
12+
13+
PAL_END_EXTERNC

0 commit comments

Comments
 (0)