SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Classes | Typedefs | Functions | Variables

Process control support. More...

Collaboration diagram for Process Control:

Classes

struct  SDL::ProcessParam
 Safely wrap Process for non owning parameters. More...
 
class  SDL::Process
 An opaque handle representing a system process. More...
 
struct  SDL::ProcessRef
 Semi-safe reference for Process. More...
 

Typedefs

using SDL::ProcessRaw = SDL_Process *
 Alias to raw representation for Process.
 
using SDL::ProcessIO = SDL_ProcessIO
 Description of where standard I/O should be directed when creating a process. More...
 

Functions

Process SDL::CreateProcess (const char *const *args, bool pipe_stdio)
 Create a new process. More...
 
Process SDL::CreateProcessWithProperties (PropertiesParam props)
 Create a new process with the specified properties. More...
 
PropertiesRef SDL::GetProcessProperties (ProcessParam process)
 Get the properties associated with a process. More...
 
StringResult SDL::ReadProcess (ProcessParam process, int *exitcode=nullptr)
 Read all the output from a process. More...
 
IOStreamRef SDL::GetProcessInput (ProcessParam process)
 Get the IOStream associated with process standard input. More...
 
IOStreamRef SDL::GetProcessOutput (ProcessParam process)
 Get the IOStream associated with process standard output. More...
 
void SDL::KillProcess (ProcessParam process, bool force)
 Stop a process. More...
 
bool SDL::WaitProcess (ProcessParam process, bool block, int *exitcode)
 Wait for a process to finish. More...
 
void SDL::DestroyProcess (ProcessRaw process)
 Destroy a previously created process object. More...
 
PropertiesRef SDL::Process::GetProperties () const
 Get the properties associated with a process. More...
 
StringResult SDL::Process::Read (int *exitcode=nullptr)
 Read all the output from a process. More...
 
IOStreamRef SDL::Process::GetInput ()
 Get the IOStream associated with process standard input. More...
 
IOStreamRef SDL::Process::GetOutput ()
 Get the IOStream associated with process standard output. More...
 
void SDL::Process::Kill (bool force)
 Stop a process. More...
 
bool SDL::Process::Wait (bool block, int *exitcode)
 Wait for a process to finish. More...
 
void SDL::Process::Destroy ()
 Destroy a previously created process object. More...
 

Variables

constexpr ProcessIO SDL::PROCESS_STDIO_INHERITED
 The I/O stream is inherited from the application. More...
 
constexpr ProcessIO SDL::PROCESS_STDIO_NULL
 The I/O stream is ignored. More...
 
constexpr ProcessIO SDL::PROCESS_STDIO_APP = SDL_PROCESS_STDIO_APP
 The I/O stream is connected to a new IOStream that the application can read or write.
 
constexpr ProcessIO SDL::PROCESS_STDIO_REDIRECT = SDL_PROCESS_STDIO_REDIRECT
 The I/O stream is redirected to an existing IOStream.
 

Detailed Description

These functions provide a cross-platform way to spawn and manage OS-level processes.

You can create a new subprocess with Process.Process() and optionally read and write to it using Process.Read() or Process.GetInput() and Process.GetOutput(). If more advanced functionality like chaining input between processes is necessary, you can use Process.Process().

You can get the status of a created process with Process.Wait(), or terminate the process with Process.Kill().

Don't forget to call Process.Destroy() to clean up, whether the process process was killed, terminated on its own, or is still running!

Typedef Documentation

◆ ProcessIO

using SDL::ProcessIO = typedef SDL_ProcessIO

If a standard I/O stream is set to PROCESS_STDIO_INHERITED, it will go to the same place as the application's I/O stream. This is the default for standard output and standard error.

If a standard I/O stream is set to SDL_PROCESS_STDIO_nullptr, it is connected to NUL: on Windows and /dev/null on POSIX systems. This is the default for standard input.

If a standard I/O stream is set to PROCESS_STDIO_APP, it is connected to a new IOStream that is available to the application. Standard input will be available as prop::process.STDIN_POINTER and allows Process.GetInput(), standard output will be available as prop::process.STDOUT_POINTER and allows Process.Read() and Process.GetOutput(), and standard error will be available as prop::process.STDERR_POINTER in the properties for the created process.

