Swift/ MacOS: Process launch with root permissions

Viewed 578

My app should check if a service is currently running or not. In terminal the command would be

sudo launchctl list | grep some_service

I can't seem to be able to find any resources to create and use a privileged helper to run the protected command.

I have however found this rather rough solution, but is it safe?

Also, is it possible to just elevate the permissions of the app to just run as root everytime? If so how would I do it?

1 Answers

The Cocoa equivalent of launchctl list is SMCopyAllJobDictionaries(_:) in the ServiceManagement framework.

It's deprecated for a while but it still works.

A privileged helper can be created with SMJobBless(::::) and an XPC service, but this is not trivial to accomplish.

Related