Skip to content

libpsl-native: add KillProcess and WaitPid functions #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libpsl-native/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 25 additions & 0 deletions src/libpsl-native/src/killprocess.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! @brief kill a process with SIGKILL

#include "killprocess.h"

#include <sys/types.h>
#include <signal.h>

//! @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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable:

Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error.

}
13 changes: 13 additions & 0 deletions src/libpsl-native/src/killprocess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include "pal.h"
#include <sys/types.h>

PAL_BEGIN_EXTERNC

bool KillProcess(pid_t pid);

PAL_END_EXTERNC
30 changes: 30 additions & 0 deletions src/libpsl-native/src/waitpid.cpp
Original file line number Diff line number Diff line change
@@ -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 <sys/types.h>
#include <sys/wait.h>

//! @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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me, checked against the man pages on BSD and Linux.

}
13 changes: 13 additions & 0 deletions src/libpsl-native/src/waitpid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include "pal.h"
#include <sys/types.h>

PAL_BEGIN_EXTERNC

pid_t WaitPid(pid_t pid, bool nohang);

PAL_END_EXTERNC