If a standard I/O stream is set to PROCESS_STDIO_REDIRECT, it is connected to an existing IOStream provided by the application. Standard input is provided using prop::process.CREATE_STDIN_POINTER, standard output is provided using prop::process.CREATE_STDOUT_POINTER, and standard error is provided using prop::process.CREATE_STDERR_POINTER in the creation properties. These existing streams should be closed by the application once the new process is created.

In order to use an IOStream with PROCESS_STDIO_REDIRECT, it must have prop::IOStream.WINDOWS_HANDLE_POINTER or prop::IOStream.FILE_DESCRIPTOR_NUMBER set. This is true for streams representing files and process I/O.

Since
This enum is available since SDL 3.2.0.
See also
Process.Process
Process.GetProperties
Process.Read
Process.GetInput
Process.GetOutput

Function Documentation

◆ CreateProcess()

Process SDL::CreateProcess ( const char *const *  args,
bool  pipe_stdio 
)
inline

The path to the executable is supplied in args[0]. args[1..N] are additional arguments passed on the command line of the new process, and the argument list should be terminated with a nullptr, e.g.:

const char *args[] = { "myprogram", "argument", nullptr };

Setting pipe_stdio to true is equivalent to setting prop::process.CREATE_STDIN_NUMBER and prop::process.CREATE_STDOUT_NUMBER to PROCESS_STDIO_APP, and will allow the use of Process.Read() or Process.GetInput() and Process.GetOutput().

See Process.Process() for more details.

Parameters
argsthe path and arguments for the new process.
pipe_stdiotrue to create pipes to the process's standard input and from the process's standard output, false for the process to have no input and inherit the application's standard output.
Returns
the newly created and running process, or nullptr if the process couldn't be created.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.GetProperties
Process.Read
Process.GetInput
Process.GetOutput
Process.Kill
Process.Wait
Process.Destroy

◆ CreateProcessWithProperties()

Process SDL::CreateProcessWithProperties ( PropertiesParam  props)
inline

These are the supported properties:

  • prop::process.CREATE_ARGS_POINTER: an array of strings containing the program to run, any arguments, and a nullptr pointer, e.g. const char *args[] = { "myprogram", "argument", nullptr }. This is a required property.
  • prop::process.CREATE_ENVIRONMENT_POINTER: an Environment pointer. If this property is set, it will be the entire environment for the process, otherwise the current environment is used.
  • prop::process.CREATE_STDIN_NUMBER: an ProcessIO value describing where standard input for the process comes from, defaults to SDL_PROCESS_STDIO_nullptr.
  • prop::process.CREATE_STDIN_POINTER: an IOStream pointer used for standard input when prop::process.CREATE_STDIN_NUMBER is set to PROCESS_STDIO_REDIRECT.
  • prop::process.CREATE_STDOUT_NUMBER: an ProcessIO value describing where standard output for the process goes to, defaults to PROCESS_STDIO_INHERITED.
  • prop::process.CREATE_STDOUT_POINTER: an IOStream pointer used for standard output when prop::process.CREATE_STDOUT_NUMBER is set to PROCESS_STDIO_REDIRECT.
  • prop::process.CREATE_STDERR_NUMBER: an ProcessIO value describing where standard error for the process goes to, defaults to PROCESS_STDIO_INHERITED.
  • prop::process.CREATE_STDERR_POINTER: an IOStream pointer used for standard error when prop::process.CREATE_STDERR_NUMBER is set to PROCESS_STDIO_REDIRECT.
  • prop::process.CREATE_STDERR_TO_STDOUT_BOOLEAN: true if the error output of the process should be redirected into the standard output of the process. This property has no effect if prop::process.CREATE_STDERR_NUMBER is set.
  • prop::process.CREATE_BACKGROUND_BOOLEAN: true if the process should run in the background. In this case the default input and output is SDL_PROCESS_STDIO_nullptr and the exitcode of the process is not available, and will always be 0.

On POSIX platforms, wait() and waitpid(-1, ...) should not be called, and SIGCHLD should not be ignored or handled because those would prevent SDL from properly tracking the lifetime of the underlying process. You should use Process.Wait() instead.

Parameters
propsthe properties to use.
Returns
the newly created and running process, or nullptr if the process couldn't be created.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.GetProperties
Process.Read
Process.GetInput
Process.GetOutput
Process.Kill
Process.Wait
Process.Destroy

