SDL 3.0
SDL_process.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryProcess
24 *
25 * Process control support.
26 *
27 * These functions provide a cross-platform way to spawn and manage OS-level
28 * processes.
29 *
30 * You can create a new subprocess with SDL_CreateProcess() and optionally
31 * read and write to it using SDL_ReadProcess() or SDL_GetProcessInput() and
32 * SDL_GetProcessOutput(). If more advanced functionality like chaining input
33 * between processes is necessary, you can use
34 * SDL_CreateProcessWithProperties().
35 *
36 * You can get the status of a created process with SDL_WaitProcess(), or
37 * terminate the process with SDL_KillProcess().
38 *
39 * Don't forget to call SDL_DestroyProcess() to clean up, whether the process
40 * process was killed, terminated on its own, or is still running!
41 */
42
43#ifndef SDL_process_h_
44#define SDL_process_h_
45
46#include <SDL3/SDL_stdinc.h>
47#include <SDL3/SDL_error.h>
48#include <SDL3/SDL_iostream.h>
49#include <SDL3/SDL_properties.h>
50
51#include <SDL3/SDL_begin_code.h>
52/* Set up for C function definitions, even when using C++ */
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/**
58 * An opaque handle representing a system process.
59 *
60 * \since This datatype is available since SDL 3.2.0.
61 *
62 * \sa SDL_CreateProcess
63 */
64typedef struct SDL_Process SDL_Process;
65
66/**
67 * Create a new process.
68 *
69 * The path to the executable is supplied in args[0]. args[1..N] are
70 * additional arguments passed on the command line of the new process, and the
71 * argument list should be terminated with a NULL, e.g.:
72 *
73 * ```c
74 * const char *args[] = { "myprogram", "argument", NULL };
75 * ```
76 *
77 * Setting pipe_stdio to true is equivalent to setting
78 * `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` and
79 * `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` to `SDL_PROCESS_STDIO_APP`, and
80 * will allow the use of SDL_ReadProcess() or SDL_GetProcessInput() and
81 * SDL_GetProcessOutput().
82 *
83 * See SDL_CreateProcessWithProperties() for more details.
84 *
85 * \param args the path and arguments for the new process.
86 * \param pipe_stdio true to create pipes to the process's standard input and
87 * from the process's standard output, false for the process
88 * to have no input and inherit the application's standard
89 * output.
90 * \returns the newly created and running process, or NULL if the process
91 * couldn't be created.
92 *
93 * \threadsafety It is safe to call this function from any thread.
94 *
95 * \since This function is available since SDL 3.2.0.
96 *
97 * \sa SDL_CreateProcessWithProperties
98 * \sa SDL_GetProcessProperties
99 * \sa SDL_ReadProcess
100 * \sa SDL_GetProcessInput
101 * \sa SDL_GetProcessOutput
102 * \sa SDL_KillProcess
103 * \sa SDL_WaitProcess
104 * \sa SDL_DestroyProcess
105 */
106extern SDL_DECLSPEC SDL_Process * SDLCALL SDL_CreateProcess(const char * const *args, bool pipe_stdio);
107
108/**
109 * Description of where standard I/O should be directed when creating a
110 * process.
111 *
112 * If a standard I/O stream is set to SDL_PROCESS_STDIO_INHERITED, it will go
113 * to the same place as the application's I/O stream. This is the default for
114 * standard output and standard error.
115 *
116 * If a standard I/O stream is set to SDL_PROCESS_STDIO_NULL, it is connected
117 * to `NUL:` on Windows and `/dev/null` on POSIX systems. This is the default
118 * for standard input.
119 *
120 * If a standard I/O stream is set to SDL_PROCESS_STDIO_APP, it is connected
121 * to a new SDL_IOStream that is available to the application. Standard input
122 * will be available as `SDL_PROP_PROCESS_STDIN_POINTER` and allows
123 * SDL_GetProcessInput(), standard output will be available as
124 * `SDL_PROP_PROCESS_STDOUT_POINTER` and allows SDL_ReadProcess() and
125 * SDL_GetProcessOutput(), and standard error will be available as
126 * `SDL_PROP_PROCESS_STDERR_POINTER` in the properties for the created
127 * process.
128 *
129 * If a standard I/O stream is set to SDL_PROCESS_STDIO_REDIRECT, it is
130 * connected to an existing SDL_IOStream provided by the application. Standard
131 * input is provided using `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`, standard
132 * output is provided using `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`, and
133 * standard error is provided using `SDL_PROP_PROCESS_CREATE_STDERR_POINTER`
134 * in the creation properties. These existing streams should be closed by the
135 * application once the new process is created.
136 *
137 * In order to use an SDL_IOStream with SDL_PROCESS_STDIO_REDIRECT, it must
138 * have `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER` or
139 * `SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER` set. This is true for streams
140 * representing files and process I/O.
141 *
142 * \since This enum is available since SDL 3.2.0.
143 *
144 * \sa SDL_CreateProcessWithProperties
145 * \sa SDL_GetProcessProperties
146 * \sa SDL_ReadProcess
147 * \sa SDL_GetProcessInput
148 * \sa SDL_GetProcessOutput
149 */
150typedef enum SDL_ProcessIO
151{
152 SDL_PROCESS_STDIO_INHERITED, /**< The I/O stream is inherited from the application. */
153 SDL_PROCESS_STDIO_NULL, /**< The I/O stream is ignored. */
154 SDL_PROCESS_STDIO_APP, /**< The I/O stream is connected to a new SDL_IOStream that the application can read or write */
155 SDL_PROCESS_STDIO_REDIRECT /**< The I/O stream is redirected to an existing SDL_IOStream. */
157
158/**
159 * Create a new process with the specified properties.
160 *
161 * These are the supported properties:
162 *
163 * - `SDL_PROP_PROCESS_CREATE_ARGS_POINTER`: an array of strings containing
164 * the program to run, any arguments, and a NULL pointer, e.g. const char
165 * *args[] = { "myprogram", "argument", NULL }. This is a required property.
166 * - `SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER`: an SDL_Environment
167 * pointer. If this property is set, it will be the entire environment for
168 * the process, otherwise the current environment is used.
169 * - `SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING`: a UTF-8 encoded
170 * string representing the working directory for the process, defaults to
171 * the current working directory.
172 * - `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER`: an SDL_ProcessIO value describing
173 * where standard input for the process comes from, defaults to
174 * `SDL_PROCESS_STDIO_NULL`.
175 * - `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`: an SDL_IOStream pointer used for
176 * standard input when `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` is set to
177 * `SDL_PROCESS_STDIO_REDIRECT`.
178 * - `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER`: an SDL_ProcessIO value
179 * describing where standard output for the process goes to, defaults to
180 * `SDL_PROCESS_STDIO_INHERITED`.
181 * - `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`: an SDL_IOStream pointer used
182 * for standard output when `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` is set
183 * to `SDL_PROCESS_STDIO_REDIRECT`.
184 * - `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER`: an SDL_ProcessIO value
185 * describing where standard error for the process goes to, defaults to
186 * `SDL_PROCESS_STDIO_INHERITED`.
187 * - `SDL_PROP_PROCESS_CREATE_STDERR_POINTER`: an SDL_IOStream pointer used
188 * for standard error when `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set to
189 * `SDL_PROCESS_STDIO_REDIRECT`.
190 * - `SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN`: true if the error
191 * output of the process should be redirected into the standard output of
192 * the process. This property has no effect if
193 * `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set.
194 * - `SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN`: true if the process should
195 * run in the background. In this case the default input and output is
196 * `SDL_PROCESS_STDIO_NULL` and the exitcode of the process is not
197 * available, and will always be 0. This is not required to launch a program
198 * asynchronously, this is for detaching a child process from its parent
199 * completely (a so-called "double fork" on Unix). Created processes run
200 * asynchronously by default, regardless of this property.
201 * - `SDL_PROP_PROCESS_CREATE_CMDLINE_STRING`: a string containing the program
202 * to run and any parameters. This string is passed directly to
203 * `CreateProcess` on Windows, and does nothing on other platforms. This
204 * property is only important if you want to start programs that does
205 * non-standard command-line processing, and in most cases using
206 * `SDL_PROP_PROCESS_CREATE_ARGS_POINTER` is sufficient.
207 *
208 * On POSIX platforms, wait() and waitpid(-1, ...) should not be called, and
209 * SIGCHLD should not be ignored or handled because those would prevent SDL
210 * from properly tracking the lifetime of the underlying process. You should
211 * use SDL_WaitProcess() instead.
212 *
213 * \param props the properties to use.
214 * \returns the newly created and running process, or NULL if the process
215 * couldn't be created.
216 *
217 * \threadsafety It is safe to call this function from any thread.
218 *
219 * \since This function is available since SDL 3.2.0.
220 *
221 * \sa SDL_CreateProcess
222 * \sa SDL_GetProcessProperties
223 * \sa SDL_ReadProcess
224 * \sa SDL_GetProcessInput
225 * \sa SDL_GetProcessOutput
226 * \sa SDL_KillProcess
227 * \sa SDL_WaitProcess
228 * \sa SDL_DestroyProcess
229 */
231
232#define SDL_PROP_PROCESS_CREATE_ARGS_POINTER "SDL.process.create.args"
233#define SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER "SDL.process.create.environment"
234#define SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING "SDL.process.create.working_directory"
235#define SDL_PROP_PROCESS_CREATE_STDIN_NUMBER "SDL.process.create.stdin_option"
236#define SDL_PROP_PROCESS_CREATE_STDIN_POINTER "SDL.process.create.stdin_source"
237#define SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER "SDL.process.create.stdout_option"
238#define SDL_PROP_PROCESS_CREATE_STDOUT_POINTER "SDL.process.create.stdout_source"
239#define SDL_PROP_PROCESS_CREATE_STDERR_NUMBER "SDL.process.create.stderr_option"
240#define SDL_PROP_PROCESS_CREATE_STDERR_POINTER "SDL.process.create.stderr_source"
241#define SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN "SDL.process.create.stderr_to_stdout"
242#define SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN "SDL.process.create.background"
243#define SDL_PROP_PROCESS_CREATE_CMDLINE_STRING "SDL.process.create.cmdline"
244
245/**
246 * Get the properties associated with a process.
247 *
248 * The following read-only properties are provided by SDL:
249 *
250 * - `SDL_PROP_PROCESS_PID_NUMBER`: the process ID of the process.
251 * - `SDL_PROP_PROCESS_STDIN_POINTER`: an SDL_IOStream that can be used to
252 * write input to the process, if it was created with
253 * `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
254 * - `SDL_PROP_PROCESS_STDOUT_POINTER`: a non-blocking SDL_IOStream that can
255 * be used to read output from the process, if it was created with
256 * `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
257 * - `SDL_PROP_PROCESS_STDERR_POINTER`: a non-blocking SDL_IOStream that can
258 * be used to read error output from the process, if it was created with
259 * `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
260 * - `SDL_PROP_PROCESS_BACKGROUND_BOOLEAN`: true if the process is running in
261 * the background.
262 *
263 * \param process the process to query.
264 * \returns a valid property ID on success or 0 on failure; call
265 * SDL_GetError() for more information.
266 *
267 * \threadsafety It is safe to call this function from any thread.
268 *
269 * \since This function is available since SDL 3.2.0.
270 *
271 * \sa SDL_CreateProcess
272 * \sa SDL_CreateProcessWithProperties
273 */
274extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetProcessProperties(SDL_Process *process);
275
276#define SDL_PROP_PROCESS_PID_NUMBER "SDL.process.pid"
277#define SDL_PROP_PROCESS_STDIN_POINTER "SDL.process.stdin"
278#define SDL_PROP_PROCESS_STDOUT_POINTER "SDL.process.stdout"
279#define SDL_PROP_PROCESS_STDERR_POINTER "SDL.process.stderr"
280#define SDL_PROP_PROCESS_BACKGROUND_BOOLEAN "SDL.process.background"
281
282/**
283 * Read all the output from a process.
284 *
285 * If a process was created with I/O enabled, you can use this function to
286 * read the output. This function blocks until the process is complete,
287 * capturing all output, and providing the process exit code.
288 *
289 * The data is allocated with a zero byte at the end (null terminated) for
290 * convenience. This extra byte is not included in the value reported via
291 * `datasize`.
292 *
293 * The data should be freed with SDL_free().
294 *
295 * \param process The process to read.
296 * \param datasize a pointer filled in with the number of bytes read, may be
297 * NULL.
298 * \param exitcode a pointer filled in with the process exit code if the
299 * process has exited, may be NULL.
300 * \returns the data or NULL on failure; call SDL_GetError() for more
301 * information.
302 *
303 * \threadsafety This function is not thread safe.
304 *
305 * \since This function is available since SDL 3.2.0.
306 *
307 * \sa SDL_CreateProcess
308 * \sa SDL_CreateProcessWithProperties
309 * \sa SDL_DestroyProcess
310 */
311extern SDL_DECLSPEC void * SDLCALL SDL_ReadProcess(SDL_Process *process, size_t *datasize, int *exitcode);
312
313/**
314 * Get the SDL_IOStream associated with process standard input.
315 *
316 * The process must have been created with SDL_CreateProcess() and pipe_stdio
317 * set to true, or with SDL_CreateProcessWithProperties() and
318 * `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
319 *
320 * Writing to this stream can return less data than expected if the process
321 * hasn't read its input. It may be blocked waiting for its output to be read,
322 * if so you may need to call SDL_GetProcessOutput() and read the output in
323 * parallel with writing input.
324 *
325 * \param process The process to get the input stream for.
326 * \returns the input stream or NULL on failure; call SDL_GetError() for more
327 * information.
328 *
329 * \threadsafety It is safe to call this function from any thread.
330 *
331 * \since This function is available since SDL 3.2.0.
332 *
333 * \sa SDL_CreateProcess
334 * \sa SDL_CreateProcessWithProperties
335 * \sa SDL_GetProcessOutput
336 */
337extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_GetProcessInput(SDL_Process *process);
338
339/**
340 * Get the SDL_IOStream associated with process standard output.
341 *
342 * The process must have been created with SDL_CreateProcess() and pipe_stdio
343 * set to true, or with SDL_CreateProcessWithProperties() and
344 * `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
345 *
346 * Reading from this stream can return 0 with SDL_GetIOStatus() returning
347 * SDL_IO_STATUS_NOT_READY if no output is available yet.
348 *
349 * \param process The process to get the output stream for.
350 * \returns the output stream or NULL on failure; call SDL_GetError() for more
351 * information.
352 *
353 * \threadsafety It is safe to call this function from any thread.
354 *
355 * \since This function is available since SDL 3.2.0.
356 *
357 * \sa SDL_CreateProcess
358 * \sa SDL_CreateProcessWithProperties
359 * \sa SDL_GetProcessInput
360 */
361extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_GetProcessOutput(SDL_Process *process);
362
363/**
364 * Stop a process.
365 *
366 * \param process The process to stop.
367 * \param force true to terminate the process immediately, false to try to
368 * stop the process gracefully. In general you should try to stop
369 * the process gracefully first as terminating a process may
370 * leave it with half-written data or in some other unstable
371 * state.
372 * \returns true on success or false on failure; call SDL_GetError() for more
373 * information.
374 *
375 * \threadsafety This function is not thread safe.
376 *
377 * \since This function is available since SDL 3.2.0.
378 *
379 * \sa SDL_CreateProcess
380 * \sa SDL_CreateProcessWithProperties
381 * \sa SDL_WaitProcess
382 * \sa SDL_DestroyProcess
383 */
384extern SDL_DECLSPEC bool SDLCALL SDL_KillProcess(SDL_Process *process, bool force);
385
386/**
387 * Wait for a process to finish.
388 *
389 * This can be called multiple times to get the status of a process.
390 *
391 * The exit code will be the exit code of the process if it terminates
392 * normally, a negative signal if it terminated due to a signal, or -255
393 * otherwise. It will not be changed if the process is still running.
394 *
395 * If you create a process with standard output piped to the application
396 * (`pipe_stdio` being true) then you should read all of the process output
397 * before calling SDL_WaitProcess(). If you don't do this the process might be
398 * blocked indefinitely waiting for output to be read and SDL_WaitProcess()
399 * will never return true;
400 *
401 * \param process The process to wait for.
402 * \param block If true, block until the process finishes; otherwise, report
403 * on the process' status.
404 * \param exitcode a pointer filled in with the process exit code if the
405 * process has exited, may be NULL.
406 * \returns true if the process exited, false otherwise.
407 *
408 * \threadsafety This function is not thread safe.
409 *
410 * \since This function is available since SDL 3.2.0.
411 *
412 * \sa SDL_CreateProcess
413 * \sa SDL_CreateProcessWithProperties
414 * \sa SDL_KillProcess
415 * \sa SDL_DestroyProcess
416 */
417extern SDL_DECLSPEC bool SDLCALL SDL_WaitProcess(SDL_Process *process, bool block, int *exitcode);
418
419/**
420 * Destroy a previously created process object.
421 *
422 * Note that this does not stop the process, just destroys the SDL object used
423 * to track it. If you want to stop the process you should use
424 * SDL_KillProcess().
425 *
426 * \param process The process object to destroy.
427 *
428 * \threadsafety This function is not thread safe.
429 *
430 * \since This function is available since SDL 3.2.0.
431 *
432 * \sa SDL_CreateProcess
433 * \sa SDL_CreateProcessWithProperties
434 * \sa SDL_KillProcess
435 */
436extern SDL_DECLSPEC void SDLCALL SDL_DestroyProcess(SDL_Process *process);
437
438/* Ends C function definitions when using C++ */
439#ifdef __cplusplus
440}
441#endif
442#include <SDL3/SDL_close_code.h>
443
444#endif /* SDL_process_h_ */
struct SDL_IOStream SDL_IOStream
SDL_IOStream * SDL_GetProcessInput(SDL_Process *process)
bool SDL_KillProcess(SDL_Process *process, bool force)
SDL_Process * SDL_CreateProcess(const char *const *args, bool pipe_stdio)
SDL_Process * SDL_CreateProcessWithProperties(SDL_PropertiesID props)
void SDL_DestroyProcess(SDL_Process *process)
void * SDL_ReadProcess(SDL_Process *process, size_t *datasize, int *exitcode)
SDL_IOStream * SDL_GetProcessOutput(SDL_Process *process)
bool SDL_WaitProcess(SDL_Process *process, bool block, int *exitcode)
SDL_PropertiesID SDL_GetProcessProperties(SDL_Process *process)
struct SDL_Process SDL_Process
Definition SDL_process.h:64
SDL_ProcessIO
@ SDL_PROCESS_STDIO_APP
@ SDL_PROCESS_STDIO_INHERITED
@ SDL_PROCESS_STDIO_REDIRECT
@ SDL_PROCESS_STDIO_NULL
Uint32 SDL_PropertiesID