SDL_process.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryProcess
  20. *
  21. * Process control support.
  22. *
  23. * These functions provide a cross-platform way to spawn and manage OS-level
  24. * processes.
  25. *
  26. * You can create a new subprocess with SDL_CreateProcess() and optionally read and write to it using SDL_ReadProcess() and SDL_WriteProcess(). If more advanced functionality like chaining input between processes is necessary, you can use SDL_CreateProcessWithProperties().
  27. *
  28. * You can get the status of a created process with SDL_WaitProcess(), or terminate the process with SDL_KillProcess().
  29. *
  30. * Don't forget to call SDL_DestroyProcess() to clean up, whether the process
  31. * process was killed, terminated on its own, or is still running!
  32. */
  33. #ifndef SDL_process_h_
  34. #define SDL_process_h_
  35. #include <SDL3/SDL_error.h>
  36. #include <SDL3/SDL_begin_code.h>
  37. /* Set up for C function definitions, even when using C++ */
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. typedef struct SDL_Process SDL_Process;
  42. /**
  43. * Create a new process.
  44. *
  45. * 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 NULL, e.g.:
  46. *
  47. * ```c
  48. * const char *args[] = { "myprogram", "argument", NULL };
  49. * ```
  50. *
  51. * Setting pipe_stdio to SDL_TRUE is equivalent to setting `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` and `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` to `SDL_PROCESS_STDIO_APP`, and will allow the use of SDL_ReadProcess() and SDL_WriteProcess().
  52. *
  53. * See SDL_CreateProcessWithProperties() for more details.
  54. *
  55. * \param args the path and arguments for the new process.
  56. * \param pipe_stdio SDL_TRUE to create pipes to the process's standard input and from the process's standard output, SDL_FALSE for the process to have no input and inherit the application's standard output.
  57. * \returns the newly created and running process, or NULL if the process couldn't be created.
  58. *
  59. * \sa SDL_CreateProcessWithProperties
  60. * \sa SDL_GetProcessProperties
  61. * \sa SDL_ReadProcess
  62. * \sa SDL_WriteProcess
  63. * \sa SDL_KillProcess
  64. * \sa SDL_WaitProcess
  65. * \sa SDL_DestroyProcess
  66. *
  67. * \threadsafety It is safe to call this function from any thread.
  68. *
  69. * \since This function is available since SDL 3.0.0.
  70. */
  71. extern SDL_DECLSPEC SDL_Process *SDLCALL SDL_CreateProcess(const char * const *args, SDL_bool pipe_stdio);
  72. /**
  73. * Description of where standard I/O should be directed when creating a process.
  74. *
  75. * If a standard I/O stream is set to SDL_PROCESS_STDIO_INHERIT, it will go to the same place as the application's I/O stream. This is the default for standard output and standard error.
  76. *
  77. * If a standard I/O stream is set to SDL_PROCESS_STDIO_NULL, it is connected to `NUL:` on Windows and `/dev/null` on POSIX systems. This is the default for standard input.
  78. *
  79. * If a standard I/O stream is set to SDL_PROCESS_STDIO_APP, it is connected to a new SDL_IOStream that is available to the application. Standard input will be available as `SDL_PROP_PROCESS_STDIN_POINTER` and allows SDL_WriteProcess(), standard output will be available as `SDL_PROP_PROCESS_STDOUT_POINTER` and allows SDL_ReadProcess(), and standard error will be available as `SDL_PROP_PROCESS_STDERR_POINTER` in the properties for the created process.
  80. *
  81. * If a standard I/O stream is set to SDL_PROCESS_STDIO_REDIRECT, it is connected to an existing SDL_IOStream provided by the application. Standard input is provided using `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`, standard output is provided using `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`, and standard error is provided using `SDL_PROP_PROCESS_CREATE_STDERR_POINTER` in the creation properties. These existing streams should be closed by the application once the new process is created.
  82. *
  83. * In order to use an SDL_IOStream with SDL_PROCESS_STDIO_REDIRECT, it must have `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER` or `SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER` set. This is true for streams representing files and process I/O.
  84. *
  85. * \sa SDL_CreateProcessWithProperties
  86. * \sa SDL_GetProcessProperties
  87. * \sa SDL_ReadProcess
  88. * \sa SDL_WriteProcess
  89. *
  90. * \since This enum is available since SDL 3.0.0.
  91. */
  92. typedef enum SDL_ProcessIO
  93. {
  94. SDL_PROCESS_STDIO_INHERITED, /**< The I/O stream is inherited from the application. */
  95. SDL_PROCESS_STDIO_NULL, /**< The I/O stream is ignored. */
  96. SDL_PROCESS_STDIO_APP, /**< The I/O stream is connected to a new SDL_IOStream that the application can read or write */
  97. SDL_PROCESS_STDIO_REDIRECT, /**< The I/O stream is redirected to an existing SDL_IOStream. */
  98. } SDL_ProcessIO;
  99. /**
  100. * Create a new process with the specified properties.
  101. *
  102. * These are the supported properties:
  103. *
  104. * - `SDL_PROP_PROCESS_CREATE_ARGS_POINTER`: an array of strings containing the program to run, any arguments, and a NULL pointer, e.g. const char *args[] = { "myprogram", "argument", NULL }. This is a required property.
  105. * - `SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER`: an array of strings containing variable=value, and a NULL pointer, e.g. const char *env[] = { "PATH=/bin:/usr/bin", NULL }. If this property is set, it will be the entire environment for the process, otherwise the current environment is used.
  106. * - `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER`: an SDL_ProcessIO value describing where standard input for the process comes from, defaults to `SDL_PROCESS_STDIO_NULL`.
  107. * - `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`: an SDL_IOStream pointer used for standard input when `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` is set to `SDL_PROCESS_STDIO_REDIRECT`.
  108. * - `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER`: an SDL_ProcessIO value describing where standard output for the process goes go, defaults to `SDL_PROCESS_STDIO_INHERITED`.
  109. * - `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`: an SDL_IOStream pointer used for standard output when `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` is set to `SDL_PROCESS_STDIO_REDIRECT`.
  110. * - `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER`: an SDL_ProcessIO value describing where standard error for the process goes go, defaults to `SDL_PROCESS_STDIO_INHERITED`.
  111. * - `SDL_PROP_PROCESS_CREATE_STDERR_POINTER`: an SDL_IOStream pointer used for standard error when `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set to `SDL_PROCESS_STDIO_REDIRECT`.
  112. * - `SDL_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 `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set.
  113. *
  114. * 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 SDL_WaitProcess() instead.
  115. *
  116. * \param props the properties to use.
  117. * \returns the newly created and running process, or NULL if the process couldn't be created.
  118. *
  119. * \sa SDL_CreateProcess
  120. * \sa SDL_GetProcessProperties
  121. * \sa SDL_ReadProcess
  122. * \sa SDL_WriteProcess
  123. * \sa SDL_KillProcess
  124. * \sa SDL_WaitProcess
  125. * \sa SDL_DestroyProcess
  126. *
  127. * \threadsafety It is safe to call this function from any thread.
  128. *
  129. * \since This function is available since SDL 3.0.0.
  130. */
  131. extern SDL_DECLSPEC SDL_Process *SDLCALL SDL_CreateProcessWithProperties(SDL_PropertiesID props);
  132. #define SDL_PROP_PROCESS_CREATE_ARGS_POINTER "SDL.process.create.args"
  133. #define SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER "SDL.process.create.environment"
  134. #define SDL_PROP_PROCESS_CREATE_STDIN_NUMBER "SDL.process.create.stdin_option"
  135. #define SDL_PROP_PROCESS_CREATE_STDIN_POINTER "SDL.process.create.stdin_source"
  136. #define SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER "SDL.process.create.stdout_option"
  137. #define SDL_PROP_PROCESS_CREATE_STDOUT_POINTER "SDL.process.create.stdout_source"
  138. #define SDL_PROP_PROCESS_CREATE_STDERR_NUMBER "SDL.process.create.stderr_option"
  139. #define SDL_PROP_PROCESS_CREATE_STDERR_POINTER "SDL.process.create.stderr_source"
  140. #define SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN "SDL.process.create.stderr_to_stdout"
  141. /**
  142. * Get the properties associated with a process.
  143. *
  144. * The following read-only properties are provided by SDL:
  145. *
  146. * - `SDL_PROP_PROCESS_PID_NUMBER`: the process ID of the process.
  147. * - `SDL_PROP_PROCESS_STDIN_POINTER`: an SDL_IOStream that can be used to write input to the process, if it was created with `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  148. * - `SDL_PROP_PROCESS_STDOUT_POINTER`: a non-blocking SDL_IOStream that can be used to read output from the process, if it was created with `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  149. * - `SDL_PROP_PROCESS_STDERR_POINTER`: a non-blocking SDL_IOStream that can be used to read error output from the process, if it was created with `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  150. *
  151. * \param process the process to query.
  152. * \returns a valid property ID on success or 0 on failure; call
  153. * SDL_GetError() for more information.
  154. *
  155. * \sa SDL_CreateProcess
  156. * \sa SDL_CreateProcessWithProperties
  157. *
  158. * \threadsafety It is safe to call this function from any thread.
  159. *
  160. * \since This function is available since SDL 3.0.0.
  161. */
  162. extern SDL_DECLSPEC SDL_PropertiesID SDL_GetProcessProperties(SDL_Process *process);
  163. #define SDL_PROP_PROCESS_PID_NUMBER "SDL.process.pid"
  164. #define SDL_PROP_PROCESS_STDIN_POINTER "SDL.process.stdin"
  165. #define SDL_PROP_PROCESS_STDOUT_POINTER "SDL.process.stdout"
  166. #define SDL_PROP_PROCESS_STDERR_POINTER "SDL.process.stderr"
  167. /**
  168. * Read all the output from a process.
  169. *
  170. * 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.
  171. *
  172. * This is just a convenience function. If you need more control over the process, you can get the output stream from the process properties and read it directly.
  173. *
  174. * The data is allocated with a zero byte at the end (null terminated) for
  175. * convenience. This extra byte is not included in the value reported via
  176. * `datasize`.
  177. *
  178. * The data should be freed with SDL_free().
  179. *
  180. * \param process The process to read.
  181. * \param datasize a pointer filled in with the number of bytes read, may be NULL.
  182. * \param exitcode a pointer filled in with the process exit code if the process has exited, may be NULL.
  183. * \returns the data or NULL on failure; call SDL_GetError() for more
  184. * information.
  185. *
  186. * \sa SDL_CreateProcess
  187. * \sa SDL_CreateProcessWithProperties
  188. * \sa SDL_GetProcessProperties
  189. * \sa SDL_WriteProcess
  190. * \sa SDL_DestroyProcess
  191. *
  192. * \threadsafety This function is not thread safe.
  193. *
  194. * \since This function is available since SDL 3.0.0.
  195. */
  196. extern SDL_DECLSPEC void * SDLCALL SDL_ReadProcess(SDL_Process *process, size_t *datasize, int *exitcode);
  197. /**
  198. * Write to a process.
  199. *
  200. * If a process was created with I/O enabled, you can use this function to send data as input to the process. This function blocks until the data is written.
  201. *
  202. * This is just a convenience function. If the process is structured so it takes large amounts of input and generates lots of output, you should get the input and output streams from the process properties and handle them simultaneously to prevent the process from being blocked waiting for I/O.
  203. *
  204. * \param process The process to write.
  205. * \param ptr a pointer to a buffer containing data to write.
  206. * \param size the number of bytes to write.
  207. * \param closeio if SDL_TRUE, closes the process input before returning,
  208. * even in the case of an error.
  209. * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
  210. * for more information.
  211. *
  212. * \sa SDL_CreateProcess
  213. * \sa SDL_CreateProcessWithProperties
  214. * \sa SDL_GetProcessProperties
  215. * \sa SDL_ReadProcess
  216. * \sa SDL_DestroyProcess
  217. *
  218. * \threadsafety This function is not thread safe.
  219. *
  220. * \since This function is available since SDL 3.0.0.
  221. */
  222. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteProcess(SDL_Process *process, const void *ptr, size_t size, SDL_bool closeio);
  223. /**
  224. * Stop a process.
  225. *
  226. * \param process The process to stop.
  227. * \param force SDL_TRUE to terminate the process immediately, SDL_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.
  228. * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
  229. * for more information.
  230. *
  231. * \sa SDL_CreateProcess
  232. * \sa SDL_CreateProcessWithProperties
  233. * \sa SDL_WaitProcess
  234. * \sa SDL_DestroyProcess
  235. *
  236. * \threadsafety This function is not thread safe.
  237. *
  238. * \since This function is available since SDL 3.0.0.
  239. */
  240. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_KillProcess(SDL_Process *process, SDL_bool force);
  241. /**
  242. * Wait for a process to finish.
  243. *
  244. * This can be called multiple times to get the status of a process.
  245. *
  246. * 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.
  247. *
  248. * \param process The process to wait for.
  249. * \param block If true, block until the process finishes; otherwise, report on the process' status.
  250. * \param exitcode a pointer filled in with the process exit code if the process has exited, may be NULL.
  251. * \returns SDL_TRUE if the process exited, SDL_FALSE otherwise.
  252. *
  253. * \sa SDL_CreateProcess
  254. * \sa SDL_CreateProcessWithProperties
  255. * \sa SDL_KillProcess
  256. * \sa SDL_DestroyProcess
  257. *
  258. * \threadsafety This function is not thread safe.
  259. *
  260. * \since This function is available since SDL 3.0.0.
  261. */
  262. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WaitProcess(SDL_Process *process, SDL_bool block, int *exitcode);
  263. /**
  264. * Destroy a previously created process object.
  265. *
  266. * 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 SDL_KillProcess().
  267. *
  268. * \param process The process object to destroy.
  269. *
  270. * \sa SDL_CreateProcess
  271. * \sa SDL_CreateProcessWithProperties
  272. * \sa SDL_KillProcess
  273. *
  274. * \threadsafety This function is not thread safe.
  275. *
  276. * \since This function is available since SDL 3.0.0.
  277. */
  278. extern SDL_DECLSPEC void SDLCALL SDL_DestroyProcess(SDL_Process *process);
  279. /* Ends C function definitions when using C++ */
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #include <SDL3/SDL_close_code.h>
  284. #endif /* SDL_process_h_ */