Skip to content

Commit

Permalink
Add additional API surfaces for searching procs
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRabil committed Mar 17, 2022
1 parent 35523f9 commit da9254d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Sources/Swexy/Utilities/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@

import Foundation

private func _NSLookupProcessesAtPath(_ path: Int32...) -> [kinfo_proc] {
let pathCount = u_int(path.count)
var length: Int = 0

return path.withUnsafeBufferPointer { pathBuffer in
switch sysctl(UnsafeMutablePointer(mutating: pathBuffer.baseAddress!), pathCount, nil, &length, nil, 0) {
case 0:
let count = length / MemoryLayout<kinfo_proc>.stride
var result = [kinfo_proc](repeating: kinfo_proc(), count: count)
switch sysctl(UnsafeMutablePointer(mutating: pathBuffer.baseAddress!), pathCount, &result, &length, nil, 0) {
case 0:
return result
case let error:
break
}
case let error:
break
}

return []
}
}

public func NSLookupProcessesForUser(uid: uid_t) -> [kinfo_proc] {
_NSLookupProcessesAtPath(CTL_KERN, KERN_PROC, KERN_PROC_UID, Int32(uid))
}

public func NSLookupProcessesForUser(ruid: uid_t) -> [kinfo_proc] {
_NSLookupProcessesAtPath(CTL_KERN, KERN_PROC, KERN_PROC_RUID, Int32(ruid))
}

public func NSLookupProcessesForProcessGroup(pid: pid_t) -> [kinfo_proc] {
_NSLookupProcessesAtPath(CTL_KERN, KERN_PROC, KERN_PROC_PGRP, Int32(pid))
}

public func NSLookupAllProcesses() -> [kinfo_proc] {
_NSLookupProcessesAtPath(CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0)
}

public func NSLookupProcessInfo(_ processIdentifier: pid_t) -> kinfo_proc? {
var proc: kinfo_proc = kinfo_proc()
var procSize = MemoryLayout<kinfo_proc>.size
Expand Down

0 comments on commit da9254d

Please sign in to comment.