Skip to content

Instantly share code, notes, and snippets.

@kainjow
Created November 24, 2018 20:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kainjow/0e7650cc797a52261e0f4ba851477c2f to your computer and use it in GitHub Desktop.
Save kainjow/0e7650cc797a52261e0f4ba851477c2f to your computer and use it in GitHub Desktop.
Swift example of C libproc API
import Darwin
// Call proc_listallpids once with nil/0 args to get the current number of pids
let initialNumPids = proc_listallpids(nil, 0)
// Allocate a buffer of these number of pids.
// Make sure to deallocate it as this class does not manage memory for us.
let buffer = UnsafeMutablePointer<pid_t>.allocate(capacity: Int(initialNumPids))
defer {
buffer.deallocate()
}
// Calculate the buffer's total length in bytes
let bufferLength = initialNumPids * Int32(MemoryLayout<pid_t>.size)
// Call the function again with our inputs now ready
let numPids = proc_listallpids(buffer, bufferLength)
// Loop through each pid
for i in 0..<numPids {
// Print the current pid
let pid = buffer[Int(i)]
print("[\(i)] \(pid)")
// Allocate a buffer to store the name
let nameBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(MAXPATHLEN))
defer {
nameBuffer.deallocate()
}
// Now get and print the name. Not all processes return a name here...
let nameLength = proc_name(pid, nameBuffer, UInt32(MAXPATHLEN))
if nameLength > 0 {
let name = String(cString: nameBuffer)
print(" name=\(name)")
}
// ...so also get the process' path
let pathBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(MAXPATHLEN))
defer {
pathBuffer.deallocate()
}
let pathLength = proc_pidpath(pid, pathBuffer, UInt32(MAXPATHLEN))
if pathLength > 0 {
let path = String(cString: pathBuffer)
print(" path=\(path)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment