blob: 8161858bfb1205da3e2af77594b5affab1c533cb [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 Hughes8d82ea02015-02-06 20:15:18 -0800140exec [ <seclabel> [ <user> [ <group> ]* ] ] -- <command> [ <argument> ]*
141 Fork and execute command with the given arguments. The command starts
142 after "--" so that an optional security context, user, and supplementary
143 groups can be provided. No other commands will be run until this one
144 finishes.
San Mehat429721c2014-09-23 07:48:47 -0700145
146execonce <path> [ <argument> ]*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700147 Fork and execute a program (<path>). This will block until
San Mehat429721c2014-09-23 07:48:47 -0700148 the program completes execution. This command can be run at most
149 once during init's lifetime. Subsequent invocations are ignored.
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800150 It is best to avoid execonce as unlike the builtin commands, it runs
San Mehat429721c2014-09-23 07:48:47 -0700151 the risk of getting init "stuck".
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152
153export <name> <value>
154 Set the environment variable <name> equal to <value> in the
155 global environment (which will be inherited by all processes
156 started after this command is executed)
157
158ifup <interface>
159 Bring the network interface <interface> online.
160
161import <filename>
162 Parse an init config file, extending the current configuration.
163
164hostname <name>
165 Set the host name.
166
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000167chdir <directory>
168 Change working directory.
169
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170chmod <octal-mode> <path>
171 Change file access permissions.
172
173chown <owner> <group> <path>
174 Change file owner and group.
175
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000176chroot <directory>
177 Change process root directory.
178
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179class_start <serviceclass>
180 Start all services of the specified class if they are
181 not already running.
182
183class_stop <serviceclass>
184 Stop all services of the specified class if they are
185 currently running.
186
187domainname <name>
188 Set the domain name.
189
JP Abgrall3beec7e2014-05-02 21:14:29 -0700190enable <servicename>
191 Turns a disabled service into an enabled one as if the service did not
192 specify disabled.
193 If the service is supposed to be running, it will be started now.
194 Typically used when the bootloader sets a variable that indicates a specific
195 service should be started when needed. E.g.
196 on property:ro.boot.myfancyhardware=1
197 enable my_fancy_service_for_my_fancy_hardware
198
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199insmod <path>
200 Install the module at <path>
201
Elliott Hughesf682b472015-02-06 12:19:48 -0800202loglevel <level>
203 Sets the kernel log level to level. Properties are expanded within <level>.
204
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205mkdir <path> [mode] [owner] [group]
206 Create a directory at <path>, optionally with the given mode, owner, and
207 group. If not provided, the directory is created with permissions 755 and
208 owned by the root user and root group.
209
210mount <type> <device> <dir> [ <mountoption> ]*
211 Attempt to mount the named device at the directory <dir>
212 <device> may be of the form mtd@name to specify a mtd block
213 device by name.
214 <mountoption>s include "ro", "rw", "remount", "noatime", ...
215
Stephen Smalley726e8f72013-10-09 16:02:09 -0400216restorecon <path> [ <path> ]*
Stephen Smalley3fb61102012-11-02 15:22:34 -0400217 Restore the file named by <path> to the security context specified
218 in the file_contexts configuration.
219 Not required for directories created by the init.rc as these are
220 automatically labeled correctly by init.
221
Stephen Smalley726e8f72013-10-09 16:02:09 -0400222restorecon_recursive <path> [ <path> ]*
223 Recursively restore the directory tree named by <path> to the
224 security contexts specified in the file_contexts configuration.
Stephen Smalley726e8f72013-10-09 16:02:09 -0400225
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800226setcon <seclabel>
Stephen Smalley3fb61102012-11-02 15:22:34 -0400227 Set the current process security context to the specified string.
228 This is typically only used from early-init to set the init context
229 before any other process is started.
230
231setenforce 0|1
232 Set the SELinux system-wide enforcing status.
233 0 is permissive (i.e. log but do not deny), 1 is enforcing.
234
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235setkey
236 TBD
237
238setprop <name> <value>
Elliott Hughesf682b472015-02-06 12:19:48 -0800239 Set system property <name> to <value>. Properties are expanded
240 within <value>.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241
242setrlimit <resource> <cur> <max>
243 Set the rlimit for a resource.
244
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500245setsebool <name> <value>
Stephen Smalley3fb61102012-11-02 15:22:34 -0400246 Set SELinux boolean <name> to <value>.
247 <value> may be 1|true|on or 0|false|off
248
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700249start <service>
250 Start a service running if it is not already running.
251
252stop <service>
253 Stop a service from running if it is currently running.
254
255symlink <target> <path>
256 Create a symbolic link at <path> with the value <target>
257
The Android Open Source Project35237d12008-12-17 18:08:08 -0800258sysclktz <mins_west_of_gmt>
259 Set the system clock base (0 if system clock ticks in GMT)
260
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261trigger <event>
262 Trigger an event. Used to queue an action from another
263 action.
264
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800265wait <path> [ <timeout> ]
266 Poll for the existence of the given file and return when found,
267 or the timeout has been reached. If timeout is not specified it
268 currently defaults to five seconds.
269
Elliott Hughesf682b472015-02-06 12:19:48 -0800270write <path> <content>
271 Open the file at <path> and write a string to it with write(2).
272 If the file does not exist, it will be created. If it does exist,
273 it will be truncated. Properties are expanded within <content>.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274
275
276Properties
277----------
278Init updates some system properties to provide some insight into
279what it's doing:
280
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800281init.action
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282 Equal to the name of the action currently being executed or "" if none
283
284init.command
285 Equal to the command being executed or "" if none.
286
287init.svc.<name>
288 State of a named service ("stopped", "running", "restarting")
289
290
Elliott Hughes841b2632015-02-12 14:28:54 -0800291Bootcharting
292------------
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700293
Elliott Hughes841b2632015-02-12 14:28:54 -0800294This version of init contains code to perform "bootcharting": generating log
295files that can be later processed by the tools provided by www.bootchart.org.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296
Elliott Hughes841b2632015-02-12 14:28:54 -0800297On the emulator, use the new -bootchart <timeout> option to boot with
298bootcharting activated for <timeout> seconds.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299
Elliott Hughes841b2632015-02-12 14:28:54 -0800300On a device, create /data/bootchart/start with a command like the following:
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301
Elliott Hughes841b2632015-02-12 14:28:54 -0800302 adb shell 'echo $TIMEOUT > /data/bootchart/start'
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303
Elliott Hughes841b2632015-02-12 14:28:54 -0800304Where the value of $TIMEOUT corresponds to the desired bootcharted period in
305seconds. Bootcharting will stop after that many seconds have elapsed.
306You can also stop the bootcharting at any moment by doing the following:
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307
Elliott Hughes841b2632015-02-12 14:28:54 -0800308 adb shell 'echo 1 > /data/bootchart/stop'
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309
Elliott Hughes841b2632015-02-12 14:28:54 -0800310Note that /data/bootchart/stop is deleted automatically by init at the end of
311the bootcharting. This is not the case with /data/bootchart/start, so don't
312forget to delete it when you're done collecting data.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313
Elliott Hughes841b2632015-02-12 14:28:54 -0800314The log files are written to /data/bootchart/. A script is provided to
315retrieve them and create a bootchart.tgz file that can be used with the
316bootchart command-line utility:
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317
Elliott Hughes841b2632015-02-12 14:28:54 -0800318 sudo apt-get install pybootchartgui
Mark Salyzynd4e5c322015-02-24 08:28:07 -0800319 ANDROID_SERIAL=<device serial number>
Elliott Hughes841b2632015-02-12 14:28:54 -0800320 $ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322
Elliott Hughes841b2632015-02-12 14:28:54 -0800323Debugging init
324--------------
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325By default, programs executed by init will drop stdout and stderr into
326/dev/null. To help with debugging, you can execute your program via the
Elliott Hughesf682b472015-02-06 12:19:48 -0800327Android program logwrapper. This will redirect stdout/stderr into the
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328Android logging system (accessed via logcat).
329
330For example
331service akmd /system/bin/logwrapper /sbin/akmd
Elliott Hughesf682b472015-02-06 12:19:48 -0800332
333For quicker turnaround when working on init itself, use:
334
Elliott Hughes841b2632015-02-12 14:28:54 -0800335 mm -j
Elliott Hughesf682b472015-02-06 12:19:48 -0800336 m ramdisk-nodeps
337 m bootimage-nodeps
338 adb reboot bootloader
339 fastboot boot $ANDROID_PRODUCT_OUT/boot.img
340
341Alternatively, use the emulator:
342
343 emulator -partition-size 1024 -verbose -show-kernel -no-window
344
345You might want to call klog_set_level(6) after the klog_init() call
346so you see the kernel logging in dmesg (or the emulator output).