SDL_process.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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
  27. * read and write to it using SDL_ReadProcess() or SDL_GetProcessInput() and
  28. * SDL_GetProcessOutput(). If more advanced functionality like chaining input
  29. * between processes is necessary, you can use
  30. * SDL_CreateProcessWithProperties().
  31. *
  32. * You can get the status of a created process with SDL_WaitProcess(), or
  33. * terminate the process with SDL_KillProcess().
  34. *
  35. * Don't forget to call SDL_DestroyProcess() to clean up, whether the process
  36. * process was killed, terminated on its own, or is still running!
  37. */
  38. #ifndef SDL_process_h_
  39. #define SDL_process_h_
  40. #include <SDL3/SDL_stdinc.h>
  41. #include <SDL3/SDL_error.h>
  42. #include <SDL3/SDL_iostream.h>
  43. #include <SDL3/SDL_properties.h>
  44. #include <SDL3/SDL_begin_code.h>
  45. /* Set up for C function definitions, even when using C++ */
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. typedef struct SDL_Process SDL_Process;
  50. /**
  51. * Create a new process.
  52. *
  53. * The path to the executable is supplied in args[0]. args[1..N] are
  54. * additional arguments passed on the command line of the new process, and the
  55. * argument list should be terminated with a NULL, e.g.:
  56. *
  57. * ```c
  58. * const char *args[] = { "myprogram", "argument", NULL };
  59. * ```
  60. *
  61. * Setting pipe_stdio to true is equivalent to setting
  62. * `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` and
  63. * `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` to `SDL_PROCESS_STDIO_APP`, and
  64. * will allow the use of SDL_ReadProcess() or SDL_GetProcessInput() and
  65. * SDL_GetProcessOutput().
  66. *
  67. * See SDL_CreateProcessWithProperties() for more details.
  68. *
  69. * \param args the path and arguments for the new process.
  70. * \param pipe_stdio true to create pipes to the process's standard input and
  71. * from the process's standard output, false for the process
  72. * to have no input and inherit the application's standard
  73. * output.
  74. * \returns the newly created and running process, or NULL if the process
  75. * couldn't be created.
  76. *
  77. * \threadsafety It is safe to call this function from any thread.
  78. *
  79. * \since This function is available since SDL 3.1.3.
  80. *
  81. * \sa SDL_CreateProcessWithProperties
  82. * \sa SDL_GetProcessProperties
  83. * \sa SDL_ReadProcess
  84. * \sa SDL_GetProcessInput
  85. * \sa SDL_GetProcessOutput
  86. * \sa SDL_KillProcess
  87. * \sa SDL_WaitProcess
  88. * \sa SDL_DestroyProcess
  89. */
  90. extern SDL_DECLSPEC SDL_Process *SDLCALL SDL_CreateProcess(const char * const *args, bool pipe_stdio);
  91. /**
  92. * Description of where standard I/O should be directed when creating a
  93. * process.
  94. *
  95. * If a standard I/O stream is set to SDL_PROCESS_STDIO_INHERIT, it will go to
  96. * the same place as the application's I/O stream. This is the default for
  97. * standard output and standard error.
  98. *
  99. * If a standard I/O stream is set to SDL_PROCESS_STDIO_NULL, it is connected
  100. * to `NUL:` on Windows and `/dev/null` on POSIX systems. This is the default
  101. * for standard input.
  102. *
  103. * If a standard I/O stream is set to SDL_PROCESS_STDIO_APP, it is connected
  104. * to a new SDL_IOStream that is available to the application. Standard input
  105. * will be available as `SDL_PROP_PROCESS_STDIN_POINTER` and allows
  106. * SDL_GetProcessInput(), standard output will be available as
  107. * `SDL_PROP_PROCESS_STDOUT_POINTER` and allows SDL_ReadProcess() and
  108. * SDL_GetProcessOutput(), and standard error will be available as
  109. * `SDL_PROP_PROCESS_STDERR_POINTER` in the properties for the created
  110. * process.
  111. *
  112. * If a standard I/O stream is set to SDL_PROCESS_STDIO_REDIRECT, it is
  113. * connected to an existing SDL_IOStream provided by the application. Standard
  114. * input is provided using `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`, standard
  115. * output is provided using `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`, and
  116. * standard error is provided using `SDL_PROP_PROCESS_CREATE_STDERR_POINTER`
  117. * in the creation properties. These existing streams should be closed by the
  118. * application once the new process is created.
  119. *
  120. * In order to use an SDL_IOStream with SDL_PROCESS_STDIO_REDIRECT, it must
  121. * have `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER` or
  122. * `SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER` set. This is true for streams
  123. * representing files and process I/O.
  124. *
  125. * \since This enum is available since SDL 3.1.3.
  126. *
  127. * \sa SDL_CreateProcessWithProperties
  128. * \sa SDL_GetProcessProperties
  129. * \sa SDL_ReadProcess
  130. * \sa SDL_GetProcessInput
  131. * \sa SDL_GetProcessOutput
  132. */
  133. typedef enum SDL_ProcessIO
  134. {
  135. SDL_PROCESS_STDIO_INHERITED, /**< The I/O stream is inherited from the application. */
  136. SDL_PROCESS_STDIO_NULL, /**< The I/O stream is ignored. */
  137. SDL_PROCESS_STDIO_APP, /**< The I/O stream is connected to a new SDL_IOStream that the application can read or write */
  138. SDL_PROCESS_STDIO_REDIRECT /**< The I/O stream is redirected to an existing SDL_IOStream. */
  139. } SDL_ProcessIO;
  140. /**
  141. * Create a new process with the specified properties.
  142. *
  143. * These are the supported properties:
  144. *
  145. * - `SDL_PROP_PROCESS_CREATE_ARGS_POINTER`: an array of strings containing
  146. * the program to run, any arguments, and a NULL pointer, e.g. const char
  147. * *args[] = { "myprogram", "argument", NULL }. This is a required property.
  148. * - `SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER`: an SDL_Environment
  149. * pointer. If this property is set, it will be the entire environment for
  150. * the process, otherwise the current environment is used.
  151. * - `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER`: an SDL_ProcessIO value describing
  152. * where standard input for the process comes from, defaults to
  153. * `SDL_PROCESS_STDIO_NULL`.
  154. * - `SDL_PROP_PROCESS_CREATE_STDIN_POINTER`: an SDL_IOStream pointer used for
  155. * standard input when `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` is set to
  156. * `SDL_PROCESS_STDIO_REDIRECT`.
  157. * - `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER`: an SDL_ProcessIO value
  158. * describing where standard output for the process goes go, defaults to
  159. * `SDL_PROCESS_STDIO_INHERITED`.
  160. * - `SDL_PROP_PROCESS_CREATE_STDOUT_POINTER`: an SDL_IOStream pointer used
  161. * for standard output when `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` is set
  162. * to `SDL_PROCESS_STDIO_REDIRECT`.
  163. * - `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER`: an SDL_ProcessIO value
  164. * describing where standard error for the process goes go, defaults to
  165. * `SDL_PROCESS_STDIO_INHERITED`.
  166. * - `SDL_PROP_PROCESS_CREATE_STDERR_POINTER`: an SDL_IOStream pointer used
  167. * for standard error when `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set to
  168. * `SDL_PROCESS_STDIO_REDIRECT`.
  169. * - `SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN`: true if the error
  170. * output of the process should be redirected into the standard output of
  171. * the process. This property has no effect if
  172. * `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` is set.
  173. * - `SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN`: true if the process should
  174. * run in the background. In this case the default input and output is
  175. * `SDL_PROCESS_STDIO_NULL` and the exitcode of the process is not
  176. * available, and will always be 0.
  177. *
  178. * On POSIX platforms, wait() and waitpid(-1, ...) should not be called, and
  179. * SIGCHLD should not be ignored or handled because those would prevent SDL
  180. * from properly tracking the lifetime of the underlying process. You should
  181. * use SDL_WaitProcess() instead.
  182. *
  183. * \param props the properties to use.
  184. * \returns the newly created and running process, or NULL if the process
  185. * couldn't be created.
  186. *
  187. * \threadsafety It is safe to call this function from any thread.
  188. *
  189. * \since This function is available since SDL 3.1.3.
  190. *
  191. * \sa SDL_CreateProcess
  192. * \sa SDL_GetProcessProperties
  193. * \sa SDL_ReadProcess
  194. * \sa SDL_GetProcessInput
  195. * \sa SDL_GetProcessOutput
  196. * \sa SDL_KillProcess
  197. * \sa SDL_WaitProcess
  198. * \sa SDL_DestroyProcess
  199. */
  200. extern SDL_DECLSPEC SDL_Process *SDLCALL SDL_CreateProcessWithProperties(SDL_PropertiesID props);
  201. #define SDL_PROP_PROCESS_CREATE_ARGS_POINTER "SDL.process.create.args"
  202. #define SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER "SDL.process.create.environment"
  203. #define SDL_PROP_PROCESS_CREATE_STDIN_NUMBER "SDL.process.create.stdin_option"
  204. #define SDL_PROP_PROCESS_CREATE_STDIN_POINTER "SDL.process.create.stdin_source"
  205. #define SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER "SDL.process.create.stdout_option"
  206. #define SDL_PROP_PROCESS_CREATE_STDOUT_POINTER "SDL.process.create.stdout_source"
  207. #define SDL_PROP_PROCESS_CREATE_STDERR_NUMBER "SDL.process.create.stderr_option"
  208. #define SDL_PROP_PROCESS_CREATE_STDERR_POINTER "SDL.process.create.stderr_source"
  209. #define SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN "SDL.process.create.stderr_to_stdout"
  210. #define SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN "SDL.process.create.background"
  211. /**
  212. * Get the properties associated with a process.
  213. *
  214. * The following read-only properties are provided by SDL:
  215. *
  216. * - `SDL_PROP_PROCESS_PID_NUMBER`: the process ID of the process.
  217. * - `SDL_PROP_PROCESS_STDIN_POINTER`: an SDL_IOStream that can be used to
  218. * write input to the process, if it was created with
  219. * `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  220. * - `SDL_PROP_PROCESS_STDOUT_POINTER`: a non-blocking SDL_IOStream that can
  221. * be used to read output from the process, if it was created with
  222. * `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  223. * - `SDL_PROP_PROCESS_STDERR_POINTER`: a non-blocking SDL_IOStream that can
  224. * be used to read error output from the process, if it was created with
  225. * `SDL_PROP_PROCESS_CREATE_STDERR_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  226. * - `SDL_PROP_PROCESS_BACKGROUND_BOOLEAN`: true if the process is running in
  227. * the background.
  228. *
  229. * \param process the process to query.
  230. * \returns a valid property ID on success or 0 on failure; call
  231. * SDL_GetError() for more information.
  232. *
  233. * \threadsafety It is safe to call this function from any thread.
  234. *
  235. * \since This function is available since SDL 3.1.3.
  236. *
  237. * \sa SDL_CreateProcess
  238. * \sa SDL_CreateProcessWithProperties
  239. */
  240. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetProcessProperties(SDL_Process *process);
  241. #define SDL_PROP_PROCESS_PID_NUMBER "SDL.process.pid"
  242. #define SDL_PROP_PROCESS_STDIN_POINTER "SDL.process.stdin"
  243. #define SDL_PROP_PROCESS_STDOUT_POINTER "SDL.process.stdout"
  244. #define SDL_PROP_PROCESS_STDERR_POINTER "SDL.process.stderr"
  245. #define SDL_PROP_PROCESS_BACKGROUND_BOOLEAN "SDL.process.background"
  246. /**
  247. * Read all the output from a process.
  248. *
  249. * If a process was created with I/O enabled, you can use this function to
  250. * read the output. This function blocks until the process is complete,
  251. * capturing all output, and providing the process exit code.
  252. *
  253. * The data is allocated with a zero byte at the end (null terminated) for
  254. * convenience. This extra byte is not included in the value reported via
  255. * `datasize`.
  256. *
  257. * The data should be freed with SDL_free().
  258. *
  259. * \param process The process to read.
  260. * \param datasize a pointer filled in with the number of bytes read, may be
  261. * NULL.
  262. * \param exitcode a pointer filled in with the process exit code if the
  263. * process has exited, may be NULL.
  264. * \returns the data or NULL on failure; call SDL_GetError() for more
  265. * information.
  266. *
  267. * \threadsafety This function is not thread safe.
  268. *
  269. * \since This function is available since SDL 3.1.3.
  270. *
  271. * \sa SDL_CreateProcess
  272. * \sa SDL_CreateProcessWithProperties
  273. * \sa SDL_DestroyProcess
  274. */
  275. extern SDL_DECLSPEC void * SDLCALL SDL_ReadProcess(SDL_Process *process, size_t *datasize, int *exitcode);
  276. /**
  277. * Get the SDL_IOStream associated with process standard input.
  278. *
  279. * The process must have been created with SDL_CreateProcess() and pipe_stdio
  280. * set to true, or with SDL_CreateProcessWithProperties() and
  281. * `SDL_PROP_PROCESS_CREATE_STDIN_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  282. *
  283. * Writing to this stream can return less data than expected if the process
  284. * hasn't read its input. It may be blocked waiting for its output to be read,
  285. * so if you may need to call SDL_GetOutputStream() and read the output in
  286. * parallel with writing input.
  287. *
  288. * \param process The process to get the input stream for.
  289. * \returns the input stream or NULL on failure; call SDL_GetError() for more
  290. * information.
  291. *
  292. * \threadsafety It is safe to call this function from any thread.
  293. *
  294. * \since This function is available since SDL 3.1.3.
  295. *
  296. * \sa SDL_CreateProcess
  297. * \sa SDL_CreateProcessWithProperties
  298. * \sa SDL_GetProcessOutput
  299. */
  300. extern SDL_DECLSPEC SDL_IOStream *SDLCALL SDL_GetProcessInput(SDL_Process *process);
  301. /**
  302. * Get the SDL_IOStream associated with process standard output.
  303. *
  304. * The process must have been created with SDL_CreateProcess() and pipe_stdio
  305. * set to true, or with SDL_CreateProcessWithProperties() and
  306. * `SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER` set to `SDL_PROCESS_STDIO_APP`.
  307. *
  308. * Reading from this stream can return 0 with SDL_GetIOStatus() returning
  309. * SDL_IO_STATUS_NOT_READY if no output is available yet.
  310. *
  311. * \param process The process to get the output stream for.
  312. * \returns the output stream or NULL on failure; call SDL_GetError() for more
  313. * information.
  314. *
  315. * \threadsafety It is safe to call this function from any thread.
  316. *
  317. * \since This function is available since SDL 3.1.3.
  318. *
  319. * \sa SDL_CreateProcess
  320. * \sa SDL_CreateProcessWithProperties
  321. * \sa SDL_GetProcessInput
  322. */
  323. extern SDL_DECLSPEC SDL_IOStream *SDLCALL SDL_GetProcessOutput(SDL_Process *process);
  324. /**
  325. * Stop a process.
  326. *
  327. * \param process The process to stop.
  328. * \param force true to terminate the process immediately, false to try to
  329. * stop the process gracefully. In general you should try to stop
  330. * the process gracefully first as terminating a process may
  331. * leave it with half-written data or in some other unstable
  332. * state.
  333. * \returns true on success or false on failure; call SDL_GetError() for more
  334. * information.
  335. *
  336. * \threadsafety This function is not thread safe.
  337. *
  338. * \since This function is available since SDL 3.1.3.
  339. *
  340. * \sa SDL_CreateProcess
  341. * \sa SDL_CreateProcessWithProperties
  342. * \sa SDL_WaitProcess
  343. * \sa SDL_DestroyProcess
  344. */
  345. extern SDL_DECLSPEC bool SDLCALL SDL_KillProcess(SDL_Process *process, bool force);
  346. /**
  347. * Wait for a process to finish.
  348. *
  349. * This can be called multiple times to get the status of a process.
  350. *
  351. * The exit code will be the exit code of the process if it terminates
  352. * normally, a negative signal if it terminated due to a signal, or -255
  353. * otherwise. It will not be changed if the process is still running.
  354. *
  355. * \param process The process to wait for.
  356. * \param block If true, block until the process finishes; otherwise, report
  357. * on the process' status.
  358. * \param exitcode a pointer filled in with the process exit code if the
  359. * process has exited, may be NULL.
  360. * \returns true if the process exited, false otherwise.
  361. *
  362. * \threadsafety This function is not thread safe.
  363. *
  364. * \since This function is available since SDL 3.1.3.
  365. *
  366. * \sa SDL_CreateProcess
  367. * \sa SDL_CreateProcessWithProperties
  368. * \sa SDL_KillProcess
  369. * \sa SDL_DestroyProcess
  370. */
  371. extern SDL_DECLSPEC bool SDLCALL SDL_WaitProcess(SDL_Process *process, bool block, int *exitcode);
  372. /**
  373. * Destroy a previously created process object.
  374. *
  375. * Note that this does not stop the process, just destroys the SDL object used
  376. * to track it. If you want to stop the process you should use
  377. * SDL_KillProcess().
  378. *
  379. * \param process The process object to destroy.
  380. *
  381. * \threadsafety This function is not thread safe.
  382. *
  383. * \since This function is available since SDL 3.1.3.
  384. *
  385. * \sa SDL_CreateProcess
  386. * \sa SDL_CreateProcessWithProperties
  387. * \sa SDL_KillProcess
  388. */
  389. extern SDL_DECLSPEC void SDLCALL SDL_DestroyProcess(SDL_Process *process);
  390. /* Ends C function definitions when using C++ */
  391. #ifdef __cplusplus
  392. }
  393. #endif
  394. #include <SDL3/SDL_close_code.h>
  395. #endif /* SDL_process_h_ */