2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @file Process.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief Functions to manipulate processes.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-04-18 16:16:24 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/Result.h>
|
2023-04-18 16:39:37 +00:00
|
|
|
#include <luna/String.h>
|
2023-04-18 16:16:24 +00:00
|
|
|
#include <sys/types.h>
|
2023-06-09 21:12:31 +00:00
|
|
|
#include <sys/wait.h>
|
2023-04-18 16:16:24 +00:00
|
|
|
|
|
|
|
namespace os
|
|
|
|
{
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief An object representing a process.
|
|
|
|
*/
|
2023-04-18 16:16:24 +00:00
|
|
|
class Process
|
|
|
|
{
|
|
|
|
public:
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Create a new process, which is an identical copy of the current one.
|
|
|
|
*
|
|
|
|
* @return Result<pid_t> The child's process ID in the parent process, and 0 in the child process.
|
|
|
|
*/
|
2023-04-18 16:16:24 +00:00
|
|
|
static Result<pid_t> fork();
|
2023-04-18 16:39:37 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Replace the current process's executable with another one.
|
|
|
|
*
|
|
|
|
* @param path The new executable's path.
|
|
|
|
* @param args The argument list to pass to the new executable.
|
|
|
|
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
|
|
|
|
* @return Result<void> Always an error, as this function does not return on success.
|
|
|
|
*/
|
2023-04-18 16:39:37 +00:00
|
|
|
static Result<void> exec(StringView path, Slice<String> args, bool search_in_path = true);
|
2023-06-09 20:45:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Replace the current process's executable with another one.
|
|
|
|
*
|
|
|
|
* @param path The new executable's path.
|
|
|
|
* @param args The argument list to pass to the new executable.
|
|
|
|
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
|
|
|
|
* @return Result<void> Always an error, as this function does not return on success.
|
|
|
|
*/
|
2023-05-20 10:47:10 +00:00
|
|
|
static Result<void> exec(StringView path, Slice<StringView> args, bool search_in_path = true);
|
2023-06-09 20:45:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Replace the current process's executable with another one.
|
|
|
|
*
|
|
|
|
* @param path The new executable's path.
|
|
|
|
* @param args The argument list to pass to the new executable.
|
|
|
|
* @param env The environment to pass to the new executable, instead of the current environment.
|
|
|
|
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
|
|
|
|
* @return Result<void> Always an error, as this function does not return on success.
|
|
|
|
*/
|
2023-04-22 11:54:47 +00:00
|
|
|
static Result<void> exec(StringView path, Slice<String> args, Slice<String> env, bool search_in_path = true);
|
2023-06-09 21:12:31 +00:00
|
|
|
|
2023-08-07 20:49:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Spawn a new process from an executable file.
|
|
|
|
*
|
|
|
|
* @param path The new executable's path.
|
|
|
|
* @param args The argument list to pass to the new process.
|
|
|
|
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
|
|
|
|
* @return Result<pid_t> An error, or the process ID of the new process.
|
|
|
|
*/
|
|
|
|
static Result<pid_t> spawn(StringView path, Slice<String> args, bool search_in_path = true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Spawn a new process from an executable file.
|
|
|
|
*
|
|
|
|
* @param path The new executable's path.
|
|
|
|
* @param args The argument list to pass to the new process.
|
|
|
|
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
|
|
|
|
* @return Result<pid_t> An error, or the process ID of the new process.
|
|
|
|
*/
|
|
|
|
static Result<pid_t> spawn(StringView path, Slice<StringView> args, bool search_in_path = true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Spawn a new process from an executable file.
|
|
|
|
*
|
|
|
|
* @param path The new executable's path.
|
|
|
|
* @param args The argument list to pass to the new process.
|
|
|
|
* @param env The environment to pass to the new process, instead of the current environment.
|
|
|
|
* @param search_in_path Determines whether to search in the system binary directories if path is just a name.
|
|
|
|
* @return Result<pid_t> An error, or the process ID of the new process.
|
|
|
|
*/
|
|
|
|
static Result<pid_t> spawn(StringView path, Slice<String> args, Slice<String> env, bool search_in_path = true);
|
|
|
|
|
2023-06-09 21:12:31 +00:00
|
|
|
// To use as the child argument to wait() to wait for any child.
|
|
|
|
static constexpr pid_t ANY_CHILD = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Wait for a child process to finish running.
|
|
|
|
*
|
|
|
|
* @param child The process ID of the child process, or ANY_CHILD to wait for any child.
|
|
|
|
* @param status The child's exit status will be stored here. If you don't want it, use nullptr.
|
|
|
|
* @param options Options to pass to waitpid(2).
|
|
|
|
* @return Result<pid_t> The process ID of the child process that was waited for (may not be the same as the
|
|
|
|
* child argument if ANY_CHILD was passed).
|
|
|
|
*/
|
|
|
|
static Result<pid_t> wait(pid_t child, int* status, int options = 0);
|
2023-07-10 20:17:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send a signal to a process.
|
|
|
|
*
|
|
|
|
* @param pid The process ID of the process.
|
|
|
|
* @param signo The signal number to send. Can be 0 to simply test for a process's existence.
|
|
|
|
* @return Result<void> Whether the function succeeded.
|
|
|
|
*/
|
|
|
|
static Result<void> kill(pid_t pid, int signo);
|
2023-07-21 19:21:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Exit the current process.
|
|
|
|
*
|
|
|
|
* @param status The exit status code to return to the parent.
|
|
|
|
*/
|
|
|
|
[[noreturn]] static void exit(int status);
|
2023-04-18 16:16:24 +00:00
|
|
|
};
|
|
|
|
}
|