Search code examples
macoslaunchd

How to check if service is enabled with launchd


I can't figure out how to check from within C/C++ if a service is enabled via launchd. I know I can use launchctl from the command line and am currently executing ' launchctl list myServiceName ' from fork/exec.

I've found that boostrap_look_up() might be the way to check this but I can't find enough documentation to condense this down to a simple example.

Can you shed light on this? All I need is a small function to test if my service is actually registered and available.


Solution

  • Apple has open sourced launchd and the source code is available at http://opensource.apple.com/source/launchd/launchd-442.26.2/

    The source code for launchctl is in support/launchctl.c. Hopefully you should be able to find what you need in there. I suspect you need to look at the list_cmd function about two thirds of the way down the file.

    If the answer isn't in launchctl.c then I really don't know where else it would be.


    Initially I started looking for symbols in the launchctl binary.

    The bootstrap_look_up() function you mention appears to be defined in launchd/liblaunch/libbootstrap.c.

    Running nm /bin/launchctl provides a number of interesting symbols:

                 ...
                 U _bootstrap_get_root
                 U _bootstrap_info
                 U _bootstrap_look_up_per_user
                 U _bootstrap_lookup_children
                 U _bootstrap_parent
                 U _bootstrap_port
                 ...
                 U _launch_data_alloc
                 U _launch_data_array_get_count
                 U _launch_data_array_get_index
                 U _launch_data_array_set_index
                 U _launch_data_copy
                 U _launch_data_dict_insert
                 U _launch_data_dict_iterate
                 U _launch_data_dict_lookup
                 U _launch_data_dict_remove
                 U _launch_data_free
                 U _launch_data_get_bool
                 U _launch_data_get_errno
                 U _launch_data_get_fd
                 U _launch_data_get_integer
                 U _launch_data_get_machport
                 U _launch_data_get_opaque
                 U _launch_data_get_opaque_size
                 U _launch_data_get_real
                 U _launch_data_get_string
                 U _launch_data_get_type
                 U _launch_data_new_bool
                 U _launch_data_new_fd
                 U _launch_data_new_opaque
                 U _launch_data_new_string
                 U _launch_data_set_bool
                 U _launch_data_set_integer
                 U _launch_data_set_opaque
                 U _launch_data_set_real
                 U _launch_data_set_string
                 U _launch_msg
                 ...
    

    These launch_data_ functions appear to be declared in launchd/launch/liblaunch.c.

    As far as I can tell liblaunch is not installed as a system library and libSystem doesn't appear to contain these symbols either (just bootstrap_init is exported).

    I would suggest downloading the launchd source, compiling your own liblaunch and seeing if you can get the functionality you require by linking it to your project. I have no idea if this will do what you want.