SDL_process.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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. #include "SDL_internal.h"
  19. #include "SDL_sysprocess.h"
  20. SDL_Process *SDL_CreateProcess(const char * const *args, bool pipe_stdio)
  21. {
  22. if (!args || !args[0] || !args[0][0]) {
  23. SDL_InvalidParamError("args");
  24. return NULL;
  25. }
  26. SDL_Process *process;
  27. SDL_PropertiesID props = SDL_CreateProperties();
  28. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)args);
  29. if (pipe_stdio) {
  30. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  31. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  32. }
  33. process = SDL_CreateProcessWithProperties(props);
  34. SDL_DestroyProperties(props);
  35. return process;
  36. }
  37. SDL_Process *SDL_CreateProcessWithProperties(SDL_PropertiesID props)
  38. {
  39. const char * const *args = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, NULL);
  40. #if defined(SDL_PLATFORM_WINDOWS)
  41. const char *cmdline = SDL_GetStringProperty(props, SDL_PROP_PROCESS_CREATE_CMDLINE_STRING, NULL);
  42. if ((!args || !args[0] || !args[0][0]) && (!cmdline || !cmdline[0])) {
  43. SDL_SetError("Either SDL_PROP_PROCESS_CREATE_ARGS_POINTER or SDL_PROP_PROCESS_CREATE_CMDLINE_STRING must be valid");
  44. return NULL;
  45. }
  46. #else
  47. if (!args || !args[0] || !args[0][0]) {
  48. SDL_InvalidParamError("SDL_PROP_PROCESS_CREATE_ARGS_POINTER");
  49. return NULL;
  50. }
  51. #endif
  52. SDL_Process *process = (SDL_Process *)SDL_calloc(1, sizeof(*process));
  53. if (!process) {
  54. return NULL;
  55. }
  56. process->background = SDL_GetBooleanProperty(props, SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN, false);
  57. process->props = SDL_CreateProperties();
  58. if (!process->props) {
  59. SDL_DestroyProcess(process);
  60. return NULL;
  61. }
  62. SDL_SetBooleanProperty(process->props, SDL_PROP_PROCESS_BACKGROUND_BOOLEAN, process->background);
  63. if (!SDL_SYS_CreateProcessWithProperties(process, props)) {
  64. SDL_DestroyProcess(process);
  65. return NULL;
  66. }
  67. process->alive = true;
  68. return process;
  69. }
  70. SDL_PropertiesID SDL_GetProcessProperties(SDL_Process *process)
  71. {
  72. if (!process) {
  73. return SDL_InvalidParamError("process");
  74. }
  75. return process->props;
  76. }
  77. void *SDL_ReadProcess(SDL_Process *process, size_t *datasize, int *exitcode)
  78. {
  79. void *result;
  80. if (datasize) {
  81. *datasize = 0;
  82. }
  83. if (exitcode) {
  84. *exitcode = -1;
  85. }
  86. if (!process) {
  87. SDL_InvalidParamError("process");
  88. return NULL;
  89. }
  90. SDL_IOStream *io = (SDL_IOStream *)SDL_GetPointerProperty(process->props, SDL_PROP_PROCESS_STDOUT_POINTER, NULL);
  91. if (!io) {
  92. SDL_SetError("Process not created with I/O enabled");
  93. return NULL;
  94. }
  95. result = SDL_LoadFile_IO(io, datasize, false);
  96. SDL_WaitProcess(process, true, exitcode);
  97. return result;
  98. }
  99. SDL_IOStream *SDL_GetProcessInput(SDL_Process *process)
  100. {
  101. if (!process) {
  102. SDL_InvalidParamError("process");
  103. return NULL;
  104. }
  105. SDL_IOStream *io = (SDL_IOStream *)SDL_GetPointerProperty(process->props, SDL_PROP_PROCESS_STDIN_POINTER, NULL);
  106. if (!io) {
  107. SDL_SetError("Process not created with standard input available");
  108. return NULL;
  109. }
  110. return io;
  111. }
  112. SDL_IOStream *SDL_GetProcessOutput(SDL_Process *process)
  113. {
  114. if (!process) {
  115. SDL_InvalidParamError("process");
  116. return NULL;
  117. }
  118. SDL_IOStream *io = (SDL_IOStream *)SDL_GetPointerProperty(process->props, SDL_PROP_PROCESS_STDOUT_POINTER, NULL);
  119. if (!io) {
  120. SDL_SetError("Process not created with standard output available");
  121. return NULL;
  122. }
  123. return io;
  124. }
  125. bool SDL_KillProcess(SDL_Process *process, bool force)
  126. {
  127. if (!process) {
  128. return SDL_InvalidParamError("process");
  129. }
  130. if (!process->alive) {
  131. return SDL_SetError("Process isn't running");
  132. }
  133. return SDL_SYS_KillProcess(process, force);
  134. }
  135. bool SDL_WaitProcess(SDL_Process *process, bool block, int *exitcode)
  136. {
  137. if (!process) {
  138. return SDL_InvalidParamError("process");
  139. }
  140. if (!process->alive) {
  141. if (exitcode) {
  142. *exitcode = process->exitcode;
  143. }
  144. return true;
  145. }
  146. if (SDL_SYS_WaitProcess(process, block, &process->exitcode)) {
  147. process->alive = false;
  148. if (exitcode) {
  149. if (process->background) {
  150. process->exitcode = 0;
  151. }
  152. *exitcode = process->exitcode;
  153. }
  154. return true;
  155. }
  156. return false;
  157. }
  158. void SDL_DestroyProcess(SDL_Process *process)
  159. {
  160. if (!process) {
  161. return;
  162. }
  163. // Check to see if the process has exited, will reap zombies on POSIX platforms
  164. if (process->alive) {
  165. SDL_WaitProcess(process, false, NULL);
  166. }
  167. SDL_SYS_DestroyProcess(process);
  168. SDL_DestroyProperties(process->props);
  169. SDL_free(process);
  170. }