How Android can not add intercepted restart (reboot) mechanism

2019-06-27

Keywords: Android reboot


 

1 Introduction

 

This article today, ah, a project needs from the hands of the author.

 

There is a saying that I hands running Android 4.4 development board, of which there is a demand: When to meet certain conditions of the system to operate on a restart in the background quietly touching ground. Oh, this is interesting, ah, because it is quietly touching the restart, then of course we can not give the user find a way to restart trigger, otherwise people find A little Android you will be able to put this mechanism to restart broken.

 

then what should we do? In fact, we just need to restart some common mechanisms of processes to clear, and this demand is well realized.

 

2, generally how to do?

 

On Android, want to reboot the system, there are generally three ways:

1, the broadcast transmission system

2, the reboot of the method call PowerManager

3, by executing the reboot command serial

 

Although each of these three ways to go a different route, but in the end are the same thing, will be called to the same assembly program to achieve the restart function. The first type and second type layer occurred in the framework, the third kind occurs at the C-executable program.

 

3, how do we achieve the goal?

 

Our fundamental purpose is to achieve a system restart action, since the Android system is really only one way to restart, the restart process of the execution or not open around.

 

Our secondary objective is to achieve a restart operation can not be intercepted, that what can not intercept it? It is a natural addition to our own, who can not change our program (such as delete, replace). Another, the others can be removed and replaced our mechanism to restart the program, but the price to pay is too high. We must be clear, this so-called "non-blocking" does not necessarily means that we can not shake the program is, in fact, as long as we can do let each other "enemy 1000 injury since the loss of eight hundred" we can say is do to this "non-blocking" the purpose.

 

That, in front of bedding so much, in the end how to achieve it?

 

The answer is simple: "sys.powerctl" value "reboot" is achieved by setting the system property .

 

why?

 

In fact, mentioned in section 2 above in three traditional ways to restart the system, they will eventually do this kind of setting "sys.powerctl" attribute value of "reboot" thing. But the above three ways to do that is "non-blocking" of what we need, because the three way too easy to be replaced. Others simply customize a framework.jar or reboot C program, so that the traditional process of restarting blank, we will be able to intercept off a restart request. So they are not.

 

As for directly "sys.powerctl", we go to see this property in Android is how to respond, that this property processes.

 

First in init.rc, we can find such a property listener statement

That is, as long as the value of "sys.powerctl" property has been changed, it will go this powerctl program execution, and the value of the property as a parameter passed in the past.

 

That this powerctl program Where is it?

 

It is in this

.\system\core\init\builtins.c

Do_powerctl have a function in this code, it is designed to handle the above command.

int do_powerctl(int nargs, char **args)

 

In this do_powerctl function, we can find it to call another function android_reboot. In this function

.\system\core\libcutils\android_reboot.c

In this android_reboot function in it, we can find it to call another reboot function

reboot(RB_AUTOBOOT)

This function above it is actually defined function in a period where the assembly program

.\bionic\libc\arch-arm\syscalls\__reboot.S

With this, it is true to reboot the system.

 

That some students would say, this is just to know its call flow only, it does not mean "non-blocking" of ah!

 

I was right! Now, we then go back and look at this builtins.c program, but this time we look at the contents of its Android.mk.

.\system\core\init\Android.mk

Through its Android.mk we can find this builtins.c program will eventually be compiled into init C program, is that the init program at the root of our development board

On this program, students are familiar with Android system development might know, it is one of those irreplaceable program can not be modified. The only way is to replace it with a re-program the system partition. And let others get yourself a system adapted to partition our hardware platforms come out, that the cost may become significant -

 

With this, we understand, the way that commands invoked powrctl saw in the previous init.rc is a very tricky way it is with the "irreplaceable", "can not be deleted" nature.

 

Then the next, let us look at this builtins.c restart the process downstream program that android_reboot.c contents of the program file Android.mk

.\system\core\libcutils\Android.mk

Here, we have unexpectedly found that the program will be compiled into what characteristics - static library static library is? Is not generated external library file, all references to the program will be the static library of the library packaged into their own programs.

 

To put it plainly, is this android_reboot.c code, you will be completely embedded into the init program to go. Besides white point is, you want to change android_reboot.c want to change it is equivalent to init program. The change init program has already been in front of us proved to be a feasible. So, we know this android_reboot.c also have "irreplaceable", "can not delete" feature.

 

Further downstream, the program is compiled. Not say ~

 

4 Conclusion

 

因此,我们如果想实现一种不可被拦截的重启系统的方式,最最简单的一种方式就是通过设置系统属性 "sys.powerctl" 的值为 "reboot" 就好了。

 

在 Java 上可以像这样做

import android.os.SystemProperties

void myReboot(){
    SystemProperties.set("sys.powerctl", "reboot");
}

 

在 C 语言上可以像这样做

#include <cutils/properties.h>

property_set("sys.powerctl", "reboot");

 

我们的开发板,由于有很多的自定义硬件,这些外围硬件的适配工作不是那么好做的。如果有其他人想要将他们自己的 Android 系统烧进去,那这些外围硬件都是不能正常工作的,这样一来,他们即使绕开了我们的重启机制,也无法正常使用我们的板子。除非他们有足够的耐心完全自己适配外围硬件。让他人感觉到破解代价太大也是一种 “防止破解” 的有效方式。


 

Guess you like

Origin www.cnblogs.com/chorm590/p/11097878.html