init.rc的语法分析

在init文件中包含四个类型Actions Commands Services Options
它们的关系我用一句话来表示:
在哪些Actions条件执行之后(on ...),将会执行哪些commands;要去启动哪一个特定的服务Services,在启动这个services时我们应该添加哪些选项Options。

//所有的import都是导入其它的rc
 //import <filename> : 包含其他的*.rc,类似include
import init.common.rc
import init.sun8i.usb.rc

/*************************************************************************

  Actions

  Actions其实就是一序列的Commands(命令)。Actions都有一个trigger(触发器),它被用于决定action的执行时间。当一个符合action触发条件的事件发生时,action会被加入到执行队列的末尾,除非它已经在队列里了。

  队列中的每一个action都被依次提取出,而这个action中的每个command(命令)都将被依次执行。 Actions的形式如下:          on <trigger/name>  在一个触发条件成立的条件下,下面的命令将一一被执行            <command1>            <command2>            <command3>

  当trigger被触发时,command1,command2,command3,会依次执行,直到下一个Action或下一个Service。 简单来说,Actions就是Android在启动时定义的一个启动脚本,当条件满足时,会执行该脚本,脚本里都是一些命令commands,不同的脚本用on来区分。

*************************************************************************/ 下面是关键性的解释:
on early-init     //最先做 其中的action, 开始early-init 段
    start ueventd   //启动一个服务,注意ueventd 必须是一个service,这个会有定义,我把它放到后面
   
   

service ueventd /sbin/ueventd  //表示service段,语法: service <服务名字> <服务对应的执行文件>; 声明服务名字为ueventd的服务,其具体执行路径
                                  //为/sbin/ueventd
    class core                 //表示属于class 类别为core 的服务,如果没有设置,则表示该服务的默认类别为default



sysclktz 0  //设置系统时钟,如果是0表示用GMT的时钟ticks



loglevel 3  //log的输出级别[0,7],控制的kernel的log输出



//export,shell命令,设置全局环境变量
export LD_LIBRARY_PATH /vendor/lib:/system/lib
export ANDROID_ROOT /system



symlink /system/etc /etc  //创建一个指向/system/etc的软连接/etc, 也就是/etc目录实际上指向/system/etc



mount cgroup none /acct cpuacct //mount <type> <device> <dir> [mountoption] 把device(none)挂载到type为cgroup 的文件系统/acct下
                                //其中<device>可以是以mtd@name形式指定的一个mtd块设备. mountoption可以是mode=0755,gid=1000



chown system system /dev/cpuctl   //改变目录(/dev/cpuctl)的使用群体为system



chmod 0660 /dev/cpuctl/tasks      //改变文件(/dev/cpuctl/tasks)的使用权限为0660



hostname localhost      //设置手机主机名为localhost
domainname localdomain  //设置域名localdomain



class_start core  //如果所有的class类别为core 的服务没有运行,则马上启动它们



on property:ro.debuggable=1    //如果用setprop命令设置属性 ro.debuggable变成1,则触发下面的start console
    start console



service servicemanager /system/bin/servicemanager
    class core
    user system        //在该服务启动前,把用户名切换到 system,默认是root
   group system       //在该服务启动前,把组名切换到 system.
   critical           //说明该服务是个对于设备很关键的服务,如果4分钟内退出大于4次,则系统将重启并进入recovery恢复模式



onrestart exec /system/bin/sync  //当该服务重启时,执行后面的命令 exec
                                //exec创建和执行一个程序(/system/bin/sync),在程序完全执行完之前,init会被阻塞。所以极有可能引起init卡死



socket vold stream 0660 root mount //语法:socket <name> <type> <perm> <user> <group>, 创建一个名字为vold<name>,类别为stream<type>
                                  //访问权限为0660<perm> 用户为root,用户组为mount



