版权声明:本文为博主原创文章,未经博主允许转载。 https://blog.csdn.net/jklinux/article/details/82597489
参考: android源码目录里的system/core/init/readme.txt.
Android系统里以*.rc为扩展名为系统初始化脚本,脚本里就是使用安卓初始化语言.
语句都是一行为一条语句,没有符号分隔. 语句里的每个词用空格隔开.
如: service ueventd /sbin/ueventd
备注语句以符号”#”作为注释.
语言基本上分为4个块: Actions Commands Services 和 Options.
每个Actions和Services关键词开始的语句作为一个新的区块.
Commands和Options就是属于就近的Actions或Services块.
如:
Actions的形式:
on <trigger>
<command>
<command>
<command>
Services的形式:
service <name> <pathname> [ <argument> ]*
<option>
<option>
rc文件里实例:
service logd /system/bin/logd
class core
socket logd stream 0666 logd logd
socket logdr seqpacket 0666 logd logd
socket logdw dgram 0222 logd logd
group root system
writepid /dev/cpuset/system-background/tasks
service healthd /sbin/healthd
class core
critical
seclabel u:r:healthd:s0
# Actions
# on <trigger>
on property:sys.powerctl=*
powerctl ${sys.powerctl}
on early-init
# Set init and its forked children's oom_adj.
write /proc/1/oom_score_adj -1000
# Set the security context of /adb_keys if present.
restorecon /adb_keys
start ueventd
每个rc文件里又可以包含其它rc文件:
import /init.environ.rc
import /init.usb.rc
import /init.${ro.hardware}.rc
import /init.usb.configfs.rc
import /init.${ro.zygote}.rc
import /init.trace.rc
Actions里常用的trigger有:
on early-init
<command>
on init
<command>
on late-init
<command>
on post-fs
<command>
on boot
<command>
on property:sys.init_log_level=* //在设置属性值时触发
<command>
on charger
<command>
on property:sys.powerctl=*
<command>
常用的Commands有:
//参考system/core/init/keywords.h
bootchart_init
Start bootcharting if configured (see below).
This is included in the default init.rc.
chmod <octal-mode> <path>
Change file access permissions.
chown <owner> <group> <path>
Change file owner and group.
class_start <serviceclass>
Start all services of the specified class if they are
not already running.
class_stop <serviceclass>
Stop and disable all services of the specified class if they are
currently running.
class_reset <serviceclass>
Stop all services of the specified class if they are
currently running, without disabling them. They can be restarted
later using class_start.
copy <src> <dst>
Copies a file. Similar to write, but useful for binary/large
amounts of data.
domainname <name>
Set the domain name.
enable <servicename>
Turns a disabled service into an enabled one as if the service did not
specify disabled.
If the service is supposed to be running, it will be started now.
Typically used when the bootloader sets a variable that indicates a specific
service should be started when needed. E.g.
on property:ro.boot.myfancyhardware=1
enable my_fancy_service_for_my_fancy_hardware
exec [ <seclabel> [ <user> [ <group> ]* ] ] -- <command> [ <argument> ]*
Fork and execute command with the given arguments. The command starts
after "--" so that an optional security context, user, and supplementary
groups can be provided. No other commands will be run until this one
finishes. <seclabel> can be a - to denote default.
export <name> <value>
Set the environment variable <name> equal to <value> in the
global environment (which will be inherited by all processes
started after this command is executed)
hostname <name>
Set the host name.
ifup <interface>
Bring the network interface <interface> online.
import <filename>
Parse an init config file, extending the current configuration.
insmod <path>
Install the module at <path>
load_all_props
Loads properties from /system, /vendor, et cetera.
This is included in the default init.rc.
load_persist_props
Loads persistent properties when /data has been decrypted.
This is included in the default init.rc.
loglevel <level>
Sets the kernel log level to level. Properties are expanded within <level>.
mkdir <path> [mode] [owner] [group]
Create a directory at <path>, optionally with the given mode, owner, and
group. If not provided, the directory is created with permissions 755 and
owned by the root user and root group. If provided, the mode, owner and group
will be updated if the directory exists already.
mount_all <fstab>
Calls fs_mgr_mount_all on the given fs_mgr-format fstab.
mount <type> <device> <dir> [ <flag> ]* [<options>]
Attempt to mount the named device at the directory <dir>
<device> may be of the form mtd@name to specify a mtd block
device by name.
<flag>s include "ro", "rw", "remount", "noatime", ...
<options> include "barrier=1", "noauto_da_alloc", "discard", ... as
a comma separated string, eg: barrier=1,noauto_da_alloc
powerctl
Internal implementation detail used to respond to changes to the
"sys.powerctl" system property, used to implement rebooting.
restart <service>
Like stop, but doesn't disable the service.
restorecon <path> [ <path> ]*
Restore the file named by <path> to the security context specified
in the file_contexts configuration.
Not required for directories created by the init.rc as these are
automatically labeled correctly by init.
restorecon_recursive <path> [ <path> ]*
Recursively restore the directory tree named by <path> to the
security contexts specified in the file_contexts configuration.
rm <path>
Calls unlink(2) on the given path. You might want to
use "exec -- rm ..." instead (provided the system partition is
already mounted).
rmdir <path>
Calls rmdir(2) on the given path.
setprop <name> <value>
Set system property <name> to <value>. Properties are expanded
within <value>.
setrlimit <resource> <cur> <max>
Set the rlimit for a resource.
start <service>
Start a service running if it is not already running.
stop <service>
Stop a service from running if it is currently running.
swapon_all <fstab>
Calls fs_mgr_swapon_all on the given fstab file.
symlink <target> <path>
Create a symbolic link at <path> with the value <target>
sysclktz <mins_west_of_gmt>
Set the system clock base (0 if system clock ticks in GMT)
trigger <event>
Trigger an event. Used to queue an action from another
action.
verity_load_state
Internal implementation detail used to load dm-verity state.
verity_update_state <mount_point>
Internal implementation detail used to update dm-verity state and
set the partition.<mount_point>.verified properties used by adb remount
because fs_mgr can't set them directly itself.
wait <path> [ <timeout> ]
Poll for the existence of the given file and return when found,
or the timeout has been reached. If timeout is not specified it
currently defaults to five seconds.
write <path> <content>
Open the file at <path> and write a string to it with write(2).
If the file does not exist, it will be created. If it does exist,
it will be truncated. Properties are expanded within <content>.
Services里常用的options有:
critical
This is a device-critical service. If it exits more than four times in
four minutes, the device will reboot into recovery mode.
disabled
This service will not automatically start with its class.
It must be explicitly started by name.
setenv <name> <value>
Set the environment variable <name> to <value> in the launched process.
socket <name> <type> <perm> [ <user> [ <group> [ <seclabel> ] ] ]
Create a unix domain socket named /dev/socket/<name> and pass
its fd to the launched process. <type> must be "dgram", "stream" or "seqpacket".
User and group default to 0.
'seclabel' is the SELinux security context for the socket.
It defaults to the service security context, as specified by seclabel or
computed based on the service executable file security context.
user <username>
Change to username before exec'ing this service.
Currently defaults to root. (??? probably should default to nobody)
Currently, if your process requires linux capabilities then you cannot use
this command. You must instead request the capabilities in-process while
still root, and then drop to your desired uid.
group <groupname> [ <groupname> ]*
Change to groupname before exec'ing this service. Additional
groupnames beyond the (required) first one are used to set the
supplemental groups of the process (via setgroups()).
Currently defaults to root. (??? probably should default to nobody)
seclabel <seclabel>
Change to 'seclabel' before exec'ing this service.
Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
Services on the system partition can instead use policy-defined transitions
based on their file security context.
If not specified and no transition is defined in policy, defaults to the init context.
oneshot
Do not restart the service when it exits.
class <name>
Specify a class name for the service. All services in a
named class may be started or stopped together. A service
is in the class "default" if one is not specified via the
class option.
onrestart
Execute a Command (see below) when service restarts.
writepid <file...>
Write the child's pid to the given files when it forks. Meant for
cgroup/cpuset usage.
实现直接使用root用户进入终端, 修改android/out/target/product/tulip-p1/root/init.rc :
574 service console /system/bin/sh
575 class core
576 console
577 disabled
578 # user shell
579 # group shell log
580 # seclabel u:r:shell:s0
//备注使用shell用户进入终端. 会默认使用root用户操作
实现系统进入后执行自定义的脚本 :
修改android/out/target/product/tulip-p1/root/init.rc , 增加内容:
service start_mytest /system/bin/mytest.sh
oneshot
on property:sys.boot_completed=1
start start_mytest
在android/out/target/product/tulip-p1/system/bin目录下,增加mytest.sh, 内容:
#!/system/bin/sh
echo "mytest ...!" > /dev/ttyS0
echo "mytest ...!" > /dev/ttyS0
echo "mytest ...!" > /dev/ttyS0
echo "mytest ...!" > /dev/ttyS0
echo "mytest ...!" > /dev/ttyS0
echo "mytest ...!" > /dev/ttyS0
echo "mytest ...!" > /dev/ttyS0