AndroidSystemProperty_第1頁
AndroidSystemProperty_第2頁
AndroidSystemProperty_第3頁
AndroidSystemProperty_第4頁
AndroidSystemProperty_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、最新資料推薦Android System PropertyAn d roid Syst em Property Syst em Propert y 代碼 中大量存在:System Properties.set()/System Properties. get(); 通過這兩個接口可以對系統(tǒng)的屬性進行讀取 /設(shè)置, 顧名思義系統(tǒng)屬 性,肯定對整個系統(tǒng)全局共享.通常程序的執(zhí)行以進程為單位各自相互獨立,如何實現(xiàn)全局共享呢? System Properties是怎么一回事,又是如何實現(xiàn)的呢?屬性系統(tǒng)是android 的一個重要特性.它作為一個效勞運行,治理系統(tǒng)配置和狀態(tài).所有這些配置和狀態(tài)都是屬性.每

2、個屬性是一個鍵值對(key/value pair ),其類型都是字 符串.這些屬性可能是有些資源的使用狀態(tài),進程的執(zhí)行狀態(tài),系統(tǒng)的特有屬性可以通過命令ad b sh ell :getprop查看 上所有屬性狀態(tài)值.或者getprop init.svc.bootanim制定查看某個屬性狀態(tài)使用 setprop init.svc.bootanim start設(shè)置某個屬性的狀態(tài) 特別屬性:如果屬性名稱以ro.開頭,那么這個屬性 被視為只讀屬性.一旦設(shè)置,屬性值不能改變.1 / 14如果屬性名稱以persist, 開頭,當設(shè)置這個屬性時,其值也將寫入 /data/property.如果屬性名稱以net

3、,開頭,當設(shè)置這個屬性時,net. change 屬性將會自動設(shè)置,以參加到最后修改的屬性名.(這是很巧妙的.netresolve模塊的使用這個屬性來追蹤在net.*屬性上的任何變化.) 屬性ctrl.start 和ctrl.stop 是用來啟動和停止效勞.每一項效勞必須在/init.rc 中定義.系統(tǒng)啟動時,與init 守 護進程將解析init.rc 和啟動屬性效勞.一旦收到設(shè)置ctrl. start屬性的請求,屬性效勞將使用該屬性值作為效勞名找到該效勞,啟動該效勞.這項效勞的啟動結(jié)果將會放入init.svc. 效勞名 屬性中.客戶端應(yīng)用程序可以輪詢那個屬性值,以確定結(jié)果.二fram ew

4、ork 訪問系統(tǒng)效勞流程fram ework 通過System Properties 接口操作系統(tǒng)屬性,System Properties 通過JNI調(diào)用 訪問系統(tǒng)屬性.fram eworksbasecore javaandroidos System Properties.java :public class SystemProperties /JNI privatestatic native String native_get(String key, String def);private static native void native_set(String key, String最新資

5、料推薦def) ; public static String get(String key, String def)return native_get(key, def) ;public static void set(String key, String val) native_set(key, val) ; Jn i代碼位置:fram eworksbase core jniandroid_os_SystemProperties.cpp獲取系統(tǒng)屬性阻塞方式:static jstring SystemProperties_getSS() len = property_get(key, buf,

6、 ) ; 操作在 bion ic li bc bion i c system _p ropert i es.c中:int _system_property_get(constchar *name, char*value) 數(shù)據(jù)已經(jīng)存儲在內(nèi)存中_system_property_area_ 等待讀取完返回 const prop_info*pi = _system_property_find(name);return_system_property_read(pi, 0, value) ; 進程啟動后數(shù)據(jù)已經(jīng)將系統(tǒng)屬性數(shù)據(jù)讀取到相應(yīng)的共享內(nèi)存中,保存在全局變量system _property_are

7、a;進程之間都是獨立的,系統(tǒng)屬性數(shù)據(jù)是如何讀取到當前進程空間中的呢?后續(xù)介紹.設(shè)置屬性異步socket通信:int _system_property_set(const char *key, const char*value) msg. cmd = PROP_MSG_SETPROP;3 / 14strlcpy(,key,sizeof msg. name)strlcpy(msg. value, value, sizeof msg. value) ; err = send_prop_msg(msg) ; static int send_prop_msg(prop_msg*msg)

