wait.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. wait.h
  8. Abstract:
  9. This header contains definitions for obtaining information about a child
  10. process that has stopped or terminated.
  11. Author:
  12. Evan Green 30-Mar-2013
  13. --*/
  14. #ifndef _WAIT_H
  15. #define _WAIT_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <libcbase.h>
  20. #include <signal.h>
  21. #include <sys/resource.h>
  22. //
  23. // --------------------------------------------------------------------- Macros
  24. //
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. //
  29. // Use this to get the exit status out of a child process.
  30. //
  31. #define WEXITSTATUS(_Status) (((_Status) & 0x0000FF00) >> 8)
  32. //
  33. // This macro evaluates to nonzero if the status indicates that the child has
  34. // been continued.
  35. //
  36. #define WIFCONTINUED(_Status) (((_Status) & 0xFFFF) == 0xFFFF)
  37. //
  38. // This macro evaluates to nonzero if the status indicates that the child has
  39. // exited.
  40. //
  41. #define WIFEXITED(_Status) (((_Status) & 0x7F) == 0)
  42. //
  43. // This macro evaluates to nonzero if the status indicates that the child has
  44. // exited due to an uncaught signal. This constant matches up to flags in
  45. // ksignals.h. The macro is looking to make sure that the lower 7 bits are not
  46. // all 0 (exited) and are not all 1 (continued or stopped).
  47. //
  48. #define WIFSIGNALED(_Status) (((((_Status) + 1) >> 1) & 0x7F) != 0)
  49. //
  50. // This macro evaluates to nonzero if the status indicates that the child has
  51. // stopped.
  52. //
  53. #define WIFSTOPPED(_Status) (((_Status) & 0xFF) == 0x7F)
  54. //
  55. // This macro returns the stop signal if the child was stopped.
  56. //
  57. #define WSTOPSIG(_Status) WEXITSTATUS(_Status)
  58. //
  59. // This macro returns the signal that caused the process to terminate, if it was
  60. // terminated.
  61. //
  62. #define WTERMSIG(_Status) ((_Status) & 0x7F)
  63. //
  64. // This macro evaluates to nonzero if the child process terminated and produced
  65. // a core dump file.
  66. //
  67. #define WCOREDUMP(_Status) ((_Status) & 0x80)
  68. //
  69. // ---------------------------------------------------------------- Definitions
  70. //
  71. //
  72. // Set this in the wait options to return immediately if no child process
  73. // information is available instead of the usual behavior of blocking until it
  74. // is.
  75. //
  76. #define WNOHANG 0x00000001
  77. //
  78. // Set this option to wait for a process that has just stopped.
  79. //
  80. #define WUNTRACED 0x00000002
  81. //
  82. // Set this option to wait for a process that has just continued.
  83. //
  84. #define WCONTINUED 0x00000004
  85. //
  86. // Set this option to wait for a process that has just exited.
  87. //
  88. #define WEXITED 0x00000008
  89. //
  90. // Set this option to keep the process whose status is returned in a waitable
  91. // state.
  92. //
  93. #define WNOWAIT 0x00000010
  94. //
  95. // ------------------------------------------------------ Data Type Definitions
  96. //
  97. //
  98. // Define the ID type, which is used to identify whether an ID is a process ID,
  99. // process group ID, or neither.
  100. //
  101. typedef enum {
  102. P_ALL = 0,
  103. P_PID = 1,
  104. P_PGID = 2
  105. } idtype_t;
  106. //
  107. // -------------------------------------------------------------------- Globals
  108. //
  109. //
  110. // -------------------------------------------------------- Function Prototypes
  111. //
  112. LIBC_API
  113. pid_t
  114. wait (
  115. int *Status
  116. );
  117. /*++
  118. Routine Description:
  119. This routine obtains status information about one of the caller's
  120. terminated child processes. This routine blocks until such status
  121. information becomes available or until the calling process receives a
  122. terminating signal.
  123. Arguments:
  124. Status - Supplies an optional pointer where the child process' exit status
  125. information will be returned.
  126. Return Value:
  127. Returns the process ID of the child process that just experienced a state
  128. change.
  129. -1 on failure, and the errno variable will contain more information.
  130. --*/
  131. LIBC_API
  132. pid_t
  133. waitpid (
  134. pid_t ProcessId,
  135. int *Status,
  136. int Options
  137. );
  138. /*++
  139. Routine Description:
  140. This routine obtains status information about one of the caller's child
  141. processes. This routine can block waiting for any child process to change,
  142. or can wait for a specific process.
  143. Arguments:
  144. ProcessId - Supplies the process ID of the process to wait for. The
  145. various valid values are as follows:
  146. If equal to -1, then this routine will be equivalent to the original
  147. routine, it will return when any process has status information.
  148. If greater than 0, then the specific process ID will be waited for.
  149. If 0, then any child whose process process group ID is equal to that of
  150. the calling process will satisfy the wait.
  151. If less than negative one, then any child process whose process group ID
  152. is equal to the absolute value of this parameter will satisfy the
  153. wait.
  154. Status - Supplies an optional pointer where the child process' exit status
  155. information will be returned.
  156. Options - Supplies a bitfield of options. This field may contain one or
  157. more of the following options:
  158. WCONTINUED - Wait for a process that just continued.
  159. WNOHANG - Return immediately if no child process information is
  160. currently available.
  161. WUNTRACED - Wait for a process that just stopped.
  162. Return Value:
  163. Returns the process ID of the child process that just experienced a state
  164. change.
  165. -1 on failure, and the errno variable will contain more information.
  166. --*/
  167. LIBC_API
  168. int
  169. waitid (
  170. idtype_t IdentifierType,
  171. id_t ProcessOrGroupIdentifier,
  172. siginfo_t *SignalInformation,
  173. int Options
  174. );
  175. /*++
  176. Routine Description:
  177. This routine suspends execution until a child process of this process
  178. changes state.
  179. Arguments:
  180. IdentifierType - Supplies a value indicating whether the process or group
  181. identifier identifies a process, group, or nothing. If nothing, then
  182. any child process changing state will satisfy the wait.
  183. ProcessOrGroupIdentifier - Supplies a process or process group identifier
  184. to wait for. If the identifier type indicates neither, then this
  185. parameter is ignored.
  186. SignalInformation - Supplies a pointer where the child signal information
  187. will be returned.
  188. Options - Supplies a bitfield of options. Valid values are WEXITED,
  189. WSTOPPED, WCONTINUED, WNOHANG, and WNOWAIT. One or more of WEXITED,
  190. WSTOPPED or WCONTINUED must be supplied.
  191. Return Value:
  192. 0 if WNOHANG was specified and no child was waiting to report status
  193. information.
  194. 0 on success (child information was returned).
  195. -1 on failure, and the errno variable will be set to indicate the error.
  196. --*/
  197. LIBC_API
  198. pid_t
  199. wait3 (
  200. int *Status,
  201. int Options,
  202. struct rusage *ResourceUsage
  203. );
  204. /*++
  205. Routine Description:
  206. This routine is equivalent to the wait function, except it can obtain
  207. resource usage about the reaped child. This function is provided for
  208. compatibility with existing applications. New applications should use the
  209. waitpid function.
  210. Arguments:
  211. Status - Supplies an optional pointer where the child process' exit status
  212. information will be returned.
  213. Options - Supplies a bitfield of options. See the waitpid function for
  214. more details.
  215. ResourceUsage - Supplies an optional pointer where the resource usage of
  216. the process will be returned on success.
  217. Return Value:
  218. Returns the process ID of the child process that just experienced a state
  219. change.
  220. -1 on failure, and the errno variable will contain more information.
  221. --*/
  222. LIBC_API
  223. pid_t
  224. wait4 (
  225. pid_t ProcessId,
  226. int *Status,
  227. int Options,
  228. struct rusage *ResourceUsage
  229. );
  230. /*++
  231. Routine Description:
  232. This routine is equivalent to the waitpid function, except it can obtain
  233. resource usage about the reaped child. This function is provided for
  234. compatibility with existing applications. New applications should use the
  235. waitpid function.
  236. Arguments:
  237. ProcessId - Supplies the process ID to wait for. See waitpid for more
  238. information.
  239. Status - Supplies an optional pointer where the child process' exit status
  240. information will be returned.
  241. Options - Supplies a bitfield of options. See the waitpid function for
  242. more details.
  243. ResourceUsage - Supplies an optional pointer where the resource usage of
  244. the process will be returned on success.
  245. Return Value:
  246. Returns the process ID of the child process that just experienced a state
  247. change.
  248. -1 on failure, and the errno variable will contain more information.
  249. --*/
  250. #ifdef __cplusplus
  251. }
  252. #endif
  253. #endif