surfaceflinger Bootanimation 服务init 启动

来源:互联网 发布:js中类的概念 编辑:程序博客网 时间:2024/06/16 00:08

在Init.rc中,用service关键字声明了一系列服务.

init.rc对service的说明如下:(详见system/core/init/readme.txt)

Services--------Services are programs which init launches and (optionally) restartswhen they exit.  

Services take the form of:service <name> <pathname> [ <argument> ]*   <option>   <option>   ...

Options-------Options are modifiers to services.  

They affect how and when initruns the service.

critical   

This is a device-critical service. If it exits more than four times in   four minutes, the device will reboot into recovery mode.

disabled  

 This service will not automatically start with its class.   It must be explicitly started by name.

setenv <name> <value>   

Set the environment variable <name> to <value> in the launched process.

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

Create a unix domain socket named /dev/socket/<name> and pass   its fd to the launched process.  <type> must be "dgram", "stream" or "seqpacket".   

User and group default to 0.

user <username>   

Change to username before exec'ing this service.   Currently defaults to root.  (??? probably should default to nobody)   Currently, if your process requires linux capabilities then you cannot use   this command. You must instead request the capabilities in-process while   still root, and then drop to your desired uid.

group <groupname> [ <groupname> ]*   

Change to groupname before exec'ing this service.  Additional   groupnames beyond the (required) first one are used to set the   supplemental groups of the process (via setgroups()).   Currently defaults to root.  (??? probably should default to nobody)oneshot   Do not restart the service when it exits.

class <name>   

Specify a class name for the service.  All services in a   named class may be started or stopped together.  A service   is in the class "default" if one is not specified via the   class option.

onrestart    Execute a Command (see below) when service restarts.

可以看出,service中比较关键的几个选项是类别(class),启动选项(oneshot disabled,critical),用户组(group,user)。

以surfaceflinger为例:

service surfaceflinger /system/bin/surfaceflinger    

class main    

user system    

group graphics    

onrestart restart zygote

它的类别是main,用户是system,属于graphics ,且没有声明disabled,所以在启动main这个类别的时候,surfaceflinger就会被启动。

main类别是在哪里启动的呢?

搜索class_start关键字,在On boot的时候,就会启动了

on boot

  #省略无关.....

 class_start core    

 class_start main

readme中对class_start有如下说明:

class_start <serviceclass> 

Start all services of the specified class if they are   not already running.

class_stop <serviceclass> 

Stop all services of the specified class if they are   currently running. 

所以,在系统启动触发boot这个trigger之后,core和main类别的没有声明为disabled的守护进程就被系统启动了,

具体的过程还得去看init对init.rc文件的解析过程,暂且放着。

那么还有一个疑问,那些声明了为disabled的守护进程是怎么启动的呢?

以Bootanimation为例:

service bootanim /system/bin/bootanimation    

class main    

user graphics    

group graphics    

disabled    

oneshot

它也是main组别的,用户和组均是graphics,且是disabled,也就是class_start main的时候不会启动它,它是在哪里启动的呢?

之前的博客上有讲过,是在surfaceflinger的readytorun函数中调用的:(也可以直接搜索bootanim关键字)

[cpp] view plaincopy
  1. status_t SurfaceFlinger::readyToRun()  
  2. {  
  3. //省略   
  4.     if(SurfaceFlinger::sBootanimEnable){  
  5.     // start boot animation   
  6.     LOGI("start bootanim!");  
  7.     property_set("ctl.start""bootanim");  
  8.     }  
  9.     return NO_ERROR;  
  10. }  


实际调用的是 system/core/libcutils/Properties.c的property_set函数:
[cpp] view plaincopy
  1. #ifdef HAVE_LIBC_SYSTEM_PROPERTIES   
  2.   
  3. #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_   
  4. #include <sys/_system_properties.h>   
  5.   
  6. int property_set(const char *key, const char *value)  
  7. {  
  8.     return __system_property_set(key, value);  
  9. }  

位于bionic/libc/bionic/system_properties.c


