blob: 16a91866be685a00414a0396c44d0e0ab4671868 [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
Stephen Smalley8348d272013-05-13 12:37:04 -040073socket <name> <type> <perm> [ <user> [ <group> [ <context> ] ] ]
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.
Stephen Smalley8348d272013-05-13 12:37:04 -040077 Context 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.
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
Stephen Smalley3fb61102012-11-02 15:22:34 -040094seclabel <securitycontext>
95 Change to securitycontext 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
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
113Triggers
114--------
115 Triggers are strings which can be used to match certain kinds
116 of events and used to cause an action to occur.
117
118boot
119 This is the first trigger that will occur when init starts
120 (after /init.conf is loaded)
121
122<name>=<value>
123 Triggers of this form occur when the property <name> is set
124 to the specific value <value>.
125
Elliott Hughesd3e37d12015-02-02 16:43:32 -0800126 One can also test multiple properties to execute a group
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700127 of commands. For example:
128
129 on property:test.a=1 && property:test.b=1
130 setprop test.c 1
131
132 The above stub sets test.c to 1 only when
133 both test.a=1 and test.b=1
134
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135Commands
136--------
137
138exec <path> [ <argument> ]*
San Mehat429721c2014-09-23 07:48:47 -0700139 This command is not implemented.
140
141execonce <path> [ <argument> ]*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142 Fork and execute a program (<path>). This will block until
San Mehat429721c2014-09-23 07:48:47 -0700143 the program completes execution. This command can be run at most
144 once during init's lifetime. Subsequent invocations are ignored.
145 It is best to avoid exec as unlike the builtin commands, it runs
146 the risk of getting init "stuck".
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700147
148export <name> <value>
149 Set the environment variable <name> equal to <value> in the
150 global environment (which will be inherited by all processes
151 started after this command is executed)
152
153ifup <interface>
154 Bring the network interface <interface> online.
155
156import <filename>
157 Parse an init config file, extending the current configuration.
158
159hostname <name>
160 Set the host name.
161
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000162chdir <directory>
163 Change working directory.
164
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700165chmod <octal-mode> <path>
166 Change file access permissions.
167
168chown <owner> <group> <path>
169 Change file owner and group.
170
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000171chroot <directory>
172 Change process root directory.
173
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700174class_start <serviceclass>
175 Start all services of the specified class if they are
176 not already running.
177
178class_stop <serviceclass>
179 Stop all services of the specified class if they are
180 currently running.
181
182domainname <name>
183 Set the domain name.
184
JP Abgrall3beec7e2014-05-02 21:14:29 -0700185enable <servicename>
186 Turns a disabled service into an enabled one as if the service did not
187 specify disabled.
188 If the service is supposed to be running, it will be started now.
189 Typically used when the bootloader sets a variable that indicates a specific
190 service should be started when needed. E.g.
191 on property:ro.boot.myfancyhardware=1
192 enable my_fancy_service_for_my_fancy_hardware
193
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194insmod <path>
195 Install the module at <path>
196
Elliott Hughesf682b472015-02-06 12:19:48 -0800197loglevel <level>
198 Sets the kernel log level to level. Properties are expanded within <level>.
199
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200mkdir <path> [mode] [owner] [group]
201 Create a directory at <path>, optionally with the given mode, owner, and
202 group. If not provided, the directory is created with permissions 755 and
203 owned by the root user and root group.
204
205mount <type> <device> <dir> [ <mountoption> ]*
206 Attempt to mount the named device at the directory <dir>
207 <device> may be of the form mtd@name to specify a mtd block
208 device by name.
209 <mountoption>s include "ro", "rw", "remount", "noatime", ...
210
Stephen Smalley726e8f72013-10-09 16:02:09 -0400211restorecon <path> [ <path> ]*
Stephen Smalley3fb61102012-11-02 15:22:34 -0400212 Restore the file named by <path> to the security context specified
213 in the file_contexts configuration.
214 Not required for directories created by the init.rc as these are
215 automatically labeled correctly by init.
216
Stephen Smalley726e8f72013-10-09 16:02:09 -0400217restorecon_recursive <path> [ <path> ]*
218 Recursively restore the directory tree named by <path> to the
219 security contexts specified in the file_contexts configuration.
Stephen Smalley726e8f72013-10-09 16:02:09 -0400220
Stephen Smalley3fb61102012-11-02 15:22:34 -0400221setcon <securitycontext>
222 Set the current process security context to the specified string.
223 This is typically only used from early-init to set the init context
224 before any other process is started.
225
226setenforce 0|1
227 Set the SELinux system-wide enforcing status.
228 0 is permissive (i.e. log but do not deny), 1 is enforcing.
229
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230setkey
231 TBD
232
233setprop <name> <value>
Elliott Hughesf682b472015-02-06 12:19:48 -0800234 Set system property <name> to <value>. Properties are expanded
235 within <value>.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236
237setrlimit <resource> <cur> <max>
238 Set the rlimit for a resource.
239
Stephen Smalley0e23fee2012-11-28 13:52:12 -0500240setsebool <name> <value>
Stephen Smalley3fb61102012-11-02 15:22:34 -0400241 Set SELinux boolean <name> to <value>.
242 <value> may be 1|true|on or 0|false|off
243
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700244start <service>
245 Start a service running if it is not already running.
246
247stop <service>
248 Stop a service from running if it is currently running.
249
250symlink <target> <path>
251 Create a symbolic link at <path> with the value <target>
252
The Android Open Source Project35237d12008-12-17 18:08:08 -0800253sysclktz <mins_west_of_gmt>
254 Set the system clock base (0 if system clock ticks in GMT)
255
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700256trigger <event>
257 Trigger an event. Used to queue an action from another
258 action.
259
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800260wait <path> [ <timeout> ]
261 Poll for the existence of the given file and return when found,
262 or the timeout has been reached. If timeout is not specified it
263 currently defaults to five seconds.
264
Elliott Hughesf682b472015-02-06 12:19:48 -0800265write <path> <content>
266 Open the file at <path> and write a string to it with write(2).
267 If the file does not exist, it will be created. If it does exist,
268 it will be truncated. Properties are expanded within <content>.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700269
270
271Properties
272----------
273Init updates some system properties to provide some insight into
274what it's doing:
275
276init.action
277 Equal to the name of the action currently being executed or "" if none
278
279init.command
280 Equal to the command being executed or "" if none.
281
282init.svc.<name>
283 State of a named service ("stopped", "running", "restarting")
284
285
286Example init.conf
287-----------------
288
289# not complete -- just providing some examples of usage
290#
291on boot
292 export PATH /sbin:/system/sbin:/system/bin
293 export LD_LIBRARY_PATH /system/lib
294
295 mkdir /dev
296 mkdir /proc
297 mkdir /sys
298
299 mount tmpfs tmpfs /dev
300 mkdir /dev/pts
301 mkdir /dev/socket
302 mount devpts devpts /dev/pts
303 mount proc proc /proc
304 mount sysfs sysfs /sys
305
306 write /proc/cpu/alignment 4
307
308 ifup lo
309
310 hostname localhost
311 domainname localhost
312
313 mount yaffs2 mtd@system /system
314 mount yaffs2 mtd@userdata /data
315
316 import /system/etc/init.conf
317
318 class_start default
319
320service adbd /sbin/adbd
321 user adb
322 group adb
323
324service usbd /system/bin/usbd -r
325 user usbd
326 group usbd
327 socket usbd 666
328
329service zygote /system/bin/app_process -Xzygote /system/bin --zygote
330 socket zygote 666
331
332service runtime /system/bin/runtime
333 user system
334 group system
335
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336service akmd /sbin/akmd
337 disabled
338 user akmd
339 group akmd
340
341Debugging notes
342---------------
343By default, programs executed by init will drop stdout and stderr into
344/dev/null. To help with debugging, you can execute your program via the
Elliott Hughesf682b472015-02-06 12:19:48 -0800345Android program logwrapper. This will redirect stdout/stderr into the
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346Android logging system (accessed via logcat).
347
348For example
349service akmd /system/bin/logwrapper /sbin/akmd
Elliott Hughesf682b472015-02-06 12:19:48 -0800350
351For quicker turnaround when working on init itself, use:
352
353 mm
354 m ramdisk-nodeps
355 m bootimage-nodeps
356 adb reboot bootloader
357 fastboot boot $ANDROID_PRODUCT_OUT/boot.img
358
359Alternatively, use the emulator:
360
361 emulator -partition-size 1024 -verbose -show-kernel -no-window
362
363You might want to call klog_set_level(6) after the klog_init() call
364so you see the kernel logging in dmesg (or the emulator output).