Where to start with a launchd daemon?

Hi,

I have a server (written in C++) that I'd like to port to a launchd service and I'm finding it difficult to navigate the macOS documentation. If I go to the developer docs search page and type in launchd I don't really get anything unless I check "Beta" in the filter. Is this an indication I should not be using launchd?


If I look at the "beta" documentation here https://developer.apple.com/library/prerelease/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/Introduction.html


It leads me to this statement

If your daemon advertises a socket, check in with

launchd
as part of your daemon initialization. For an example implementation of the check-in process, see SampleD.


This sounds exactly what I need to know as I'd like my service started on demand and it currently listens on a socket for clients.


However when I take a look at that sample it first calls kqueue() which I cannot find any documentation for on the developer docs search page. There is a man page on my 10.11 machine. Then the sample calls a bunch of XPC calls (like launch_data_dict_lookup) that are all marked deprecated. If I shouldn't be calling those what are their replacements, their pages don't list any. Again this doesn't sound like an API I should be looking at for the future.


Any suggestions on what I should read or use?


- James

I'm finding it difficult to navigate the macOS documentation.

Yeah, I sympathise; documentation for this part of the system is not as well maintained as it should be. Where you have problems I recommend that you file bugs against the docs; that’ll help DevPubs understand where the rough edges are.

Having said that, a lot of this stuff is covered, at least at the reference level, in the system man pages; I’ll provide some links below but, if you have Xcode installed, you can access man pages from Terminal via the traditional UNIX

man
command.

This sounds exactly what I need to know as I'd like my service started on demand and it currently listens on a socket for clients.

Right. In general macOS daemons should strive to launch on demand. If the only thing your daemon does is respond to network requests, having launchd open a listening socket and launch your daemon when folks connect to that socket is the right approach.

However when I take a look at that sample it first calls kqueue() which I cannot find any documentation for on the developer docs search page.

This is covered by the

kqueue
page.

Note that kqueues are just a mechanism to integrate various event sources into an event loop. Most folks on Darwin platforms use:

  • NSRunLoop, if they’re already using high-level frameworks

  • dispatch event sources (see here), if they want to avoid the high-level frameworks for some reason

SampleD hasn’t been updated in a while, which is why it still uses kqueues.

Then the sample calls a bunch of XPC calls (like

launch_data_dict_lookup
) that are all marked deprecated.

Yeah, that could be clearer. In

<launch.h>
, right next to all this deprecated stuff, you’ll find
launch_activate_socket
, which is the modern way to do this.

Please do file a bug against SampleD requesting it be updated to use the new stuff.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for your reply, Filed bug 27357721

- James

So it's been almost three years since this question was asked, and I see that SampleD.c has not been updated to use the launch_activate_socket function instead of the deprecated launch_data_* functions. Is there a quick summary of how to use the launch_activate_socket function to perform the checkin with launchd when your Launch Daemon starts up?

And now nearly five years later, it is still not updated, but it has been "archived" with no obvious replacement. The Apple documentation seems to be archived these days instead of being updated which is not helpful.
I have updated SampleD.c and attached it below.



Where to start with a launchd daemon?
 
 
Q