blob: 4dda34032905972cadee47ffa787ebea859b97ca [file] [log] [blame]
  1. Android Init Language
  2. ---------------------
  3. The Android Init Language consists of four broad classes of statements,
  4. which are Actions, Commands, Services, and Options.
  5. All of these are line-oriented, consisting of tokens separated by
  6. whitespace. The c-style backslash escapes may be used to insert
  7. whitespace into a token. Double quotes may also be used to prevent
  8. whitespace from breaking text into multiple tokens. The backslash,
  9. when it is the last character on a line, may be used for line-folding.
  10. Lines which start with a # (leading whitespace allowed) are comments.
  11. Actions and Services implicitly declare a new section. All commands
  12. or options belong to the section most recently declared. Commands
  13. or options before the first section are ignored.
  14. Actions and Services have unique names. If a second Action or Service
  15. is declared with the same name as an existing one, it is ignored as
  16. an error. (??? should we override instead)
  17. Actions
  18. -------
  19. Actions are named sequences of commands. Actions have a trigger which
  20. is used to determine when the action should occur. When an event
  21. occurs which matches an action's trigger, that action is added to
  22. the tail of a to-be-executed queue (unless it is already on the
  23. queue).
  24. Each action in the queue is dequeued in sequence and each command in
  25. that action is executed in sequence. Init handles other activities
  26. (device creation/destruction, property setting, process restarting)
  27. "between" the execution of the commands in activities.
  28. Actions take the form of:
  29. on <trigger>
  30. <command>
  31. <command>
  32. <command>
  33. Services
  34. --------
  35. Services are programs which init launches and (optionally) restarts
  36. when they exit. Services take the form of:
  37. service <name> <pathname> [ <argument> ]*
  38. <option>
  39. <option>
  40. ...
  41. Options
  42. -------
  43. Options are modifiers to services. They affect how and when init
  44. runs the service.
  45. critical
  46. This is a device-critical service. If it exits more than four times in
  47. four minutes, the device will reboot into recovery mode.
  48. disabled
  49. This service will not automatically start with its class.
  50. It must be explicitly started by name.
  51. setenv <name> <value>
  52. Set the environment variable <name> to <value> in the launched process.
  53. socket <name> <type> <perm> [ <user> [ <group> [ <seclabel> ] ] ]
  54. Create a unix domain socket named /dev/socket/<name> and pass
  55. its fd to the launched process. <type> must be "dgram", "stream" or "seqpacket".
  56. User and group default to 0.
  57. 'seclabel' is the SELinux security context for the socket.
  58. It defaults to the service security context, as specified by seclabel or
  59. computed based on the service executable file security context.
  60. user <username>
  61. Change to username before exec'ing this service.
  62. Currently defaults to root. (??? probably should default to nobody)
  63. Currently, if your process requires linux capabilities then you cannot use
  64. this command. You must instead request the capabilities in-process while
  65. still root, and then drop to your desired uid.
  66. group <groupname> [ <groupname> ]*
  67. Change to groupname before exec'ing this service. Additional
  68. groupnames beyond the (required) first one are used to set the
  69. supplemental groups of the process (via setgroups()).
  70. Currently defaults to root. (??? probably should default to nobody)
  71. seclabel <seclabel>
  72. Change to 'seclabel' before exec'ing this service.
  73. Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
  74. Services on the system partition can instead use policy-defined transitions
  75. based on their file security context.
  76. If not specified and no transition is defined in policy, defaults to the init context.
  77. oneshot
  78. Do not restart the service when it exits.
  79. class <name>
  80. Specify a class name for the service. All services in a
  81. named class may be started or stopped together. A service
  82. is in the class "default" if one is not specified via the
  83. class option.
  84. onrestart
  85. Execute a Command (see below) when service restarts.
  86. writepid <file...>
  87. Write the child's pid to the given files when it forks. Meant for
  88. cgroup/cpuset usage.
  89. Triggers
  90. --------
  91. Triggers are strings which can be used to match certain kinds
  92. of events and used to cause an action to occur.
  93. boot
  94. This is the first trigger that will occur when init starts
  95. (after /init.conf is loaded)
  96. <name>=<value>
  97. Triggers of this form occur when the property <name> is set
  98. to the specific value <value>.
  99. One can also test multiple properties to execute a group
  100. of commands. For example:
  101. on property:test.a=1 && property:test.b=1
  102. setprop test.c 1
  103. The above stub sets test.c to 1 only when
  104. both test.a=1 and test.b=1
  105. Commands
  106. --------
  107. bootchart_init
  108. Start bootcharting if configured (see below).
  109. This is included in the default init.rc.
  110. chmod <octal-mode> <path>
  111. Change file access permissions.
  112. chown <owner> <group> <path>
  113. Change file owner and group.
  114. class_start <serviceclass>
  115. Start all services of the specified class if they are
  116. not already running.
  117. class_stop <serviceclass>
  118. Stop and disable all services of the specified class if they are
  119. currently running.
  120. class_reset <serviceclass>
  121. Stop all services of the specified class if they are
  122. currently running, without disabling them. They can be restarted
  123. later using class_start.
  124. copy <src> <dst>
  125. Copies a file. Similar to write, but useful for binary/large
  126. amounts of data.
  127. domainname <name>
  128. Set the domain name.
  129. enable <servicename>
  130. Turns a disabled service into an enabled one as if the service did not
  131. specify disabled.
  132. If the service is supposed to be running, it will be started now.
  133. Typically used when the bootloader sets a variable that indicates a specific
  134. service should be started when needed. E.g.
  135. on property:ro.boot.myfancyhardware=1
  136. enable my_fancy_service_for_my_fancy_hardware
  137. exec [ <seclabel> [ <user> [ <group> ]* ] ] -- <command> [ <argument> ]*
  138. Fork and execute command with the given arguments. The command starts
  139. after "--" so that an optional security context, user, and supplementary
  140. groups can be provided. No other commands will be run until this one
  141. finishes. <seclabel> can be a - to denote default.
  142. export <name> <value>
  143. Set the environment variable <name> equal to <value> in the
  144. global environment (which will be inherited by all processes
  145. started after this command is executed)
  146. hostname <name>
  147. Set the host name.
  148. ifup <interface>
  149. Bring the network interface <interface> online.
  150. import <filename>
  151. Parse an init config file, extending the current configuration.
  152. insmod <path>
  153. Install the module at <path>
  154. load_all_props
  155. Loads properties from /system, /vendor, et cetera.
  156. This is included in the default init.rc.
  157. load_persist_props
  158. Loads persistent properties when /data has been decrypted.
  159. This is included in the default init.rc.
  160. loglevel <level>
  161. Sets the kernel log level to level. Properties are expanded within <level>.
  162. mkdir <path> [mode] [owner] [group]
  163. Create a directory at <path>, optionally with the given mode, owner, and
  164. group. If not provided, the directory is created with permissions 755 and
  165. owned by the root user and root group. If provided, the mode, owner and group
  166. will be updated if the directory exists already.
  167. mount_all <fstab>
  168. Calls fs_mgr_mount_all on the given fs_mgr-format fstab.
  169. mount <type> <device> <dir> [ <flag> ]* [<options>]
  170. Attempt to mount the named device at the directory <dir>
  171. <device> may be of the form mtd@name to specify a mtd block
  172. device by name.
  173. <flag>s include "ro", "rw", "remount", "noatime", ...
  174. <options> include "barrier=1", "noauto_da_alloc", "discard", ... as
  175. a comma separated string, eg: barrier=1,noauto_da_alloc
  176. powerctl
  177. Internal implementation detail used to respond to changes to the
  178. "sys.powerctl" system property, used to implement rebooting.
  179. restart <service>
  180. Like stop, but doesn't disable the service.
  181. restorecon <path> [ <path> ]*
  182. Restore the file named by <path> to the security context specified
  183. in the file_contexts configuration.
  184. Not required for directories created by the init.rc as these are
  185. automatically labeled correctly by init.
  186. restorecon_recursive <path> [ <path> ]*
  187. Recursively restore the directory tree named by <path> to the
  188. security contexts specified in the file_contexts configuration.
  189. rm <path>
  190. Calls unlink(2) on the given path. You might want to
  191. use "exec -- rm ..." instead (provided the system partition is
  192. already mounted).
  193. rmdir <path>
  194. Calls rmdir(2) on the given path.
  195. setprop <name> <value>
  196. Set system property <name> to <value>. Properties are expanded
  197. within <value>.
  198. setrlimit <resource> <cur> <max>
  199. Set the rlimit for a resource.
  200. start <service>
  201. Start a service running if it is not already running.
  202. stop <service>
  203. Stop a service from running if it is currently running.
  204. swapon_all <fstab>
  205. Calls fs_mgr_swapon_all on the given fstab file.
  206. symlink <target> <path>
  207. Create a symbolic link at <path> with the value <target>
  208. sysclktz <mins_west_of_gmt>
  209. Set the system clock base (0 if system clock ticks in GMT)
  210. trigger <event>
  211. Trigger an event. Used to queue an action from another
  212. action.
  213. verity_load_state
  214. Internal implementation detail used to load dm-verity state.
  215. verity_update_state <mount_point>
  216. Internal implementation detail used to update dm-verity state and
  217. set the partition.<mount_point>.verified properties used by adb remount
  218. because fs_mgr can't set them directly itself.
  219. wait <path> [ <timeout> ]
  220. Poll for the existence of the given file and return when found,
  221. or the timeout has been reached. If timeout is not specified it
  222. currently defaults to five seconds.
  223. write <path> <content>
  224. Open the file at <path> and write a string to it with write(2).
  225. If the file does not exist, it will be created. If it does exist,
  226. it will be truncated. Properties are expanded within <content>.
  227. Properties
  228. ----------
  229. Init updates some system properties to provide some insight into
  230. what it's doing:
  231. init.action
  232. Equal to the name of the action currently being executed or "" if none
  233. init.command
  234. Equal to the command being executed or "" if none.
  235. init.svc.<name>
  236. State of a named service ("stopped", "running", "restarting")
  237. Bootcharting
  238. ------------
  239. This version of init contains code to perform "bootcharting": generating log
  240. files that can be later processed by the tools provided by
  241. On the emulator, use the -bootchart <timeout> option to boot with bootcharting
  242. activated for <timeout> seconds.
  243. On a device, create /data/bootchart/start with a command like the following:
  244. adb shell 'echo $TIMEOUT > /data/bootchart/start'
  245. Where the value of $TIMEOUT corresponds to the desired bootcharted period in
  246. seconds. Bootcharting will stop after that many seconds have elapsed.
  247. You can also stop the bootcharting at any moment by doing the following:
  248. adb shell 'echo 1 > /data/bootchart/stop'
  249. Note that /data/bootchart/stop is deleted automatically by init at the end of
  250. the bootcharting. This is not the case with /data/bootchart/start, so don't
  251. forget to delete it when you're done collecting data.
  252. The log files are written to /data/bootchart/. A script is provided to
  253. retrieve them and create a bootchart.tgz file that can be used with the
  254. bootchart command-line utility:
  255. sudo apt-get install pybootchartgui
  256. # uses $ANDROID_SERIAL.
  257. $ANDROID_BUILD_TOP/system/core/init/
  258. One thing to watch for is that the bootchart will show init as if it started
  259. running at 0s. You'll have to look at dmesg to work out when the kernel
  260. actually started init.
  261. Comparing two bootcharts
  262. ------------------------
  263. A handy script named can be used to compare the
  264. start/end time of selected processes. The aforementioned
  265. will leave a bootchart tarball named bootchart.tgz at /tmp/android-bootchart.
  266. If two such barballs are preserved on the host machine under different
  267. directories, the script can list the timestamps differences. For example:
  268. Usage: system/core/init/ base_bootchart_dir
  269. exp_bootchart_dir
  270. process: baseline experiment (delta)
  271. - Unit is ms (a jiffy is 10 ms on the system)
  272. ------------------------------------
  273. /init: 50 40 (-10)
  274. /system/bin/surfaceflinger: 4320 4470 (+150)
  275. /system/bin/bootanimation: 6980 6990 (+10)
  276. zygote64: 10410 10640 (+230)
  277. zygote: 10410 10640 (+230)
  278. system_server: 15350 15150 (-200)
  279. bootanimation ends at: 33790 31230 (-2560)
  280. Debugging init
  281. --------------
  282. By default, programs executed by init will drop stdout and stderr into
  283. /dev/null. To help with debugging, you can execute your program via the
  284. Android program logwrapper. This will redirect stdout/stderr into the
  285. Android logging system (accessed via logcat).
  286. For example
  287. service akmd /system/bin/logwrapper /sbin/akmd
  288. For quicker turnaround when working on init itself, use:
  289. mm -j
  290. m ramdisk-nodeps
  291. m bootimage-nodeps
  292. adb reboot bootloader
  293. fastboot boot $ANDROID_PRODUCT_OUT/boot.img
  294. Alternatively, use the emulator:
  295. emulator -partition-size 1024 -verbose -show-kernel -no-window
  296. You might want to call klog_set_level(6) after the klog_init() call
  297. so you see the kernel logging in dmesg (or the emulator output).