SystemServer的启动过程

了解SystemServer之前,我们知道

  • Android系统是基于Linux内核的,而在Linux系统中,所有的进程都是init进程的子孙进程,也就是说,所有的进程都是直接或者间接地由init进程fork出来的。Zygote进程也不例外,它是在系统启动的过程,由init进程创建的。在系统启动脚本system/core/rootdir/init.rc文件中。
  • 系统启动时init进程会创建Zygote进程,Zygote进程负责后续Android应用程序框架层的其它进程的创建和启动工作。
  • Zygote进程会首先创建一个SystemServer进程,SystemServer进程负责启动系统的关键服务,如AMS,PMS等。
  • 下面查看一波源码,源码版本是2.3.7。

本文分析流程图

这里写图片描述

1>system/core/rootdir/init.rc

去打开一个叫做app_process的文件。

2>frameworks/base/cmds/app_process.rc



这里说下,从这一步到下一步之间这段过程都是在准备java vm以及android app运行环境,这期间的过程涉及比较多的细节,暂时TODO,我先理解从这一步到下一步则抵达了ZygoteInit.java。

3>frameworks\base\core\java\com\android\internal\os\ZygoteInit.java

我们看zygoteInit的main方法。精简之后就是这样:

        public static void main(String argv[]) {
            try {

                ......

                if (argv[1].equals("true")) {
                    startSystemServer();
                } else if (!argv[1].equals("false")) {
                    throw new RuntimeException(argv[0] + USAGE_STRING);
                }

                ......

            } catch (MethodAndArgsCaller caller) {

                ......

            } catch (RuntimeException ex) {

                ......

            }
        }

startSystemServer()方法:
1)Prepare the arguments and fork for the system server process.,准备fork系统服务进程的参数。
2)Zygote.forkSystemServer(...)调用forkSystemServer方法发起一个fork请求。进入forkSystemServer方法。


    /**
     * Prepare the arguments and fork for the system server process.
     */
    private static boolean startSystemServer()
            throws MethodAndArgsCaller, RuntimeException {
        /* Hardcoded command line to start the system server */
        String args[] = {
            "--setuid=1000",
            "--setgid=1000",
            "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003",
            "--capabilities=130104352,130104352",
            "--runtime-init",
            "--nice-name=system_server",
            "com.android.server.SystemServer",
        };
        ZygoteConnection.Arguments parsedArgs = null;

        int pid;

        try {
            parsedArgs = new ZygoteConnection.Arguments(args);

            ......

            if ("1".equals(SystemProperties.get("ro.debuggable")))
                debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;

            /* Request to fork the system server process */
            pid = Zygote.forkSystemServer(
                    parsedArgs.uid, parsedArgs.gid,
                    parsedArgs.gids, debugFlags, null,
                    parsedArgs.permittedCapabilities,
                    parsedArgs.effectiveCapabilities);

        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        }

        /* For child process */
        if (pid == 0) {
            handleSystemServerProcess(parsedArgs);
        }

        return true;
    }

forkSystemServer调用到了Zygote.javanative public static int forkAndSpecialize()方法:

    /**
     * ......
     *
     * @return 0 if this is the child, pid of the child
     * if this is the parent, or -1 on error.
     */
    native public static int forkSystemServer(int uid, int gid,
            int[] gids, int debugFlags, int[][] rlimits,
            long permittedCapabilities, long effectiveCapabilities);

我们去看看这个forkSystemServer方法的c++实现:
位置:dalvik\vm\native\dalvik_system_Zygote.c

    static void Dalvik_dalvik_system_Zygote_forkSystemServer(
            const u4* args, JValue* pResult)
    {
        pid_t pid;
        pid = forkAndSpecializeCommon(args, true);

        /* The zygote process checks whether the child process has died or not. */
        if (pid > 0) {
            int status;

            LOGI("System server process %d has been created", pid);
            gDvm.systemServerPid = pid;
            /* There is a slight window that the system server process has crashed
             * but it went unnoticed because we haven't published its pid yet. So
             * we recheck here just to make sure that all is well.
             */
            if (waitpid(pid, &status, WNOHANG) == pid) {
                LOGE("System server process %d has died. Restarting Zygote!", pid);
                kill(getpid(), SIGKILL);
            }
        }
        RETURN_INT(pid);
    }

可以看到这个方法forkAndSpecializeCommon(args, true);传递了之前定义的args,forkAndSpecializeCommon这个方法代码我就不贴了,我们只需要知道这个方法内部又调用到了fork方法:

int  fork(void)
{
    int  ret;

    /* Posix mandates that the timers of a fork child process be
     * disarmed, but not destroyed. To avoid a race condition, we're
     * going to stop all timers now, and only re-start them in case
     * of error, or in the parent process
     */
    __timer_table_start_stop(1);
    ret = __fork();
    if (ret != 0) {  /* not a child process */
        __timer_table_start_stop(0);
    } else {
        /*
         * Newly created process must update cpu accounting.
         * Call cpuacct_add passing in our uid, which will take
         * the current task id and add it to the uid group passed
         * as a parameter.
         */
        cpuacct_add(getuid());
    }
    return ret;
}

再回顾一下我们之前的args参数:

   private static boolean startSystemServer()
            throws MethodAndArgsCaller, RuntimeException {
        /* Hardcoded command line to start the system server */
        String args[] = {
            "--setuid=1000",
            "--setgid=1000",
            "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003",
            "--capabilities=130104352,130104352",
            "--runtime-init",
            "--nice-name=system_server",
            "com.android.server.SystemServer",
        };
        ......

        return true;
    }

com.android.server.SystemServer这也是我们判定当前fork的就是SystemServer进程的依据。
至此,SystemServer已经起来了。它会启动各种各样的服务,比如著名的AMS,PMS,WMS等,但是本文先不讨论。等下篇讲解Binder的时候再来看这部分代码。

猜你喜欢

转载自blog.csdn.net/user11223344abc/article/details/80625185