[launchd-dev] Daemon & Network Availability?

Quinn eskimo1 at apple.com
Fri Nov 14 02:38:13 PST 2008


At 9:19 -0500 11/11/08, Karl Moskowski wrote:
>I had hoped that KeepAlive={NetworkState=true} would automatically 
>start & stop the daemon with the network being connected & 
>disconnected. Since that seems to be incorrect, I guess I have to 
>make it launch at startup and keep running. I can programmatically 
>check if the network is available (using SystemConfiguration 
>notifications, I think, unless there's a better way), and start/stop 
>activity appropriately.

"NetworkState" is a very clumsy switch.  For example, it doesn't 
distinguish between normal IP addresses and link-local IP addresses. 
I suggest you use this in concert with your System Configuration 
framework code.  That is, use "NetworkState" to allow you to 
completely shut down when the user has no networking then, when you 
are launched, use SCF to decide whether to actually try connecting or 
not.

Earlier you wrote:

>- launchd loads the daemon as soon as the network interface is 
>available, before it has a valid IP address.

Seems unlikely to me.  It may be that the interface has a valid IPv6 
address (the link local one, which arrives very quickly), but not a 
valid IPv4 address (which typically comes from DHCP, and thus arrives 
very slowly).

You can see the exact criteria that launchd uses to determine whether 
the network is up or down by looking at the get_network_state in 
"launchd.c".

<http://www.opensource.apple.com/darwinsource/10.5.5/launchd-258.18/launchd/src/launchd.c>

[This link may require an APSL <http://www.opensource.apple.com/apsl/>
account.]

>- launchd loads the daemon at boot if any network interfaces are 
>connected (e.g., Parallels ports)

Does "Parallels ports" mean that that you have Parallels installed? 
If so, their network virtualisation is probably installing interfaces 
that are messing things up.  Like I said, "NetworkState" is a very 
clumsy switch |-:

>- launchd doesn't unload the daemon automatically if the network goes down

That's expected behaviour.  "KeepAlive" controls when launchd starts 
your job; stopping your job is your problem.  If you want to stop 
when the network goes away, just use SCF to monitor the network state 
and, if it goes away and stays away for a reasonable period of time, 
exit the process.

S+E
-- 
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


More information about the launchd-dev mailing list