8、/sokcet 通信 /dev/socket/property_services = socket(AF_LOCAL, SOCK_STREAM,0) ;connect(s,send(s, msg, 通過socket 向 運行在哪里呢? 進程啟動監(jiān)聽過(struct sockaddr *) addr, alen) sizeof(prop_msg) , 0) close(s) ; property_service發(fā)送 7肖 H,property_service三 Propert y Service 創(chuàng)立效勞端 socket in it程中:system coreinitI nit.c int ma

9、in(int argc, char*argv) 參加到 action queue 隊列queue_builtin_action(property_service_init_action, property_service_init) ; for(; ; )/執(zhí)行action queue隊歹!J/ 接收通過 socket 向 propertyservice 發(fā)送的數(shù)據(jù); nr = poll(ufds, fd_count, timeout) ;handle_property_set_fd() ; static int property_service_init_action(int nargs,

10、char *args) start_property_service() ;systemcoreinitproperty_service. cvoid start_property_service(void) /加載屬性配最新資料推薦load_properties_from_file(PROP_PATH_SYSTEM_BUILD);load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);load_persistent_properties()

11、;/倉 |建 socket 資源并綁定fd = create_socket(PROP_SERVICE_NAME,SOCK_STREAM, 0666, 0, 0) ;/監(jiān)聽 listen(fd,8) ; Propert y Servi ce是運行在in it 守護進程中.接收到消息之后干什么,還是要先弄清楚整個PropertyService是如何實現(xiàn)的呢,后續(xù)介紹.先看看Property Service接收到消息后的處理.四 Propert y Service 監(jiān)聽 socket 處理 Propert y Service 監(jiān)聽socket消息的處理過程:void handle_property_

12、set_fd() /等待建立通信s = accept(property_set_fd, (struct sockaddr *) addr, addr_size)/獲取套接字相關(guān)信息 uid gidgetsockopt(s, SOL_SOCKET, SO_PEERCRED,cr, cr_size); /接收屬性設(shè)置請求消息recv(s, msg, sizeof(msg) , 0);/ 處理消息switch(msg. cmd)casePROP_MSG_SETPROP:/通過設(shè)置系統(tǒng)屬性 處理ctl. 開頭消息if(memcmp(msg. name, ctl. , 4)= 0)權(quán)限檢測if5 / 1

13、4(check_control_perms(msg. value, cr. uid, cr. gid) )handle_control_message(char*) + 4, (char*) msg. value) ;else更改系統(tǒng)屬性值if (check_perms(msg. name,cr.uid,cr. gid) )property_set(char*) msg. name, (char*)msg. value) ; break; close(s) ; 通過設(shè)置系統(tǒng)屬性啟動/關(guān)閉Servi ce :權(quán)限判斷:staticintcheck_control_perms(c

14、onstchar *name,unsigned int uid, unsigned int gid) / system /root用戶直接有權(quán)限if(uid = AID_SYSTEM | uid= AID_ROOT)return 1;/查詢用戶名單,判斷是否存在表中并具有對應(yīng)權(quán)限for (i = 0; control_permsi . service; i+) if (strcmp(control_permsi . service, name)= 0) if (uid control_permsi . uid = uid) | |(gid control_permsi . gid = gid)

15、return1; return 0; 所以如果想要應(yīng)用有權(quán)限啟動/關(guān)閉某Nat ive Service :需要具有system /root 權(quán)限找到對應(yīng)應(yīng)用uid gid , 將應(yīng) 用名稱參加到control_perm s 列表中處理消息可以通過設(shè)置最新資料推薦系統(tǒng)屬性 改變效勞的執(zhí)行狀態(tài)start / st op :void handle_control_message(const char *msg, constchar *arg) if (! strcmp(msg, start) ) msg_start(arg) ; else if (! strcmp(msg,stop) ) msg_s

