solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
Functions
process.c File Reference

Implementation of the process management API. More...

#include "../include/process.h"
#include "../include/file.h"
#include <errno.h>
#include <stddef.h>
#include <unistd.h>
Include dependency graph for process.c:

Go to the source code of this file.

Functions

bool pipe_read_closed (PipeHandle *handle)
 
bool pipe_write_closed (PipeHandle *handle)
 
PipeFd pipe_read_fd (PipeHandle *handle)
 
PipeFd pipe_write_fd (PipeHandle *handle)
 
const char * process_error_string (ProcessError error)
 Get a string description of a process error.
 
ProcessError pipe_create (PipeHandle **pipeHandle)
 Create a new pipe for IPC.
 
ProcessError pipe_set_nonblocking (PipeHandle *pipe, bool nonblocking)
 Set non-blocking mode on a pipe.
 
ProcessError pipe_read (PipeHandle *pipe, void *buffer, size_t size, size_t *bytes_read, int timeout_ms)
 Read data from a pipe.
 
ProcessError pipe_write (PipeHandle *pipe, const void *buffer, size_t size, size_t *bytes_written, int timeout_ms)
 Write data to a pipe.
 
void pipe_close (PipeHandle *pipe)
 Close a pipe.
 
ProcessError process_create (ProcessHandle **handle, const char *command, const char *const argv[], const ProcessOptions *options)
 Create a new process When custom environment is provided, command must be an absolute path or relative path (containing '/'), as execve() does not search PATH.
 
void process_free (ProcessHandle *handle)
 Free resources associated with a process handle.
 
ProcessError process_wait (ProcessHandle *handle, ProcessResult *result, int timeout_ms)
 Wait for a process to complete.
 
ProcessError process_terminate (ProcessHandle *handle, bool force)
 Terminate a running process.
 
ProcessError process_run_and_capture (const char *command, const char *const argv[], ProcessOptions *options, int *exit_code)
 Run a command and capture its output.
 
ProcessError process_redirect_to_file (FileRedirection **redirection, const char *filepath, int flags, unsigned int mode)
 Create a new file redirection for a process.
 
ProcessError process_redirect_to_fd (FileRedirection **redirection, int fd, bool close_on_exec)
 Create a file redirection from an existing file descriptor.
 
void process_close_redirection (FileRedirection *redirection)
 Close and free a file redirection.
 
ProcessError process_create_with_redirection (ProcessHandle **handle, const char *command, const char *const argv[], const ExtProcessOptions *options)
 Create a process with extended redirection options.
 
ProcessError process_run_with_multiwriter (ProcessResult *result, const char *cmd, const char *args[], int output_fds[], int error_fds[])
 Run the command in a process that duplicates output to multiple destinations.
 
ProcessError process_run_with_file_redirection (ProcessHandle **handle, const char *command, const char *const argv[], const char *stdout_file, const char *stderr_file, bool append)
 Helper function to set up redirection to a file.
 

Detailed Description

Implementation of the process management API.

Definition in file process.c.

Function Documentation

◆ pipe_close()

void pipe_close ( PipeHandle *  pipe)

Close a pipe.

Parameters
[in]pipePipe handle to close

Definition at line 547 of file process.c.

◆ pipe_create()

ProcessError pipe_create ( PipeHandle **  pipe)

Create a new pipe for IPC.

Parameters
[out]pipePointer to receive the pipe handle
Returns
PROCESS_SUCCESS on success, error code otherwise

Definition at line 240 of file process.c.

◆ pipe_read()

ProcessError pipe_read ( PipeHandle *  pipe,
void *  buffer,
size_t  size,
size_t *  bytes_read,
int  timeout_ms 
)

Read data from a pipe.

Parameters
[in]pipePipe handle
[out]bufferBuffer to receive the data
[in]sizeBuffer size
[out]bytes_readPointer to receive the number of bytes read (can be NULL)
[in]timeout_msTimeout in milliseconds (-1 = wait indefinitely)
Returns
PROCESS_SUCCESS on success, error code otherwise

Definition at line 335 of file process.c.

◆ pipe_read_closed()

bool pipe_read_closed ( PipeHandle *  handle)

Returns True if pipe read closed.

Definition at line 184 of file process.c.

◆ pipe_read_fd()

PipeFd pipe_read_fd ( PipeHandle *  handle)

Returns the pipe write read descriptor.

Definition at line 194 of file process.c.

◆ pipe_set_nonblocking()

ProcessError pipe_set_nonblocking ( PipeHandle *  pipe,
bool  nonblocking 
)

Set non-blocking mode on a pipe.

Parameters
pipePipe handle
nonblockingtrue to enable non-blocking mode, false for blocking
Returns
ProcessError

Definition at line 283 of file process.c.

◆ pipe_write()

ProcessError pipe_write ( PipeHandle *  pipe,
const void *  buffer,
size_t  size,
size_t *  bytes_written,
int  timeout_ms 
)

Write data to a pipe.

Parameters
[in]pipePipe handle
[in]bufferData to write
[in]sizeNumber of bytes to write
[out]bytes_writtenPointer to receive the number of bytes written (can be NULL)
[in]timeout_msTimeout in milliseconds (-1 = wait indefinitely)
Returns
PROCESS_SUCCESS on success, error code otherwise

Definition at line 445 of file process.c.

◆ pipe_write_closed()

bool pipe_write_closed ( PipeHandle *  handle)

Returns True if pipe write closed.

Definition at line 189 of file process.c.

◆ pipe_write_fd()

PipeFd pipe_write_fd ( PipeHandle *  handle)

Returns the pipe write file descriptor.

Definition at line 199 of file process.c.

◆ process_close_redirection()

void process_close_redirection ( FileRedirection *  redirection)

Close and free a file redirection.

