Android 5.1 以太网服务启动过程

来源:互联网 发布:淘宝化妆品店铺介绍 编辑:程序博客网 时间:2024/06/07 01:03

转自:http://blog.csdn.net/moyu123456789/article/details/50573358

1.SystemServer简介

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

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

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



[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. import /init.${ro.zygote}.rc  
  2. service servicemanager /system/bin/servicemanager  
  3.     class core  
  4.     user system  
  5.     group system  
  6.     critical  
  7.     onrestart restart healthd  
  8.     onrestart restart zygote  
  9.     onrestart restart media  
  10.     onrestart restart surfaceflinger  
  11.     onrestart restart drm  
  12.   
  13. service surfaceflinger /system/bin/surfaceflinger  
  14.     class core  
  15.     user system  
  16.     group graphics drmrpc  
  17.     onrestart restart zygote  

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

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

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server  
  2.     class main  
  3.     socket zygote stream 660 root system  
  4.     onrestart write /sys/android_power/request_state wake  
  5.     onrestart write /sys/power/state on  
  6.     onrestart restart media  
  7.     onrestart restart netd  

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

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. public class ZygoteInit {  
  2.     private static final String TAG = "Zygote";  
  3.       
  4.     public static void main(String argv[]) {  
  5.         try {  
  6.             // Start profiling the zygote initialization.  
  7.             SamplingProfilerIntegration.start();  
  8.   
  9.             boolean startSystemServer = false;  
  10.             String socketName = "zygote";  
  11.             String abiList = null;  
  12.             for (int i = 1; i < argv.length; i++) {  
  13.                 if ("start-system-server".equals(argv[i])) {  
  14.                     startSystemServer = true;  
  15.                 } else if (argv[i].startsWith(ABI_LIST_ARG)) {  
  16.                     abiList = argv[i].substring(ABI_LIST_ARG.length());  
  17.                 } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {  
  18.                     socketName = argv[i].substring(SOCKET_NAME_ARG.length());  
  19.                 } else {  
  20.                     throw new RuntimeException("Unknown command line argument: " + argv[i]);  
  21.                 }  
  22.             }  
  23.   
  24.             if (abiList == null) {  
  25.                 throw new RuntimeException("No ABI list supplied.");  
  26.             }  
  27.   
  28.             registerZygoteSocket(socketName);  
  29.             EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,  
  30.                 SystemClock.uptimeMillis());  
  31.             preload();  
  32.             EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,  
  33.                 SystemClock.uptimeMillis());  
  34.   
  35.             // Finish profiling the zygote initialization.  
  36.             SamplingProfilerIntegration.writeZygoteSnapshot();  
  37.   
  38.             // Do an initial gc to clean up after startup  
  39.             gc();  
  40.   
  41.             // Disable tracing so that forked processes do not inherit stale tracing tags from  
  42.             // Zygote.  
  43.             Trace.setTracingEnabled(false);  
  44.   
  45.             if (startSystemServer) {  
  46.                 startSystemServer(abiList, socketName);/*启动SystemServer的地方*/  
  47.             }  
  48.   
  49.             Log.i(TAG, "Accepting command socket connections");  
  50.             runSelectLoop(abiList);  
  51.   
  52.             closeServerSocket();  
  53.         } catch (MethodAndArgsCaller caller) {  
  54.             caller.run();  
  55.         } catch (RuntimeException ex) {  
  56.             Log.e(TAG, "Zygote died with exception", ex);  
  57.             closeServerSocket();  
  58.             throw ex;  
  59.         }  
  60.     }  
 

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

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1.  /*  
  2.  * Prepare the arguments and fork for the system server process.  
  3.  */  
  4. private static boolean startSystemServer(String abiList, String socketName)  
  5.     int pid;  
  6.   
  7.     try {  
  8.         parsedArgs = new ZygoteConnection.Arguments(args);  
  9.         ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);  
  10.         ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);  
  11.   
  12.         /* Request to fork the system server process */  
  13.         pid = Zygote.forkSystemServer(/*这里fork了SystemServer*/  
  14.                 parsedArgs.uid, parsedArgs.gid,  
  15.                 parsedArgs.gids,  
  16.                 parsedArgs.debugFlags,  
  17.                 null,  
  18.                 parsedArgs.permittedCapabilities,  
  19.                 parsedArgs.effectiveCapabilities);  
  20.     } catch (IllegalArgumentException ex) {  
  21.         throw new RuntimeException(ex);  
  22.     }  
  23.   
  24.     /* For child process */  
  25.     if (pid == 0) {  
  26.         if (hasSecondZygote(abiList)) {  
  27.             waitForSecondaryZygote(socketName);  
  28.         }  
  29.   
  30.         handleSystemServerProcess(parsedArgs);  
  31.     }  
  32.   
  33.     return true;  
  34. }  

2.以太网服务的启动

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

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

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. public final class SystemServer {  
  2.     private static final String TAG = "SystemServer";  
  3.         .......................  
  4.     private static final String ETHERNET_SERVICE_CLASS =  
  5.             "com.android.server.ethernet.EthernetService";  
  6.  /**  
  7.      * The main entry point from zygote.  
  8.      */  
  9.     public static void main(String[] args) {  
  10.         new SystemServer().run();  
  11.     }  
  12.   
  13. private void run() {  
  14.         // Create the system service manager.  
  15.         mSystemServiceManager = new SystemServiceManager(mSystemContext);  
  16.         LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);  
  17.   
  18.         // Start services.  
  19. /* SystemServer中启动服务的三个方法 */  
  20.         try {  
  21.             startBootstrapServices();  
  22.             startCoreServices();  
  23.             startOtherServices(); /* 这个方法里启动了以太网服务进程 */  
  24.         } catch (Throwable ex) {  
  25.             Slog.e("System", "******************************************");  
  26.             Slog.e("System", "************ Failure starting system services", ex);  
  27.             throw ex;  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. private void startOtherServices() {  
  33. if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_ETHERNET)) {  
  34.                     mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS);  
  35.                 }  
  36. }  

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

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



0 0
原创粉丝点击