Daemons: Difference between revisions

From iPhone Development Wiki
(you trelled us good)
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Daemons are programs that run as a background process. These programs may or may not have a long life cycle and exist in a paradigm different to tweaks. They do not require to be injected onto other processes, that means that they exist by themselves.
'''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, ssh.
Several tweaks and programs use them, such as f.lux, Gremlin, RocketBootstrap, SafariDownloadEnabler, and OpenSSH.


== How to make <!-- summon --> a daemon ==
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]].)


There's two steps: making a program and making a plist for it.
= How to make <!-- summon --> a daemon =


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


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


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 ([[notifications]], XPC, sockets) to communicate with the daemon.
While idle, the program should use the least possible resources. Remember that it is working along with many other already existing processes.


=== The plist ===
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.


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


'''Required Keys'''
'''Required Keys'''
Line 22: Line 24:
|-
|-
! Key
! Key
! Type
! Description
! Description
! Type of Value
! Note
! Note
|-
|-
| Label
| Label
| String
| Unique identifier for your daemon
| Unique identifier for your daemon
| String
| ---
| ---
|-
|-
| Program
| Program
| String
| This key maps to the first argument of execvp(3). If missing, the first Argument of ProgramArguments is used.
| This key maps to the first argument of execvp(3). If missing, the first Argument of ProgramArguments is used.
| String
| Only required in absense of '''ProgramArguments''' key
| Only required in absense of '''ProgramArguments''' key
|-
|-
| ProgramArguments
| ProgramArguments
| Array of Strings
| This key maps to the second argument of execvp(3). If missing, the Program key is used.
| This key maps to the second argument of execvp(3). If missing, the Program key is used.
| Array of Strings
| Only required in absense of '''Program''' key
| Only required in absense of '''Program''' key
|}
|}
Line 47: Line 49:
|-
|-
! Key
! Key
! Type
! Description
! Description
! Type of Value
|-
|-
| UserName
| UserName
| String
| Specifies the user to run the job as
| Specifies the user to run the job as
| String
|-
|-
| GroupName
| GroupName
| String
| Specifies the group to run the job as
| Specifies the group to run the job as
| String
|}
|}


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


Line 105: Line 107:
|-
|-
! Key
! Key
! Type
! Description
! Description
! Type of Value
|-
|-
| KeepAlive
| 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
| 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
| boolean or dictionary of stuff (well said Apple)
|-
|-
| TimeOut
| TimeOut
| Integer
| Idle time out (in seconds) to pass to the job. If not specified, the default time will be passed to launchd.
| Idle time out (in seconds) to pass to the job. If not specified, the default time will be passed to launchd.
| Integer
|-
|-
| ExitTimeOut
| 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.
| The amount of time launchd waits before sending a SIGKILL signal. The default value is 20 seconds. The value zero is interpreted as infinity.
| Integer
|-
|-
| ThrottleInterval
| ThrottleInterval
| Integer
| Jobs will be respawned not more than once every 10 seconds. This key overrides the default throttling policy of launchd.
| Jobs will be respawned not more than once every 10 seconds. This key overrides the default throttling policy of launchd.
| Integer
|}
|}


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


Line 167: Line 169:
|-
|-
! Key
! Key
! Type
! Description
! Description
! Type of Value
|-
|-
| inetdCompatibility
| inetdCompatibility
| Dictionary
| The daemon expects to be run as if it were launched from inetd
| The daemon expects to be run as if it were launched from inetd
| Dictionary
|}
|}


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


== Examples ==
= Examples =


* [https://github.com/uroboro/iOS-daemon iOS daemon]
* [https://github.com/uroboro/iOS-daemon iOS daemon]


== References ==
= See also =


* [[launchd]]
* [[launchd]]
* http://www.launchd.info/
= External links =
* [https://developer.apple.com/library/mac/documentation/macosx/conceptual/bpsystemstartup/Chapters/Introduction.html#//apple_ref/doc/uid/10000172i-SW1-SW1 Daemons and Services Programming Guide]
* [https://developer.apple.com/library/mac/documentation/macosx/conceptual/bpsystemstartup/Chapters/Introduction.html#//apple_ref/doc/uid/10000172i-SW1-SW1 Daemons and Services Programming Guide]
* [https://developer.apple.com/library/mac/technotes/tn2083/_index.html#//apple_ref/doc/uid/DTS10003794-CH1-SECTION2 Technical Note TN2083]
* [https://developer.apple.com/library/mac/technotes/tn2083/_index.html#//apple_ref/doc/uid/DTS10003794-CH1-SECTION2 Technical Note TN2083]


[[Category:Daemons]]
[[Category:Daemons]]

Latest revision as of 03:10, 3 November 2018

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