spawnos.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*++
  2. Copyright (c) 2017 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. spawnos.h
  9. Abstract:
  10. This header contains definitions for the spawn module.
  11. Author:
  12. Evan Green 21-Jun-2017
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. #include <sys/types.h>
  18. //
  19. // --------------------------------------------------------------------- Macros
  20. //
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. #ifdef _WIN32
  25. #include "spnwin32.h"
  26. #define SPAWN_DEVNULL_PATH "nul"
  27. #else
  28. #define SPAWN_DEVNULL_PATH "/dev/null"
  29. #endif
  30. #define SPAWN_NONE -1LL
  31. #define SPAWN_DEVNULL -2LL
  32. #define SPAWN_PIPE -3LL
  33. #define SPAWN_OPTION_SHELL 0x00000001
  34. #define SPAWN_OPTION_CHECK 0x00000002
  35. #define SPAWN_OPTION_CLOSE_FDS 0x00000004
  36. #define SPAWN_OPTION_NEW_SESSION 0x00000008
  37. #define CK_SPAWN_MAX_OUTPUT (1024 * 1024 * 1024)
  38. #define SPAWN_DEBUG_BASIC_LAUNCH 0x00000001
  39. #define SPAWN_DEBUG_DETAILED_LAUNCH 0x00000002
  40. #define SPAWN_DEBUG_IO 0x00000004
  41. //
  42. // ------------------------------------------------------ Data Type Definitions
  43. //
  44. /*++
  45. Structure Description:
  46. This structure stores a standard descriptor for a child.
  47. Members:
  48. Fd - Stores the child descriptor number to use, or -1 for no change.
  49. ParentPipe - Stores the parent's side of the pipe if this descriptor is
  50. piped.
  51. CloseFd - Stores a file descriptor that needs to be closed if the child
  52. process isn't fully created.
  53. --*/
  54. typedef struct _SPAWN_DESCRIPTOR {
  55. int Fd;
  56. int ParentPipe;
  57. int CloseFd;
  58. } SPAWN_DESCRIPTOR, *PSPAWN_DESCRIPTOR;
  59. /*++
  60. Structure Description:
  61. This structure stores the attributes passed when creating a new process.
  62. Members:
  63. Stdin - Stores the file descriptor for stdin.
  64. Stdout - Stores the file descriptor for stdout.
  65. Stderr - Stores the file descriptor for stderr.
  66. Cwd - Stores an optional pointer to the working directory to switch to, or
  67. NULL for no change.
  68. Environment - Stores an optional pointer to an environment array, or NULL
  69. for no change.
  70. PassFds - Stores an optional pointer to a set of file descriptors not to
  71. close.
  72. PassFdCount - Stores the number of elements in the PassFds array.
  73. Arguments - Stores a pointer to the array of arguments to execute.
  74. Executable - Stores the executable to execute.
  75. Pid - Stores the returned pid of the new process.
  76. ProcessHandle - Stores the handle to the process (Windows only).
  77. Options - Stores the spawn options. See SPAWN_OPTION_* definitions.
  78. ErrorMessage - Stores the error message. This must be freed if populated.
  79. ReturnCode - Stores the return code populated when the process exits.
  80. Debug - Stores the debug mask, used to print more information to stderr.
  81. --*/
  82. typedef struct _SPAWN_ATTRIBUTES {
  83. SPAWN_DESCRIPTOR Stdin;
  84. SPAWN_DESCRIPTOR Stdout;
  85. SPAWN_DESCRIPTOR Stderr;
  86. const char *Cwd;
  87. char **Environment;
  88. int *PassFds;
  89. size_t PassFdCount;
  90. char **Arguments;
  91. const char *Executable;
  92. pid_t Pid;
  93. void *ProcessHandle;
  94. int Options;
  95. char *ErrorMessage;
  96. int ReturnCode;
  97. int Debug;
  98. } SPAWN_ATTRIBUTES, *PSPAWN_ATTRIBUTES;
  99. //
  100. // -------------------------------------------------------------------- Globals
  101. //
  102. //
  103. // -------------------------------------------------------- Function Prototypes
  104. //
  105. //
  106. // OS-specific functions.
  107. //
  108. INT
  109. CkpOsSpawn (
  110. PSPAWN_ATTRIBUTES Attributes
  111. );
  112. /*++
  113. Routine Description:
  114. This routine spawns a subprocess.
  115. Arguments:
  116. Attributes - Supplies the attributes of the process to launch.
  117. Return Value:
  118. 0 on success.
  119. Non-zero on failure.
  120. --*/
  121. INT
  122. CkpOsWait (
  123. PSPAWN_ATTRIBUTES Attributes,
  124. int Milliseconds
  125. );
  126. /*++
  127. Routine Description:
  128. This routine waits for the process to exit. It sets the return code if
  129. the process exited, and sets the return value.
  130. Arguments:
  131. Attributes - Supplies a pointer to the attributes.
  132. Milliseconds - Supplies the number of milliseconds to wait.
  133. Return Value:
  134. 0 on success.
  135. 1 on timeout.
  136. -1 on failure.
  137. --*/
  138. INT
  139. CkpOsCommunicate (
  140. PSPAWN_ATTRIBUTES Attributes,
  141. const char *Input,
  142. size_t InputSize,
  143. int Milliseconds,
  144. char **OutData,
  145. size_t *OutDataSize,
  146. char **ErrorData,
  147. size_t *ErrorDataSize
  148. );
  149. /*++
  150. Routine Description:
  151. This routine communicates with the subprocess, and waits for it to
  152. terminate.
  153. Arguments:
  154. Attributes - Supplies a pointer to the attributes.
  155. Input - Supplies an optional pointer to the input to send into the
  156. process.
  157. InputSize - Supplies the size of the input data in bytes.
  158. Milliseconds - Supplies the number of milliseconds to wait.
  159. OutData - Supplies a pointer where the data from stdout will be returned.
  160. The caller is responsible for freeing this buffer.
  161. OutDataSize - Supplies the number of bytes in the output data buffer.
  162. ErrorData - Supplies a pointer where the data from stderr will be returned.
  163. The caller is responsible for freeing this buffer.
  164. ErrorDataSize - Supplies a pointer where the size of the stderr data will
  165. be returned.
  166. Return Value:
  167. 0 on success.
  168. 1 on timeout.
  169. -1 on failure.
  170. --*/
  171. INT
  172. CkpOsSendSignal (
  173. PSPAWN_ATTRIBUTES Attributes,
  174. INT Signal
  175. );
  176. /*++
  177. Routine Description:
  178. This routine sends a signal to the process. On Windows, it calls
  179. TerminateProcess for SIGTERM and SIGKILL.
  180. Arguments:
  181. Attributes - Supplies a pointer to the attributes.
  182. Signal - Supplies the signal to send to the process.
  183. Return Value:
  184. 0 on success.
  185. -1 on failure.
  186. --*/
  187. VOID
  188. CkpOsTearDownSpawnAttributes (
  189. PSPAWN_ATTRIBUTES Attributes
  190. );
  191. /*++
  192. Routine Description:
  193. This routine closes all OS-specific resources associated with a spawn
  194. attributes structure.
  195. Arguments:
  196. Attributes - Supplies a pointer to the attributes to tear down.
  197. Return Value:
  198. None.
  199. --*/