GoogleGit

blob: d70c6f3e23c1bb0d377e19dcfde0de0c1db51560 [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 <path>
  151. Parse an init config file, extending the current configuration.
  152. If <path> is a directory, each file in the directory is parsed as
  153. a config file. It is not recursive, nested directories will
  154. not be parsed.
  155. insmod <path>
  156. Install the module at <path>
  157. load_all_props
  158. Loads properties from /system, /vendor, et cetera.
  159. This is included in the default init.rc.
  160. load_persist_props
  161. Loads persistent properties when /data has been decrypted.
  162. This is included in the default init.rc.
  163. loglevel <level>
  164. Sets the kernel log level to level. Properties are expanded within <level>.
  165. mkdir <path> [mode] [owner] [group]
  166. Create a directory at <path>, optionally with the given mode, owner, and
  167. group. If not provided, the directory is created with permissions 755 and
  168. owned by the root user and root group. If provided, the mode, owner and group
  169. will be updated if the directory exists already.
  170. mount_all <fstab>
  171. Calls fs_mgr_mount_all on the given fs_mgr-format fstab.
  172. mount <type> <device> <dir> [ <flag> ]* [<options>]
  173. Attempt to mount the named device at the directory <dir>
  174. <device> may be of the form mtd@name to specify a mtd block
  175. device by name.
  176. <flag>s include "ro", "rw", "remount", "noatime", ...
  177. <options> include "barrier=1", "noauto_da_alloc", "discard", ... as
  178. a comma separated string, eg: barrier=1,noauto_da_alloc
  179. powerctl
  180. Internal implementation detail used to respond to changes to the
  181. "sys.powerctl" system property, used to implement rebooting.
  182. restart <service>
  183. Like stop, but doesn't disable the service.
  184. restorecon <path> [ <path> ]*
  185. Restore the file named by <path> to the security context specified
  186. in the file_contexts configuration.
  187. Not required for directories created by the init.rc as these are
  188. automatically labeled correctly by init.
  189. restorecon_recursive <path> [ <path> ]*
  190. Recursively restore the directory tree named by <path> to the
  191. security contexts specified in the file_contexts configuration.
  192. rm <path>
  193. Calls unlink(2) on the given path. You might want to
  194. use "exec -- rm ..." instead (provided the system partition is
  195. already mounted).
  196. rmdir <path>
  197. Calls rmdir(2) on the given path.
  198. setprop <name> <value>
  199. Set system property <name> to <value>. Properties are expanded
  200. within <value>.
  201. setrlimit <resource> <cur> <max>
  202. Set the rlimit for a resource.
  203. start <service>
  204. Start a service running if it is not already running.
  205. stop <service>
  206. Stop a service from running if it is currently running.
  207. swapon_all <fstab>
  208. Calls fs_mgr_swapon_all on the given fstab file.
  209. symlink <target> <path>
  210. Create a symbolic link at <path> with the value <target>
  211. sysclktz <mins_west_of_gmt>
  212. Set the system clock base (0 if system clock ticks in GMT)
  213. trigger <event>
  214. Trigger an event. Used to queue an action from another
  215. action.
  216. verity_load_state
  217. Internal implementation detail used to load dm-verity state.
  218. verity_update_state <mount_point>
  219. Internal implementation detail used to update dm-verity state and
  220. set the partition.<mount_point>.verified properties used by adb remount
  221. because fs_mgr can't set them directly itself.
  222. wait <path> [ <timeout> ]
  223. Poll for the existence of the given file and return when found,
  224. or the timeout has been reached. If timeout is not specified it
  225. currently defaults to five seconds.
  226. write <path> <content>
  227. Open the file at <path> and write a string to it with write(2).
  228. If the file does not exist, it will be created. If it does exist,
  229. it will be truncated. Properties are expanded within <content>.
  230. Properties
  231. ----------
  232. Init updates some system properties to provide some insight into
  233. what it's doing:
  234. init.action
  235. Equal to the name of the action currently being executed or "" if none
  236. init.command
  237. Equal to the command being executed or "" if none.
  238. init.svc.<name>
  239. State of a named service ("stopped", "running", "restarting")
  240. Bootcharting
  241. ------------
  242. This version of init contains code to perform "bootcharting": generating log
  243. files that can be later processed by the tools provided by www.bootchart.org.
  244. On the emulator, use the -bootchart <timeout> option to boot with bootcharting
  245. activated for <timeout> seconds.
  246. On a device, create /data/bootchart/start with a command like the following:
  247. adb shell 'echo $TIMEOUT > /data/bootchart/start'
  248. Where the value of $TIMEOUT corresponds to the desired bootcharted period in
  249. seconds. Bootcharting will stop after that many seconds have elapsed.
  250. You can also stop the bootcharting at any moment by doing the following:
  251. adb shell 'echo 1 > /data/bootchart/stop'
  252. Note that /data/bootchart/stop is deleted automatically by init at the end of
  253. the bootcharting. This is not the case with /data/bootchart/start, so don't
  254. forget to delete it when you're done collecting data.
  255. The log files are written to /data/bootchart/. A script is provided to
  256. retrieve them and create a bootchart.tgz file that can be used with the
  257. bootchart command-line utility:
  258. sudo apt-get install pybootchartgui
  259. # grab-bootchart.sh uses $ANDROID_SERIAL.
  260. $ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh
  261. One thing to watch for is that the bootchart will show init as if it started
  262. running at 0s. You'll have to look at dmesg to work out when the kernel
  263. actually started init.
  264. Comparing two bootcharts
  265. ------------------------
  266. A handy script named compare-bootcharts.py can be used to compare the
  267. start/end time of selected processes. The aforementioned grab-bootchart.sh
  268. will leave a bootchart tarball named bootchart.tgz at /tmp/android-bootchart.
  269. If two such barballs are preserved on the host machine under different
  270. directories, the script can list the timestamps differences. For example:
  271. Usage: system/core/init/compare-bootcharts.py base_bootchart_dir
  272. exp_bootchart_dir
  273. process: baseline experiment (delta)
  274. - Unit is ms (a jiffy is 10 ms on the system)
  275. ------------------------------------
  276. /init: 50 40 (-10)
  277. /system/bin/surfaceflinger: 4320 4470 (+150)
  278. /system/bin/bootanimation: 6980 6990 (+10)
  279. zygote64: 10410 10640 (+230)
  280. zygote: 10410 10640 (+230)
  281. system_server: 15350 15150 (-200)
  282. bootanimation ends at: 33790 31230 (-2560)
  283. Systrace
  284. --------
  285. Systrace [1] can be used for obtaining performance analysis reports during boot
  286. time on userdebug or eng builds.
  287. Here is an example of trace events of "wm" and "am" categories:
  288. $ANDROID_BUILD_TOP/external/chromium-trace/systrace.py wm am --boot
  289. This command will cause the device to reboot. After the device is rebooted and
  290. the boot sequence has finished, the trace report is obtained from the device
  291. and written as trace.html on the host by hitting Ctrl+C.
  292. LIMITATION
  293. Recording trace events is started after persistent properties are loaded, so
  294. the trace events that are emitted before that are not recorded. Several
  295. services such as vold, surfaceflinger, and servicemanager are affected by this
  296. limitation since they are started before persistent properties are loaded.
  297. Zygote initialization and the processes that are forked from the zygote are not
  298. affected.
  299. [1] http://developer.android.com/tools/help/systrace.html
  300. Debugging init
  301. --------------
  302. By default, programs executed by init will drop stdout and stderr into
  303. /dev/null. To help with debugging, you can execute your program via the
  304. Android program logwrapper. This will redirect stdout/stderr into the
  305. Android logging system (accessed via logcat).
  306. For example
  307. service akmd /system/bin/logwrapper /sbin/akmd
  308. For quicker turnaround when working on init itself, use:
  309. mm -j
  310. m ramdisk-nodeps
  311. m bootimage-nodeps
  312. adb reboot bootloader
  313. fastboot boot $ANDROID_PRODUCT_OUT/boot.img
  314. Alternatively, use the emulator:
  315. emulator -partition-size 1024 -verbose -show-kernel -no-window
  316. You might want to call klog_set_level(6) after the klog_init() call
  317. so you see the kernel logging in dmesg (or the emulator output).