1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.am; 18 19 import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; 20 21 import android.os.Process; 22 23 /** 24 * A collection of interfaces to manage the freezer. All access to the freezer goes through an 25 * instance of this class. The class can be overridden for testing. 26 * 27 * Methods may be called without external synchronization. Multiple instances of this class can be 28 * used concurrently. 29 */ 30 class Freezer { 31 32 /** 33 * Freeze or unfreeze the specified process. 34 * 35 * @param pid Identifier of the process to freeze or unfreeze. 36 * @param uid Identifier of the user the process is running under. 37 * @param frozen Specify whether to free (true) or unfreeze (false). 38 */ setProcessFrozen(int pid, int uid, boolean frozen)39 public void setProcessFrozen(int pid, int uid, boolean frozen) { 40 Process.setProcessFrozen(pid, uid, frozen); 41 } 42 43 /** 44 * Informs binder that a process is about to be frozen. If freezer is enabled on a process via 45 * this method, this method will synchronously dispatch all pending transactions to the 46 * specified pid. This method will not add significant latencies when unfreezing. 47 * After freezing binder calls, binder will block all transaction to the frozen pid, and return 48 * an error to the sending process. 49 * 50 * @param pid the target pid for which binder transactions are to be frozen 51 * @param freeze specifies whether to flush transactions and then freeze (true) or unfreeze 52 * binder for the specified pid. 53 * @param timeoutMs the timeout in milliseconds to wait for the binder interface to freeze 54 * before giving up. 55 * 56 * @throws RuntimeException in case a flush/freeze operation could not complete successfully. 57 * @return 0 if success, or -EAGAIN indicating there's pending transaction. 58 */ freezeBinder(int pid, boolean freeze, int timeoutMs)59 public int freezeBinder(int pid, boolean freeze, int timeoutMs) { 60 return nativeFreezeBinder(pid, freeze, timeoutMs); 61 } 62 63 /** 64 * Retrieves binder freeze info about a process. 65 * @param pid the pid for which binder freeze info is to be retrieved. 66 * 67 * @throws RuntimeException if the operation could not complete successfully. 68 * @return a bit field reporting the binder freeze info for the process. 69 */ getBinderFreezeInfo(int pid)70 public int getBinderFreezeInfo(int pid) { 71 return nativeGetBinderFreezeInfo(pid); 72 } 73 74 /** 75 * Determines whether the freezer is supported by this system. 76 * @return true if the freezer is supported. 77 */ isFreezerSupported()78 public boolean isFreezerSupported() { 79 return nativeIsFreezerSupported(); 80 } 81 82 // Native methods 83 84 /** 85 * Informs binder that a process is about to be frozen. If freezer is enabled on a process via 86 * this method, this method will synchronously dispatch all pending transactions to the 87 * specified pid. This method will not add significant latencies when unfreezing. 88 * After freezing binder calls, binder will block all transaction to the frozen pid, and return 89 * an error to the sending process. 90 * 91 * @param pid the target pid for which binder transactions are to be frozen 92 * @param freeze specifies whether to flush transactions and then freeze (true) or unfreeze 93 * binder for the specified pid. 94 * @param timeoutMs the timeout in milliseconds to wait for the binder interface to freeze 95 * before giving up. 96 * 97 * @throws RuntimeException in case a flush/freeze operation could not complete successfully. 98 * @return 0 if success, or -EAGAIN indicating there's pending transaction. 99 */ nativeFreezeBinder(int pid, boolean freeze, int timeoutMs)100 private static native int nativeFreezeBinder(int pid, boolean freeze, int timeoutMs); 101 102 /** 103 * Retrieves binder freeze info about a process. 104 * @param pid the pid for which binder freeze info is to be retrieved. 105 * 106 * @throws RuntimeException if the operation could not complete successfully. 107 * @return a bit field reporting the binder freeze info for the process. 108 */ nativeGetBinderFreezeInfo(int pid)109 private static native int nativeGetBinderFreezeInfo(int pid); 110 111 /** 112 * Return 0 if the freezer is supported on this platform and -1 otherwise. 113 */ nativeIsFreezerSupported()114 private static native boolean nativeIsFreezerSupported(); 115 } 116