16、top(arg) ; else if (!strcmp(msg, restart) )msg_stop(arg) ;msg_start(arg) ; static void msg_start(constchar *name) service_start(svc, args) ; void service_start(struct service *svc, const char *dynamic_args)/創(chuàng)立進程啟動效勞pid = fork();execve(svc-args 0 , (char*) svc-args, (char*) ENV);/ 修改效勞的系統(tǒng)屬性 執(zhí)行狀態(tài) noti

17、fy_service_state(svc-name, running) ; 連著前面就是ct r.st art 和ct r.st o p 系統(tǒng)屬性:用來啟動和停止效勞的.例如:/ / start boot anim ation property_set(ctl.start, bootanim ); 在init. rc中說明效勞是否在開機時啟動:service adbd /sbin/adbd class core disabled /不自動啟動啟動效勞的時候會判斷:static void service_start_if_not_disabled(struct7 / 14service *svc

18、) /判斷是否啟動if (!(svc-flagsSVC_DISABLED) service_start(svc, NULL) ; 修改系統(tǒng)屬性值:static int check_perms(const char *name, unsigned intuid, unsigned int gid) /進行權(quán)限檢測for (i = 0;property_permsi . prefix;i+)int tmp;if (strncmp(property_permsiprefix,name,strlen(property_permsiprefix) )=0) if(uid property_permsi

19、. uid = uid) |(gidproperty_permsi . gid = gid) )return1; return 0; 看這個修改系統(tǒng)屬性權(quán)限表:property_perms = net. dns,AID_RADIO, 0 , net. , AID_SYSTEM,0 , dev. , AID_SYSTEM, 0 , runtime. , AID_SYSTEM, 0 , sys.,AID_SYSTEM, 0 , service. , AID_SYSTEM,0 , persist. sys. , AID_SYSTEM, 0 , persist. service. , AID_SYST

20、EM,0 NULL, 0, 0 ;指定了特定的用戶有用修改 帶有某最新資料推薦些前綴的系統(tǒng)屬性值.到這里根本就是Property 對外的根本工作流程,Property Service內(nèi)部具體如何實現(xiàn), 操作運行, 跨進程空想內(nèi)存等問題 仍未去除是如何處理的.五屬性系統(tǒng)設(shè)計屬性系統(tǒng)的上層架構(gòu)如下列圖所示:Property Service 運行在init 進程中,開機附屬性文件 中加載到共享內(nèi)存中;設(shè)置系統(tǒng)屬性通過 socket與PropertyService 通信.Property Consum er進程將存儲系統(tǒng)屬性值的共享內(nèi)存,加載到當前進程虛擬空間中,實現(xiàn)對系統(tǒng)屬性值的讀取.Propert

21、y Setter進程修改系統(tǒng)屬性,通過socket 向Property Service 發(fā)送消息,更改系統(tǒng)屬性值.六屬性系統(tǒng)實現(xiàn)屬性系統(tǒng)設(shè)計的關(guān)鍵就是:跨進程共享內(nèi)存的實現(xiàn).下面將看看屬性系統(tǒng)實現(xiàn)具體過程:I n it進程執(zhí)行:int main(int argc, char *argv) /將屬性系統(tǒng)初 始 化 函 數(shù) 加 入action queuenargs,queue_builtin_action(property_init_action, property_init);for(; ; ) static int property_init_action(int9 / 14char *arg

22、s) property_init(load_defaults) ; 始化 Property Servi ce :system coreinitproperty_service. c void property_init(bool load_defaults) /初始化共享內(nèi)存空 間 init_property_area() ;加 載屬 性文件load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT) ;初始化共享內(nèi)存空間:static int init_property_area(void) /倉建 匿 名 內(nèi) 存 空 間 PA_SIZE =327

23、68init_workspace(pa_workspace, PA_SIZE) / 將內(nèi)存區(qū)域分 成兩局部:屬性系統(tǒng)根本信息和屬性鍵值對pa_info_array = (void*)(char*) pa_workspace. data) + PA_INFO_START); / 初始化屬性系統(tǒng)信息pa = pa_workspace. data;memset(pa, 0, PA_SIZE) ; pa-magic = PROP_AREA_MAGIC; pa-version = PROP_AREA_VERSION; /* plug into the lib property services */ _

