bug描述:设置静态IP成功后,机器突然断电,然后在上电开机,发现机器一直卡在开机动画,无法成功进入桌面
第一时间抓取日志分析,Log如下:
08-13 11:26:42.455 2803 2803 I EthernetServiceImpl: Starting Ethernet service
08-13 11:26:42.457 2803 2924 D ConnectivityService: Got NetworkProvider Messenger for EthernetNetworkFactory
08-13 11:26:42.458 2803 2803 E IpConfigStore: Bad version on IP configuration file, ignore read
08-13 11:26:42.460 2803 2803 V SystemServerTiming: OnBootPhase_500_com.android.server.ConnectivityServiceInitializer took to complete: 5ms
08-13 11:26:42.460 2803 2803 E System : ******************************************
08-13 11:26:42.461 2803 2803 E System : ************ Failure starting system services
08-13 11:26:42.461 2803 2803 E System : java.lang.RuntimeException: Failed to boot service com.android.server.ConnectivityServiceInitializer: onBootPhase threw an exception during phase 500
08-13 11:26:42.461 2803 2803 E System : at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:297)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.SystemServer.startOtherServices(SystemServer.java:2694)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.SystemServer.run(SystemServer.java:939)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.SystemServer.main(SystemServer.java:649)
08-13 11:26:42.461 2803 2803 E System : at java.lang.reflect.Method.invoke(Native Method)
08-13 11:26:42.461 2803 2803 E System : at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
08-13 11:26:42.461 2803 2803 E System : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:914)
08-13 11:26:42.461 2803 2803 E System : Caused by: java.lang.NullPointerException: Attempt to read from field 'int android.util.ArrayMap.mSize' on a null object reference in method 'void android.util.ArrayMap.putAll(android.util.ArrayMap)'
08-13 11:26:42.461 2803 2803 E System : at android.util.ArrayMap.putAll(ArrayMap.java:705)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.ethernet.EthernetConfigStore.loadConfigFileLocked(EthernetConfigStore.java:112)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.ethernet.EthernetConfigStore.read(EthernetConfigStore.java:95)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.ethernet.EthernetConfigStore.read(EthernetConfigStore.java:87)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.ethernet.EthernetTracker.start(EthernetTracker.java:184)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.ethernet.EthernetServiceImpl.start(EthernetServiceImpl.java:102)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.ConnectivityServiceInitializer.onBootPhase(ConnectivityServiceInitializer.java:97)
08-13 11:26:42.461 2803 2803 E System : at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:294)
08-13 11:26:42.461 2803 2803 E System : ... 6 more
08-13 11:26:42.462 2803 2803 V SystemServerTiming: OnBootPhase_500 took to complete: 660ms
08-13 11:26:42.463 2803 2803 E Zygote : System zygote died with fatal exception
08-13 11:26:42.463 2803 2803 E Zygote : java.lang.RuntimeException: Failed to boot service com.android.server.ConnectivityServiceInitializer: onBootPhase threw an exception during phase 500
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:297)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.SystemServer.startOtherServices(SystemServer.java:2694)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.SystemServer.run(SystemServer.java:939)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.SystemServer.main(SystemServer.java:649)
08-13 11:26:42.463 2803 2803 E Zygote : at java.lang.reflect.Method.invoke(Native Method)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:914)
08-13 11:26:42.463 2803 2803 E Zygote : Caused by: java.lang.NullPointerException: Attempt to read from field 'int android.util.ArrayMap.mSize' on a null object reference in method 'void android.util.ArrayMap.putAll(android.util.ArrayMap)'
08-13 11:26:42.463 2803 2803 E Zygote : at android.util.ArrayMap.putAll(ArrayMap.java:705)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.ethernet.EthernetConfigStore.loadConfigFileLocked(EthernetConfigStore.java:112)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.ethernet.EthernetConfigStore.read(EthernetConfigStore.java:95)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.ethernet.EthernetConfigStore.read(EthernetConfigStore.java:87)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.ethernet.EthernetTracker.start(EthernetTracker.java:184)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.ethernet.EthernetServiceImpl.start(EthernetServiceImpl.java:102)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.ConnectivityServiceInitializer.onBootPhase(ConnectivityServiceInitializer.java:97)
08-13 11:26:42.463 2803 2803 E Zygote : at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:294)
08-13 11:26:42.463 2803 2803 E Zygote : ... 6 more
08-13 11:26:42.463 2803 2803 D AndroidRuntime: Shutting down VM
08-13 11:26:42.477 2803 2916 D WifiConfigStore: Reading from all stores completed in 45 ms.
08-13 11:26:42.478 2803 2803 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: main
08-13 11:26:42.478 2803 2803 E AndroidRuntime: java.lang.RuntimeException: Failed to boot service com.android.server.ConnectivityServiceInitializer: onBootPhase threw an exception during phase 500
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:297)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.SystemServer.startOtherServices(SystemServer.java:2694)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.SystemServer.run(SystemServer.java:939)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.SystemServer.main(SystemServer.java:649)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:914)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from field 'int android.util.ArrayMap.mSize' on a null object reference in method 'void android.util.ArrayMap.putAll(android.util.ArrayMap)'
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at android.util.ArrayMap.putAll(ArrayMap.java:705)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.ethernet.EthernetConfigStore.loadConfigFileLocked(EthernetConfigStore.java:112)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.ethernet.EthernetConfigStore.read(EthernetConfigStore.java:95)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.ethernet.EthernetConfigStore.read(EthernetConfigStore.java:87)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.ethernet.EthernetTracker.start(EthernetTracker.java:184)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.ethernet.EthernetServiceImpl.start(EthernetServiceImpl.java:102)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.ConnectivityServiceInitializer.onBootPhase(ConnectivityServiceInitializer.java:97)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:294)
08-13 11:26:42.478 2803 2803 E AndroidRuntime: ... 6 more
08-13 11:26:42.479 2803 2916 W WifiConfigManager: No stored networks found.
08-13 11:26:42.484 2803 2916 D AlarmManager: setImplLocked() callingPackage=android
08-13 11:26:42.484 2803 2916 I WifiInjector: enableVerboseLogging false hal false
08-13 11:26:42.485 2803 2916 D WifiNative: enableVerboseLogging false hal false
08-13 11:26:42.485 2803 2916 E SupplicantStaIfaceHalAidlImpl: Can't call setDebugParams, ISupplicant is null
08-13 11:26:42.488 2803 2916 I WifiService: WifiService starting up with Wi-Fi disabled
08-13 11:26:42.499 2803 2803 I DropBoxManagerService: add tag=system_server_crash isTagEnabled=true flags=0x2
08-13 11:26:42.499 2736 2736 I Zygote : ...preloaded 16430 classes in 2478ms.
08-13 11:26:42.499 2736 2736 V ZygoteInitTiming_lazy: PreloadClasses took to complete: 2478ms
分析日志,发现system_server进程去启动Ethernet service,读取Ip配置的时候失败了.失败的原因:E IpConfigStore: Bad version on IP configuration file, ignore read
.
上面这个先按下不表,我更好奇为什么Ethernet service启动失败,为什么导致设备一直卡在开机动画.
/frameworks/base/services/java/com/android/server/SystemServer.java
服务管理,启动系统服务.
系统服务大概分成三类:引导服务,核心服务,其他服务,分别对于三个方法,startBootstrapServices、startCoreServices、startOtherServices、startApexServices(Android 13),
这三个函数的主要作用就是去启动不同的服务.
比如我们常见的引导服务:ActivityManagerService, PowerManagerService, LightsService,
DisplayManagerService,PackageManagerService.
核心服务:BatteryService...
其他服务:AlarmManagerService,InputManagerService,ConnectivityService,WindowManagerService...
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startOtherServices");
mSystemServiceManager.updateOtherServicesStartIndex();
...
t.traceBegin("StartWindowManagerService");
// WMS needs sensor service ready //WMS需要 sensor service 先启动
mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_SENSOR_SERVICE);//查看各种服务的启动状态(阶段)
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
t.traceEnd();
...
t.traceBegin("StartConnectivityService");//启动网络服务
// This has to be called after NetworkManagementService, NetworkStatsService
// and NetworkPolicyManager because ConnectivityService needs to take these
// services to initialize.
//CONNECTIVITY_SERVICE_INITIALIZER_CLASS = com.android.server.ConnectivityServiceInitializer
mSystemServiceManager.startServiceFromJar(CONNECTIVITY_SERVICE_INITIALIZER_CLASS,
CONNECTIVITY_SERVICE_APEX_PATH);
networkPolicy.bindConnectivityManager();
t.traceEnd();
...
}
系统启动了这么多的服务,万一有bug出现,如何去找是那个服务没有启动成功?没有启动成功的某个服务如何快速定位问题?
当然有办法的啦,Android 把系统启动分为了几个阶段,不同的阶段干了不同的事情,前面一个阶段的事情完成了才可以进入下一个阶段.
/*
* Boot Phases 启动阶段
*/
public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency?
/**
* After receiving this boot phase, services can obtain lock settings data.
*
* 在接收到这个引导阶段之后,服务可以获得锁设置数据.
*/
public static final int PHASE_LOCK_SETTINGS_READY = 480;
/**
* After receiving this boot phase, services can safely call into core system services
* such as the PowerManager or PackageManager.
*
* 在这个阶段之后,服务可以安全地调用核心系统服务,如 PowerManagerService 和 PackageManagerService
*/
public static final int PHASE_SYSTEM_SERVICES_READY = 500;
/**
* After receiving this boot phase, services can safely call into device specific services.
*
* 在这个阶段之后,可以安全调用设备特定的服务
*/
public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520;
/**
* After receiving this boot phase, services can broadcast Intents.
*
* 在接收到这个引导阶段后,服务可以广播
*/
public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
/**
* After receiving this boot phase, services can start/bind to third party apps.
* Apps will be able to make Binder calls into services at this point.
*
* 在接收到这个引导阶段之后,服务可以启动/绑定到第三方应用程序,此时应用程序将能够使用Binder绑定服务
*/
public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
/**
* After receiving this boot phase, services can allow user interaction with the device.
* This phase occurs when boot has completed and the home application has started.
* System services may prefer to listen to this phase rather than registering a
* broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency.
*
* 在这个阶段之后,服务允许用户和设备交互.
* 此阶段发生在引导完成并且主应用程序(home app)启动时.
* 系统服务更倾向于监听这个阶段,而不是监听启动广播 ACTION_BOOT_COMPLETED,以降低延迟
*/
public static final int PHASE_BOOT_COMPLETED = 1000;
通过函数startBootPhase,然后根据phase确定启动到哪一步.
./frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
/**
* Starts the specified boot phase for all system services that have been started up to
* this point.
*
* @param t trace logger
* @param phase The boot phase to start.
*/
public void startBootPhase(@NonNull TimingsTraceAndSlog t, int phase) {
if (phase <= mCurrentPhase) {
throw new IllegalArgumentException("Next phase must be larger than previous");
}
mCurrentPhase = phase;
Slog.i(TAG, "Starting phase " + mCurrentPhase);
try {
t.traceBegin("OnBootPhase_" + phase);
final int serviceLen = mServices.size();
for (int i = 0; i < serviceLen; i++) {
final SystemService service = mServices.get(i);
long time = SystemClock.elapsedRealtime();
t.traceBegin("OnBootPhase_" + phase + "_" + service.getClass().getName());
try {
service.onBootPhase(mCurrentPhase);
} catch (Exception ex) {
throw new RuntimeException("Failed to boot service "
+ service.getClass().getName()
+ ": onBootPhase threw an exception during phase "
+ mCurrentPhase, ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onBootPhase");
t.traceEnd();
}
} finally {
t.traceEnd();
}
if (phase == SystemService.PHASE_BOOT_COMPLETED) {
final long totalBootTime = SystemClock.uptimeMillis() - mRuntimeStartUptime;
t.logDuration("TotalBootTime", totalBootTime);
SystemServerInitThreadPool.shutdown();
}
}
每个阶段做的事情都必须完成,假设某一个阶段执行的任务失败了.就会抛出异常,后面的就无法继续执行.
比如启动网络服务失败的,来源于上面的日志E System : java.lang.RuntimeException: Failed to boot service com.android.server.ConnectivityServiceInitializer: onBootPhase threw an exception during phase 500
解释了第二个问题:为什么卡在开机动画.(不能开机是因为以太网服务读取ipconfig.txt文件异常而一直重启服务,导致系统卡住)
gityuan_system-server
Zygote家的大儿子 ------ SystemServer
Android进程系列第四篇---SystemServer进程的启动流程
然后回来思考第一个问题:为什么会读写文件失败?(Bad version on IP configuration file)
ipconfig.txt异常是系统对文件缓存引起.因为设置静态IP时,会创建ipconfig.txt,当(30s之内)突然掉电的时候,文件内容还没有完全刷到文件中!!!
如何解决:
方案1:找个好的ipconfig.txt文件,push到/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt替换它
方案2:保存用户的静态参数,直接删除,rm -rf /data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt,然后利用开机广播在设置一次静态IP
这两个方案都是思路,都能让系统开机.
方案3:/packages/modules/Connectivity/service-t/src/com/android/server/net/IpConfigStore.java
,
这个方案可以避免系统卡死,能正常开机,倒是静态IP需要重新配置.(要恢复出厂设置,才能重新设置静态ip)(所以只是解决了开机问题,ip的缓存问题未解决)
/** Returns a map of network identity token and {@link IpConfiguration}. */
public static ArrayMap<String, IpConfiguration> readIpConfigurations(
InputStream inputStream) {
ArrayMap<String, IpConfiguration> networks = new ArrayMap<>();
DataInputStream in = null;
try {
in = new DataInputStream(inputStream);
int version = in.readInt();
if (version != 3 && version != 2 && version != 1) {
loge("Bad version on IP configuration file, ignore read");
- return null;
+ return networks;//add text
}
...
方案4:(检测到文件被损坏,直接跳过文件读取)只是思路,尝试了下:可以避免系统卡死,能正常开机(只是解决了开机问题,ip的缓存问题为解决)
搞个判断条件,如果文件被损坏,flag变化,跳过mConfigStore.read()
private final EthernetConfigStore mConfigStore;
void start(boolean isEnabled) {
mFactory.register();
mConfigStore.read();//负责读的
// Default interface is just the first one we want to track.
mIpConfigForDefaultInterface = mConfigStore.getIpConfigurationForDefaultInterface();
final ArrayMap<String, IpConfiguration> configs = mConfigStore.getIpConfigurations();
for (int i = 0; i < configs.size(); i++) {
mIpConfigurations.put(configs.keyAt(i), configs.valueAt(i));
}
...