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