[cpp] view plaincopy
  1. int __system_property_set(const char *key, const char *value)  
  2. {  
  3.     int err;  
  4.     int tries = 0;  
  5.     int update_seen = 0;  
  6.     prop_msg msg;  
  7.   
  8.     if(key == 0) return -1;  
  9.     if(value == 0) value = "";  
  10.     if(strlen(key) >= PROP_NAME_MAX) return -1;  
  11.     if(strlen(value) >= PROP_VALUE_MAX) return -1;  
  12.   
  13.     memset(&msg, 0, sizeof msg);  
  14.     msg.cmd = PROP_MSG_SETPROP;  
  15.     strlcpy(msg.name, key, sizeof msg.name);  
  16.     strlcpy(msg.value, value, sizeof msg.value);  
  17.   
  18.     err = send_prop_msg(&msg);  
  19.     if(err < 0) {  
  20.         return err;  
  21.     }  
  22.   
  23.     return 0;  
  24. }  


将key value cmd 打包成msg,交由send_prop_msg处理,在此函数中,将msg打包,通过socket发送给对端的property_service。

[cpp] view plaincopy
  1. static int send_prop_msg(prop_msg *msg)  
  2. {  
  3.     struct pollfd pollfds[1];  
  4.     struct sockaddr_un addr;  
  5.     socklen_t alen;  
  6.     size_t namelen;  
  7.     int s;  
  8.     int r;  
  9.     int result = -1;  
  10.     s = socket(AF_LOCAL, SOCK_STREAM, 0);  
  11.     if(s < 0) {  
  12.         return result;  
  13.     }  
  14.   
  15.     memset(&addr, 0, sizeof(addr));  
  16.     namelen = strlen(property_service_socket);//property_service的socket位于 /dev/socket/property_service  
  17.     strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);  
  18.     addr.sun_family = AF_LOCAL;  
  19.     alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;  
  20.   
  21.     if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen) < 0)) {  
  22.         close(s);  
  23.         return result;  
  24.     }  
  25.     r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));  
  26.   
  27.     if(r == sizeof(prop_msg)) {  
  28.         // We successfully wrote to the property server but now we  
  29.         // wait for the property server to finish its work.  It  
  30.         // acknowledges its completion by closing the socket so we  
  31.         // poll here (on nothing), waiting for the socket to close.  
  32.         // If you 'adb shell setprop foo bar' you'll see the POLLHUP  
  33.         // once the socket closes.  Out of paranoia we cap our poll  
  34.         // at 250 ms.   
  35.         pollfds[0].fd = s;  
  36.         pollfds[0].events = 0;  
  37.         r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));  
  38.         if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {  
  39.             result = 0;  
  40.         } else {  
  41.             // Ignore the timeout and treat it like a success anyway.  
  42.             // The init process is single-threaded and its property  
  43.             // service is sometimes slow to respond (perhaps it's off  
  44.             // starting a child process or something) and thus this  
  45.             // times out and the caller thinks it failed, even though  
  46.             // it's still getting around to it.  So we fake it here,  
  47.             // mostly for ctl.* properties, but we do try and wait 250  
  48.             // ms so callers who do read-after-write can reliably see  
  49.             // what they've written.  Most of the time.   
  50.             // TODO: fix the system properties design.   
  51.             result = 0;  
  52.         }  
  53.     }  
  54.   
  55.     close(s);  
  56.     return result;  
  57. }  

到这里有必要说下property_service,这货是init进程创建的,在init.c的main函数中,    

queue_builtin_action(property_service_init_action, "property_service_init");

会调用到

[cpp] view plaincopy
  1. void start_property_service(void)  
  2. {  
  3.     int fd;  
  4.   
  5.     load_properties_from_file(PROP_PATH_SYSTEM_BUILD);  
  6.     load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);  
  7.     load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);  
  8.     /* Read persistent properties after all default values have been loaded. */  
  9.     load_persistent_properties();  
  10.   
  11.     fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);  
  12.     if(fd < 0) return;  
  13.     fcntl(fd, F_SETFD, FD_CLOEXEC);  
  14.     fcntl(fd, F_SETFL, O_NONBLOCK);  
  15.   
  16.     listen(fd, 8);  
  17.     property_set_fd = fd; //记住propservice的fd   
  18. }  