24、system_property_area_ = pa; _syst em _p ropert y_area_每個進程都會使用此變量,指向系統(tǒng)屬性共享內(nèi)存區(qū)域,訪問系統(tǒng)屬性,很重要.位于:bioniclibcbionicsystem _properties. c 中, 屬于最新資料推薦bionic 庫.后面將介紹各進程如何加載共享內(nèi)存.將文件作為共享內(nèi)存映射到進程空間內(nèi)存使用:static int init_workspace(workspace *w, size_t size) /dev is a tmpfs是一種虛擬內(nèi)存文件系統(tǒng)int fd =open(/dev/_properties_,

25、O_RDWR O_CREAT, 0600); /將文件映射為共享進程空間內(nèi)存使其可以與操作內(nèi)存方式一致void *data = mmap(NULL, size, PROT_READ PROT_WRITE,MAP_SHARED, fd, 0) ; close(fd) ;/刪除文件fd =open(/dev/_properties_,O_RDONLY);unlink(/dev/_properties_);/ 保存 fd size 將作為環(huán)境變量傳遞給每個進程w-data = data; w-size = size; w-fd=fd; 加載系統(tǒng)屬性默認數(shù)據(jù)文件:#define PROP_PATH_R

26、AMDISK_DEFAULefault. prop static void load_properties_from_file(const char *fn) /讀取系統(tǒng)屬性鍵值對數(shù)據(jù)寫入到共享內(nèi)存中data =read_file(fn, sz) ; load_properties(data) ; 力口上上面所述:Property Service Socket 資源的創(chuàng)立, 來監(jiān)聽socket 通信 連接設(shè)置系統(tǒng)屬性,在Init 進程中Property Service完成了初始化.11 / 14將得到該內(nèi)存區(qū)域數(shù)據(jù)結(jié)構(gòu):七 進程共享系統(tǒng)屬性內(nèi)存空間實現(xiàn)Property Service 運行于i

27、nit進程中,將文件映射為創(chuàng)立一塊共享內(nèi)存空間,但在整個系統(tǒng)中,其他進程也能夠讀取這塊內(nèi)存映射到當前進程空間中,是如何實現(xiàn)的呢? Servi ce進程啟動:將共享內(nèi)存空間fd size作為環(huán)境變量傳遞給新創(chuàng)立進程void service_start(structservice*svc, constchar*dynamic_args) / 創(chuàng)立進程 pid = fork();if (pid = 0)if (properties_inited() )獲取系統(tǒng)屬性空間文件描述get_property_workspace(fd, sz) ; /dup最/J、的可用文件描述符sprintf(tmp, %

28、d, %d, dup(fd),sz) ;/力口入 ANDROID_PROPERTY_WORKSPACE量 到 ENV / 包含共享 內(nèi) 存 fd add_environment(ANDROID_PROPERTY_WORKSPACE,tmp) ;/執(zhí)行程序 傳遞環(huán)境變量 ENVexecve(svc-args0 , (char*) svc-args, (char*) ENV)設(shè) 置Service 系 統(tǒng) 屬 性notify_service_state(svc-name, running) ; voidget_property_workspace(int *fd, int *sz) *fd = pa_workspace. fd; *sz = pa_workspace. size; 共享內(nèi)存空間fd size作為環(huán)境變量傳遞給新創(chuàng)立進程后,將在何處使用最新資料推薦呢?將系統(tǒng)屬性內(nèi)存空間映射到當前進程虛擬空間:進程在啟動時,會加載動態(tài)庫bion ic l ib c 庫:bioniclibcbioniclibc_init_dynam ic. c中:void _attribute_(constructor) _libc_preinit(void);根據(jù) GCC 的 con st ru ct or/ d est ru ct o r屬性:給 個函 數(shù)賦予 constructor 或

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論