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
process.h
Go to the documentation of this file.
1
6#ifndef PROCESS_H
7#define PROCESS_H
8
9#ifndef _GNU_SOURCE
10#define _GNU_SOURCE
11#endif
12
13#include "env.h"
14
15#include <assert.h>
16#include <stdbool.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21
22#ifdef _WIN32
23#include "wintypes.h"
24
25#include <io.h>
26#include <sys/stat.h>
27#include <windows.h>
28#else
29#include <fcntl.h>
30#include <signal.h>
31#include <sys/select.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#endif
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
44typedef enum {
45 PROCESS_SUCCESS = 0,
46 PROCESS_ERROR_INVALID_ARGUMENT = -1,
47 PROCESS_ERROR_FORK_FAILED = -2,
48 PROCESS_ERROR_EXEC_FAILED = -3,
49 PROCESS_ERROR_PIPE_FAILED = -4,
50 PROCESS_ERROR_MEMORY = -5,
51 PROCESS_ERROR_WAIT_FAILED = -6,
52 PROCESS_ERROR_KILL_FAILED = -7,
53 PROCESS_ERROR_PERMISSION_DENIED = -8,
54 PROCESS_ERROR_IO = -9,
55 PROCESS_ERROR_TIMEOUT = -10, // Operation timed out
56 PROCESS_ERROR_WOULD_BLOCK = -11, // Non-blocking operation would block
57 PROCESS_ERROR_PIPE_CLOSED = -12, // Pipe end was closed
58 PROCESS_ERROR_TERMINATE_FAILED = -13,
59 PROCESS_ERROR_UNKNOWN = -14
61
62#ifdef _WIN32
63typedef HANDLE PipeFd; // Pipe handle
64#else
65typedef int PipeFd; // Pipe file descriptor.
66#endif
67
71typedef enum { PROCESS_STREAM_STDIN = 0, PROCESS_STREAM_STDOUT = 1, PROCESS_STREAM_STDERR = 2 } ProcessStream;
72
78
83typedef struct PipeHandle PipeHandle;
84
89
93typedef struct {
94 const char* working_directory; // NULL = inherit current
95 bool inherit_environment; // If true, child inherits parent env
96 const char* const* environment; // NULL-terminated array of "KEY=VALUE" strings
97 bool detached; // Run process in background (no wait by default)
98
99 // Stream redirection options
100 struct {
101 PipeHandle* stdin_pipe; // NULL = inherit
102 PipeHandle* stdout_pipe; // NULL = inherit
103 PipeHandle* stderr_pipe; // NULL = inherit
104 bool merge_stderr; // Redirect stderr to stdout
105 } io;
107
108#ifndef _WIN32
109// Extend ProcessIO structure with file redirections
110typedef struct {
111 PipeHandle* stdin_pipe; // stdin pipe
112 PipeHandle* stdout_pipe; // stdout pipe
113 PipeHandle* stderr_pipe; // stderr pipe
114 bool merge_stderr; // whether to merge stdout and stderr.
115
116 FileRedirection* stdout_file; // stdout redirections
117 FileRedirection* stderr_file; // stderr redirections
118} ExtendedProcessIO;
119
120// Extended ProcessOptions to include extended IO
121typedef struct {
122 const char* working_directory;
123 bool inherit_environment;
124 char* const* environment;
125 bool detached;
126 ExtendedProcessIO io;
127} ExtProcessOptions;
128
129#endif // Linux only
130
134typedef struct {
135 int exit_code;
136 bool exited_normally;
137 int term_signal; // Only valid if !exited_normally
139
151ProcessError process_create(ProcessHandle** handle, const char* command, const char* const argv[],
152 const ProcessOptions* options);
153
162ProcessError process_wait(ProcessHandle* handle, ProcessResult* result, int timeout_ms);
163
172ProcessError process_terminate(ProcessHandle* handle, bool force);
173
179void process_free(ProcessHandle* handle);
180
184bool pipe_read_closed(PipeHandle* handle);
185
189bool pipe_write_closed(PipeHandle* handle);
190
194PipeFd pipe_read_fd(PipeHandle* handle);
195
199PipeFd pipe_write_fd(PipeHandle* handle);
200
208
216ProcessError pipe_set_nonblocking(PipeHandle* pipe, bool nonblocking);
217
229ProcessError pipe_read(PipeHandle* pipe, void* buffer, size_t size, size_t* bytes_read, int timeout_ms);
230
242ProcessError pipe_write(PipeHandle* pipe, const void* buffer, size_t size, size_t* bytes_written, int timeout_ms);
243
249void pipe_close(PipeHandle* pipe);
250ProcessError pipe_close_read_end(PipeHandle* pipe);
251ProcessError pipe_close_write_end(PipeHandle* pipe);
252
259const char* process_error_string(ProcessError error);
260
273ProcessError process_run_and_capture(const char* command, const char* const argv[], ProcessOptions* options,
274 int* exit_code);
275
276#ifndef _WIN32
286ProcessError process_redirect_to_file(FileRedirection** redirection, const char* filepath, int flags,
287 unsigned int mode);
288
297ProcessError process_redirect_to_fd(FileRedirection** redirection, int fd, bool close_on_exec);
298
305
322ProcessError process_run_with_multiwriter(ProcessResult* result, const char* cmd, const char* args[], int output_fds[],
323 int error_fds[]);
324
334ProcessError process_create_with_redirection(ProcessHandle** handle, const char* command, const char* const argv[],
335 const ExtProcessOptions* options);
336
349ProcessError process_run_with_file_redirection(ProcessHandle** handle, const char* command, const char* const argv[],
350 const char* stdout_file, const char* stderr_file, bool append);
351
352#endif // Linux only
353
354#ifdef __cplusplus
355}
356#endif
357
358#endif /* PROCESS_H */
Environment variable utilities and safe access functions.
void process_close_redirection(FileRedirection *redirection)
Close and free a file redirection.
Definition process.c:1117
ProcessError process_redirect_to_fd(FileRedirection **redirection, int fd, bool close_on_exec)
Create a file redirection from an existing file descriptor.
Definition process.c:1096
ProcessError process_run_and_capture(const char *command, const char *const argv[], ProcessOptions *options, int *exit_code)
Run a command and capture its output.
Definition process.c:1025
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 relativ...
Definition process.c:843
ProcessError process_redirect_to_file(FileRedirection **redirection, const char *filepath, int flags, unsigned int mode)
Create a new file redirection for a process.
Definition process.c:1063
struct FileRedirection FileRedirection
Definition process.h:88
PipeFd pipe_read_fd(PipeHandle *handle)
Definition process.c:194
ProcessError process_terminate(ProcessHandle *handle, bool force)
Terminate a running process.
Definition process.c:988
ProcessStream
Standard IO stream types for redirection.
Definition process.h:71
ProcessError pipe_write(PipeHandle *pipe, const void *buffer, size_t size, size_t *bytes_written, int timeout_ms)
Write data to a pipe.
Definition process.c:445
ProcessError pipe_read(PipeHandle *pipe, void *buffer, size_t size, size_t *bytes_read, int timeout_ms)
Read data from a pipe.
Definition process.c:335
struct PipeHandle PipeHandle
Pipe handle type for IPC (platform-specific details hidden in implementation)
Definition process.h:83
ProcessError pipe_create(PipeHandle **pipe)
Create a new pipe for IPC.
Definition process.c:240
void process_free(ProcessHandle *handle)
Free resources associated with a process handle.
Definition process.c:869
struct ProcessHandle ProcessHandle
Process handle type (platform-specific details hidden in implementation)
Definition process.h:77
ProcessError
Error codes for process operations.
Definition process.h:44
ProcessError process_wait(ProcessHandle *handle, ProcessResult *result, int timeout_ms)
Wait for a process to complete.
Definition process.c:906
bool pipe_read_closed(PipeHandle *handle)
Definition process.c:184
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)
Create a process that redirects its output from stdout and/or stderr to files.
Definition process.c:1431
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.
Definition process.c:1255
PipeFd pipe_write_fd(PipeHandle *handle)
Definition process.c:199
void pipe_close(PipeHandle *pipe)
Close a pipe.
Definition process.c:547
bool pipe_write_closed(PipeHandle *handle)
Definition process.c:189
ProcessError process_create_with_redirection(ProcessHandle **handle, const char *command, const char *const argv[], const ExtProcessOptions *options)
Create a process with extended redirection options.
Definition process.c:1139
const char * process_error_string(ProcessError error)
Get a string description of a process error.
Definition process.c:202
ProcessError pipe_set_nonblocking(PipeHandle *pipe, bool nonblocking)
Set non-blocking mode on a pipe.
Definition process.c:283
Options for process creation.
Definition process.h:93
Result information after a process completes.
Definition process.h:134
Windows-specific type definitions and compatibility macros for cross-platform development.