Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dbcf2d74 authored by Nick Kralevich's avatar Nick Kralevich
Browse files

PowerManagerService: Don't reboot directly.

Instead of calling the reboot system call ourselves, send
a message to init asking it to reboot the system. Init is in
a better position to make sure the system is cleanly shutdown.

Get rid of CAP_SYS_BOOT from system_server.

Bug: 8646621
Change-Id: I200722412844ad8d99e35a442021c6263c3ebc05
parent d71bfdab
Loading
Loading
Loading
Loading
+0 −1
Original line number Original line Diff line number Diff line
@@ -479,7 +479,6 @@ public class ZygoteInit {
            OsConstants.CAP_NET_BIND_SERVICE,
            OsConstants.CAP_NET_BIND_SERVICE,
            OsConstants.CAP_NET_BROADCAST,
            OsConstants.CAP_NET_BROADCAST,
            OsConstants.CAP_NET_RAW,
            OsConstants.CAP_NET_RAW,
            OsConstants.CAP_SYS_BOOT,
            OsConstants.CAP_SYS_MODULE,
            OsConstants.CAP_SYS_MODULE,
            OsConstants.CAP_SYS_NICE,
            OsConstants.CAP_SYS_NICE,
            OsConstants.CAP_SYS_RESOURCE,
            OsConstants.CAP_SYS_RESOURCE,
+15 −8
Original line number Original line Diff line number Diff line
@@ -50,6 +50,7 @@ import android.os.PowerManager;
import android.os.Process;
import android.os.Process;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.SystemService;
import android.os.SystemService;
import android.os.UserHandle;
import android.os.UserHandle;
import android.os.WorkSource;
import android.os.WorkSource;
@@ -364,8 +365,6 @@ public final class PowerManagerService extends IPowerManager.Stub
    private long mLastWarningAboutUserActivityPermission = Long.MIN_VALUE;
    private long mLastWarningAboutUserActivityPermission = Long.MIN_VALUE;


    private native void nativeInit();
    private native void nativeInit();
    private static native void nativeShutdown();
    private static native void nativeReboot(String reason) throws IOException;


    private static native void nativeSetPowerState(boolean screenOn, boolean screenBright);
    private static native void nativeSetPowerState(boolean screenOn, boolean screenBright);
    private static native void nativeAcquireSuspendBlocker(String name);
    private static native void nativeAcquireSuspendBlocker(String name);
@@ -2164,18 +2163,26 @@ public final class PowerManagerService extends IPowerManager.Stub
     * to be clean.  Most people should use {@link ShutdownThread} for a clean shutdown.
     * to be clean.  Most people should use {@link ShutdownThread} for a clean shutdown.
     */
     */
    public static void lowLevelShutdown() {
    public static void lowLevelShutdown() {
        nativeShutdown();
        SystemProperties.set("sys.powerctl", "shutdown");
    }
    }


    /**
    /**
     * Low-level function to reboot the device.
     * Low-level function to reboot the device. On success, this function
     * doesn't return. If more than 5 seconds passes from the time,
     * a reboot is requested, this method returns.
     *
     *
     * @param reason code to pass to the kernel (e.g. "recovery"), or null.
     * @param reason code to pass to the kernel (e.g. "recovery"), or null.
     * @throws IOException if reboot fails for some reason (eg, lack of
     *         permission)
     */
     */
    public static void lowLevelReboot(String reason) throws IOException {
    public static void lowLevelReboot(String reason) {
        nativeReboot(reason);
        if (reason == null) {
            reason = "";
        }
        SystemProperties.set("sys.powerctl", "reboot," + reason);
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    }


    @Override // Watchdog.Monitor implementation
    @Override // Watchdog.Monitor implementation
+2 −5
Original line number Original line Diff line number Diff line
@@ -490,11 +490,8 @@ public final class ShutdownThread extends Thread {
    public static void rebootOrShutdown(boolean reboot, String reason) {
    public static void rebootOrShutdown(boolean reboot, String reason) {
        if (reboot) {
        if (reboot) {
            Log.i(TAG, "Rebooting, reason: " + reason);
            Log.i(TAG, "Rebooting, reason: " + reason);
            try {
            PowerManagerService.lowLevelReboot(reason);
            PowerManagerService.lowLevelReboot(reason);
            } catch (Exception e) {
            Log.e(TAG, "Reboot failed, will attempt shutdown instead");
                Log.e(TAG, "Reboot failed, will attempt shutdown instead", e);
            }
        } else if (SHUTDOWN_VIBRATE_MS > 0) {
        } else if (SHUTDOWN_VIBRATE_MS > 0) {
            // vibrate before shutting down
            // vibrate before shutting down
            Vibrator vibrator = new SystemVibrator();
            Vibrator vibrator = new SystemVibrator();
+0 −21
Original line number Original line Diff line number Diff line
@@ -32,7 +32,6 @@
#include <utils/Log.h>
#include <utils/Log.h>
#include <hardware/power.h>
#include <hardware/power.h>
#include <hardware_legacy/power.h>
#include <hardware_legacy/power.h>
#include <cutils/android_reboot.h>
#include <suspend/autosuspend.h>
#include <suspend/autosuspend.h>


#include "com_android_server_power_PowerManagerService.h"
#include "com_android_server_power_PowerManagerService.h"
@@ -189,22 +188,6 @@ static void nativeSetAutoSuspend(JNIEnv *env, jclass clazz, jboolean enable) {
    }
    }
}
}


static void nativeShutdown(JNIEnv *env, jclass clazz) {
    android_reboot(ANDROID_RB_POWEROFF, 0, 0);
}

static void nativeReboot(JNIEnv *env, jclass clazz, jstring reason) {
    if (reason == NULL) {
        android_reboot(ANDROID_RB_RESTART, 0, 0);
    } else {
        const char *chars = env->GetStringUTFChars(reason, NULL);
        android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
        env->ReleaseStringUTFChars(reason, chars);  // In case it fails.
    }
    jniThrowIOException(env, errno);
}


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


static JNINativeMethod gPowerManagerServiceMethods[] = {
static JNINativeMethod gPowerManagerServiceMethods[] = {
@@ -221,10 +204,6 @@ static JNINativeMethod gPowerManagerServiceMethods[] = {
            (void*) nativeSetInteractive },
            (void*) nativeSetInteractive },
    { "nativeSetAutoSuspend", "(Z)V",
    { "nativeSetAutoSuspend", "(Z)V",
            (void*) nativeSetAutoSuspend },
            (void*) nativeSetAutoSuspend },
    { "nativeShutdown", "()V",
            (void*) nativeShutdown },
    { "nativeReboot", "(Ljava/lang/String;)V",
            (void*) nativeReboot },
};
};


#define FIND_CLASS(var, className) \
#define FIND_CLASS(var, className) \