当前位置:首页 > 公众号精选 > 嵌入式云IOT技术圈
[导读]Android系统里以*.rc为扩展名为系统初始化脚本,脚本里就是使用安卓初始化语言。语句都是一行为一条语句,没有符号分隔. 语句里的每个词用空格隔开。

微信公众号:杨源鑫
如果你觉得文章对你有帮助,欢迎在评论区交流讨论

参考: android源码目录里的system/core/init/readme.txt.


Android系统里以*.rc为扩展名为系统初始化脚本,脚本里就是使用安卓初始化语言.


语句都是一行为一条语句,没有符号分隔. 语句里的每个词用空格隔开. 

如: service ueventd /sbin/ueventd


备注语句以符号”#”作为注释.


语言基本上分为4个块: Actions Commands Services 和 Options.


每个Actions和Services关键词开始的语句作为一个新的区块. 

Commands和Options就是属于就近的Actions或Services块. 

如: 

Actions的形式:

1on <trigger>
2   <command>
3   <command>
4   <command>

Services的形式:

1service <name> <pathname> [ <argument> ]*
2   <option>
3   <option>

rc文件里实例:

 1service logd /system/bin/logd
2    class core
3    socket logd stream 0666 logd logd
4    socket logdr seqpacket 0666 logd logd
5    socket logdw dgram 0222 logd logd
6    group root system
7     writepid /dev/cpuset/system-background/tasks
8
9service healthd /sbin/healthd
10    class core
11    critical
12    seclabel u:r:healthd:s0
13
14# Actions
15# on       
16on property:sys.powerctl=*
17    powerctl ${sys.powerctl}
18
19on early-init
20    # Set init and its forked children's oom_adj.
21    write /proc/1/oom_score_adj -1000
22    # Set the security context of /adb_keys if present.
23    restorecon /adb_keys
24    start ueventd

每个rc文件里又可以包含其它rc文件:

1import /init.environ.rc
2import /init.usb.rc
3import /init.${ro.hardware}.rc
4import /init.usb.configfs.rc
5import /init.${ro.zygote}.rc
6import /init.trace.rc

Actions里常用的trigger有:

 1on early-init
2    
3
4on init
5    
6
7on late-init
8    
9
10on post-fs
11   
12
13on boot
14   
15
16on property:sys.init_log_level=*  //在设置属性值时触发
17   
18
19on charger
20   
21
22on property:sys.powerctl=*
23   

常用的Commands有: 
//参考system/core/init/keywords.h

 1bootchart_init
2   Start bootcharting if configured (see below).
3   This is included in the default init.rc.
4
5chmod mode> <path>
6   Change file access permissions.
7
8chown  <group> <path>
9   Change file owner and group.
10
11class_start 
12   Start all services of the specified class if they are
13   not already running.
14
15class_stop 
16   Stop and disable all services of the specified class if they are
17   currently running.
18
19class_reset 
20   Stop all services of the specified class if they are
21   currently running, without disabling them. They can be restarted
22   later using class_start.
23
24copy  
25   Copies a file. Similar to write, but useful for binary/large
26   amounts of data.
27
28domainname <name>
29   Set the domain name.
30
31enable 
32   Turns a disabled service into an enabled one as if the service did not
33   specify disabled.
34   If the service is supposed to be running, it will be started now.
35   Typically used when the bootloader sets a variable that indicates a specific
36   service should be started when needed. E.g.
37     on property:ro.boot.myfancyhardware=1
38        enable my_fancy_service_for_my_fancy_hardware
39
40exec [  [ <user> [ <group> ]* ] ] --  [  ]*
41   Fork and execute command with the given arguments. The command starts
42   after "--" so that an optional security contextuserand supplementary
43   groups can be provided. No other commands will be run until this one
44   finishes.  can be a - to denote default.
45
46export <name> <value>
47   Set the environment variable <name> equal to <valuein the
48   global environment (which will be inherited by all processes
49   started after this command is executed)
50
51hostname <name>
52   Set the host name.
53
54ifup <interface>
55   Bring the network interface <interface> online.
56
57import 
58   Parse an init config file, extending the current configuration.
59
60insmod <path>
61   Install the module at <path>
62
63load_all_props
64   Loads properties from /system, /vendor, et cetera.
65   This is included in the default init.rc.
66
67load_persist_props
68   Loads persistent properties when /data has been decrypted.
69   This is included in the default init.rc.
70
71loglevel <level>
72   Sets the kernel log level to level. Properties are expanded within <level>.
73
74mkdir <path> [mode] [owner] [group]
75   Create a directory at <path>, optionally with the given mode, owner, and
76   group. If not provided, the directory is created with permissions 755 and
77   owned by the root user and root group. If provided, the mode, owner and group
78   will be updated if the directory exists already.
79
80mount_all 
81   Calls fs_mgr_mount_all on the given fs_mgr-format fstab.
82
83mount <type 
 [  ]* []