◆ Destroy()

void SDL::Process::Destroy ( )
inline

Note that this does not stop the process, just destroys the SDL object used to track it. If you want to stop the process you should use Process.Kill().

Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Kill

◆ DestroyProcess()

void SDL::DestroyProcess ( ProcessRaw  process)
inline

Note that this does not stop the process, just destroys the SDL object used to track it. If you want to stop the process you should use Process.Kill().

Parameters
processThe process object to destroy.
Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Kill

◆ GetInput()

IOStreamRef SDL::Process::GetInput ( )
inline

The process must have been created with Process.Process() and pipe_stdio set to true, or with Process.Process() and prop::process.CREATE_STDIN_NUMBER set to PROCESS_STDIO_APP.

Writing to this stream can return less data than expected if the process hasn't read its input. It may be blocked waiting for its output to be read, if so you may need to call Process.GetOutput() and read the output in parallel with writing input.

Returns
the input stream or nullptr on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.GetOutput

◆ GetOutput()

IOStreamRef SDL::Process::GetOutput ( )
inline

The process must have been created with Process.Process() and pipe_stdio set to true, or with Process.Process() and prop::process.CREATE_STDOUT_NUMBER set to PROCESS_STDIO_APP.

Reading from this stream can return 0 with IOStream.GetStatus() returning IO_STATUS_NOT_READY if no output is available yet.

Returns
the output stream or nullptr on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.GetInput

◆ GetProcessInput()

IOStreamRef SDL::GetProcessInput ( ProcessParam  process)
inline

The process must have been created with Process.Process() and pipe_stdio set to true, or with Process.Process() and prop::process.CREATE_STDIN_NUMBER set to PROCESS_STDIO_APP.

Writing to this stream can return less data than expected if the process hasn't read its input. It may be blocked waiting for its output to be read, if so you may need to call Process.GetOutput() and read the output in parallel with writing input.

Parameters
processThe process to get the input stream for.
Returns
the input stream or nullptr on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.GetOutput

◆ GetProcessOutput()

IOStreamRef SDL::GetProcessOutput ( ProcessParam  process)
inline

The process must have been created with Process.Process() and pipe_stdio set to true, or with Process.Process() and prop::process.CREATE_STDOUT_NUMBER set to PROCESS_STDIO_APP.

Reading from this stream can return 0 with IOStream.GetStatus() returning IO_STATUS_NOT_READY if no output is available yet.

Parameters
processThe process to get the output stream for.
Returns
the output stream or nullptr on failure; call GetError() for more information.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.GetInput

◆ GetProcessProperties()

PropertiesRef SDL::GetProcessProperties ( ProcessParam  process)
inline

The following read-only properties are provided by SDL:

  • prop::process.PID_NUMBER: the process ID of the process.
  • prop::process.STDIN_POINTER: an IOStream that can be used to write input to the process, if it was created with prop::process.CREATE_STDIN_NUMBER set to PROCESS_STDIO_APP.
  • prop::process.STDOUT_POINTER: a non-blocking IOStream that can be used to read output from the process, if it was created with prop::process.CREATE_STDOUT_NUMBER set to PROCESS_STDIO_APP.
  • prop::process.STDERR_POINTER: a non-blocking IOStream that can be used to read error output from the process, if it was created with prop::process.CREATE_STDERR_NUMBER set to PROCESS_STDIO_APP.
  • prop::process.BACKGROUND_BOOLEAN: true if the process is running in the background.
Parameters
processthe process to query.
Returns
a valid property ID on success.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process

◆ GetProperties()

PropertiesRef SDL::Process::GetProperties ( ) const
inline

The following read-only properties are provided by SDL:

  • prop::process.PID_NUMBER: the process ID of the process.
  • prop::process.STDIN_POINTER: an IOStream that can be used to write input to the process, if it was created with prop::process.CREATE_STDIN_NUMBER set to PROCESS_STDIO_APP.
  • prop::process.STDOUT_POINTER: a non-blocking IOStream that can be used to read output from the process, if it was created with prop::process.CREATE_STDOUT_NUMBER set to PROCESS_STDIO_APP.
  • prop::process.STDERR_POINTER: a non-blocking IOStream that can be used to read error output from the process, if it was created with prop::process.CREATE_STDERR_NUMBER set to PROCESS_STDIO_APP.
  • prop::process.BACKGROUND_BOOLEAN: true if the process is running in the background.
