2

I have an XPC service, running as root, that is required to monitor if other specific processes are running.

Retrieving the list of running processes from NSWorkspace only seems to produce the list of user processes and no root-owned daemons

let workspace = NSWorkspace.shared
let applications = workspace.runningApplications        

for application in applications {

    if let url = (application.executableURL?.absoluteString) {

            os_log("%{public}s", log:scribe, type:.debug, url)
        }
    }
}

How would you retrieve the complete set of running processes, including daemons?

Of-course, it could execute ps and parse the output, but that's not ideal to be calling frequently.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Have a look at https://stackoverflow.com/questions/5906180/return-a-list-of-running-background-apps-processes-in-ios/8843073#8843073 – That is in C, but can be easily translated to Swift. – Martin R Mar 20 '19 at 17:33
  • Thanks Martin, I'm aware of how to do this in other languages, but would rather not have to create a bridging header. The question is to seek out if there's a method for doing this in Swift. Also, the XPC helper may need to call this frequently, so relying on sys calls to the kernel via sysctl is not ideal. – TheDarkKnight Mar 21 '19 at 09:02
  • You don't need a bridging header, you can call sysctl directly from Swift ([example](https://stackoverflow.com/a/33177600/1187415)). – I don't know about the performance, but Swift is just a *language* and does not provide that information itself. If no Cocoa/Foundation class helps then you may need to use a system call. – Martin R Mar 21 '19 at 09:27
  • *you can call sysctl directly from Swift* - Thanks for pointing that out, I stupidly assumed that as a c function call it wasn't directly available. – TheDarkKnight Mar 21 '19 at 10:53
  • someone get it running? – muescha Sep 24 '22 at 03:23

0 Answers0