Brief introduction:
As we mentioned in the previous blog post, the shutdown process was finally done by modifying the Android properties for shutdown (Systemproperties.java access to System properties via JNI calls), and of course we can also use the ADB command to modify the Android system properties to perform a shutdown operation , such as adb shell Setpro sys.powerctl shutdown, here we briefly describe the principle or process of modifying the Android properties shutdown.
Native_set () <SystemProperties.java>--->systemproperties_set () <android_os_SystemProperties.cpp>
This is the method for setting system functions in the Systemproperties.java class.
0119/**
0120 * Set The value for the given key.
0121 * @throws IllegalArgumentException if the key exceeds characters
0122 * @throws IllegalArgumentException If the value exceeds characters
0123 */
0124 public static voidSet(string key, String val) {
0125 if (key.length () > Prop_name_max) {
0126 throw new IllegalArgumentException ("Key.length >" + Prop_name_max);
0127}
0128 if (val! = null && val.length () > Prop_value_max) {
0129 throw new IllegalArgumentException ("Val.length >" +
0130 Prop_value_max);
0131}
0132Native_set(Key, Val);//systemproperties.java accessing system properties through JNI calls
0133}
The Systemproperties interface class registers the corresponding CPP interface android_os_systemproperties.cpp in the initial environment, and the actual operation calls the CPP file via JNI, frameworks/base/core/jni/ AndroidRuntime.cpp. Click to view Source code
extern int register_android_os_systemproperties (jnienv *env);
Frameworks/base/core/jni/android_os_systemproperties.cpp; Click to view full source
0162 static void systemproperties_set(jnienv *env, Jobject clazz,
0163 jstring Keyj, jstring Valj)
0164 {
0165 int err;
0166 Const char* Key;
0167 Const char* Val;
0168
0169 if (Keyj = = NULL) {
0170 jnithrownullpointerexception (env, "key must not is null.");
0171 return;
0172}
0173 key = Env->getstringutfchars (Keyj, NULL);
0174 * The string object passed from the Java program corresponds to the jstring type in the local method, the Jstring type differs from the char* in C, and if you use it directly as a char*, an error occurs. Therefore, conversion is required before use. The conversion method is Getstringutfchars (Keyj, NULL) <jnienv method, the jstring will be converted to UTF-8 format of char*. */
0175 if (Valj = = NULL) {
0176 val = ""; /* NULL pointer not allowed here */
0177} else {
0178 val = env->getstringutfchars (Valj, NULL);
0179}
0180
0181 err = Property_set (key, Val);
0182
0183 env->releasestringutfchars (Keyj, key);
0184/ * release a pointer to char* in UTF-8 format * /
0185 if (Valj! = NULL) {
0186 Env->releasestringutfchars (Valj, Val);
0187}
0188
0189 if (Err < 0) {
0190 jnithrowexception (env, "java/lang/runtimeexception",
0191 "failed to set system");
0192}
0193}
Modify Android System Properties Systemproperties.set ("Sys.powerctl", "shutdown") shutdown analysis