Returns
a valid property ID on success.
Exceptions
Erroron failure.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process

◆ Kill()

void SDL::Process::Kill ( bool  force)
inline
Parameters
forcetrue to terminate the process immediately, false to try to stop the process gracefully. In general you should try to stop the process gracefully first as terminating a process may leave it with half-written data or in some other unstable state.
Exceptions
Erroron failure.
Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Wait
Process.Destroy

◆ KillProcess()

void SDL::KillProcess ( ProcessParam  process,
bool  force 
)
inline
Parameters
processThe process to stop.
forcetrue to terminate the process immediately, false to try to stop the process gracefully. In general you should try to stop the process gracefully first as terminating a process may leave it with half-written data or in some other unstable state.
Exceptions
Erroron failure.
Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Wait
Process.Destroy

◆ Read()

StringResult SDL::Process::Read ( int *  exitcode = nullptr)
inline

If a process was created with I/O enabled, you can use this function to read the output. This function blocks until the process is complete, capturing all output, and providing the process exit code.

The data is allocated with a zero byte at the end (null terminated) for convenience. This extra byte is not included in the value reported via datasize.

The data should be freed with free().

Parameters
exitcodea pointer filled in with the process exit code if the process has exited, may be nullptr.
Returns
the data or nullptr on failure; call GetError() for more information.
Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Destroy

◆ ReadProcess()

StringResult SDL::ReadProcess ( ProcessParam  process,
int *  exitcode = nullptr 
)
inline

If a process was created with I/O enabled, you can use this function to read the output. This function blocks until the process is complete, capturing all output, and providing the process exit code.

The data is allocated with a zero byte at the end (null terminated) for convenience. This extra byte is not included in the value reported via datasize.

The data should be freed with free().

Parameters
processThe process to read.
exitcodea pointer filled in with the process exit code if the process has exited, may be nullptr.
Returns
the data or nullptr on failure; call GetError() for more information.
Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Destroy

◆ Wait()

bool SDL::Process::Wait ( bool  block,
int *  exitcode 
)
inline

This can be called multiple times to get the status of a process.

The exit code will be the exit code of the process if it terminates normally, a negative signal if it terminated due to a signal, or -255 otherwise. It will not be changed if the process is still running.

If you create a process with standard output piped to the application (pipe_stdio being true) then you should read all of the process output before calling Process.Wait(). If you don't do this the process might be blocked indefinitely waiting for output to be read and Process.Wait() will never return true;

Parameters
blockIf true, block until the process finishes; otherwise, report on the process' status.
exitcodea pointer filled in with the process exit code if the process has exited, may be nullptr.
Returns
true if the process exited, false otherwise.
Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Kill
Process.Destroy

◆ WaitProcess()

bool SDL::WaitProcess ( ProcessParam  process,
bool  block,
int *  exitcode 
)
inline

This can be called multiple times to get the status of a process.

The exit code will be the exit code of the process if it terminates normally, a negative signal if it terminated due to a signal, or -255 otherwise. It will not be changed if the process is still running.

If you create a process with standard output piped to the application (pipe_stdio being true) then you should read all of the process output before calling Process.Wait(). If you don't do this the process might be blocked indefinitely waiting for output to be read and Process.Wait() will never return true;

Parameters
processThe process to wait for.
blockIf true, block until the process finishes; otherwise, report on the process' status.
exitcodea pointer filled in with the process exit code if the process has exited, may be nullptr.
Returns
true if the process exited, false otherwise.
Thread safety:
This function is not thread safe.
Since
This function is available since SDL 3.2.0.
See also
Process.Process
Process.Process
Process.Kill
Process.Destroy

Variable Documentation

◆ PROCESS_STDIO_INHERITED

constexpr ProcessIO SDL::PROCESS_STDIO_INHERITED
constexpr
Initial value:
=
SDL_PROCESS_STDIO_INHERITED

◆ PROCESS_STDIO_NULL

constexpr ProcessIO SDL::PROCESS_STDIO_NULL
constexpr
Initial value:
=
SDL_PROCESS_STDIO_NULL