下面是一些代码,不全:
on init
    # See storage config details at http://source.android.com/tech/storage/
    mkdir /mnt/shell/emulated 0700 shell shell //创建目录,具体用法和shell中mkdir一样    mkdir /storage/emulated 0555 root root

    export EXTERNAL_STORAGE /storage/emulated/legacy
    export EMULATED_STORAGE_SOURCE /mnt/shell/emulated
    export EMULATED_STORAGE_TARGET /storage/emulated

    # Support legacy paths
    symlink /storage/emulated/legacy /sdcard
    symlink /storage/emulated/legacy /mnt/sdcard
    symlink /storage/emulated/legacy /storage/sdcard0
    symlink /mnt/shell/emulated/0 /storage/emulated/legacy

    mkdir /mnt/media_rw/extsd 0700 media_rw media_rw
    mkdir /storage/extsd 0700 root root
    export SECONDARY_STORAGE /storage/extsd
    symlink /storage/extsd /mnt/extsd

    mkdir /mnt/media_rw/usbhost 0700 media_rw media_rw
    mkdir /storage/usbhost 0700 root root
    symlink /storage/usbhost /mnt/usbhost

    mount debugfs debugfs /sys/kernel/debug

on post-fs   //当文件系统起来之后

on post-fs-data
    mkdir /data/media 0770 media_rw media_rw
    # Set indication (checked by vold) that we have finished this action
    setprop vold.post_fs_data_done 1
    # to force to start sdcard
    class_start late_start

    # This module write data to /data should insmod in post-fs-data
    # due to encryption

on charger    //当充电时
    insmod nand.ko
    exec /sbin/busybox sh /sbin/init_parttion.sh
    mount_all /fstab.sun8i
    insmod /system/vendor/modules/disp.ko
    insmod /system/vendor/modules/lcd.ko
    class_start charger
    write /sys/module/printk/parameters/console_suspend N
    #write /proc/sys/kernel/printk 7

on early-boot   //在boot启动之前时

# sw-keystore
    mkdir /cache/HWML 0770 root system
    insmod /system/vendor/modules/sunxi_schw.ko
    chmod 0660 /dev/scdev
    chown keystore keystore /dev/scdev

on boot  //当boot启动时
    chown system system /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
    chmod 0660 /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

    chown system system /sys/kernel/autohotplug/enable
    chmod 0660 /sys/kernel/autohotplug/enable

    chown system system /sys/devices/system/cpu/cpu0/cpufreq/boot_lock
    chmod 0660 /sys/devices/system/cpu/cpu0/cpufreq/boot_lock

# sw-keyboard
    insmod /system/vendor/modules/sw-device.ko
    insmod /system/vendor/modules/sunxi-keyboard.ko

# bluetooth
    # UART device
    chmod 0660 /dev/ttyS1
    chown bluetooth net_bt_stack /dev/ttyS1

    # power up/down interface
    chmod 0660 /sys/class/rfkill/rfkill0/state
    chmod 0660 /sys/class/rfkill/rfkill0/type
    chown bluetooth net_bt_stack /sys/class/rfkill/rfkill0/state
    chown bluetooth net_bt_stack /sys/class/rfkill/rfkill0/type
    #write /sys/class/rfkill/rfkill0/state 0

    chown bluetooth net_bt_stack ro.bt.bdaddr_path
    chown bluetooth net_bt_stack /system/etc/bluetooth
    chown bluetooth net_bt_stack /data/misc/bluetooth
    setprop ro.bt.bdaddr_path "/data/misc/bluetooth/bdaddr"

    # bluetooth LPM
    chmod 0220 /proc/bluetooth/sleep/lpm
    chmod 0220 /proc/bluetooth/sleep/btwrite
    chown bluetooth net_bt_stack /proc/bluetooth/sleep/lpm
    chown bluetooth net_bt_stack /proc/bluetooth/sleep/btwrite
    write /proc/bluetooth/sleep/lpm 1

# touchscreen gt818    //触摸屏  型号gt818
#insmod /system/vendor/modules/gt818_ts.ko
#insmod /system/vendor/modules/mxc622x.ko

# csi module    加载一下驱动模块
    insmod /system/vendor/modules/videobuf-core.ko
    insmod /system/vendor/modules/videobuf-dma-contig.ko
    insmod /system/vendor/modules/cam_detect.ko
    insmod /system/vendor/modules/cci.ko
    insmod /system/vendor/modules/vfe_os.ko
    insmod /system/vendor/modules/vfe_subdev.ko
    insmod /system/vendor/modules/gc0308.ko
    insmod /system/vendor/modules/gc2035.ko
    insmod /system/vendor/modules/gc0329.ko
    insmod /system/vendor/modules/sp0838.ko
    insmod /system/vendor/modules/ov5640.ko
    insmod /system/vendor/modules/gc0309.ko
    insmod /system/vendor/module

