2025-05-23

Android 屬性系統 Property service 設定分析

在Window中有個註冊表的東東,可以存儲一些類似key:value的鍵值對,而在android平臺上也有類似的機制叫做屬性服務(Property service)進行初始化,設置及修改和查詢的功能,adb shell命令使用 setprop 及 getprop 可以看到。

問題:
SurfaceFlinger啟動後線程調用readyToRun函數時設定有一個屬性值:
status_t SurfaceFlinger::readyToRun()
{
    LOGI(   "SurfaceFlinger's main thread ready to run. "
            "Initializing graphics H/W…");

    /*
     *  We're now ready to accept clients…
     */

    // start boot animation
    property_set("ctl.start", "bootanim");
   
    return NO_ERROR;
}

是如何啟動bootanim這個服務的呢?bootanim就是開機動畫的一個單獨進程,在init.rc中以 service bootanim /system/bin/bootanimation 作為一個服務啟動,為瞭使用圖形系統功能必須等待SurfaceFlinger啟動後才能執行,這裡就利用屬性服務作為進程同步之用法。

下面我們就這個流程進行一個簡單梳理:

一、屬性客戶端流程

property_set("ctl.start", "bootanim"); 這就是啟動觸發點!!!
–>
property_set @ /system/core/libcutils/properties.c
int property_set(const char *key, const char *value)
{
    msg.cmd = PROP_MSG_SETPROP;
    strcpy((char*) msg.name, key);
    strcpy((char*) msg.value, value);

    return send_prop_msg(&msg);
}
–>
這裡就是通過一個普通的TCP(SOCK_STREAM)套接字進行通訊
static int send_prop_msg(prop_msg *msg)
{
    s = socket_local_client(PROP_SERVICE_NAME,
                            ANDROID_SOCKET_NAMESPACE_RESERVED,
                            SOCK_STREAM);
    if(s < 0) return -1;
   
    while((r = send(s, msg, sizeof(prop_msg), 0)) < 0) {
        if((errno == EINTR) || (errno == EAGAIN)) continue;
        break;
    }
    close(s);
    return r;
}

 

二、服務端是如何監聽並實現註程

 

main @ /system/core/init/init.c
int main(int argc, char **argv)
{
int property_set_fd = -1;

  /* read any property files on system or data and
   * fire up the property service.  This must happen
   * after the ro.foo properties are set above so
   * that /data/local.prop cannot interfere with them.
   */
  property_set_fd = start_property_service();

 

  // 將 property_set_fd 設定到poll監聽隊列

  ufds[0].fd = device_fd;
  ufds[0].events = POLLIN;
  ufds[1].fd = property_set_fd;
  ufds[1].events = POLLIN; 
 
  for(;;) {
   …
   nr = poll(ufds, fd_count, timeout);
  

// 監聽到有屬性服務請求需要處理
if (ufds[1].revents == POLLIN)
       handle_property_set_fd(property_set_fd); 
    …
  }
  return 0;
}

先看一下 start_property_service 如何實現的?
int start_property_service(void)
{
    int fd;

    load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
    load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
    load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
    /* Read persistent properties after all default values have been loaded. */
    load_persistent_properties();

    fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
    if(fd < 0) return -1;
    fcntl(fd, F_SETFD, FD_CLOEXEC);
    fcntl(fd, F_SETFL, O_NONBLOCK);

    listen(fd, 8);
    return fd;

}

 

ok,明白瞭吧,創建瞭一個SOCK_STREAM套接字並進入監聽listen狀態

handle_property_set_fd @ /system/core/init/property_service.c

