process_posix_darwin.odin 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //+private
  2. package os2
  3. import "base:runtime"
  4. import "base:intrinsics"
  5. import "core:bytes"
  6. import "core:sys/darwin"
  7. import "core:sys/posix"
  8. import "core:sys/unix"
  9. import "core:time"
  10. foreign import lib "system:System.framework"
  11. foreign lib {
  12. sysctl :: proc(
  13. name: [^]i32, namelen: u32,
  14. oldp: rawptr, oldlenp: ^uint,
  15. newp: rawptr, newlen: uint,
  16. ) -> posix.result ---
  17. }
  18. _process_info_by_pid :: proc(pid: int, selection: Process_Info_Fields, allocator: runtime.Allocator) -> (info: Process_Info, err: Error) {
  19. get_pidinfo :: proc(pid: int, selection: Process_Info_Fields) -> (ppid: u32, prio: Maybe(i32), uid: posix.uid_t, ok: bool) {
  20. // Short info is enough and requires less permissions if the priority isn't requested.
  21. if .Priority in selection {
  22. info: darwin.proc_taskallinfo
  23. ret := darwin.proc_pidinfo(posix.pid_t(pid), .TASKALLINFO, 0, &info, size_of(info))
  24. if ret > 0 {
  25. assert(ret == size_of(info))
  26. ppid = info.pbsd.pbi_ppid
  27. prio = info.ptinfo.pti_priority
  28. uid = info.pbsd.pbi_uid
  29. ok = true
  30. return
  31. }
  32. }
  33. // Try short info, requires less permissions, but doesn't give a `nice`.
  34. psinfo: darwin.proc_bsdshortinfo
  35. ret := darwin.proc_pidinfo(posix.pid_t(pid), .SHORTBSDINFO, 0, &psinfo, size_of(psinfo))
  36. if ret > 0 {
  37. assert(ret == size_of(psinfo))
  38. ppid = psinfo.pbsi_ppid
  39. uid = psinfo.pbsi_uid
  40. ok = true
  41. }
  42. return
  43. }
  44. info.pid = pid
  45. // Thought on errors is: allocation failures return immediately (also why the non-allocation stuff is done first),
  46. // other errors usually mean other parts of the info could be retrieved though, so in those cases we keep trying to get the other information.
  47. pidinfo: {
  48. if selection & {.PPid, .Priority, .Username } != {} {
  49. ppid, mprio, uid, ok := get_pidinfo(pid, selection)
  50. if !ok {
  51. if err == nil {
  52. err = _get_platform_error()
  53. }
  54. break pidinfo
  55. }
  56. if .PPid in selection {
  57. info.ppid = int(ppid)
  58. info.fields += {.PPid}
  59. }
  60. if prio, has_prio := mprio.?; has_prio && .Priority in selection {
  61. info.priority = int(prio)
  62. info.fields += {.Priority}
  63. }
  64. if .Username in selection {
  65. pw := posix.getpwuid(uid)
  66. if pw == nil {
  67. if err == nil {
  68. err = _get_platform_error()
  69. }
  70. break pidinfo
  71. }
  72. info.username = clone_string(string(pw.pw_name), allocator) or_return
  73. info.fields += {.Username}
  74. }
  75. }
  76. }
  77. if .Working_Dir in selection {
  78. pinfo: darwin.proc_vnodepathinfo
  79. ret := darwin.proc_pidinfo(posix.pid_t(pid), .VNODEPATHINFO, 0, &pinfo, size_of(pinfo))
  80. if ret > 0 {
  81. assert(ret == size_of(pinfo))
  82. info.working_dir = clone_string(string(cstring(raw_data(pinfo.pvi_cdir.vip_path[:]))), allocator) or_return
  83. info.fields += {.Working_Dir}
  84. } else if err == nil {
  85. err = _get_platform_error()
  86. }
  87. }
  88. if .Executable_Path in selection {
  89. buffer: [darwin.PIDPATHINFO_MAXSIZE]byte = ---
  90. ret := darwin.proc_pidpath(posix.pid_t(pid), raw_data(buffer[:]), len(buffer))
  91. if ret > 0 {
  92. info.executable_path = clone_string(string(buffer[:ret]), allocator) or_return
  93. info.fields += {.Executable_Path}
  94. } else if err == nil {
  95. err = _get_platform_error()
  96. }
  97. }
  98. args: if selection & { .Command_Line, .Command_Args, .Environment } != {} {
  99. mib := []i32{
  100. unix.CTL_KERN,
  101. unix.KERN_PROCARGS2,
  102. i32(pid),
  103. }
  104. length: uint
  105. if sysctl(raw_data(mib), 3, nil, &length, nil, 0) != .OK {
  106. if err == nil {
  107. err = _get_platform_error()
  108. }
  109. break args
  110. }
  111. buf := runtime.make_aligned([]byte, length, 4, temp_allocator())
  112. if sysctl(raw_data(mib), 3, raw_data(buf), &length, nil, 0) != .OK {
  113. if err == nil {
  114. err = _get_platform_error()
  115. // Looks like EINVAL is returned here if you don't have permission.
  116. if err == Platform_Error(posix.Errno.EINVAL) {
  117. err = .Permission_Denied
  118. }
  119. }
  120. break args
  121. }
  122. buf = buf[:length]
  123. if len(buf) < 4 {
  124. break args
  125. }
  126. // Layout isn't really documented anywhere, I deduced it to be:
  127. // i32 - argc
  128. // cstring - command name (skipped)
  129. // [^]byte - couple of 0 bytes (skipped)
  130. // [^]cstring - argv (up to argc entries)
  131. // [^]cstring - key=value env entries until the end (many intermittent 0 bytes and entries without `=` we skip here too)
  132. argc := (^i32)(raw_data(buf))^
  133. buf = buf[size_of(i32):]
  134. {
  135. command_line: [dynamic]byte
  136. command_line.allocator = allocator
  137. argv: [dynamic]string
  138. argv.allocator = allocator
  139. defer if err != nil {
  140. for arg in argv { delete(arg, allocator) }
  141. delete(argv)
  142. delete(command_line)
  143. }
  144. _, _ = bytes.split_iterator(&buf, {0})
  145. buf = bytes.trim_left(buf, {0})
  146. first_arg := true
  147. for arg in bytes.split_iterator(&buf, {0}) {
  148. if .Command_Line in selection {
  149. if !first_arg {
  150. append(&command_line, ' ') or_return
  151. }
  152. append(&command_line, ..arg) or_return
  153. }
  154. if .Command_Args in selection {
  155. sarg := clone_string(string(arg), allocator) or_return
  156. append(&argv, sarg) or_return
  157. }
  158. first_arg = false
  159. argc -= 1
  160. if argc == 0 {
  161. break
  162. }
  163. }
  164. if .Command_Line in selection {
  165. info.command_line = string(command_line[:])
  166. info.fields += {.Command_Line}
  167. }
  168. if .Command_Args in selection {
  169. info.command_args = argv[:]
  170. info.fields += {.Command_Args}
  171. }
  172. }
  173. if .Environment in selection {
  174. environment: [dynamic]string
  175. environment.allocator = allocator
  176. defer if err != nil {
  177. for entry in environment { delete(entry, allocator) }
  178. delete(environment)
  179. }
  180. for entry in bytes.split_iterator(&buf, {0}) {
  181. if bytes.index_byte(entry, '=') > -1 {
  182. sentry := clone_string(string(entry), allocator) or_return
  183. append(&environment, sentry) or_return
  184. }
  185. }
  186. info.environment = environment[:]
  187. info.fields += {.Environment}
  188. }
  189. }
  190. // Fields were requested that we didn't add.
  191. if err == nil && selection - info.fields != {} {
  192. err = .Unsupported
  193. }
  194. return
  195. }
  196. _process_list :: proc(allocator: runtime.Allocator) -> (list: []int, err: Error) {
  197. ret := darwin.proc_listallpids(nil, 0)
  198. if ret < 0 {
  199. err = _get_platform_error()
  200. return
  201. }
  202. TEMP_ALLOCATOR_GUARD()
  203. buffer := make([]i32, ret, temp_allocator())
  204. ret = darwin.proc_listallpids(raw_data(buffer), ret*size_of(i32))
  205. if ret < 0 {
  206. err = _get_platform_error()
  207. return
  208. }
  209. list = make([]int, ret, allocator) or_return
  210. #no_bounds_check for &entry, i in list {
  211. entry = int(buffer[i])
  212. }
  213. return
  214. }
  215. _process_open :: proc(pid: int, flags: Process_Open_Flags) -> (process: Process, err: Error) {
  216. rusage: darwin.rusage_info_v0
  217. if ret := darwin.proc_pid_rusage(posix.pid_t(pid), .V0, &rusage); ret != 0 {
  218. err = _get_platform_error()
  219. return
  220. }
  221. // Using the start time as the handle, there is no pidfd or anything on Darwin.
  222. // There is a uuid, but once a process becomes a zombie it changes...
  223. process.handle = uintptr(rusage.ri_proc_start_abstime)
  224. process.pid = int(pid)
  225. return
  226. }
  227. _process_handle_still_valid :: proc(p: Process) -> Error {
  228. rusage: darwin.rusage_info_v0
  229. if ret := darwin.proc_pid_rusage(posix.pid_t(p.pid), .V0, &rusage); ret != 0 {
  230. return _get_platform_error()
  231. }
  232. handle := uintptr(rusage.ri_proc_start_abstime)
  233. if p.handle != handle {
  234. return posix.Errno.ESRCH
  235. }
  236. return nil
  237. }
  238. _process_state_update_times :: proc(p: Process, state: ^Process_State) {
  239. rusage: darwin.rusage_info_v0
  240. if ret := darwin.proc_pid_rusage(posix.pid_t(p.pid), .V0, &rusage); ret != 0 {
  241. return
  242. }
  243. // NOTE(laytan): I have no clue if this is correct, the output seems correct comparing it with `time`'s output.
  244. HZ :: 20000000
  245. state.user_time = (
  246. (time.Duration(rusage.ri_user_time) / HZ * time.Second) +
  247. time.Duration(rusage.ri_user_time % HZ))
  248. state.system_time = (
  249. (time.Duration(rusage.ri_system_time) / HZ * time.Second) +
  250. time.Duration(rusage.ri_system_time % HZ))
  251. return
  252. }