Path: blob/master/thirdparty/sdl/include/SDL3/SDL_process.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryProcess23*24* Process control support.25*26* These functions provide a cross-platform way to spawn and manage OS-level27* processes.28*29* You can create a new subprocess with SDL_CreateProcess() and optionally30* read and write to it using SDL_ReadProcess() or SDL_GetProcessInput() and31* SDL_GetProcessOutput(). If more advanced functionality like chaining input32* between processes is necessary, you can use33* SDL_CreateProcessWithProperties().34*35* You can get the status of a created process with SDL_WaitProcess(), or36* terminate the process with SDL_KillProcess().37*38* Don't forget to call SDL_DestroyProcess() to clean up, whether the process39* process was killed, terminated on its own, or is still running!40*/4142#ifndef SDL_process_h_43#define SDL_process_h_4445#include <SDL3/SDL_stdinc.h>46#include <SDL3/SDL_error.h>47#include <SDL3/SDL_iostream.h>48#include <SDL3/SDL_properties.h>4950#include <SDL3/SDL_begin_code.h>51/* Set up for C function definitions, even when using C++ */52#ifdef __cplusplus53extern "C" {54#endif5556/**57* An opaque handle representing a system process.58*59* \since This datatype is available since SDL 3.2.0.60*61* \sa SDL_CreateProcess62*/63typedef struct SDL_Process SDL_Process;6465/**66* Create a new process.67*68* The path to the executable is supplied in args[0]. args[1..N] are69* additional arguments passed on the command line of the new process, and the70* argument list should be terminated with a NULL, e.g.:71*72* ```c73* const char *args[] = { "myprogram", "argument", NULL };74* ```75*76* Setting pipe_stdio to true is equivalent to setting77* `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` and78* `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` to `SDL_PROCESS_STDIO_APP`, and79* will allow the use of SDL_ReadProcess() or SDL_GetProcessInput() and80* SDL_GetProcessOutput().81*82* See SDL_CreateProcessWithProperties() for more details.83*84* \param args the path and arguments for the new process.85* \param pipe_stdio true to create pipes to the process's standard input and86* from the process's standard output, false for the process87* to have no input and inherit the application's standard88* output.89* \returns the newly created and running process, or NULL if the process90* couldn't be created.91*92* \threadsafety It is safe to call this function from any thread.93*94* \since This function is available since SDL 3.2.0.95*96* \sa SDL_CreateProcessWithProperties97* \sa SDL_GetProcessProperties98* \sa SDL_ReadProcess99* \sa SDL_GetProcessInput100* \sa SDL_GetProcessOutput101* \sa SDL_KillProcess102* \sa SDL_WaitProcess103* \sa SDL_DestroyProcess104*/105extern SDL_DECLSPEC SDL_Process * SDLCALL SDL_CreateProcess(const char * const *args, bool pipe_stdio);106107/**108* Description of where standard I/O should be directed when creating a109* process.110*111* If a standard I/O stream is set to SDL_PROCESS_STDIO_INHERITED, it will go112* to the same place as the application's I/O stream. This is the default for113* standard output and standard error.114*115* If a standard I/O stream is set to SDL_PROCESS_STDIO_NULL, it is connected116* to `NUL:` on Windows and `/dev/null` on POSIX systems. This is the default117* for standard input.118*119* If a standard I/O stream is set to SDL_PROCESS_STDIO_APP, it is connected120* to a new SDL_IOStream that is available to the application. Standard input121* will be available as `SDL_PROP_PROCESS_STDIN_POINTER` and allows122* SDL_GetProcessInput(), standard output will be available as123* `SDL_PROP_PROCESS_STDOUT_POINTER` and allows SDL_ReadProcess() and124* SDL_GetProcessOutput(), and standard error will be available as125* `SDL_PROP_PROCESS_STDERR_POINTER` in the properties for the created126* process.127*128* If a standard I/O stream is set to SDL_PROCESS_STDIO_REDIRECT, it is129* connected to an existing SDL_IOStream provided by the application. Standard130* input is provided using `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`, standard131* output is provided using `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`, and132* standard error is provided using `SDL_PROP_PROCESS_CREATE_STDERR_POINTER`133* in the creation properties. These existing streams should be closed by the134* application once the new process is created.135*136* In order to use an SDL_IOStream with SDL_PROCESS_STDIO_REDIRECT, it must137* have `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER` or138* `SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER` set. This is true for streams139* representing files and process I/O.140*141* \since This enum is available since SDL 3.2.0.142*143* \sa SDL_CreateProcessWithProperties144* \sa SDL_GetProcessProperties145* \sa SDL_ReadProcess146* \sa SDL_GetProcessInput147* \sa SDL_GetProcessOutput148*/149typedef enum SDL_ProcessIO150{151SDL_PROCESS_STDIO_INHERITED, /**< The I/O stream is inherited from the application. */152SDL_PROCESS_STDIO_NULL, /**< The I/O stream is ignored. */153SDL_PROCESS_STDIO_APP, /**< The I/O stream is connected to a new SDL_IOStream that the application can read or write */154SDL_PROCESS_STDIO_REDIRECT /**< The I/O stream is redirected to an existing SDL_IOStream. */155} SDL_ProcessIO;156157/**158* Create a new process with the specified properties.159*160* These are the supported properties:161*162* - `SDL_PROP_PROCESS_CREATE_ARGS_POINTER`: an array of strings containing163* the program to run, any arguments, and a NULL pointer, e.g. const char164* *args[] = { "myprogram", "argument", NULL }. This is a required property.165* - `SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER`: an SDL_Environment166* pointer. If this property is set, it will be the entire environment for167* the process, otherwise the current environment is used.168* - `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER`: an SDL_ProcessIO value describing169* where standard input for the process comes from, defaults to170* `SDL_PROCESS_STDIO_NULL`.171* - `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`: an SDL_IOStream pointer used for172* standard input when `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` is set to173* `SDL_PROCESS_STDIO_REDIRECT`.174* - `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER`: an SDL_ProcessIO value175* describing where standard output for the process goes to, defaults to176* `SDL_PROCESS_STDIO_INHERITED`.177* - `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`: an SDL_IOStream pointer used178* for standard output when `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` is set179* to `SDL_PROCESS_STDIO_REDIRECT`.180* - `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER`: an SDL_ProcessIO value181* describing where standard error for the process goes to, defaults to182* `SDL_PROCESS_STDIO_INHERITED`.183* - `SDL_PROP_PROCESS_CREATE_STDERR_POINTER`: an SDL_IOStream pointer used184* for standard error when `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set to185* `SDL_PROCESS_STDIO_REDIRECT`.186* - `SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN`: true if the error187* output of the process should be redirected into the standard output of188* the process. This property has no effect if189* `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set.190* - `SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN`: true if the process should191* run in the background. In this case the default input and output is192* `SDL_PROCESS_STDIO_NULL` and the exitcode of the process is not193* available, and will always be 0.194*195* On POSIX platforms, wait() and waitpid(-1, ...) should not be called, and196* SIGCHLD should not be ignored or handled because those would prevent SDL197* from properly tracking the lifetime of the underlying process. You should198* use SDL_WaitProcess() instead.199*200* \param props the properties to use.201* \returns the newly created and running process, or NULL if the process202* couldn't be created.203*204* \threadsafety It is safe to call this function from any thread.205*206* \since This function is available since SDL 3.2.0.207*208* \sa SDL_CreateProcess209* \sa SDL_GetProcessProperties210* \sa SDL_ReadProcess211* \sa SDL_GetProcessInput212* \sa SDL_GetProcessOutput213* \sa SDL_KillProcess214* \sa SDL_WaitProcess215* \sa SDL_DestroyProcess216*/217extern SDL_DECLSPEC SDL_Process * SDLCALL SDL_CreateProcessWithProperties(SDL_PropertiesID props);218219#define SDL_PROP_PROCESS_CREATE_ARGS_POINTER "SDL.process.create.args"220#define SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER "SDL.process.create.environment"221#define SDL_PROP_PROCESS_CREATE_STDIN_NUMBER "SDL.process.create.stdin_option"222#define SDL_PROP_PROCESS_CREATE_STDIN_POINTER "SDL.process.create.stdin_source"223#define SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER "SDL.process.create.stdout_option"224#define SDL_PROP_PROCESS_CREATE_STDOUT_POINTER "SDL.process.create.stdout_source"225#define SDL_PROP_PROCESS_CREATE_STDERR_NUMBER "SDL.process.create.stderr_option"226#define SDL_PROP_PROCESS_CREATE_STDERR_POINTER "SDL.process.create.stderr_source"227#define SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN "SDL.process.create.stderr_to_stdout"228#define SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN "SDL.process.create.background"229230/**231* Get the properties associated with a process.232*233* The following read-only properties are provided by SDL:234*235* - `SDL_PROP_PROCESS_PID_NUMBER`: the process ID of the process.236* - `SDL_PROP_PROCESS_STDIN_POINTER`: an SDL_IOStream that can be used to237* write input to the process, if it was created with238* `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` set to `SDL_PROCESS_STDIO_APP`.239* - `SDL_PROP_PROCESS_STDOUT_POINTER`: a non-blocking SDL_IOStream that can240* be used to read output from the process, if it was created with241* `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` set to `SDL_PROCESS_STDIO_APP`.242* - `SDL_PROP_PROCESS_STDERR_POINTER`: a non-blocking SDL_IOStream that can243* be used to read error output from the process, if it was created with244* `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` set to `SDL_PROCESS_STDIO_APP`.245* - `SDL_PROP_PROCESS_BACKGROUND_BOOLEAN`: true if the process is running in246* the background.247*248* \param process the process to query.249* \returns a valid property ID on success or 0 on failure; call250* SDL_GetError() for more information.251*252* \threadsafety It is safe to call this function from any thread.253*254* \since This function is available since SDL 3.2.0.255*256* \sa SDL_CreateProcess257* \sa SDL_CreateProcessWithProperties258*/259extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetProcessProperties(SDL_Process *process);260261#define SDL_PROP_PROCESS_PID_NUMBER "SDL.process.pid"262#define SDL_PROP_PROCESS_STDIN_POINTER "SDL.process.stdin"263#define SDL_PROP_PROCESS_STDOUT_POINTER "SDL.process.stdout"264#define SDL_PROP_PROCESS_STDERR_POINTER "SDL.process.stderr"265#define SDL_PROP_PROCESS_BACKGROUND_BOOLEAN "SDL.process.background"266267/**268* Read all the output from a process.269*270* If a process was created with I/O enabled, you can use this function to271* read the output. This function blocks until the process is complete,272* capturing all output, and providing the process exit code.273*274* The data is allocated with a zero byte at the end (null terminated) for275* convenience. This extra byte is not included in the value reported via276* `datasize`.277*278* The data should be freed with SDL_free().279*280* \param process The process to read.281* \param datasize a pointer filled in with the number of bytes read, may be282* NULL.283* \param exitcode a pointer filled in with the process exit code if the284* process has exited, may be NULL.285* \returns the data or NULL on failure; call SDL_GetError() for more286* information.287*288* \threadsafety This function is not thread safe.289*290* \since This function is available since SDL 3.2.0.291*292* \sa SDL_CreateProcess293* \sa SDL_CreateProcessWithProperties294* \sa SDL_DestroyProcess295*/296extern SDL_DECLSPEC void * SDLCALL SDL_ReadProcess(SDL_Process *process, size_t *datasize, int *exitcode);297298/**299* Get the SDL_IOStream associated with process standard input.300*301* The process must have been created with SDL_CreateProcess() and pipe_stdio302* set to true, or with SDL_CreateProcessWithProperties() and303* `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` set to `SDL_PROCESS_STDIO_APP`.304*305* Writing to this stream can return less data than expected if the process306* hasn't read its input. It may be blocked waiting for its output to be read,307* if so you may need to call SDL_GetProcessOutput() and read the output in308* parallel with writing input.309*310* \param process The process to get the input stream for.311* \returns the input stream or NULL on failure; call SDL_GetError() for more312* information.313*314* \threadsafety It is safe to call this function from any thread.315*316* \since This function is available since SDL 3.2.0.317*318* \sa SDL_CreateProcess319* \sa SDL_CreateProcessWithProperties320* \sa SDL_GetProcessOutput321*/322extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_GetProcessInput(SDL_Process *process);323324/**325* Get the SDL_IOStream associated with process standard output.326*327* The process must have been created with SDL_CreateProcess() and pipe_stdio328* set to true, or with SDL_CreateProcessWithProperties() and329* `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` set to `SDL_PROCESS_STDIO_APP`.330*331* Reading from this stream can return 0 with SDL_GetIOStatus() returning332* SDL_IO_STATUS_NOT_READY if no output is available yet.333*334* \param process The process to get the output stream for.335* \returns the output stream or NULL on failure; call SDL_GetError() for more336* information.337*338* \threadsafety It is safe to call this function from any thread.339*340* \since This function is available since SDL 3.2.0.341*342* \sa SDL_CreateProcess343* \sa SDL_CreateProcessWithProperties344* \sa SDL_GetProcessInput345*/346extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_GetProcessOutput(SDL_Process *process);347348/**349* Stop a process.350*351* \param process The process to stop.352* \param force true to terminate the process immediately, false to try to353* stop the process gracefully. In general you should try to stop354* the process gracefully first as terminating a process may355* leave it with half-written data or in some other unstable356* state.357* \returns true on success or false on failure; call SDL_GetError() for more358* information.359*360* \threadsafety This function is not thread safe.361*362* \since This function is available since SDL 3.2.0.363*364* \sa SDL_CreateProcess365* \sa SDL_CreateProcessWithProperties366* \sa SDL_WaitProcess367* \sa SDL_DestroyProcess368*/369extern SDL_DECLSPEC bool SDLCALL SDL_KillProcess(SDL_Process *process, bool force);370371/**372* Wait for a process to finish.373*374* This can be called multiple times to get the status of a process.375*376* The exit code will be the exit code of the process if it terminates377* normally, a negative signal if it terminated due to a signal, or -255378* otherwise. It will not be changed if the process is still running.379*380* If you create a process with standard output piped to the application381* (`pipe_stdio` being true) then you should read all of the process output382* before calling SDL_WaitProcess(). If you don't do this the process might be383* blocked indefinitely waiting for output to be read and SDL_WaitProcess()384* will never return true;385*386* \param process The process to wait for.387* \param block If true, block until the process finishes; otherwise, report388* on the process' status.389* \param exitcode a pointer filled in with the process exit code if the390* process has exited, may be NULL.391* \returns true if the process exited, false otherwise.392*393* \threadsafety This function is not thread safe.394*395* \since This function is available since SDL 3.2.0.396*397* \sa SDL_CreateProcess398* \sa SDL_CreateProcessWithProperties399* \sa SDL_KillProcess400* \sa SDL_DestroyProcess401*/402extern SDL_DECLSPEC bool SDLCALL SDL_WaitProcess(SDL_Process *process, bool block, int *exitcode);403404/**405* Destroy a previously created process object.406*407* Note that this does not stop the process, just destroys the SDL object used408* to track it. If you want to stop the process you should use409* SDL_KillProcess().410*411* \param process The process object to destroy.412*413* \threadsafety This function is not thread safe.414*415* \since This function is available since SDL 3.2.0.416*417* \sa SDL_CreateProcess418* \sa SDL_CreateProcessWithProperties419* \sa SDL_KillProcess420*/421extern SDL_DECLSPEC void SDLCALL SDL_DestroyProcess(SDL_Process *process);422423/* Ends C function definitions when using C++ */424#ifdef __cplusplus425}426#endif427#include <SDL3/SDL_close_code.h>428429#endif /* SDL_process_h_ */430431432