84   Attempt to mount the named device at the directory 

85    may be of the form mtd@name to specify a mtd block
86   device by name.
87   include "ro""rw""remount""noatime", ...
88    include "barrier=1""noauto_da_alloc""discard", ... as
89   a comma separated string, eg: barrier=1,noauto_da_alloc
90
91powerctl
92   Internal implementation detail used to respond to changes to the
93   "sys.powerctl" system property, used to implement rebooting.
94
95restart 
96   Like stop, but doesn't disable the service.
97
98restorecon  [  ]*
99   Restore the file named by  to the security context specified
100   in the file_contexts configuration.
101   Not required for directories created by the init.rc as these are
102   automatically labeled correctly by init.
103
104restorecon_recursive  [  ]*
105   Recursively restore the directory tree named by  to the
106   security contexts specified in the file_contexts configuration.
107
108rm 
109   Calls unlink(2) on the given path. You might want to
110   use "exec -- rm ..." instead (provided the system partition is
111   already mounted).
112
113rmdir 
114   Calls rmdir(2) on the given path.
115
116setprop  
117   Set system property  to . Properties are expanded
118   within .
119
120setrlimit   
121   Set the rlimit for a resource.
122
123start 
124   Start a service running if it is not already running.
125
126stop 
127   Stop a service from running if it is currently running.
128
129swapon_all 
130   Calls fs_mgr_swapon_all on the given fstab file.
131
132symlink  
133   Create a symbolic link at  with the value 
134
135sysclktz 
136   Set the system clock base (0 if system clock ticks in GMT)
137
138trigger 
139   Trigger an event.  Used to queue an action from another
140   action.
141
142verity_load_state
143   Internal implementation detail used to load dm-verity state.
144
145verity_update_state 
146   Internal implementation detail used to update dm-verity state and
147   set the partition..verified properties used by adb remount
148   because fs_mgr can'
set them directly itself.
149
150wait <path> [ <timeout> ]
151   Poll for the existence of the given file and return when found,
152   or the timeout has been reached. If timeout is not specified it
153   currently defaults to five seconds.
154
155write <path> <content>
156   Open the file at <pathand write a string to it with write(2).
157   If the file does not exist, it will be created. If it does exist,
158   it will be truncated. Properties are expanded within <content>.

Services里常用的options有:

 1critical
2  This is a device-critical service. If it exits more than four times in
3  four minutes, the device will reboot into recovery mode.
4
5disabled
6  This service will not automatically start with its class.
7  It must be explicitly started by name.
8
9setenv  
10  Set the environment variable  to  in the launched process.
11
12socket    [  [ <group> [  ] ] ]
13  Create a unix domain socket named /dev/socket/ and pass
14  its fd to the launched process.   must be "dgram""stream" or "seqpacket".
15  User and group default to 0.
16  'seclabel' is the SELinux security context for the socket.
17  It defaults to the service security context, as specified by seclabel or
18  computed based on the service executable file security context.
19
20user 
21  Change to username before exec'ing this service.
22  Currently defaults to root.  (??? probably should default to nobody)
23  Currently, if your process requires linux capabilities then you cannot use
24  this command. You must instead request the capabilities in-process while
25  still root, and then drop to your desired uid.
26
27group  [  ]*
28  Change to groupname before exec'ing this service.  Additional
29  groupnames beyond the (required) first one are used to set the
30  supplemental groups of the process (via setgroups()).
31  Currently defaults to root.  (??? probably should default to nobody)
32
33seclabel 
34  Change to 'seclabel' before exec'ing this service.
35  Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
36  Services on the system partition can instead use policy-defined transitions
37  based on their file security context.
38  If not specified and no transition is defined in policy, defaults to the init context.
39
40oneshot
41  Do not restart the service when it exits.
42
43class 
44  Specify a class name for the service.  All services in a
45  named class may be started or stopped together.  A service
46  is in the class "default" if one is not specified via the
47  class option.
48
49onrestart
50  Execute a Command (see below) when service restarts.
51
52writepid 
53  Write the child's pid to the given files when it forks. Meant for
54  cgroup/cpuset usage.