就是创建了socket,注意这里的PROP_SERVICE_NAME =“property_service”

property_service是怎么接收到调用的呢?

init.c的main函数的最后,会有轮询:

[cpp] view plaincopy
  1. void main ()  
  2. {  
  3.   //.....   
  4.    
  5.   for(;;) {  
  6.         int nr, i, timeout = -1;  
  7.   
  8.         execute_one_command();  
  9.         restart_processes();  
  10.   
  11.         if (!property_set_fd_init && get_property_set_fd() > 0) { //检查property_service是否已经初始化好了  
  12.             ufds[fd_count].fd = get_property_set_fd();  
  13.             ufds[fd_count].events = POLLIN;  
  14.             ufds[fd_count].revents = 0;  
  15.             fd_count++;          //如果OK,则将计数+1   
  16.             property_set_fd_init = 1;  
  17.         }  
  18.         if (!signal_fd_init && get_signal_fd() > 0) {  
  19.             ufds[fd_count].fd = get_signal_fd();  
  20.             ufds[fd_count].events = POLLIN;  
  21.             ufds[fd_count].revents = 0;  
  22.             fd_count++;  
  23.             signal_fd_init = 1;  
  24.         }  
  25.         if (!keychord_fd_init && get_keychord_fd() > 0) {  
  26.             ufds[fd_count].fd = get_keychord_fd();  
  27.             ufds[fd_count].events = POLLIN;  
  28.             ufds[fd_count].revents = 0;  
  29.             fd_count++;  
  30.             keychord_fd_init = 1;  
  31.         }  
  32.   
  33.         if (process_needs_restart) {  
  34.             timeout = (process_needs_restart - gettime()) * 1000;  
  35.             if (timeout < 0)  
  36.                 timeout = 0;  
  37.         }  
  38.         if (!action_queue_empty() || cur_action)  
  39.             timeout = 0;  
  40.   
  41. #if BOOTCHART   
  42.         if (bootchart_count > 0) {  
  43.             if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)  
  44.                 timeout = BOOTCHART_POLLING_MS;  
  45.             if (bootchart_step() < 0 || --bootchart_count == 0) {  
  46.                 bootchart_finish();  
  47.                 bootchart_count = 0;  
  48.             }  
  49.         }  
  50. #endif   
  51.   
  52.         nr = poll(ufds, fd_count, timeout);//轮询   
  53.         if (nr <= 0)  
  54.             continue;  
  55.   
  56.         for (i = 0; i < fd_count; i++) {  
  57.             if (ufds[i].revents == POLLIN) {  
  58.                 if (ufds[i].fd == get_property_set_fd()) //如果是property_service,则调用<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">handle_property_set_fd</SPAN>  
  59.                     handle_property_set_fd();  
  60.                 else if (ufds[i].fd == get_keychord_fd())  
  61.                     handle_keychord();  
  62.                 else if (ufds[i].fd == get_signal_fd())  
  63.                     handle_signal();  
  64.             }  
  65.         }  
  66.     }  
  67.   
  68.     return 0;  
  69. }  

也就是说,当有人给/dev/socket/property_service发送消息后,这里就会调用handle_property_set_fd来处理。