# network
    insmod /system/vendor/modules/usbnet.ko
    insmod /system/vendor/modules/asix.ko
    insmod /system/vendor/modules/qf9700.ko
    insmod /system/vendor/modules/mcs7830.ko
    insmod /system/vendor/modules/rtl8150.ko
    insmod /system/vendor/modules/cdc_ether.ko

#led njm
    insmod /system/vendor/modules/fspad723_led.ko    //加载电灯
    chmod 777 /dev/led

# to observe dnsmasq.leases file for dhcp information of soft ap.
    chown dhcp system /data/misc/dhcp

on early-fs   //在文件系统启动之前
    insmod nand.ko
    exec /sbin/busybox sh /sbin/init_parttion.sh
    mount_all /fstab.sun8i
    setprop ro.crypto.fuse_sdcard true
    swapon_all /fstab.sun8i

    insmod /system/vendor/modules/disp.ko
    insmod /system/vendor/modules/lcd.ko

# gpu
    insmod /system/vendor/modules/mali.ko

on fs   在文件系统启动中
    # UDISK would be mounted as data partition when multiple user enabled.
    #format_userdata /dev/block/by-name/UDISK ASTAR

service sensors /sbin/busybox sh /system/bin/sensors.sh
    user root
    group root
    disabled
    oneshot //oneshot表示该服务值启动一次,退出后不再运行


on property:sys.sensors=1    //=1表示开启,0表示关闭传感器服务
    start sensors

# virtual sdcard daemon running as media_rw (1023)   //开启虚拟sd卡的守护进程,我们可以把系统跑到虚拟机上而不是真机,我没试过
/**************************************************************************************

Services

}  Services(服务)是一个程序,它在初始化时启动,并在退出时可选择让其重启。Services(服务)的形式如下: 
        service <name><pathname> [ <argument> ]*
           <option>
           <option>

name:服务名

pathname:当前服务对应的程序位置

option:当前服务设置的选项

Options

     Options(选项)是一个Services(服务)的修正者。他们影响Services(服务)在何时,并以何种方式运行。

     critical

     disabled

     setenv <name><value>

     socket <name><type> <perm> [ <user> [ <group> ] ]

     class <name>


**************************************************************************************/
service sdcard /system/bin/sdcard -u 1023 -g 1023 -l /data/media /mnt/shell/emulated
    class late_start

service fuse_extsd /system/bin/sdcard -u 1023 -g 1023 -w 1023 -d /mnt/media_rw/extsd /storage/extsd
    class late_start
    disabled

service fuse_usbhost /system/bin/sdcard -u 1023 -g 1023 -w 1023 -d /mnt/media_rw/usbhost /storage/usbhost
    class late_start
    disabled

#3G init.rc   //3g服务
#    import init.sunxi.3gdongle.rc



# 1. realtek & eagle wifi service    //开启wifi相关服务
# 1.1 realtek & eagle wifi sta service
service wpa_supplicant /system/bin/wpa_supplicant \
    -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \
    -O/data/misc/wifi/sockets \
    -e/data/misc/wifi/entropy.bin -g@android:wpa_wlan0
    #   we will start as root and wpa_supplicant will switch to user wifi
    #   after setting up the capabilities required for WEXT
    #   user wifi
    #   group wifi inet keystore
    class main
    socket wpa_wlan0 dgram 660 wifi wifi
    disabled
    oneshot

# 1.2 realtek & eagle wifi sta p2p concurrent service  //wifi 对等联网并发服务
service p2p_supplicant /system/bin/wpa_supplicant \
    -ip2p0 -Dnl80211 -c/data/misc/wifi/p2p_supplicant.conf \
    -e/data/misc/wifi/entropy.bin -N \
    -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \
    -O/data/misc/wifi/sockets \
    -g@android:wpa_wlan0
    class main
    socket wpa_wlan0 dgram 660 wifi wifi
    disabled



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章