process.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package os2
  2. import "base:runtime"
  3. import "core:time"
  4. /*
  5. In procedures that explicitly state this as one of the allowed values,
  6. specifies an infinite timeout.
  7. */
  8. TIMEOUT_INFINITE :: time.MIN_DURATION // Note(flysand): Any negative duration will be treated as infinity
  9. /*
  10. Arguments to the current process.
  11. */
  12. args := get_args()
  13. @(private="file", require_results)
  14. get_args :: proc() -> []string {
  15. result := make([]string, len(runtime.args__), heap_allocator())
  16. for rt_arg, i in runtime.args__ {
  17. result[i] = string(rt_arg)
  18. }
  19. return result
  20. }
  21. /*
  22. Exit the current process.
  23. */
  24. exit :: proc "contextless" (code: int) -> ! {
  25. _exit(code)
  26. }
  27. /*
  28. Obtain the UID of the current process.
  29. **Note(windows)**: Windows doesn't follow the posix permissions model, so
  30. the function simply returns -1.
  31. */
  32. @(require_results)
  33. get_uid :: proc() -> int {
  34. return _get_uid()
  35. }
  36. /*
  37. Obtain the effective UID of the current process.
  38. The effective UID is typically the same as the UID of the process. In case
  39. the process was run by a user with elevated permissions, the process may
  40. lower the privilege to perform some tasks without privilege. In these cases
  41. the real UID of the process and the effective UID are different.
  42. **Note(windows)**: Windows doesn't follow the posix permissions model, so
  43. the function simply returns -1.
  44. */
  45. @(require_results)
  46. get_euid :: proc() -> int {
  47. return _get_euid()
  48. }
  49. /*
  50. Obtain the GID of the current process.
  51. **Note(windows)**: Windows doesn't follow the posix permissions model, so
  52. the function simply returns -1.
  53. */
  54. @(require_results)
  55. get_gid :: proc() -> int {
  56. return _get_gid()
  57. }
  58. /*
  59. Obtain the effective GID of the current process.
  60. The effective GID is typically the same as the GID of the process. In case
  61. the process was run by a user with elevated permissions, the process may
  62. lower the privilege to perform some tasks without privilege. In these cases
  63. the real GID of the process and the effective GID are different.
  64. **Note(windows)**: Windows doesn't follow the posix permissions model, so
  65. the function simply returns -1.
  66. */
  67. @(require_results)
  68. get_egid :: proc() -> int {
  69. return _get_egid()
  70. }
  71. /*
  72. Obtain the ID of the current process.
  73. */
  74. @(require_results)
  75. get_pid :: proc() -> int {
  76. return _get_pid()
  77. }
  78. /*
  79. Obtain the ID of the parent process.
  80. **Note(windows)**: Windows does not mantain strong relationships between
  81. parent and child processes. This function returns the ID of the process
  82. that has created the current process. In case the parent has died, the ID
  83. returned by this function can identify a non-existent or a different
  84. process.
  85. */
  86. @(require_results)
  87. get_ppid :: proc() -> int {
  88. return _get_ppid()
  89. }
  90. /*
  91. Obtain ID's of all processes running in the system.
  92. */
  93. @(require_results)
  94. process_list :: proc(allocator: runtime.Allocator) -> ([]int, Error) {
  95. return _process_list(allocator)
  96. }
  97. /*
  98. Bit set specifying which fields of the `Process_Info` struct need to be
  99. obtained by the `process_info()` procedure. Each bit corresponds to a
  100. field in the `Process_Info` struct.
  101. */
  102. Process_Info_Fields :: bit_set[Process_Info_Field]
  103. Process_Info_Field :: enum {
  104. Executable_Path,
  105. PPid,
  106. Priority,
  107. Command_Line,
  108. Command_Args,
  109. Environment,
  110. Username,
  111. Working_Dir,
  112. }
  113. /*
  114. Contains information about the process as obtained by the `process_info()`
  115. procedure.
  116. */
  117. Process_Info :: struct {
  118. // The information about a process the struct contains. `pid` is always
  119. // stored, no matter what.
  120. fields: Process_Info_Fields,
  121. // The ID of the process.
  122. pid: int,
  123. // The ID of the parent process.
  124. ppid: int,
  125. // The process priority.
  126. priority: int,
  127. // The path to the executable, which the process runs.
  128. executable_path: string,
  129. // The command line supplied to the process.
  130. command_line: string,
  131. // The arguments supplied to the process.
  132. command_args: []string,
  133. // The environment of the process.
  134. environment: []string,
  135. // The username of the user who started the process.
  136. username: string,
  137. // The current working directory of the process.
  138. working_dir: string,
  139. }
  140. /*
  141. Obtain information about a process.
  142. This procedure obtains an information, specified by `selection` parameter of
  143. a process given by `pid`.
  144. Use `free_process_info` to free the memory allocated by this procedure. In
  145. case the function returns an error all temporary allocations would be freed
  146. and as such, calling `free_process_info()` is not needed.
  147. **Note**: The resulting information may or may contain the fields specified
  148. by the `selection` parameter. Always check whether the returned
  149. `Process_Info` struct has the required fields before checking the error code
  150. returned by this function.
  151. */
  152. @(require_results)
  153. process_info_by_pid :: proc(pid: int, selection: Process_Info_Fields, allocator: runtime.Allocator) -> (Process_Info, Error) {
  154. return _process_info_by_pid(pid, selection, allocator)
  155. }
  156. /*
  157. Obtain information about a process.
  158. This procedure obtains information, specified by `selection` parameter
  159. about a process that has been opened by the application, specified in
  160. the `process` parameter.
  161. Use `free_process_info` to free the memory allocated by this procedure. In
  162. case the function returns an error, all temporary allocations would be freed
  163. and as such, calling `free_process_info` is not needed.
  164. **Note**: The resulting information may or may contain the fields specified
  165. by the `selection` parameter. Always check whether the returned
  166. `Process_Info` struct has the required fields before checking the error code
  167. returned by this function.
  168. */
  169. @(require_results)
  170. process_info_by_handle :: proc(process: Process, selection: Process_Info_Fields, allocator: runtime.Allocator) -> (Process_Info, Error) {
  171. return _process_info_by_handle(process, selection, allocator)
  172. }
  173. /*
  174. Obtain information about the current process.
  175. This procedure obtains the information, specified by `selection` parameter
  176. about the currently running process.
  177. Use `free_process_info` to free the memory allocated by this function. In
  178. case this function returns an error, all temporary allocations would be
  179. freed and as such calling `free_process_info()` is not needed.
  180. **Note**: The resulting information may or may contain the fields specified
  181. by the `selection` parameter. Always check whether the returned
  182. `Process_Info` struct has the required fields before checking the error code
  183. returned by this function.
  184. */
  185. @(require_results)
  186. current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime.Allocator) -> (Process_Info, Error) {
  187. return _current_process_info(selection, allocator)
  188. }
  189. /*
  190. Obtain information about the specified process.
  191. */
  192. process_info :: proc {
  193. process_info_by_pid,
  194. process_info_by_handle,
  195. current_process_info,
  196. }
  197. /*
  198. Free the information about the process.
  199. This procedure frees the memory occupied by process info using the provided
  200. allocator. The allocator needs to be the same allocator that was supplied
  201. to the `process_info` function.
  202. */
  203. free_process_info :: proc(pi: Process_Info, allocator: runtime.Allocator) {
  204. delete(pi.executable_path, allocator)
  205. delete(pi.command_line, allocator)
  206. delete(pi.command_args, allocator)
  207. for s in pi.environment {
  208. delete(s, allocator)
  209. }
  210. delete(pi.environment, allocator)
  211. delete(pi.working_dir, allocator)
  212. }
  213. /*
  214. Represents a process handle.
  215. When a process dies, the OS is free to re-use the pid of that process. The
  216. `Process` struct represents a handle to the process that will refer to a
  217. specific process, even after it has died.
  218. **Note(linux)**: The `handle` will be referring to pidfd.
  219. */
  220. Process :: struct {
  221. pid: int,
  222. handle: uintptr,
  223. }
  224. Process_Open_Flags :: bit_set[Process_Open_Flag]
  225. Process_Open_Flag :: enum {
  226. // Request for reading from the virtual memory of another process.
  227. Mem_Read,
  228. // Request for writing to the virtual memory of another process.
  229. Mem_Write,
  230. }
  231. /*
  232. Open a process handle using it's pid.
  233. This procedure obtains a process handle of a process specified by `pid`.
  234. This procedure can be subject to race conditions. See the description of
  235. `Process`.
  236. Use `process_close()` function to close the process handle.
  237. */
  238. @(require_results)
  239. process_open :: proc(pid: int, flags := Process_Open_Flags {}) -> (Process, Error) {
  240. return _process_open(pid, flags)
  241. }
  242. /*
  243. The description of how a process should be created.
  244. */
  245. Process_Desc :: struct {
  246. // OS-specific attributes.
  247. sys_attr: _Sys_Process_Attributes,
  248. // The working directory of the process. If the string has length 0, the
  249. // working directory is assumed to be the current working directory of the
  250. // current process.
  251. working_dir: string,
  252. // The command to run. Each element of the slice is a separate argument to
  253. // the process. The first element of the slice would be the executable.
  254. command: []string,
  255. // A slice of strings, each having the format `KEY=VALUE` representing the
  256. // full environment that the child process will receive.
  257. // In case this slice is `nil`, the current process' environment is used.
  258. env: []string,
  259. // The `stderr` handle to give to the child process. It can be either a file
  260. // or a writeable end of a pipe. Passing `nil` will shut down the process'
  261. // stderr output.
  262. stderr: ^File,
  263. // The `stdout` handle to give to the child process. It can be either a file
  264. // or a writeabe end of a pipe. Passing a `nil` will shut down the process'
  265. // stdout output.
  266. stdout: ^File,
  267. // The `stdin` handle to give to the child process. It can either be a file
  268. // or a readable end of a pipe. Passing a `nil` will shut down the process'
  269. // input.
  270. stdin: ^File,
  271. }
  272. /*
  273. Create a new process and obtain its handle.
  274. This procedure creates a new process, with a given command and environment
  275. strings as parameters. Use `environ()` to inherit the environment of the
  276. current process.
  277. The `desc` parameter specifies the description of how the process should
  278. be created. It contains information such as the command line, the
  279. environment of the process, the starting directory and many other options.
  280. Most of the fields in the struct can be set to `nil` or an empty value.
  281. Use `process_close` to close the handle to the process. Note, that this
  282. is not the same as terminating the process. One can terminate the process
  283. and not close the handle, in which case the handle would be leaked. In case
  284. the function returns an error, an invalid handle is returned.
  285. This procedure is not thread-safe. It may alter the inheritance properties
  286. of file handles in an unpredictable manner. In case multiple threads change
  287. handle inheritance properties, make sure to serialize all those calls.
  288. */
  289. @(require_results)
  290. process_start :: proc(desc := Process_Desc {}) -> (Process, Error) {
  291. return _process_start(desc)
  292. }
  293. /*
  294. The state of the process after it has finished execution.
  295. */
  296. Process_State :: struct {
  297. // The ID of the process.
  298. pid: int,
  299. // Specifies whether the process has terminated or is still running.
  300. exited: bool,
  301. // The exit code of the process, if it has exited.
  302. // Will also store the number of the exception or signal that has crashed the
  303. // process.
  304. exit_code: int,
  305. // Specifies whether the termination of the process was successfull or not,
  306. // i.e. whether it has crashed or not.
  307. // **Note(windows)**: On windows `true` is always returned, as there is no
  308. // reliable way to obtain information about whether the process has crashed.
  309. success: bool,
  310. // The time the process has spend executing in kernel time.
  311. system_time: time.Duration,
  312. // The time the process has spend executing in userspace.
  313. user_time: time.Duration,
  314. }
  315. /*
  316. Wait for a process event.
  317. This procedure blocks the execution until the process has exited or the
  318. timeout (if specified) has reached zero. If the timeout is `TIMEOUT_INFINITE`,
  319. no timeout restriction is imposed and the procedure can block indefinately.
  320. If the timeout has expired, the `General_Error.Timeout` is returned as
  321. the error.
  322. If an error is returned for any other reason, other than timeout, the
  323. process state is considered undetermined.
  324. */
  325. @(require_results)
  326. process_wait :: proc(process: Process, timeout := TIMEOUT_INFINITE) -> (Process_State, Error) {
  327. return _process_wait(process, timeout)
  328. }
  329. /*
  330. Close the handle to a process.
  331. This procedure closes the handle associated with a process. It **does not**
  332. terminate a process, in case it was running. In case a termination is
  333. desired, kill the process first, wait for the process to finish,
  334. then close the handle.
  335. */
  336. @(require_results)
  337. process_close :: proc(process: Process) -> (Error) {
  338. return _process_close(process)
  339. }
  340. /*
  341. Terminate a process.
  342. This procedure terminates a process, specified by it's handle, `process`.
  343. */
  344. @(require_results)
  345. process_kill :: proc(process: Process) -> (Error) {
  346. return _process_kill(process)
  347. }