blob: 4cbe600a1911b54db16a8f3bf37e62a59cd6885e [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001
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
Elliott Hughes8d82ea02015-02-06 20:15:18 -080073socket <name> <type> <perm> [ <user> [ <group> [ <seclabel> ] ] ]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074 Create a unix domain socket named /dev/socket/<name> and pass
Mike Lockwood912ff852010-10-01 08:20:36 -040075 its fd to the launched process. <type> must be "dgram", "stream" or "seqpacket".
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 User and group default to 0.
Elliott Hughes8d82ea02015-02-06 20:15:18 -080077 'seclabel' is the SELinux security context for the socket.
Stephen Smalley8348d272013-05-13 12:37:04 -040078 It defaults to the service security context, as specified by seclabel or
79 computed based on the service executable file security context.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080
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
Elliott Hughes8d82ea02015-02-06 20:15:18 -080094seclabel <seclabel>
95 Change to 'seclabel' before exec'ing this service.
Stephen Smalley3fb61102012-11-02 15:22:34 -040096 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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101oneshot
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
Elliott Hughes841b2632015-02-12 14:28:54 -0800113
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114Triggers
115--------
116 Triggers are strings which can be used to match certain kinds
117 of events and used to cause an action to occur.
118
119boot
120 This is the first trigger that will occur when init starts
121 (after /init.conf is loaded)
122
123<name>=<value>
124 Triggers of this form occur when the property <name> is set
125 to the specific value <value>.
126
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800127 One can also test multiple properties to execute a group
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700128 of commands. For example:
129
130 on property:test.a=1 && property:test.b=1
131 setprop test.c 1
132
133 The above stub sets test.c to 1 only when
134 both test.a=1 and test.b=1
135
Elliott Hughes841b2632015-02-12 14:28:54 -0800136
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137Commands
138--------
139
Elliott Hughes91a3be52015-03-20 11:00:15 -0700140bootchart_init
141 Start bootcharting if configured (see below).
142 This is included in the default init.rc.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143
144chmod <octal-mode> <path>
145 Change file access permissions.
146
147chown <owner> <group> <path>
148 Change file owner and group.
149
150class_start <serviceclass>
151 Start all services of the specified class if they are
152 not already running.
153
154class_stop <serviceclass>
Elliott Hughes91a3be52015-03-20 11:00:15 -0700155 Stop and disable all services of the specified class if they are
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700156 currently running.
157
Elliott Hughes91a3be52015-03-20 11:00:15 -0700158class_reset <serviceclass>
159 Stop all services of the specified class if they are
160 currently running, without disabling them. They can be restarted
161 later using class_start.
162
163copy <src> <dst>
164 Copies a file. Similar to write, but useful for binary/large
165 amounts of data.
166
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700167domainname <name>
168 Set the domain name.
169
JP Abgrall3beec7e2014-05-02 21:14:29 -0700170enable <servicename>
171 Turns a disabled service into an enabled one as if the service did not
172 specify disabled.
173 If the service is supposed to be running, it will be started now.
174 Typically used when the bootloader sets a variable that indicates a specific
175 service should be started when needed. E.g.
176 on property:ro.boot.myfancyhardware=1
177 enable my_fancy_service_for_my_fancy_hardware
178
Elliott Hughes91a3be52015-03-20 11:00:15 -0700179exec [ <seclabel> [ <user> [ <group> ]* ] ] -- <command> [ <argument> ]*
180 Fork and execute command with the given arguments. The command starts
181 after "--" so that an optional security context, user, and supplementary
182 groups can be provided. No other commands will be run until this one
Mark Salyzyn17fff892015-06-02 11:11:02 -0700183 finishes. <seclabel> can be a - to denote default.
Elliott Hughes91a3be52015-03-20 11:00:15 -0700184
Elliott Hughes91a3be52015-03-20 11:00:15 -0700185export <name> <value>
186 Set the environment variable <name> equal to <value> in the
187 global environment (which will be inherited by all processes
188 started after this command is executed)
189
190hostname <name>
191 Set the host name.
192
193ifup <interface>
194 Bring the network interface <interface> online.
195
196import <filename>
197 Parse an init config file, extending the current configuration.
198
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199insmod <path>
200 Install the module at <path>
201
Elliott Hughes91a3be52015-03-20 11:00:15 -0700202load_all_props
203 Loads properties from /system, /vendor, et cetera.
204 This is included in the default init.rc.
205
206load_persist_props
207 Loads persistent properties when /data has been decrypted.
208 This is included in the default init.rc.
209
Elliott Hughesf682b472015-02-06 12:19:48 -0800210loglevel <level>
211 Sets the kernel log level to level. Properties are expanded within <level>.
212
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213mkdir <path> [mode] [owner] [group]
214 Create a directory at <path>, optionally with the given mode, owner, and
215 group. If not provided, the directory is created with permissions 755 and
Johan Redestig0b42ba22015-03-15 17:39:41 +0100216 owned by the root user and root group. If provided, the mode, owner and group
217 will be updated if the directory exists already.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218
Elliott Hughes91a3be52015-03-20 11:00:15 -0700219mount_all <fstab>
220 Calls fs_mgr_mount_all on the given fs_mgr-format fstab.
221
Niklas Tibblingbc3f69f2012-08-01 13:15:38 +0200222mount <type> <device> <dir> [ <flag> ]* [<options>]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223 Attempt to mount the named device at the directory <dir>
224 <device> may be of the form mtd@name to specify a mtd block
225 device by name.
Niklas Tibblingbc3f69f2012-08-01 13:15:38 +0200226 <flag>s include "ro", "rw", "remount", "noatime", ...
227 <options> include "barrier=1", "noauto_da_alloc", "discard", ... as
228 a comma separated string, eg: barrier=1,noauto_da_alloc
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229
Elliott Hughes91a3be52015-03-20 11:00:15 -0700230powerctl
231 Internal implementation detail used to respond to changes to the
232 "sys.powerctl" system property, used to implement rebooting.
233
234restart <service>
235 Like stop, but doesn't disable the service.
236
Stephen Smalley726e8f72013-10-09 16:02:09 -0400237restorecon <path> [ <path> ]*
Stephen Smalley3fb61102012-11-02 15:22:34 -0400238 Restore the file named by <path> to the security context specified
239 in the file_contexts configuration.
240 Not required for directories created by the init.rc as these are
241 automatically labeled correctly by init.
242
Stephen Smalley726e8f72013-10-09 16:02:09 -0400243restorecon_recursive <path> [ <path> ]*
244 Recursively restore the directory tree named by <path> to the
245 security contexts specified in the file_contexts configuration.
Stephen Smalley726e8f72013-10-09 16:02:09 -0400246
Elliott Hughes91a3be52015-03-20 11:00:15 -0700247rm <path>
248 Calls unlink(2) on the given path. You might want to
249 use "exec -- rm ..." instead (provided the system partition is
250 already mounted).
251
252rmdir <path>
253 Calls rmdir(2) on the given path.
254
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700255setprop <name> <value>
Elliott Hughesf682b472015-02-06 12:19:48 -0800256 Set system property <name> to <value>. Properties are expanded
257 within <value>.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258
259setrlimit <resource> <cur> <max>
260 Set the rlimit for a resource.
261
262start <service>
263 Start a service running if it is not already running.
264
265stop <service>
266 Stop a service from running if it is currently running.
267
Elliott Hughes91a3be52015-03-20 11:00:15 -0700268swapon_all <fstab>
269 Calls fs_mgr_swapon_all on the given fstab file.
270
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271symlink <target> <path>
272 Create a symbolic link at <path> with the value <target>
273
The Android Open Source Project35237d12008-12-17 18:08:08 -0800274sysclktz <mins_west_of_gmt>
275 Set the system clock base (0 if system clock ticks in GMT)
276
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277trigger <event>
278 Trigger an event. Used to queue an action from another
279 action.
280
Elliott Hughes91a3be52015-03-20 11:00:15 -0700281verity_load_state
282 Internal implementation detail used to load dm-verity state.
283
284verity_update_state <mount_point>
285 Internal implementation detail used to update dm-verity state and
286 set the partition.<mount_point>.verified properties used by adb remount
287 because fs_mgr can't set them directly itself.
288
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800289wait <path> [ <timeout> ]
Elliott Hughes91a3be52015-03-20 11:00:15 -0700290 Poll for the existence of the given file and return when found,
291 or the timeout has been reached. If timeout is not specified it
292 currently defaults to five seconds.
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800293
Elliott Hughesf682b472015-02-06 12:19:48 -0800294write <path> <content>
295 Open the file at <path> and write a string to it with write(2).
296 If the file does not exist, it will be created. If it does exist,
297 it will be truncated. Properties are expanded within <content>.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700298
299
300Properties
301----------
302Init updates some system properties to provide some insight into
303what it's doing:
304
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800305init.action
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 Equal to the name of the action currently being executed or "" if none
307
308init.command
309 Equal to the command being executed or "" if none.
310
311init.svc.<name>
312 State of a named service ("stopped", "running", "restarting")
313
314
Elliott Hughes841b2632015-02-12 14:28:54 -0800315Bootcharting
316------------
Elliott Hughes841b2632015-02-12 14:28:54 -0800317This version of init contains code to perform "bootcharting": generating log
318files that can be later processed by the tools provided by www.bootchart.org.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319
Elliott Hughes59abac22015-03-28 12:12:51 -0700320On the emulator, use the -bootchart <timeout> option to boot with bootcharting
321activated for <timeout> seconds.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322
Elliott Hughes841b2632015-02-12 14:28:54 -0800323On a device, create /data/bootchart/start with a command like the following:
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324
Elliott Hughes841b2632015-02-12 14:28:54 -0800325 adb shell 'echo $TIMEOUT > /data/bootchart/start'
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326
Elliott Hughes841b2632015-02-12 14:28:54 -0800327Where the value of $TIMEOUT corresponds to the desired bootcharted period in
328seconds. Bootcharting will stop after that many seconds have elapsed.
329You can also stop the bootcharting at any moment by doing the following:
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330
Elliott Hughes841b2632015-02-12 14:28:54 -0800331 adb shell 'echo 1 > /data/bootchart/stop'
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332
Elliott Hughes841b2632015-02-12 14:28:54 -0800333Note that /data/bootchart/stop is deleted automatically by init at the end of
334the bootcharting. This is not the case with /data/bootchart/start, so don't
335forget to delete it when you're done collecting data.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336
Elliott Hughes841b2632015-02-12 14:28:54 -0800337The log files are written to /data/bootchart/. A script is provided to
338retrieve them and create a bootchart.tgz file that can be used with the
339bootchart command-line utility:
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700340
Elliott Hughes841b2632015-02-12 14:28:54 -0800341 sudo apt-get install pybootchartgui
Elliott Hughes59abac22015-03-28 12:12:51 -0700342 # grab-bootchart.sh uses $ANDROID_SERIAL.
Elliott Hughes841b2632015-02-12 14:28:54 -0800343 $ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700344
Elliott Hughes59abac22015-03-28 12:12:51 -0700345One thing to watch for is that the bootchart will show init as if it started
346running at 0s. You'll have to look at dmesg to work out when the kernel
347actually started init.
348
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700349
Ben Cheng50bbde02015-06-09 17:04:36 +0800350Comparing two bootcharts
351------------------------
352A handy script named compare-bootcharts.py can be used to compare the
353start/end time of selected processes. The aforementioned grab-bootchart.sh
354will leave a bootchart tarball named bootchart.tgz at /tmp/android-bootchart.
355If two such barballs are preserved on the host machine under different
356directories, the script can list the timestamps differences. For example:
357
358Usage: system/core/init/compare-bootcharts.py base_bootchart_dir
359 exp_bootchart_dir
360
361process: baseline experiment (delta)
362 - Unit is ms (a jiffy is 10 ms on the system)
363------------------------------------
364/init: 50 40 (-10)
365/system/bin/surfaceflinger: 4320 4470 (+150)
366/system/bin/bootanimation: 6980 6990 (+10)
367zygote64: 10410 10640 (+230)
368zygote: 10410 10640 (+230)
369system_server: 15350 15150 (-200)
370bootanimation ends at: 33790 31230 (-2560)
371
372
Elliott Hughes841b2632015-02-12 14:28:54 -0800373Debugging init
374--------------
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375By default, programs executed by init will drop stdout and stderr into
376/dev/null. To help with debugging, you can execute your program via the
Elliott Hughesf682b472015-02-06 12:19:48 -0800377Android program logwrapper. This will redirect stdout/stderr into the
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378Android logging system (accessed via logcat).
379
380For example
381service akmd /system/bin/logwrapper /sbin/akmd
Elliott Hughesf682b472015-02-06 12:19:48 -0800382
383For quicker turnaround when working on init itself, use:
384
Elliott Hughes841b2632015-02-12 14:28:54 -0800385 mm -j
Elliott Hughesf682b472015-02-06 12:19:48 -0800386 m ramdisk-nodeps
387 m bootimage-nodeps
388 adb reboot bootloader
389 fastboot boot $ANDROID_PRODUCT_OUT/boot.img
390
391Alternatively, use the emulator:
392
393 emulator -partition-size 1024 -verbose -show-kernel -no-window
394
395You might want to call klog_set_level(6) after the klog_init() call
396so you see the kernel logging in dmesg (or the emulator output).