The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | Android Init Language |
| 2 | --------------------- |
| 3 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 4 | The Android Init Language consists of five broad classes of statements, |
| 5 | which are Actions, Commands, Services, Options, and Imports. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 6 | |
| 7 | All of these are line-oriented, consisting of tokens separated by |
| 8 | whitespace. The c-style backslash escapes may be used to insert |
| 9 | whitespace into a token. Double quotes may also be used to prevent |
| 10 | whitespace from breaking text into multiple tokens. The backslash, |
| 11 | when it is the last character on a line, may be used for line-folding. |
| 12 | |
| 13 | Lines which start with a # (leading whitespace allowed) are comments. |
| 14 | |
| 15 | Actions and Services implicitly declare a new section. All commands |
| 16 | or options belong to the section most recently declared. Commands |
| 17 | or options before the first section are ignored. |
| 18 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 19 | Actions and Services have unique names. If a second Action is defined |
| 20 | with the same name as an existing one, its commands are appended to |
| 21 | the commands of the existing action. If a second Service is defined |
| 22 | with the same name as an existing one, it is ignored and an error |
| 23 | message is logged. |
| 24 | |
| 25 | |
| 26 | Init .rc Files |
| 27 | -------------- |
| 28 | The init language is used in plaintext files that take the .rc file |
| 29 | extension. These are typically multiple of these in multiple |
| 30 | locations on the system, described below. |
| 31 | |
| 32 | /init.rc is the primary .rc file and is loaded by the init executable |
| 33 | at the beginning of its execution. It is responsible for the initial |
| 34 | set up of the system. It imports /init.${ro.hardware}.rc which is the |
| 35 | primary vendor supplied .rc file. |
| 36 | |
| 37 | During the mount_all command, the init executable loads all of the |
| 38 | files contained within the /{system,vendor,odm}/etc/init/ directories. |
| 39 | These directories are intended for all Actions and Services used after |
| 40 | file system mounting. |
| 41 | |
Tom Cherry | 1fb20b8 | 2016-01-20 16:53:08 -0800 | [diff] [blame] | 42 | One may specify paths in the mount_all command line to have it import |
| 43 | .rc files at the specified paths instead of the default ones listed above. |
| 44 | This is primarily for supporting factory mode and other non-standard boot |
| 45 | modes. The three default paths should be used for the normal boot process. |
| 46 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 47 | The intention of these directories is as follows |
| 48 | 1) /system/etc/init/ is for core system items such as |
Tom Cherry | 1fb20b8 | 2016-01-20 16:53:08 -0800 | [diff] [blame] | 49 | SurfaceFlinger, MediaService, and logcatd. |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 50 | 2) /vendor/etc/init/ is for SoC vendor items such as actions or |
| 51 | daemons needed for core SoC functionality. |
| 52 | 3) /odm/etc/init/ is for device manufacturer items such as |
| 53 | actions or daemons needed for motion sensor or other peripheral |
| 54 | functionality. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | |
Tom Cherry | 1fb20b8 | 2016-01-20 16:53:08 -0800 | [diff] [blame] | 56 | All services whose binaries reside on the system, vendor, or odm |
| 57 | partitions should have their service entries placed into a |
| 58 | corresponding init .rc file, located in the /etc/init/ |
| 59 | directory of the partition where they reside. There is a build |
| 60 | system macro, LOCAL_INIT_RC, that handles this for developers. Each |
| 61 | init .rc file should additionally contain any actions associated with |
| 62 | its service. |
| 63 | |
| 64 | An example is the logcatd.rc and Android.mk files located in the |
| 65 | system/core/logcat directory. The LOCAL_INIT_RC macro in the |
| 66 | Android.mk file places logcatd.rc in /system/etc/init/ during the |
| 67 | build process. Init loads logcatd.rc during the mount_all command and |
| 68 | allows the service to be run and the action to be queued when |
| 69 | appropriate. |
| 70 | |
| 71 | This break up of init .rc files according to their daemon is preferred |
| 72 | to the previously used monolithic init .rc files. This approach |
| 73 | ensures that the only service entries that init reads and the only |
| 74 | actions that init performs correspond to services whose binaries are in |
| 75 | fact present on the file system, which was not the case with the |
| 76 | monolithic init .rc files. This additionally will aid in merge |
| 77 | conflict resolution when multiple services are added to the system, as |
| 78 | each one will go into a separate file. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 80 | There are two options "early" and "late" in mount_all command |
| 81 | which can be set after optional paths. With "--early" set, the |
| 82 | init executable will skip mounting entries with "latemount" flag |
| 83 | and triggering fs encryption state event. With "--late" set, |
| 84 | init executable will only mount entries with "latemount" flag but skip |
| 85 | importing rc files. By default, no option is set, and mount_all will |
| 86 | mount_all will process all entries in the given fstab. |
| 87 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | Actions |
| 89 | ------- |
| 90 | Actions are named sequences of commands. Actions have a trigger which |
| 91 | is used to determine when the action should occur. When an event |
| 92 | occurs which matches an action's trigger, that action is added to |
| 93 | the tail of a to-be-executed queue (unless it is already on the |
| 94 | queue). |
| 95 | |
| 96 | Each action in the queue is dequeued in sequence and each command in |
| 97 | that action is executed in sequence. Init handles other activities |
| 98 | (device creation/destruction, property setting, process restarting) |
| 99 | "between" the execution of the commands in activities. |
| 100 | |
| 101 | Actions take the form of: |
| 102 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 103 | on <trigger> [&& <trigger>]* |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | <command> |
| 105 | <command> |
| 106 | <command> |
| 107 | |
| 108 | |
| 109 | Services |
| 110 | -------- |
| 111 | Services are programs which init launches and (optionally) restarts |
| 112 | when they exit. Services take the form of: |
| 113 | |
| 114 | service <name> <pathname> [ <argument> ]* |
| 115 | <option> |
| 116 | <option> |
| 117 | ... |
| 118 | |
| 119 | |
| 120 | Options |
| 121 | ------- |
| 122 | Options are modifiers to services. They affect how and when init |
| 123 | runs the service. |
| 124 | |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 125 | console [<console>] |
| 126 | This service needs a console. The optional second parameter chooses a |
| 127 | specific console instead of the default. The default "/dev/console" can |
| 128 | be changed by setting the "androidboot.console" kernel parameter. In |
| 129 | all cases the leading "/dev/" should be omitted, so "/dev/tty0" would be |
| 130 | specified as just "console tty0". |
| 131 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | critical |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 133 | This is a device-critical service. If it exits more than four times in |
| 134 | four minutes, the device will reboot into recovery mode. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | |
| 136 | disabled |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 137 | This service will not automatically start with its class. |
| 138 | It must be explicitly started by name. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | |
| 140 | setenv <name> <value> |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 141 | Set the environment variable <name> to <value> in the launched process. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 143 | socket <name> <type> <perm> [ <user> [ <group> [ <seclabel> ] ] ] |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 144 | Create a unix domain socket named /dev/socket/<name> and pass its fd to the |
| 145 | launched process. <type> must be "dgram", "stream" or "seqpacket". User and |
| 146 | group default to 0. 'seclabel' is the SELinux security context for the |
| 147 | socket. It defaults to the service security context, as specified by |
| 148 | seclabel or computed based on the service executable file security context. |
| 149 | For native executables see libcutils android_get_control_socket(). |
| 150 | |
Mark Salyzyn | 978fd0e | 2016-12-02 08:05:22 -0800 | [diff] [blame] | 151 | file <path> <type> |
| 152 | Open a file path and pass its fd to the launched process. <type> must be |
| 153 | "r", "w" or "rw". For native executables see libcutils |
| 154 | android_get_control_file(). |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | |
| 156 | user <username> |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 157 | Change to 'username' before exec'ing this service. |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 158 | Currently defaults to root. (??? probably should default to nobody) |
Tom Cherry | dbddb40 | 2015-12-11 12:54:50 -0800 | [diff] [blame] | 159 | As of Android M, processes should use this option even if they |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 160 | require Linux capabilities. Previously, to acquire Linux |
Tom Cherry | dbddb40 | 2015-12-11 12:54:50 -0800 | [diff] [blame] | 161 | capabilities, a process would need to run as root, request the |
| 162 | capabilities, then drop to its desired uid. There is a new |
| 163 | mechanism through fs_config that allows device manufacturers to add |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 164 | Linux capabilities to specific binaries on a file system that should |
Tom Cherry | dbddb40 | 2015-12-11 12:54:50 -0800 | [diff] [blame] | 165 | be used instead. This mechanism is described on |
| 166 | http://source.android.com/devices/tech/config/filesystem.html. When |
| 167 | using this new mechanism, processes can use the user option to |
| 168 | select their desired uid without ever running as root. |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 169 | As of Android O, processes can also request capabilities directly in their .rc |
| 170 | files. See the "capabilities" option below. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | |
| 172 | group <groupname> [ <groupname> ]* |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 173 | Change to 'groupname' before exec'ing this service. Additional |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 174 | groupnames beyond the (required) first one are used to set the |
| 175 | supplemental groups of the process (via setgroups()). |
| 176 | Currently defaults to root. (??? probably should default to nobody) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 178 | capabilities <capability> [ <capability> ]* |
| 179 | Set capabilities when exec'ing this service. 'capability' should be a Linux |
| 180 | capability without the "CAP_" prefix, like "NET_ADMIN" or "SETPCAP". See |
| 181 | http://man7.org/linux/man-pages/man7/capabilities.7.html for a list of Linux |
| 182 | capabilities. |
| 183 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 184 | seclabel <seclabel> |
| 185 | Change to 'seclabel' before exec'ing this service. |
Stephen Smalley | 3fb6110 | 2012-11-02 15:22:34 -0400 | [diff] [blame] | 186 | Primarily for use by services run from the rootfs, e.g. ueventd, adbd. |
| 187 | Services on the system partition can instead use policy-defined transitions |
| 188 | based on their file security context. |
| 189 | If not specified and no transition is defined in policy, defaults to the init context. |
| 190 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | oneshot |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 192 | Do not restart the service when it exits. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | |
| 194 | class <name> |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 195 | Specify a class name for the service. All services in a |
| 196 | named class may be started or stopped together. A service |
| 197 | is in the class "default" if one is not specified via the |
| 198 | class option. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 199 | |
| 200 | onrestart |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 201 | Execute a Command (see below) when service restarts. |
| 202 | |
| 203 | writepid <file...> |
| 204 | Write the child's pid to the given files when it forks. Meant for |
| 205 | cgroup/cpuset usage. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 206 | |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 207 | priority <priority> |
| 208 | Scheduling priority of the service process. This value has to be in range |
| 209 | -20 to 19. Default priority is 0. Priority is set via setpriority(). |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 210 | |
Jorge Lucangeli Obes | cd2518c | 2016-08-31 15:23:44 -0400 | [diff] [blame] | 211 | namespace <pid|mnt> |
| 212 | Enter a new PID or mount namespace when forking the service. |
| 213 | |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 214 | oom_score_adjust <value> |
| 215 | Sets the child's /proc/self/oom_score_adj to the specified value, |
| 216 | which must range from -1000 to 1000. |
| 217 | |
Jorge Lucangeli Obes | cd2518c | 2016-08-31 15:23:44 -0400 | [diff] [blame] | 218 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | Triggers |
| 220 | -------- |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 221 | Triggers are strings which can be used to match certain kinds of |
| 222 | events and used to cause an action to occur. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 223 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 224 | Triggers are subdivided into event triggers and property triggers. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 226 | Event triggers are strings triggered by the 'trigger' command or by |
| 227 | the QueueEventTrigger() function within the init executable. These |
| 228 | take the form of a simple string such as 'boot' or 'late-init'. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 230 | Property triggers are strings triggered when a named property changes |
| 231 | value to a given new value or when a named property changes value to |
| 232 | any new value. These take the form of 'property:<name>=<value>' and |
| 233 | 'property:<name>=*' respectively. Property triggers are additionally |
| 234 | evaluated and triggered accordingly during the initial boot phase of |
| 235 | init. |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 236 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 237 | An Action can have multiple property triggers but may only have one |
| 238 | event trigger. |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 239 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 240 | For example: |
| 241 | 'on boot && property:a=b' defines an action that is only executed when |
| 242 | the 'boot' event trigger happens and the property a equals b. |
| 243 | |
| 244 | 'on property:a=b && property:c=d' defines an action that is executed |
| 245 | at three times, |
| 246 | 1) During initial boot if property a=b and property c=d |
| 247 | 2) Any time that property a transitions to value b, while property |
| 248 | c already equals d. |
| 249 | 3) Any time that property c transitions to value d, while property |
| 250 | a already equals b. |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 251 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 252 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | Commands |
| 254 | -------- |
| 255 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 256 | bootchart_init |
| 257 | Start bootcharting if configured (see below). |
| 258 | This is included in the default init.rc. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | |
| 260 | chmod <octal-mode> <path> |
| 261 | Change file access permissions. |
| 262 | |
| 263 | chown <owner> <group> <path> |
| 264 | Change file owner and group. |
| 265 | |
| 266 | class_start <serviceclass> |
| 267 | Start all services of the specified class if they are |
| 268 | not already running. |
| 269 | |
| 270 | class_stop <serviceclass> |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 271 | Stop and disable all services of the specified class if they are |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 272 | currently running. |
| 273 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 274 | class_reset <serviceclass> |
| 275 | Stop all services of the specified class if they are |
| 276 | currently running, without disabling them. They can be restarted |
| 277 | later using class_start. |
| 278 | |
| 279 | copy <src> <dst> |
| 280 | Copies a file. Similar to write, but useful for binary/large |
| 281 | amounts of data. |
| 282 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | domainname <name> |
| 284 | Set the domain name. |
| 285 | |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 286 | enable <servicename> |
| 287 | Turns a disabled service into an enabled one as if the service did not |
| 288 | specify disabled. |
| 289 | If the service is supposed to be running, it will be started now. |
| 290 | Typically used when the bootloader sets a variable that indicates a specific |
| 291 | service should be started when needed. E.g. |
| 292 | on property:ro.boot.myfancyhardware=1 |
| 293 | enable my_fancy_service_for_my_fancy_hardware |
| 294 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 295 | exec [ <seclabel> [ <user> [ <group> ]* ] ] -- <command> [ <argument> ]* |
| 296 | Fork and execute command with the given arguments. The command starts |
| 297 | after "--" so that an optional security context, user, and supplementary |
| 298 | groups can be provided. No other commands will be run until this one |
Mark Salyzyn | 17fff89 | 2015-06-02 11:11:02 -0700 | [diff] [blame] | 299 | finishes. <seclabel> can be a - to denote default. |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 300 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 301 | export <name> <value> |
| 302 | Set the environment variable <name> equal to <value> in the |
| 303 | global environment (which will be inherited by all processes |
| 304 | started after this command is executed) |
| 305 | |
| 306 | hostname <name> |
| 307 | Set the host name. |
| 308 | |
| 309 | ifup <interface> |
| 310 | Bring the network interface <interface> online. |
| 311 | |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 312 | insmod [-f] <path> [<options>] |
| 313 | Install the module at <path> with the specified options. |
| 314 | -f |
| 315 | Force installation of the module even if the version of the running kernel |
| 316 | and the version of the kernel for which the module was compiled do not match. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 317 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 318 | load_all_props |
| 319 | Loads properties from /system, /vendor, et cetera. |
| 320 | This is included in the default init.rc. |
| 321 | |
| 322 | load_persist_props |
| 323 | Loads persistent properties when /data has been decrypted. |
| 324 | This is included in the default init.rc. |
| 325 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 326 | loglevel <level> |
| 327 | Sets the kernel log level to level. Properties are expanded within <level>. |
| 328 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 329 | mkdir <path> [mode] [owner] [group] |
| 330 | Create a directory at <path>, optionally with the given mode, owner, and |
| 331 | group. If not provided, the directory is created with permissions 755 and |
Johan Redestig | 0b42ba2 | 2015-03-15 17:39:41 +0100 | [diff] [blame] | 332 | owned by the root user and root group. If provided, the mode, owner and group |
| 333 | will be updated if the directory exists already. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 334 | |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 335 | mount_all <fstab> [ <path> ]* [--<option>] |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 336 | Calls fs_mgr_mount_all on the given fs_mgr-format fstab and imports .rc files |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 337 | at the specified paths (e.g., on the partitions just mounted) with optional |
| 338 | options "early" and "late". |
| 339 | Refer to the section of "Init .rc Files" for detail. |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 340 | |
Niklas Tibbling | bc3f69f | 2012-08-01 13:15:38 +0200 | [diff] [blame] | 341 | mount <type> <device> <dir> [ <flag> ]* [<options>] |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | Attempt to mount the named device at the directory <dir> |
Niklas Tibbling | bc3f69f | 2012-08-01 13:15:38 +0200 | [diff] [blame] | 343 | <flag>s include "ro", "rw", "remount", "noatime", ... |
| 344 | <options> include "barrier=1", "noauto_da_alloc", "discard", ... as |
| 345 | a comma separated string, eg: barrier=1,noauto_da_alloc |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 346 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 347 | powerctl |
| 348 | Internal implementation detail used to respond to changes to the |
| 349 | "sys.powerctl" system property, used to implement rebooting. |
| 350 | |
| 351 | restart <service> |
| 352 | Like stop, but doesn't disable the service. |
| 353 | |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 354 | restorecon <path> [ <path> ]* |
Stephen Smalley | 3fb6110 | 2012-11-02 15:22:34 -0400 | [diff] [blame] | 355 | Restore the file named by <path> to the security context specified |
| 356 | in the file_contexts configuration. |
| 357 | Not required for directories created by the init.rc as these are |
| 358 | automatically labeled correctly by init. |
| 359 | |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 360 | restorecon_recursive <path> [ <path> ]* |
| 361 | Recursively restore the directory tree named by <path> to the |
| 362 | security contexts specified in the file_contexts configuration. |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 363 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 364 | rm <path> |
| 365 | Calls unlink(2) on the given path. You might want to |
| 366 | use "exec -- rm ..." instead (provided the system partition is |
| 367 | already mounted). |
| 368 | |
| 369 | rmdir <path> |
| 370 | Calls rmdir(2) on the given path. |
| 371 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 372 | setprop <name> <value> |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 373 | Set system property <name> to <value>. Properties are expanded |
| 374 | within <value>. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 375 | |
| 376 | setrlimit <resource> <cur> <max> |
| 377 | Set the rlimit for a resource. |
| 378 | |
| 379 | start <service> |
| 380 | Start a service running if it is not already running. |
| 381 | |
| 382 | stop <service> |
| 383 | Stop a service from running if it is currently running. |
| 384 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 385 | swapon_all <fstab> |
| 386 | Calls fs_mgr_swapon_all on the given fstab file. |
| 387 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 388 | symlink <target> <path> |
| 389 | Create a symbolic link at <path> with the value <target> |
| 390 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 391 | sysclktz <mins_west_of_gmt> |
| 392 | Set the system clock base (0 if system clock ticks in GMT) |
| 393 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 394 | trigger <event> |
| 395 | Trigger an event. Used to queue an action from another |
| 396 | action. |
| 397 | |
Alex Light | 68ab20f | 2016-06-23 11:11:39 -0700 | [diff] [blame] | 398 | umount <path> |
| 399 | Unmount the filesystem mounted at that path. |
| 400 | |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 401 | verity_load_state |
| 402 | Internal implementation detail used to load dm-verity state. |
| 403 | |
| 404 | verity_update_state <mount_point> |
| 405 | Internal implementation detail used to update dm-verity state and |
| 406 | set the partition.<mount_point>.verified properties used by adb remount |
| 407 | because fs_mgr can't set them directly itself. |
| 408 | |
Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 409 | wait <path> [ <timeout> ] |
Elliott Hughes | 91a3be5 | 2015-03-20 11:00:15 -0700 | [diff] [blame] | 410 | Poll for the existence of the given file and return when found, |
| 411 | or the timeout has been reached. If timeout is not specified it |
| 412 | currently defaults to five seconds. |
Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 413 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 414 | write <path> <content> |
| 415 | Open the file at <path> and write a string to it with write(2). |
| 416 | If the file does not exist, it will be created. If it does exist, |
| 417 | it will be truncated. Properties are expanded within <content>. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 418 | |
| 419 | |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 420 | Imports |
| 421 | ------- |
| 422 | The import keyword is not a command, but rather its own section and is |
| 423 | handled immediately after the .rc file that contains it has finished |
| 424 | being parsed. It takes the below form: |
| 425 | |
| 426 | import <path> |
| 427 | Parse an init config file, extending the current configuration. |
| 428 | If <path> is a directory, each file in the directory is parsed as |
| 429 | a config file. It is not recursive, nested directories will |
| 430 | not be parsed. |
| 431 | |
| 432 | There are only two times where the init executable imports .rc files, |
| 433 | 1) When it imports /init.rc during initial boot |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 434 | 2) When it imports /{system,vendor,odm}/etc/init/ or .rc files at specified |
| 435 | paths during mount_all |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 436 | |
| 437 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 438 | Properties |
| 439 | ---------- |
Tom Cherry | 3be66ed | 2015-09-01 14:49:38 -0700 | [diff] [blame] | 440 | Init provides information about the services that it is responsible |
| 441 | for via the below properties. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 442 | |
| 443 | init.svc.<name> |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 444 | State of a named service ("stopped", "stopping", "running", "restarting") |
| 445 | |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 446 | |
| 447 | Boot timing |
| 448 | ----------- |
| 449 | Init records some boot timing information in system properties. |
| 450 | |
| 451 | ro.boottime.init |
| 452 | Time after boot in ns (via the CLOCK_BOOTTIME clock) at which the first |
| 453 | stage of init started. |
| 454 | |
| 455 | ro.boottime.init.selinux |
| 456 | How long it took the first stage to initialize SELinux. |
| 457 | |
| 458 | ro.boottime.init.cold_boot_wait |
| 459 | How long init waited for ueventd's coldboot phase to end. |
| 460 | |
| 461 | ro.boottime.<service-name> |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 462 | Time after boot in ns (via the CLOCK_BOOTTIME clock) that the service was |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 463 | first started. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 464 | |
| 465 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 466 | Bootcharting |
| 467 | ------------ |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 468 | This version of init contains code to perform "bootcharting": generating log |
| 469 | files that can be later processed by the tools provided by www.bootchart.org. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 470 | |
Elliott Hughes | 59abac2 | 2015-03-28 12:12:51 -0700 | [diff] [blame] | 471 | On the emulator, use the -bootchart <timeout> option to boot with bootcharting |
| 472 | activated for <timeout> seconds. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 473 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 474 | On a device, create /data/bootchart/start with a command like the following: |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 475 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 476 | adb shell 'echo $TIMEOUT > /data/bootchart/start' |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 477 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 478 | Where the value of $TIMEOUT corresponds to the desired bootcharted period in |
| 479 | seconds. Bootcharting will stop after that many seconds have elapsed. |
| 480 | You can also stop the bootcharting at any moment by doing the following: |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 481 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 482 | adb shell 'echo 1 > /data/bootchart/stop' |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 483 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 484 | Note that /data/bootchart/stop is deleted automatically by init at the end of |
| 485 | the bootcharting. This is not the case with /data/bootchart/start, so don't |
| 486 | forget to delete it when you're done collecting data. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 487 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 488 | The log files are written to /data/bootchart/. A script is provided to |
| 489 | retrieve them and create a bootchart.tgz file that can be used with the |
| 490 | bootchart command-line utility: |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 491 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 492 | sudo apt-get install pybootchartgui |
Elliott Hughes | 59abac2 | 2015-03-28 12:12:51 -0700 | [diff] [blame] | 493 | # grab-bootchart.sh uses $ANDROID_SERIAL. |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 494 | $ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 495 | |
Elliott Hughes | 59abac2 | 2015-03-28 12:12:51 -0700 | [diff] [blame] | 496 | One thing to watch for is that the bootchart will show init as if it started |
| 497 | running at 0s. You'll have to look at dmesg to work out when the kernel |
| 498 | actually started init. |
| 499 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 500 | |
Ben Cheng | 50bbde0 | 2015-06-09 17:04:36 +0800 | [diff] [blame] | 501 | Comparing two bootcharts |
| 502 | ------------------------ |
| 503 | A handy script named compare-bootcharts.py can be used to compare the |
| 504 | start/end time of selected processes. The aforementioned grab-bootchart.sh |
| 505 | will leave a bootchart tarball named bootchart.tgz at /tmp/android-bootchart. |
| 506 | If two such barballs are preserved on the host machine under different |
| 507 | directories, the script can list the timestamps differences. For example: |
| 508 | |
| 509 | Usage: system/core/init/compare-bootcharts.py base_bootchart_dir |
| 510 | exp_bootchart_dir |
| 511 | |
| 512 | process: baseline experiment (delta) |
| 513 | - Unit is ms (a jiffy is 10 ms on the system) |
| 514 | ------------------------------------ |
| 515 | /init: 50 40 (-10) |
| 516 | /system/bin/surfaceflinger: 4320 4470 (+150) |
| 517 | /system/bin/bootanimation: 6980 6990 (+10) |
| 518 | zygote64: 10410 10640 (+230) |
| 519 | zygote: 10410 10640 (+230) |
| 520 | system_server: 15350 15150 (-200) |
| 521 | bootanimation ends at: 33790 31230 (-2560) |
| 522 | |
| 523 | |
Yasuhiro Matsuda | f93db4b | 2015-06-15 18:49:35 +0900 | [diff] [blame] | 524 | Systrace |
| 525 | -------- |
| 526 | Systrace [1] can be used for obtaining performance analysis reports during boot |
| 527 | time on userdebug or eng builds. |
| 528 | Here is an example of trace events of "wm" and "am" categories: |
| 529 | |
| 530 | $ANDROID_BUILD_TOP/external/chromium-trace/systrace.py wm am --boot |
| 531 | |
| 532 | This command will cause the device to reboot. After the device is rebooted and |
| 533 | the boot sequence has finished, the trace report is obtained from the device |
| 534 | and written as trace.html on the host by hitting Ctrl+C. |
| 535 | |
| 536 | LIMITATION |
| 537 | Recording trace events is started after persistent properties are loaded, so |
| 538 | the trace events that are emitted before that are not recorded. Several |
| 539 | services such as vold, surfaceflinger, and servicemanager are affected by this |
| 540 | limitation since they are started before persistent properties are loaded. |
| 541 | Zygote initialization and the processes that are forked from the zygote are not |
| 542 | affected. |
| 543 | |
| 544 | [1] http://developer.android.com/tools/help/systrace.html |
| 545 | |
| 546 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 547 | Debugging init |
| 548 | -------------- |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 549 | By default, programs executed by init will drop stdout and stderr into |
| 550 | /dev/null. To help with debugging, you can execute your program via the |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 551 | Android program logwrapper. This will redirect stdout/stderr into the |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 552 | Android logging system (accessed via logcat). |
| 553 | |
| 554 | For example |
| 555 | service akmd /system/bin/logwrapper /sbin/akmd |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 556 | |
| 557 | For quicker turnaround when working on init itself, use: |
| 558 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 559 | mm -j && |
| 560 | m ramdisk-nodeps && |
| 561 | m bootimage-nodeps && |
| 562 | adb reboot bootloader && |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 563 | fastboot boot $ANDROID_PRODUCT_OUT/boot.img |
| 564 | |
| 565 | Alternatively, use the emulator: |
| 566 | |
| 567 | emulator -partition-size 1024 -verbose -show-kernel -no-window |