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



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