Enable, disable, and start services programmatically on macOS

I am writing a program that comes with a service. What I have done so far is to create a helper tool that can perform advanced tasks for my process and can communicate through XPC.

My program comes bundled with the service, and I want to use an auxiliary tool to install and start / stop this service so that my program can set the "run service with system" checkbox in the settings.

I can successfully copy the plist for the service, but I cannot find a way to enable, disable, start or stop the program programmatically. I think the decision to name is system("launchctl load /path/to/service.plist");pretty ugly. Is there any mechanism in lens C to accomplish this task and achieve success or failure?

+4
source share
1 answer

Apple has an outdated C API to start, stop, and enable startup services in launch.h. The source code for the API is on their open source site: https://opensource.apple.com/source/launchd/launchd-442.26.2/liblaunch/

Here is a sample code that asks the startup to start the LittleSnitchUIAgent service:

#include <launch.h>

int main(int argc, const char * argv[]) {
    const char *job = "at.obdev.LittleSnitchUIAgent";
    launch_data_t resp, msg;
    msg = launch_data_alloc(LAUNCH_DATA_DICTIONARY);
    launch_data_dict_insert(
        msg, launch_data_new_string(job), LAUNCH_KEY_STARTJOB);
    resp = launch_msg(msg);
    launch_data_free(msg);
    return 0;
}

The LittleSnitchUIAgent - . , , .

, man- " " . Launchd : , , , , . , . , , API.

+1

Source: https://habr.com/ru/post/1683396/


All Articles