I need launchd to execute a script when a certain kind of usb device is connected. Specifically, an iOS device.
is this possible? If so, what should the launchd plist look like?
Any idea on what I am doing wrong?
Failing to understand the
launchd
executable model )-:
I like to say that
launchd
is
level triggered? It will keep the job running as long as the conditions expressed in the job’s property list are true. Consider the classic networking scenario:
The job declares that it wants to listen on a specific port.
Starting with the job stopped…
Someone connects to the port.
starts the job.launchd
The job accepts the connection from the socket and runs it.
The job keeps doing this until it exits for some reason (typically an idle exit or a memory pressure exit).
Imagine, however, if the job crashes before accepting the connection (this it, at the start of step 5). The connection is still pending on the socket, and thus
launchd
starts the job again. And it will keep doing this until the job handles all the connections.
This is a good thing in general.
In your case the conditions for starting the job are the presence of a particular USB peripheral. That peripheral doesn’t go away when you finish talking to it, and thus
launchd
keeps you running.
What you’re supposed to do here is as follows:
When you’re launched, use
to listen for match events coming in from the system.xpc_set_event_stream_handler
When you get one, use the
property to find the corresponding I/O Registry entry.IOMatchLaunchServiceID
Run that entry.
It seems like in your case you don’t actually want to do anything with the entry, so this step boils down to:
- Log the entry.
- Ignore it.
On the other hand, if you also want to log disconnects running the entry is a little more complex.
Either opt in to memory pressure exits (
), or implement an idle exit, or just let your job run continuously.EnablePressuredExit
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"