Modify the Android system attributes SystemProperties. set (& quot; sys. powerctl & quot;, & quot; shutdown & quot;) shutdown Analysis
Introduction:
As mentioned in the previous blog post, the shutdown process is to modify the Android attribute to perform the shutdown operation (SystemProperties. java accesses system attributes through JNI calls). Of course, we can also use the adb command to modify the Android system attributes and execute the shutdown operation, for example, adb shell setpro sys. powerctl shutdown. Here we will briefly introduce the principle or process of modifying the Android attribute to shut down.
Native_set () ---> SystemProperties_set ()
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 32 characters
0122 * @ throws IllegalArgumentException if the value exceeds 92 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 calls JNI to Access System Properties
0133}
The SystemProperties interface class registers the corresponding CPP interface android_ OS _SystemProperties.cpp in the initial environment. The actual operation is to call the corresponding cpp file through JNI, frameworks/base/core/jni/AndroidRuntime. cpp. Click to view the source code
extern int register_android_os_SystemProperties(JNIEnv *env);
Frameworks/base/core/jni/android_ OS _SystemProperties.cpp; click to view the complete source code
0162 static voidSystemProperties_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 be null .");
0171 return;
0172}
0173 key = env-> GetStringUTFChars (keyJ, NULL );
0174 * The String object passed in from the java program corresponds to the jstring type in the local method. The jstring type is different from the char * in c. If you use it directly as char, an error occurs. Therefore, conversion is required before use. The conversion method is GetStringUTFChars (keyJ, NULL) To convert jstring to char * In UTF-8 format *. */
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 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 property ");
0192}
0193}