[cpp] view plaincopy
  1. void handle_property_set_fd()  
  2. {  
  3.     prop_msg msg;  
  4.     int s;  
  5.     int r;  
  6.     int res;  
  7.     struct ucred cr;  
  8.     struct sockaddr_un addr;  
  9.     socklen_t addr_size = sizeof(addr);  
  10.     socklen_t cr_size = sizeof(cr);  
  11.   
  12.     if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {  
  13.         return;  
  14.     }  
  15.     /* Check socket options here */  
  16.     if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {  
  17.         close(s);  
  18.         ERROR("Unable to recieve socket options\n");  
  19.         return;  
  20.     }  
  21.   
  22.     r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));  
  23.     if(r != sizeof(prop_msg)) {  
  24.         ERROR("sys_prop: mis-match msg size recieved: %d expected: %d errno: %d\n",  
  25.               r, sizeof(prop_msg), errno);  
  26.         close(s);  
  27.         return;  
  28.     }  
  29.     switch(msg.cmd) { //根据传入的msg的cmd分别做处理,我们传入的是setprop,且是ctl.start  
  30.     case PROP_MSG_SETPROP:  
  31.         msg.name[PROP_NAME_MAX-1] = 0;  
  32.         msg.value[PROP_VALUE_MAX-1] = 0;  
  33.   
  34.         if(memcmp(msg.name,"ctl.",4) == 0) {  
  35.             // Keep the old close-socket-early behavior when handling  
  36.             // ctl.* properties.   
  37.             close(s);  
  38.             if (check_control_perms(msg.value, cr.uid, cr.gid)) { //检查权限,system和root可以无视,其他只有对应组和user完全相同才能启动  
  39.                 handle_control_message((char*) msg.name + 4, (char*) msg.value);//处理请求,传入的字符串是start  
  40.             } else {  
  41.                 ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",  
  42.                         msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);  
  43.             }  
  44.         } else {  
  45.             if (check_perms(msg.name, cr.uid, cr.gid)) {  
  46.                 property_set((char*) msg.name, (char*) msg.value);  
  47.             } else {  
  48.                 ERROR("sys_prop: permission denied uid:%d  name:%s\n",  
  49.                       cr.uid, msg.name);  
  50.             }  
  51.   
  52.             // Note: bionic's property client code assumes that the  
  53.             // property server will not close the socket until *AFTER*  
  54.             // the property is written to memory.   
  55.             close(s);  
  56.         }  
  57.         break;  
  58.     default:  
  59.         close(s);  
  60.         break;  
  61.     }  
  62. }  

handle_control_message位于 system/core/init/init.c


[cpp] view plaincopy
  1. void handle_control_message(const char *msg, const char *arg)  
  2. {  
  3.     if (!strcmp(msg,"start")) {//我们是start  
  4.         msg_start(arg);   
  5.     } else if (!strcmp(msg,"stop")) {  
  6.         msg_stop(arg);  
  7.     } else if (!strcmp(msg,"restart")) {  
  8.         msg_stop(arg);  
  9.         msg_start(arg);  
  10.     } else {  
  11.         ERROR("unknown control msg '%s'\n", msg);  
  12.     }  
  13. }  
[cpp] view plaincopy
  1. static void msg_start(const char *name)  
  2. {  
  3.     struct service *svc;  
  4.     char *tmp = NULL;  
  5.     char *args = NULL;  
  6. //从之前init.rc中解析的servicelist中找到对应的service   
  7.  if (!strchr(name, ':'))   
  8.  svc = service_find_by_name(name);   
  9.  else { tmp = strdup(name);   
  10.  args = strchr(tmp, ':');   
  11.  *args = '\0'; args++;   
  12.  svc = service_find_by_name(tmp); }   
  13.  if (svc) { service_start(svc, args); //启动service  
  14.   }   
  15.   else   
  16.   {   
  17.   ERROR("no such service '%s'\n", name);   
  18.   }   
  19.   if (tmp)  
  20.    free(tmp);  
  21.     
  22.    }  

接下来看下service_start这个函数,在这个函数中,做的主要几件工作是添加环境变量,fork出pid,创建socket,再exec

