On Sep 25, 2008, at 7:00 AM, launchd-dev-request@lists.macosforge.org wrote:
I'm wondering about the relationship between NetworkState and the networking stack. I haven't found a reference about the KeepAlive/NetworkState value and the relative order in launching jobs with it versus the loading of the network stack.
When NetworkState is true, will that result in a job that starts before, during, or after the loading of the network stack? Is that order guaranteed?
Ultimately, I'd like to launch something _before_ the network stack has loaded, but only really need load it when -- and as long as -- the network stack will be active.
Jeremy, launchd and the mechanisms it uses to receive notification that the network stack is up are asynchronous and callback-oriented, which means there is no guarantee of ordering. When launchd is notified an IP address has been assigned to an interface, the network stack is already "up", so there's no way to use NetworkState to be called before interfaces have had any addresses assigned to them. Even so, with all the launchd plists installed by Leopard, it's very likely that by the time launchd gets around to loading your process, the network is already up and sending out mDNS packets (on a Mac Pro that uses a static IP address with wired Ethernet, an interface coming online happens *very* early and *very* quickly during system startup) . On my latest-gen iMac, we're process ID 50 and the network is very much alive when we load. However you could set the RunAtLoad key, which will start your daemon as soon as the machine starts up. The network may already be up at that point so you should use SystemConfiguration/ SCNetworkCheckReachability/ifconfig/etc to check if the network is already up, and do your work appropriately. Otherwise it could exit, and hope the network doesn't come online while you are exiting (as I think there's a race condition in there that we've experienced). When the network goes offline, launchd should then ask you to exit, and then relaunch you as necessary. If you absolutely must be guaranteed that your daemon is called before any addresses are assigned to the machine, you cannot use launchd. Again, this is all from my experience—somebody else might prove me wrong. Ryan