实现直接使用root用户进入终端, 修改android/out/target/product/tulip-p1/root/init.rc :

1service console /system/bin/sh
2     class core
3     console
4     disabled
5 #    user shell
6 #    group shell log
7 #    seclabel u:r:shell:s0

//备注使用shell用户进入终端. 会默认使用root用户操作

实现系统进入后执行自定义的脚本
修改android/out/target/product/tulip-p1/root/init.rc , 增加内容:

1service start_mytest /system/bin/mytest.sh    
2    oneshot
3    on property:sys.boot_completed=1
4    start start_mytest

在android/out/target/product/tulip-p1/system/bin目录下,增加mytest.sh, 内容:

1#!/system/bin/sh
2

3echo "mytest ...!" > /dev/ttyS0
4echo "mytest ...!" > /dev/ttyS0
5echo "mytest ...!" > /dev/ttyS0
6echo "mytest ...!" > /dev/ttyS0
7echo "mytest ...!" > /dev/ttyS0
8echo "mytest ...!" > /dev/ttyS0
9echo "mytest ...!" > /dev/ttyS0


免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

从内容入手提升游戏视效,让丝滑的游戏体验成为标配 上海2022年4月19日  /美通社/ -- 领先的视频和显示处理创新方案供应商Pixelworks,Inc.(纳斯达克股票代码:PXLW)逐点半导体今日宣布...

关键字: Pixelworks 半导体 ni

(全球TMT2022年4月19日讯)为满足年轻消费者的需求,全新华为nova 9 SE配备了108MP高分辨率摄影、创意vlog体验、令人惊叹的设计和时尚前卫的元素。它是nova 9家族的最新成员。 使用Pet...

关键字: 华为 rc nova

(全球TMT2022年4月19日讯)TopEdit(投必得)与Turnitin正式宣布成为官方合作伙伴关系。作为Turnitin防剽窃服务的官方授权合作伙伴,投必得将为中国科研工作者提供文章查重服务。投必得是为中国研究...

关键字: ni

(全球TMT2022年4月19日讯)新思科技(Synopsys, Inc.)联合Juniper Networks(简称“Juniper”)于近日宣布,双方已完成交易成立一家独立的新公司。该新公司将为业界提供开放式硅光子...

关键字: ip ni 新思科技

(全球TMT2022年4月20日讯)视频和显示处理创新方案供应商Pixelworks,Inc.逐点半导体宣布其已正式成为Unity验证解决方案合作伙伴。这意味着其专为提升手游视效所打造的Pixelworks Rende...

关键字: Pixelworks 半导体 ni

深圳2022年4月18日 /美通社/ -- 为满足年轻消费者的需求,全新华为nova 9 SE配备了108MP高分辨率摄影、创意vlog体验、令人惊叹的设计和时尚前卫的元素。它是nova 9家族的最新成员。 使用Pet...

关键字: rc nova

TopEdit旨在为中国研究人员提供学术编辑和出版服务,现已成为Turnitin防剽窃服务iThenticate中国区官方合作伙伴 上海2022年4月19日 /美通社/ -- 今天,TopEdit(投必得)与Turni...

关键字: ni

共同投资的新公司将为业界提供首个“片上激光”开放式硅光子平台,用于电信、数据通信、激光雷达、医疗保健、HPC、AI及光学计算领域。 加利福尼亚山景城2022年4月19日 /美通社/ -- 新思科技(Synopsys,...

关键字: ip ni 新思科技

彻底卸载不需要的应用程序 都柏林2022年4月4日 /美通社/ -- EnigmaSoft Limited的SpyHunter for Mac(用于Mac的Spy...

关键字: App installer ni

(全球TMT2022年4月5日讯)EnigmaSoft Limited的SpyHunter for Mac(用于Mac的SpyHunter)是一款防恶意软件应用,它包括一个免费的、易于使用的App Uninstalle...

关键字: App installer ni
关闭
关闭