[cpp] view plaincopy
  1. void service_start(struct service *svc, const char *dynamic_args)  
  2. {  
  3.     struct stat s;  
  4.     pid_t pid;  
  5.     int needs_console;  
  6.     int n;  
  7.   
  8.         /* starting a service removes it from the disabled or reset 
  9.          * state and immediately takes it out of the restarting 
  10.          * state if it was in there 
  11.          */  
  12.     svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));  
  13.     svc->time_started = 0;  
  14.   
  15.         /* running processes require no additional work -- if 
  16.          * they're in the process of exiting, we've ensured 
  17.          * that they will immediately restart on exit, unless 
  18.          * they are ONESHOT 
  19.          */  
  20.     if (svc->flags & SVC_RUNNING) { //如果service当前已经在运行,则return  
  21.         return;  
  22.     }  
  23.     needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; //如果service有console的声明  
  24.     if (needs_console && (!have_console)) {  
  25.         ERROR("service '%s' requires console\n", svc->name);  
  26.         svc->flags |= SVC_DISABLED;  
  27.         return;  
  28.     }  
  29.   
  30.     if (stat(svc->args[0], &s) != 0) {  
  31.         ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);  
  32.         svc->flags |= SVC_DISABLED;  
  33.         return;  
  34.     }  
  35.   
  36.     if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {  
  37.         ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",  
  38.                svc->args[0]);  
  39.         svc->flags |= SVC_DISABLED;  
  40.         return;  
  41.     }  
  42.   
  43.     NOTICE("starting '%s'\n", svc->name);  
  44.   
  45.     pid = fork(); //fork出pid   
  46.   
  47.     if (pid == 0) {  
  48.         struct socketinfo *si;  
  49.         struct svcenvinfo *ei;  
  50.         char tmp[32];  
  51.         int fd, sz;  
  52.   
  53.         if (properties_inited()) {//property area 在init的main中在init之前已经初始化完毕  
  54.             get_property_workspace(&fd, &sz); workspace位于/dev/__properties__  
  55.             sprintf(tmp, "%d,%d", dup(fd), sz);  
  56.             add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);  
  57.         }  
  58.   
  59.         for (ei = svc->envvars; ei; ei = ei->next)//如果在service的声明中有setenv选项,则会将这些添加到env中  
  60.             add_environment(ei->name, ei->value);  
  61.   
  62.         for (si = svc->sockets; si; si = si->next) {//如果在service的声明中有socket选项的,则会创建socket,如zygote中就有声明     socket zygote stream 666  
  63.             int socket_type = (  
  64.                     !strcmp(si->type, "stream") ? SOCK_STREAM :  
  65.                         (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));  
  66.             int s = create_socket(si->name, socket_type,  
  67.                                   si->perm, si->uid, si->gid);  
  68.             if (s >= 0) {  
  69.                 publish_socket(si->name, s);//将socket加入env中  
  70.             }  
  71.         }  
  72.         if (svc->ioprio_class != IoSchedClass_NONE) { //init.rc中没找到例子,也没弄清楚是啥意思 - -...  
  73.             if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {  
  74.                 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",  
  75.                       getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));  
  76.             }  
  77.         }  
  78.   
  79.         if (needs_console) {  
  80.             setsid();  
  81.             open_console();  
  82.         } else {  
  83.             zap_stdio();  
  84.         }  
  85.   
  86. #if 0   
  87.         for (n = 0; svc->args[n]; n++) {  
  88.             INFO("args[%d] = '%s'\n", n, svc->args[n]);  
  89.         }  
  90.         for (n = 0; ENV[n]; n++) {  
  91.             INFO("env[%d] = '%s'\n", n, ENV[n]);  
  92.         }  
  93. #endif   
  94.   
  95.         setpgid(0, getpid());  
  96.     /* as requested, set our gid, supplemental gids, and uid */  
  97.         if (svc->gid) { //service的gid和uid是通过传入的user和group映射得到的,详细可以看init_parser.c的parse_line_service函数中的    case K_user:。这里简单的说下:gid和uid都是通过decode_uid来得到的,其实就是从android_ids这个全局变量中查找到对应的组/用户对应的值。如 graphics就是AID_GRAPHICS,所以bootanim的uid和gid都是1003  
  98.             if (setgid(svc->gid) != 0) {  
  99.                 ERROR("setgid failed: %s\n", strerror(errno));  
  100.                 _exit(127);  
  101.             }  
  102.         }  
  103.         if (svc->nr_supp_gids) {  
  104.             if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {  
  105.                 ERROR("setgroups failed: %s\n", strerror(errno));  
  106.                 _exit(127);  
  107.             }  
  108.         }  
  109.         if (svc->uid) {  
  110.             if (setuid(svc->uid) != 0) {  
  111.                 ERROR("setuid failed: %s\n", strerror(errno));  
  112.                 _exit(127);  
  113.             }  
  114.         }  
  115.  //接下来就是exec了,实际就是执行了bin文件,并将参数传入,就会进入BootAnimation_main.cpp的main函数  
  116.         if (!dynamic_args) {  
  117.             if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {  
  118.                 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));  
  119.             }  
  120.         } else {  
  121.             char *arg_ptrs[INIT_PARSER_MAXARGS+1];  
  122.             int arg_idx = svc->nargs;  
  123.             char *tmp = strdup(dynamic_args);  
  124.             char *next = tmp;  
  125.             char *bword;  
  126.   
  127.             /* Copy the static arguments */  
  128.             memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));  
  129.   
  130.             while((bword = strsep(&next, " "))) {  
  131.                 arg_ptrs[arg_idx++] = bword;  
  132.                 if (arg_idx == INIT_PARSER_MAXARGS)  
  133.                     break;  
  134.             }  
  135.             arg_ptrs[arg_idx] = '\0';  
  136.             execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);  
  137.         }  
  138.         _exit(127);  
  139.     }  
  140.   
  141.     if (pid < 0) {  
  142.         ERROR("failed to start '%s'\n", svc->name);  
  143.         svc->pid = 0;  
  144.         return;  
  145.     }  
  146.     svc->time_started = gettime();  
  147.     svc->pid = pid;  
  148.     svc->flags |= SVC_RUNNING;//标记service的状态和启动时间   
  149.   
  150.     if (properties_inited())  
  151.         notify_service_state(svc->name, "running");//通知服务已经运行了  
  152. }  

