Android System Server进程源码分析 上


一 System Server

System Server是Zygote启动的第一个进程,它的核心功能是启动和管理Android系统的各类服务。

1.0 startSystemServer

private static boolean startSystemServer(String abiList, String socketName) // abiList为arm64-v8a,socketName为zygote
        throws MethodAndArgsCaller, RuntimeException {
    long capabilities = posixCapabilitiesAsBits( // Linux的Capabilities安全机制,可参考include/uapi/linux/capability.h
        OsConstants.CAP_BLOCK_SUSPEND, // 允许阻止系统挂起
        OsConstants.CAP_KILL, // 允许对不属于自己的进程发送信号
        OsConstants.CAP_NET_ADMIN, // 允许执行网络管理任务
        OsConstants.CAP_NET_BIND_SERVICE, // 允许绑定到小于1024的端口
        OsConstants.CAP_NET_BROADCAST, // 允许网络广播和多播访问
        OsConstants.CAP_NET_RAW, // 允许使用原始套接字
        OsConstants.CAP_SYS_MODULE, // 允许插入和删除内核模块
        OsConstants.CAP_SYS_NICE, // 允许提升优先级及设置其他进程的优先级
        OsConstants.CAP_SYS_RESOURCE, // 忽略资源限制
        OsConstants.CAP_SYS_TIME, // 允许改变系统时钟
        OsConstants.CAP_SYS_TTY_CONFIG // 允许配置TTY设备
    );
    /* 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,1021,1032,3001,3002,3003,3006,3007,3009,3010",
        "--capabilities=" + capabilities + "," + capabilities,
        "--nice-name=system_server", // 进程名是system_server
        "--runtime-args",
        "com.android.server.SystemServer",
    };
    ZygoteConnection.Arguments parsedArgs = null;

    int pid;

    try {
        parsedArgs = new ZygoteConnection.Arguments(args); // 将参数转化为Arguments格式
        ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
        ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);

        /* Request to fork the system server process */
        pid = Zygote.forkSystemServer( // // fork system_server进程
                parsedArgs.uid, parsedArgs.gid,
                parsedArgs.gids,
                parsedArgs.debugFlags,
                null,
                parsedArgs.permittedCapabilities,
                parsedArgs.effectiveCapabilities);
    } catch (IllegalArgumentException ex) {
        throw new RuntimeException(ex);
    }

    /* For child process */
    if (pid == 0) { // fork返回0,说明在子进程(system_server)中
        if (hasSecondZygote(abiList)) { // 判断是否存在第二个zygote需要启动,由于64位系统为了兼容32位应用程序,将同时启动zygote64和zygote,所以这里为true
            waitForSecondaryZygote(socketName); // 等待zygote_secondary启动完成
        }

        handleSystemServerProcess(parsedArgs); // 完成system_server进程剩余的工作
    }

    return true;
}

二 forkSystemServer

2.0 forkSystemServer
public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
        int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
    VM_HOOKS.preFork();
    int pid = nativeForkSystemServer(  // 调用native方法fork system_server进程
            uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
    // Enable tracing as soon as we enter the system_server.
    if (pid == 0) {
        Trace.setTracingEnabled(true); // 在system_server进程中重新使能Systrace追踪
    }
    VM_HOOKS.postForkCommon();
    return pid;
}

public void preFork() {
    Daemons.stop();  // 停止HeapTaskDaemon、ReferenceQueueDaemon、FinalizerDaemon、FinalizerWatchdogDaemon等四个Daemon子线程
    waitUntilAllThreadsStopped();  // 等待所有子线程结束
    token = nativePreFork(); // 完成一些运行时fork前期工作
}

public void postForkCommon() {
    Daemons.start();  // 启动HeapTaskDaemon、ReferenceQueueDaemon、FinalizerDaemon、FinalizerWatchdogDaemon等四个Daemon子线程
}

2.1 com_android_internal_os_Zygote_nativeForkSystemServer

nativeForkSystemServer对应JNI函数是com_android_internal_os_Zygote_nativeForkSystemServer
static jint com_android_internal_os_Zygote_nativeForkSystemServer(
        JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
        jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
        jlong effectiveCapabilities) {
<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值