Android 5.1 以太网服务启动过程

1.SystemServer简介

Android系统中的好多功能能够运行起来,在代码层面好多都是以服务的方式实现的。而几乎所有的服务都是在SystemServer中创建的。SystemServer作为Android系统的一个核心进程,它是在zygote进程中孕育出来的。

那么zygote进程是怎么来的呢?再次我稍作解释。

我们知道,Android系统是以Linux为内核的,在Linux系统中,所有的进程无一例外的都是由init进程直接或者间接创建的,也就是说所有的进程都是init进程的后代(这个和我们现在所讲的,中国人都是炎黄子孙一个道理,炎黄相当于是init进程,我们这些人都是其子孙进程)。那么zygote进程也不例外,它就是在系统启动过程中由init进程创建的。在系统启动脚本system/core/rootdir/init.rc文件中,我们可以看到启动Zygote进程的脚本命令:



import /init.${ro.zygote}.rc
service servicemanager /system/bin/servicemanager
    class core
    user system
    group system
    critical
    onrestart restart healthd
    onrestart restart zygote
    onrestart restart media
    onrestart restart surfaceflinger
    onrestart restart drm

service surfaceflinger /system/bin/surfaceflinger
    class core
    user system
    group graphics drmrpc
    onrestart restart zygote

rootdir中还有4个和zygote相关的脚本:init.zygote32.rcinit.zygote32_64.rcinit.zygote64.rcinit.zygote64_32.rc,这个是为了区分64位和32才这么做的。

我们可以看一下init.zygote32.rc中的内容:

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
    class main
    socket zygote stream 660 root system
    onrestart write /sys/android_power/request_state wake
    onrestart write /sys/power/state on
    onrestart restart media
    onrestart restart netd

那么SystemServer又是怎样创建的呢?答案是:在ZygoteInit中创建的,看下边的代码。

public class ZygoteInit {
    private static final String TAG = "Zygote";
    
    public static void main(String argv[]) {
        try {
            // Start profiling the zygote initialization.
            SamplingProfilerIntegration.start();

            boolean startSystemServer = false;
            String socketName = "zygote";
            String abiList = null;
            for (int i = 1; i < argv.length; i++) {
                if ("start-system-server".equals(argv[i])) {
                    startSystemServer = true;
                } else if (argv[i].startsWith(ABI_LIST_ARG)) {
                    abiList = argv[i].substring(ABI_LIST_ARG.length());
                } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
                    socketName = argv[i].substring(SOCKET_NAME_ARG.length());
                } else {
                    throw new RuntimeException("Unknown command line argument: " + argv[i]);
                }
            }

            if (abiList == null) {
                throw new RuntimeException("No ABI list supplied.");
            }

            registerZygoteSocket(socketName);
            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
                SystemClock.uptimeMillis());
            preload();
            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
                SystemClock.uptimeMillis());

            // Finish profiling the zygote initialization.
            SamplingProfilerIntegration.writeZygoteSnapshot();

            // Do an initial gc to clean up after startup
            gc();

            // Disable tracing so that forked processes do not inherit stale tracing tags from
            // Zygote.
            Trace.setTracingEnabled(false);

            if (startSystemServer) {
                startSystemServer(abiList, socketName);/*启动SystemServer的地方*/
            }

            Log.i(TAG, "Accepting command socket connections");
            runSelectLoop(abiList);

            closeServerSocket();
        } catch (MethodAndArgsCaller caller) {
            caller.run();
        } catch (RuntimeException ex) {
            Log.e(TAG, "Zygote died with exception", ex);
            closeServerSocket();
            throw ex;
        }
    }
 

    我们在来看看startSystemServer方法的代码实现:

     /*
     * Prepare the arguments and fork for the system server process.
     */
    private static boolean startSystemServer(String abiList, String socketName)
        int pid;

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

            /* Request to fork the system server process */
            pid = Zygote.forkSystemServer(/*这里fork了SystemServer*/
                    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) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }

            handleSystemServerProcess(parsedArgs);
        }

        return true;
    }

2.以太网服务的启动

以太网在Android中能够运行,也是作为一个系统级服务来执行的,那么自然我们能联想到它是在SystemServer中启动的。代码路径如下:

frameworks/base/services/java/com/android/server/SystemServer.java

public final class SystemServer {
    private static final String TAG = "SystemServer";
        .......................
    private static final String ETHERNET_SERVICE_CLASS =
            "com.android.server.ethernet.EthernetService";
 /**
     * The main entry point from zygote.
     */
    public static void main(String[] args) {
        new SystemServer().run();
    }

private void run() {
        // Create the system service manager.
        mSystemServiceManager = new SystemServiceManager(mSystemContext);
        LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

        // Start services.
/* SystemServer中启动服务的三个方法 */
        try {
            startBootstrapServices();
            startCoreServices();
            startOtherServices(); /* 这个方法里启动了以太网服务进程 */
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        }
    }
}

private void startOtherServices() {
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_ETHERNET)) {
                    mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS);
                }
}

至此,Android以太网服务进程就算启动起来了

以太网服务启动后,以太网卡对应的端口又是怎样up起来的呢?又是怎样分配到地址的呢?我的另一篇博文《Android5.1系统启动过程中启动有线网卡并为其分配静态IP地址》http://blog.csdn.net/moyu123456789/article/details/50002099中有比较明确的解释。









你可能感兴趣的:(源码,android,网卡,以太网)