这样,通过property_set,surfaceflinger就把bootanim给起来了,播放开机动画.


前面说过,在on boot的时候,通过class_start把main和core类型的service起来,下面看下过程。
在我看来,init启动的过程中,最精髓的过程就是对init.rc的解析,是通过init_parser.c来实现的。
对service的解析是parse_service和parse_line_service函数,将service加入servicelist,并通过关键字解析出值,将service的结构体填充完毕。
各个关键字对应的命令可以看Keywords.h,我们可以看到如下定义:
    KEYWORD(class_start, COMMAND, 1, do_class_start)

也就是说,当on boot被触发后,就会调用do_class_start来启动对应的service

[cpp] view plaincopy
  1. int do_class_start(int nargs, char **args)  
  2. {  
  3.         /* Starting a class does not start services 
  4.          * which are explicitly disabled.  They must 
  5.          * be started individually. 
  6.          */  
  7.     service_for_each_class(args[1], service_start_if_not_disabled);  
  8.     return 0;  
  9. }  
  10. void service_for_each_class(const char *classname,  
  11.                             void (*func)(struct service *svc))  
  12. {  
  13.     struct listnode *node;  
  14.     struct service *svc;  
  15.     list_for_each(node, &service_list) {  
  16.         svc = node_to_item(node, struct service, slist);  
  17.         if (!strcmp(svc->classname, classname)) {  
  18.             func(svc);  
  19.         }  
  20.     }  
  21. }  
  22.   
  23. static void service_start_if_not_disabled(struct service *svc)  
  24. {  
  25.     if (!(svc->flags & SVC_DISABLED)) {  
  26.         service_start(svc, NULL);  
  27.     }  
  28. }  

实际上就是,通过classname从servicelist中查找到对应的service,如果service不是disabled的,就调用service_start来启动它。

这个就和上面的通过ctl.start的流程最后一样了。


0 0
原创粉丝点击