当前位置:首页 > 嵌入式 > 嵌入式云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获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!

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

LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: 驱动电源

在工业自动化蓬勃发展的当下,工业电机作为核心动力设备,其驱动电源的性能直接关系到整个系统的稳定性和可靠性。其中,反电动势抑制与过流保护是驱动电源设计中至关重要的两个环节,集成化方案的设计成为提升电机驱动性能的关键。

关键字: 工业电机 驱动电源

LED 驱动电源作为 LED 照明系统的 “心脏”,其稳定性直接决定了整个照明设备的使用寿命。然而,在实际应用中,LED 驱动电源易损坏的问题却十分常见,不仅增加了维护成本,还影响了用户体验。要解决这一问题,需从设计、生...

关键字: 驱动电源 照明系统 散热

根据LED驱动电源的公式,电感内电流波动大小和电感值成反比,输出纹波和输出电容值成反比。所以加大电感值和输出电容值可以减小纹波。

关键字: LED 设计 驱动电源

电动汽车(EV)作为新能源汽车的重要代表,正逐渐成为全球汽车产业的重要发展方向。电动汽车的核心技术之一是电机驱动控制系统,而绝缘栅双极型晶体管(IGBT)作为电机驱动系统中的关键元件,其性能直接影响到电动汽车的动力性能和...

关键字: 电动汽车 新能源 驱动电源

在现代城市建设中,街道及停车场照明作为基础设施的重要组成部分,其质量和效率直接关系到城市的公共安全、居民生活质量和能源利用效率。随着科技的进步,高亮度白光发光二极管(LED)因其独特的优势逐渐取代传统光源,成为大功率区域...

关键字: 发光二极管 驱动电源 LED

LED通用照明设计工程师会遇到许多挑战,如功率密度、功率因数校正(PFC)、空间受限和可靠性等。

关键字: LED 驱动电源 功率因数校正

在LED照明技术日益普及的今天,LED驱动电源的电磁干扰(EMI)问题成为了一个不可忽视的挑战。电磁干扰不仅会影响LED灯具的正常工作,还可能对周围电子设备造成不利影响,甚至引发系统故障。因此,采取有效的硬件措施来解决L...

关键字: LED照明技术 电磁干扰 驱动电源

开关电源具有效率高的特性,而且开关电源的变压器体积比串联稳压型电源的要小得多,电源电路比较整洁,整机重量也有所下降,所以,现在的LED驱动电源

关键字: LED 驱动电源 开关电源

LED驱动电源是把电源供应转换为特定的电压电流以驱动LED发光的电压转换器,通常情况下:LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: LED 隧道灯 驱动电源
关闭