系統屬性中有一類是永久儲存在檔案中的:
bionic/libc/include/sys/_system_properties.h
#define PROP_SERVICE_NAME "property_service"
#define PROP_PATH_RAMDISK_DEFAULT "/default.prop"
#define PROP_PATH_SYSTEM_BUILD "/system/build.prop"
#define PROP_PATH_SYSTEM_DEFAULT "/system/default.prop"
#define PROP_PATH_LOCAL_OVERRIDE "/data/local.prop
那麼我們在編譯的時候,這些檔案中的值是在何處進行初始化的呢?代碼中又是在何時產生的這些永久檔案的呢?本文以/default.prop進行探索。
在編譯組建檔案中有如下,這裡第二行我們可以看到確實在根目錄(/)下產生有default.prop檔案。
./out/target/product/sangfei82_we_jb5/recovery/root/default.prop:2:# ADDITIONAL_DEFAULT_PROPERTIES
./out/target/product/sangfei82_we_jb5/root/default.prop:2:# ADDITIONAL_DEFAULT_PROPERTIES
我們開啟default.prop查看其中的內容:
root@android:/ # cat default.prop
#
# ADDITIONAL_DEFAULT_PROPERTIES
#
ro.secure=1
ro.allow.mock.location=0
persist.mtk.aee.aed=on
ro.debuggable=0
persist.sys.usb.config=mass_storage
persist.service.acm.enable=0
ro.mount.fs=EXT4
這之中我們需要關注的是ADDITIONAL_DEFAULT_PROPERTIES這個變數,這是makefile檔案中定義的目標變數,在makefile檔案中ro.secure這些變數都被定義在ADDITIONAL_DEFAULT_PROPERTIES這個目標變數上。我們在/build中搜尋ADDITIONAL_DEFAULT_PROPERTIES:
grep -nr "ADDITIONAL_DEFAULT_PROPERTIES" .
./build/core/main.mk:330: ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1
./build/core/main.mk:359: ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=0
./build/core/main.mk:368: ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0
./build/core/main.mk:370: ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=1
./build/core/main.mk:375:ADDITIONAL_DEFAULT_PROPERTIES += persist.mtk.aee.aed=on
./build/core/main.mk:379: ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
./build/core/main.mk:384: ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0
./build/core/main.mk:389: ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mass_storage
./build/core/main.mk:391: ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mtp
./build/core/main.mk:396:ADDITIONAL_DEFAULT_PROPERTIES += persist.service.acm.enable=1
./build/core/main.mk:398:ADDITIONAL_DEFAULT_PROPERTIES += persist.service.acm.enable=0
./build/core/Makefile:101:ADDITIONAL_DEFAULT_PROPERTIES := \
./build/core/Makefile:102: $(call collapse-pairs, $(ADDITIONAL_DEFAULT_PROPERTIES))
./build/core/Makefile:103:ADDITIONAL_DEFAULT_PROPERTIES += \
./build/core/Makefile:105:ADDITIONAL_DEFAULT_PROPERTIES := $(call uniq-pairs-by-first-component, \
./build/core/Makefile:106: $(ADDITIONAL_DEFAULT_PROPERTIES),=)
./build/core/Makefile:112: echo "# ADDITIONAL_DEFAULT_PROPERTIES" >> $@; \
./build/core/Makefile:114: $(hide) $(foreach line,$(ADDITIONAL_DEFAULT_PROPERTIES), \
進入main.mk的這段代碼:
## user/userdebug ##
user_variant := $(filter user userdebug,$(TARGET_BUILD_VARIANT))
enable_target_debugging := true
tags_to_install :=
ifneq (,$(user_variant))
# Target is secure in user builds.
ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1
ifeq ($(user_variant),userdebug)
# Pick up some extra useful tools
tags_to_install += debug
# Enable Dalvik lock contention logging for userdebug builds.
ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.lockprof.threshold=500
else
# Disable debugging in plain user builds.
enable_target_debugging :=
endif
# enable dex pre-optimization for all TARGET projects in default to
# speed up device first boot-up
WITH_DEXPREOPT := true
# Turn on Dalvik preoptimization for user builds, but only if not
# explicitly disabled and the build is running on Linux (since host
# Dalvik isn't built for non-Linux hosts).
ifneq (true,$(DISABLE_DEXPREOPT))
ifeq ($(user_variant),user)
ifeq ($(HOST_OS),linux)
WITH_DEXPREOPT := true
endif
endif
endif
# Disallow mock locations by default for user builds
ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=0
else # !user_variant
# Turn on checkjni for non-user builds.
# Kirby: turn off it temporarily to gain performance {
# ADDITIONAL_BUILD_PROPERTIES += ro.kernel.android.checkjni=1
ADDITIONAL_BUILD_PROPERTIES += ro.kernel.android.checkjni=0
# } Kirby
# Set device insecure for non-user builds.
ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0
# Allow mock locations by default for non user builds
ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=1
endif # !user_variant
# always enable aed
ADDITIONAL_DEFAULT_PROPERTIES += persist.mtk.aee.aed=on
ifeq (true,$(strip $(enable_target_debugging)))
# Target is more debuggable and adbd is on by default
ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
# Include the debugging/testing OTA keys in this build.
INCLUDE_TEST_OTA_KEYS := true
else # !enable_target_debugging
# Target is less debuggable and adbd is off by default
ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0
endif # !enable_target_debugging
# default usb function
ifeq ($(strip $(MTK_MASS_STORAGE)),yes)
ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mass_storage
else
ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mtp
endif
# serial port open or not
ifeq ($(strip $(MTK_SERIAL_PORT_DEFAULT_ON)),yes)
ADDITIONAL_DEFAULT_PROPERTIES += persist.service.acm.enable=1
else
ADDITIONAL_DEFAULT_PROPERTIES += persist.service.acm.enable=0
endif
可以得出,在這裡我們開始了定義ADDITIONAL_DEFAULT_PROPERTIES(default.prop)裡的"有用內容"(因為這裡並沒有產生default.prop檔案,也沒有往其中寫入任何內容,但定義的這些內容是系統真正會用到的內容),並賦了初值。
進入makefile檔案:
# -----------------------------------------------------------------
# default.prop
INSTALLED_DEFAULT_PROP_TARGET := $(TARGET_ROOT_OUT)/default.prop
ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_DEFAULT_PROP_TARGET)
ADDITIONAL_DEFAULT_PROPERTIES := \
$(call collapse-pairs, $(ADDITIONAL_DEFAULT_PROPERTIES))
ADDITIONAL_DEFAULT_PROPERTIES += \
$(call collapse-pairs, $(PRODUCT_DEFAULT_PROPERTY_OVERRIDES))
ADDITIONAL_DEFAULT_PROPERTIES := $(call uniq-pairs-by-first-component, \
$(ADDITIONAL_DEFAULT_PROPERTIES),=)
$(INSTALLED_DEFAULT_PROP_TARGET):
@echo Target buildinfo: $@
@mkdir -p $(dir $@)
$(hide) echo "#" > $@; \
echo "# ADDITIONAL_DEFAULT_PROPERTIES" >> $@; \
echo "#" >> $@;
$(hide) $(foreach line,$(ADDITIONAL_DEFAULT_PROPERTIES), \
echo "$(line)" >> $@;)
build/tools/post_process_props.py $@
重點關注$(INSTALLED_DEFAULT_PROP_TARGET):之後的這一部分內容,我們可以發現這裡的格式和default.prop檔案中開頭的格式一樣。如果熟悉makefile檔案的編寫文法的話,那麼就好理解了,簡單的作下說明:
:= , += 都表示賦值
makefile的執行文法為:
目標:條件
(Tab鍵)命令
我們只需要關注命令即可:
@echo Target buildinfo: $@ @表示不顯示此行內容,$@ 等同於目標,echo是shell命令,顯示內容到控制台。如果沒有"@"default.prop檔案中
會多此行內容: Target buildinfo: default.prop
@mkdir -p $(dir $@) shell命令:建立檔案,因此default.prop在此處建立的
$(hide) echo "#" > $@; \
echo "# ADDITIONAL_DEFAULT_PROPERTIES" >> $@; \ shell重新導向輸入到檔案,這裡就是default.prop開頭處的三行內容
echo "#" >> $@;
$(hide) $(foreach line,$(ADDITIONAL_DEFAULT_PROPERTIES), \ makefile的函數迴圈方法,即輸出每一行重新導向到檔案中
echo "$(line)" >> $@;)
build/tools/post_process_props.py $@ 一個python指令碼,做了一些其它操作。
END...