void handle_property_set_fd(int fd)
{

//1、接收socket請求連接www.aiwalls.com
    if ((s = accept(fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
        return;
    }

 

//2、收取屬性請求數

 r = recv(s, &msg, sizeof(msg), 0);

close(s);

 

//3、處理屬情請求數據
    switch(msg.cmd) {
    case PROP_MSG_SETPROP:
    …
    if(memcmp(msg.name,"ctl.",4) == 0) {
    if (check_control_perms(msg.value, cr.uid, cr.gid)) {
                handle_control_message((char*) msg.name + 4, (char*) msg.value);
          }
      }else {
          if (check_perms(msg.name, cr.uid, cr.gid)) {
              property_set((char*) msg.name, (char*) msg.value);
          }           
      }
    }
}

由於請求消息是:ctl.start 則執行 handle_control_message 這個函數:
handle_control_message @ /system/core/init/init.c
void handle_control_message(const char *msg, const char *arg)
{
    if (!strcmp(msg,"start")) {
        msg_start(arg);
    } else if (!strcmp(msg,"stop")) {
        msg_stop(arg);
    } else {
        ERROR("unknown control msg '%s'\n", msg);
    }
}

static void msg_start(const char *name)
{
svc = service_find_by_name(name);

service_start(svc, args);
}

static void msg_stop(const char *name)
{
    struct service *svc = service_find_by_name(name);
    service_stop(svc);
}

看下上面的代碼大傢應該明白瞭吧,就是請求ServiceManager服務進行啟動或停止某個服務,這裡就是那是 bootanim 服務瞭。

還有一點為何 bootanim 在init.rc 腳本中沒有開機就啟動呢?請見 init.rc 腳本:
service bootanim /system/bin/bootanimation
    user graphics
    group graphics
    disabled
    oneshot

看到 disabled 沒有,這個關鍵字是在添加到 service_list 雙鍵表時使用:

#define SVC_DISABLED    0x01  /* do not autostart with class */
#define SVC_ONESHOT     0x02  /* do not restart on exit */
#define SVC_RUNNING     0x04  /* currently active */
#define SVC_RESTARTING  0x08  /* waiting to restart */
#define SVC_CONSOLE     0x10  /* requires console */
#define SVC_CRITICAL    0x20  /* will reboot into recovery if keeps crashing */
   
static void parse_line_service(struct parse_state *state, int nargs, char **args)
{
    kw = lookup_keyword(args[0]);
    switch (kw) {
    case K_disabled:
        svc->flags |= SVC_DISABLED;
        break;
    …
}

而在執行 service_start 及 service_stop 時都會判定這個 flags 值:

void service_stop(struct service *svc)
{
/* if the service has not yet started, prevent
         * it from auto-starting with its class
  */
svc->flags |= SVC_DISABLED;

}

初始啟動Service流程:
int do_class_start(int nargs, char **args)
{
        /* Starting a class does not start services
         * which are explicitly disabled.  They must
         * be started inpidually.
         */
    service_for_each_class(args[1], service_start_if_not_disabled);
    return 0;
}

static void service_start_if_not_disabled(struct service *svc)
{
    if (!(svc->flags & SVC_DISABLED)) {
        service_start(svc, NULL);
    }
}

這裡會決定這個 Service 是否初始開機啟機,通過這個 SVC_DISABLED flag即可判定。

 

還有一個補充說明一下:

ctr.start和ctr.stop系統屬性?

每一項服務必須在/init.rc中定義.Android系統啟動時,init守護進程將解析init.rc和啟動屬性服務,屬性“ ctl.start ”和“ ctl.stop ”是用來啟動和停止服務的。一旦收到設置“ ctrl.start ”屬性的請求,屬性服務將使用該屬性值作為服務名找到該服務,啟動該服務。這項服務的啟動結果將會放入“ init.svc.<服務名>“屬性中 。客戶端應用程序可以輪詢那個屬性值,以確定結果。

 

 

基本常用代碼寫法:

static const char DAEMON_NAME[]        = "dhcpcd";
static const char DAEMON_PROP_NAME[]   = "init.svc.dhcpcd";

 

 

int dhcp_stop(const char *interface)
{
    char result_prop_name[PROPERTY_KEY_MAX];
    const char *ctrl_prop = "ctl.stop";
    const char *desired_status = "stopped";

    …

    /* Stop the daemon and wait until it's reported to be stopped */
    property_set(ctrl_prop, DAEMON_NAME);
    if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) {
        return -1;
    }

}

 

摘自  andyhuabing的專欄 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *