Android系统的reboot之我见
jopen
13年前
近段时间关注Android系统的reboot部分,在应用程序调用reboot函数可以实现重启。顺着流程看看reboot如何运作。 <p>在Watchdog.java文件里,有一例:</p> <p> void rebootSystem(String reason) {</p> <p> Slog.i(TAG, "Rebooting system because: " + reason);</p> <p> //注册PowerManager服务</p> <p>PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");</p> <p>//调用reboot()方法</p> <p> pms.reboot(reason);</p> <p> }</p> <p>下面顺着往下走,</p> <p>Step1:</p> <p>在PowerManagerService.java文件</p> <p> public void reboot(String reason)</p> <p> {</p> <p> // REBOOT权限</p> <p> mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);</p> <p>…</p> <p> final String finalReason = reason;</p> <p> Runnable runnable = new Runnable() {</p> <p> public void run() {</p> <p> synchronized (this) {</p> <p> //这里执行reboot</p> <p> ShutdownThread.reboot(mContext, finalReason, false); </p> <p> }</p> <p> </p> <p> }</p> <p> };</p> <p> …..</p> <p> }</p> <p>Step2:</p> <p> 在ShutdownThread.java文件:</p> <p> public final class ShutdownThread extends Thread {</p> <p> …</p> <p> //这里是reboot函数</p> <p> public static void reboot(final Context context, String reason, boolean confirm) {</p> <p> mReboot = true;// mReboot为true</p> <p> ….</p> <p> }</p> <p> public void run() {</p> <p> </p> <p> …</p> <p> rebootOrShutdown(mReboot, mRebootReason);//其实这里执行是reboot</p> <p> }</p> <p> public static void rebootOrShutdown(boolean reboot, String reason) {</p> <p> // reboot为真,执行Power.reboot()方法</p> <p> if (reboot) {</p> <p> Log.i(TAG, "Rebooting, reason: " + reason);</p> <p> try {</p> <p> Power.reboot(reason);</p> <p> } catch (Exception e) {</p> <p> Log.e(TAG, "Reboot failed, will attempt shutdown instead", e);</p> <p> }</p> <p> } else if (SHUTDOWN_VIBRATE_MS > 0) {</p> <p> …</p> <p> // Shutdown power</p> <p> Log.i(TAG, "Performing low-level shutdown...");</p> <p> Power.shutdown();//执行Power类的shutdown方法</p> <p> }</p> <p>}</p> <p> </p> <p>Step3:</p> <p>在Power.java文件:</p> <p>public class Power{</p> <p> …</p> <p> public static void reboot(String reason) throws IOException</p> <p> {</p> <p> rebootNative(reason);//调用JNI的reboo方法</p> <p> }</p> <p> //声明rebootNative</p> <p> private static native void rebootNative(String reason) throws IOException ;</p> <p> …</p> <p>}</p> <p> </p> <p>Step4:</p> <p>在android_os_Power.cpp文件:</p> <p>// #define HAVE_ANDROID_OS 1</p> <p>static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)</p> <p>{</p> <p> sync();</p> <p>#ifdef HAVE_ANDROID_OS</p> <p> // 字符reason为空的话,执行自动重起</p> <p> if (reason == NULL) {</p> <p> reboot(RB_AUTOBOOT);</p> <p> } else {</p> <p> //有原因的重启</p> <p> const char *chars = env->GetStringUTFChars(reason, NULL);</p> <p> //调用__reboot()</p> <p> __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,</p> <p> LINUX_REBOOT_CMD_RESTART2, (char*) chars);</p> <p> env->ReleaseStringUTFChars(reason, chars); // In case it fails.</p> <p> }</p> <p> jniThrowIOException(env, errno);</p> <p>#endif</p> <p>}</p> <p> </p> <p>Step5:</p> <p>关于reboot()方法的来头,在bionic\libc\unistd\reboot.c文件里:</p> <p>int reboot (int mode) </p> <p>{</p> <p> //传NULL到__reboot函数</p> <p> return __reboot( LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, mode, NULL );</p> <p>}</p> <p>总的来说,归结一点是__reboot()函数。</p> <p> </p> <p>Step6:</p> <p>看看__reboot()的来由,在bionic\libc\arch-arm\syscalls\__reboot.S文件里:</p> <p>// # define __NR_SYSCALL_BASE 0x900000</p> <p>//#define __NR_reboot (__NR_SYSCALL_BASE + 88)</p> <p>.text</p> <p> .type __reboot, #function</p> <p> .globl __reboot</p> <p> .align 4</p> <p> .fnstart</p> <p>__reboot:</p> <p> .save {r4, r7}</p> <p> stmfd sp!, {r4, r7}</p> <p> ldr r7, =__NR_reboot</p> <p> swi #0</p> <p> ldmfd sp!, {r4, r7}</p> <p> movs r0, r0</p> <p> bxpl lr</p> <p> b __set_syscall_errno</p> <p> .fnend</p> <p>这是__reboot函数用汇编实现,用C语言来调用。关机部分可以这样来分析。<br /> <br /> 转自:<a href="/misc/goto?guid=4959500652331356695" target="_blank">http://blog.csdn.net/zzobin/article/details/7195060</a></p>