Parameters
redirectionThe redirection to close

Definition at line 1117 of file process.c.

Referenced by process_run_with_file_redirection().

Here is the caller graph for this function:

◆ process_create()

ProcessError process_create ( ProcessHandle **  handle,
const char *  command,
const char *const  argv[],
const ProcessOptions options 
)

Create a new process When custom environment is provided, command must be an absolute path or relative path (containing '/'), as execve() does not search PATH.

Parameters
[out]handlePointer to receive the process handle
[in]commandPath to executable
[in]argvNULL-terminated array of arguments (argv[0] should be command name)
[in]optionsProcess creation options (NULL for defaults)
Returns
PROCESS_SUCCESS on success, error code otherwise

Definition at line 843 of file process.c.

Referenced by process_run_and_capture().

Here is the caller graph for this function:

◆ process_create_with_redirection()

ProcessError process_create_with_redirection ( ProcessHandle **  handle,
const char *  command,
const char *const  argv[],
const ExtProcessOptions *  options 
)

Create a process with extended redirection options.

Parameters
[out]handlePointer to store the process handle
[in]commandCommand to execute
[in]argvArguments for the command (NULL-terminated)
[in]optionsProcess options with extended IO
Returns
ProcessError

Definition at line 1139 of file process.c.

Referenced by process_run_with_file_redirection().

Here is the caller graph for this function:

◆ process_error_string()

const char * process_error_string ( ProcessError  error)

Get a string description of a process error.

Parameters
[in]errorError code
Returns
Human-readable error description

Definition at line 202 of file process.c.

◆ process_free()

void process_free ( ProcessHandle *  handle)

Free resources associated with a process handle.

Parameters
[in]handleProcess handle to free

Definition at line 869 of file process.c.

Referenced by process_run_and_capture().

Here is the caller graph for this function:

◆ process_redirect_to_fd()

ProcessError process_redirect_to_fd ( FileRedirection **  redirection,
int  fd,
bool  close_on_exec 
)

Create a file redirection from an existing file descriptor.

Parameters
[out]redirectionPointer to store the created redirection
[in]fdExisting file descriptor
[in]close_on_execWhether to close the FD when the process exits
Returns
ProcessError

Definition at line 1096 of file process.c.

◆ process_redirect_to_file()

ProcessError process_redirect_to_file ( FileRedirection **  redirection,
const char *  filepath,
int  flags,
unsigned int  mode 
)

Create a new file redirection for a process.

Parameters
[out]redirectionPointer to store the created redirection
[in]filepathPath to the file
[in]flagsFile open flags (O_RDONLY, O_WRONLY, O_RDWR, etc.)
[in]modeFile mode for creation (if O_CREAT is used)
Returns
ProcessError

Definition at line 1063 of file process.c.

Referenced by process_run_with_file_redirection().

Here is the caller graph for this function:

◆ process_run_and_capture()

ProcessError process_run_and_capture ( const char *  command,
const char *const  argv[],
ProcessOptions options,
int *  exit_code 
)

Run a command and capture its output.

Convenience function that creates a process, captures its output, waits for completion, and cleans up resources.

Parameters
[in]commandPath to executable
[in]argvNULL-terminated array of arguments
[in]optionsProcess creation options (NULL for defaults)
[out]exit_codePointer to receive the exit code (can be NULL)
Returns
PROCESS_SUCCESS on success, error code otherwise

Definition at line 1025 of file process.c.

References process_create(), process_free(), and process_wait().

Here is the call graph for this function:

◆ process_run_with_file_redirection()

ProcessError process_run_with_file_redirection ( ProcessHandle **  handle,
const char *  command,
const char *const  argv[],
const char *  stdout_file,
const char *  stderr_file,
bool  append 
)

Helper function to set up redirection to a file.

Create a process that redirects its output from stdout and/or stderr to files.

Parameters
[in]commandCommand to run
[in]argvCommand arguments
[in]stdout_fileFile to redirect stdout to, or NULL
[in]stderr_fileFile to redirect stderr to, or NULL
[in]appendWhether to append to files (true) or overwrite (false)
Returns
ProcessError

Definition at line 1431 of file process.c.

References process_close_redirection(), process_create_with_redirection(), and process_redirect_to_file().

Here is the call graph for this function:

◆ process_run_with_multiwriter()

ProcessError process_run_with_multiwriter ( ProcessResult result,
const char *  cmd,
const char *  args[],
int  output_fds[],
int  error_fds[] 
)

Run the command in a process that duplicates output to multiple destinations.

This function sets up a process whose output is duplicated to multiple files/pipes

Parameters
[out]resultWill hold process exit information.
[in]cmdCommand to execute
[in]argvArguments (NULL-terminated)
[in]output_fdsArray of file descriptors to duplicate output to (NULL-terminated)
[in]error_fdsArray of file descriptors to duplicate errors to (NULL-terminated)
Returns
ProcessError

Definition at line 1255 of file process.c.

◆ process_terminate()

ProcessError process_terminate ( ProcessHandle *  handle,
bool  force 
)

Terminate a running process.

Parameters
[in]handleProcess handle
[in]forceIf true, force immediate termination (SIGKILL/TerminateProcess)
Returns
PROCESS_SUCCESS on success, error code otherwise

Definition at line 988 of file process.c.

◆ process_wait()

ProcessError process_wait ( ProcessHandle *  handle,
ProcessResult result,
int  timeout_ms 
)

Wait for a process to complete.

Parameters
[in]handleProcess handle
[out]resultPointer to receive exit information (can be NULL)
[in]timeout_msTimeout in milliseconds (-1 = wait indefinitely)
Returns
PROCESS_SUCCESS on success, error code otherwise

Definition at line 906 of file process.c.

Referenced by process_run_and_capture().

Here is the caller graph for this function: