Daemons

From iPhone Development Wiki

Daemons are programs that run as background processes. These programs may have a long life cycle. They exist in a paradigm different from tweaks: they can exist by themselves and do not have to be injected into other processes.

Several tweaks and programs use them, such as f.lux, Gremlin, RocketBootstrap, SafariDownloadEnabler, and OpenSSH.

Daemons running as part of iOS include backboardd (handles events from hardware), launchd (handles other daemons), lockdownd (provides system information), notifyd (exchanges Darwin notifications), and wifid (the back-end of MobileWiFi.framework). (See Category:Daemons.)

How to make a daemon

There are two steps: making a program and making a plist for it.

The program

While idle, the program should use the least possible resources. Remember that it is working along with many other already existing processes.

Given that memory isn't shared across processes, one cannot call functions or get objects freely between processes. To solve this, one must use some form of IPC to communicate with the daemon.

The plist

Place your plist, following the reverse DNS notation (com.your.daemon), in layout/Library/LaunchDaemons. Use XML version of plist files, not NeXTSTEP, as some types may be misinterpreted by launchctl upon loading the plist.

Required Keys

Key Type Description Note
Label String Unique identifier for your daemon ---
Program String This key maps to the first argument of execvp(3). If missing, the first Argument of ProgramArguments is used. Only required in absense of ProgramArguments key
ProgramArguments Array of Strings This key maps to the second argument of execvp(3). If missing, the Program key is used. Only required in absense of Program key

Permission related Keys

Key Type Description
UserName String Specifies the user to run the job as
GroupName String Specifies the group to run the job as

Launch related Keys

Key Type Description
RunAtLoad Boolean Controls whether your job is launched at boot time (when launchd launches) or not.
LaunchOnlyOnce Boolean Set this to true if you just want to execute your code once.
Disabled Boolean Tells launchctl that it should not submit this job to launchd
EnvironmentVariables Dictionary of Strings Allows you to set additional environmental variables for your job. As an example: DYLD_INSERT_LIBRARIES.
WatchPaths Array of Strings launchd will watch the given paths and it will launch your job when one of these paths has been modified.
QueueDirectories Array of Strings Similar to WatchPaths with the difference that watches directories not being empty.
StartInterval Integer This optional key causes the job to be started every N seconds.
StartCalendarInterval Dictionary of integers or array of dictionary of integers Specify a date or time for your Job to be started. e.g every Monday at 7PM.

Lifetime related Keys

Key Type Description
KeepAlive Boolean or dictionary of stuff (well said Apple) Determines whether your job is to be kept continuously running. If this is set to true and your job exits, it will respawn. Keeping the daemon alive as of 'not exiting' is your job
TimeOut Integer Idle time out (in seconds) to pass to the job. If not specified, the default time will be passed to launchd.
ExitTimeOut Integer The amount of time launchd waits before sending a SIGKILL signal. The default value is 20 seconds. The value zero is interpreted as infinity.
ThrottleInterval Integer Jobs will be respawned not more than once every 10 seconds. This key overrides the default throttling policy of launchd.

Control and Debug related Keys

Key Type Description
RootDirectory String This key allows you to change the root directory for your job before it launches.
WorkingDirectory String This key allows you to change the working directory for your job before it launches.
StandardInPath String You can choose a file where the data supplied by stdin will be saved,
StandardOutPath String Output from stdout will be saved to that file when using stdio(3).
StandardErrorPath String Specify an error path where stdio(3)'s stderr is written to.
Debug Boolean Control wether launchd should adjust its log mask temporarily to LOG_DEBUG or not.
WaitForDebugger Boolean Instructs the kernel to wait until a debugger is attached before executing any code in your daemon.

Other Keys

Key Type Description
inetdCompatibility Dictionary The daemon expects to be run as if it were launched from inetd

To be done: Subrows for Dictionary Keys, maybe add a few more keys

Examples

See also

External links