1 /* 2 * Copyright (C) 2016 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 #include <unistd.h> 18 #include <fcntl.h> 19 20 #include <android/permission_manager.h> 21 #include <binder/ActivityManager.h> 22 #include <binder/IActivityManager.h> 23 #include <binder/Parcel.h> 24 #include <utils/Errors.h> 25 26 namespace android { 27 28 // ------------------------------------------------------------------------------------ 29 30 class BpActivityManager : public BpInterface<IActivityManager> 31 { 32 public: BpActivityManager(const sp<IBinder> & impl)33 explicit BpActivityManager(const sp<IBinder>& impl) 34 : BpInterface<IActivityManager>(impl) 35 { 36 } 37 openContentUri(const String16 & stringUri)38 virtual int openContentUri(const String16& stringUri) 39 { 40 Parcel data, reply; 41 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 42 data.writeString16(stringUri); 43 status_t ret = remote()->transact(OPEN_CONTENT_URI_TRANSACTION, data, & reply); 44 int fd = -1; 45 if (ret == NO_ERROR) { 46 int32_t exceptionCode = reply.readExceptionCode(); 47 if (!exceptionCode) { 48 // Success is indicated here by a nonzero int followed by the fd; 49 // failure by a zero int with no data following. 50 if (reply.readInt32() != 0) { 51 fd = fcntl(reply.readParcelFileDescriptor(), F_DUPFD_CLOEXEC, 0); 52 } 53 } else { 54 // An exception was thrown back; fall through to return failure 55 ALOGD("openContentUri(%s) caught exception %d\n", String8(stringUri).c_str(), 56 exceptionCode); 57 } 58 } 59 return fd; 60 } 61 registerUidObserver(const sp<IUidObserver> & observer,const int32_t event,const int32_t cutpoint,const String16 & callingPackage)62 virtual status_t registerUidObserver(const sp<IUidObserver>& observer, 63 const int32_t event, 64 const int32_t cutpoint, 65 const String16& callingPackage) 66 { 67 Parcel data, reply; 68 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 69 data.writeStrongBinder(IInterface::asBinder(observer)); 70 data.writeInt32(event); 71 data.writeInt32(cutpoint); 72 data.writeString16(callingPackage); 73 status_t err = remote()->transact(REGISTER_UID_OBSERVER_TRANSACTION, data, &reply); 74 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 75 return err; 76 } 77 return OK; 78 } 79 registerUidObserverForUids(const sp<IUidObserver> & observer,const int32_t event,const int32_t cutpoint,const String16 & callingPackage,const int32_t uids[],size_t nUids,sp<IBinder> & observerToken)80 virtual status_t registerUidObserverForUids(const sp<IUidObserver>& observer, 81 const int32_t event, const int32_t cutpoint, 82 const String16& callingPackage, 83 const int32_t uids[], size_t nUids, 84 /*out*/ sp<IBinder>& observerToken) { 85 Parcel data, reply; 86 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 87 data.writeStrongBinder(IInterface::asBinder(observer)); 88 data.writeInt32(event); 89 data.writeInt32(cutpoint); 90 data.writeString16(callingPackage); 91 data.writeInt32Array(nUids, uids); 92 status_t err = 93 remote()->transact(REGISTER_UID_OBSERVER_FOR_UIDS_TRANSACTION, data, &reply); 94 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 95 return err; 96 } 97 err = reply.readStrongBinder(&observerToken); 98 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 99 return err; 100 } 101 return OK; 102 } 103 unregisterUidObserver(const sp<IUidObserver> & observer)104 virtual status_t unregisterUidObserver(const sp<IUidObserver>& observer) 105 { 106 Parcel data, reply; 107 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 108 data.writeStrongBinder(IInterface::asBinder(observer)); 109 status_t err = remote()->transact(UNREGISTER_UID_OBSERVER_TRANSACTION, data, &reply); 110 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 111 return err; 112 } 113 return OK; 114 } 115 addUidToObserver(const sp<IBinder> & observerToken,const String16 & callingPackage,int32_t uid)116 virtual status_t addUidToObserver(const sp<IBinder>& observerToken, 117 const String16& callingPackage, int32_t uid) { 118 Parcel data, reply; 119 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 120 data.writeStrongBinder(observerToken); 121 data.writeString16(callingPackage); 122 data.writeInt32(uid); 123 status_t err = remote()->transact(ADD_UID_TO_OBSERVER_TRANSACTION, data, &reply); 124 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 125 return err; 126 } 127 return OK; 128 } 129 removeUidFromObserver(const sp<IBinder> & observerToken,const String16 & callingPackage,int32_t uid)130 virtual status_t removeUidFromObserver(const sp<IBinder>& observerToken, 131 const String16& callingPackage, int32_t uid) { 132 Parcel data, reply; 133 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 134 data.writeStrongBinder(observerToken); 135 data.writeString16(callingPackage); 136 data.writeInt32(uid); 137 status_t err = remote()->transact(REMOVE_UID_FROM_OBSERVER_TRANSACTION, data, &reply); 138 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 139 return err; 140 } 141 return OK; 142 } 143 isUidActive(const uid_t uid,const String16 & callingPackage)144 virtual bool isUidActive(const uid_t uid, const String16& callingPackage) 145 { 146 Parcel data, reply; 147 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 148 data.writeInt32(uid); 149 data.writeString16(callingPackage); 150 remote()->transact(IS_UID_ACTIVE_TRANSACTION, data, &reply); 151 // fail on exception 152 if (reply.readExceptionCode() != 0) return false; 153 return reply.readInt32() == 1; 154 } 155 getUidProcessState(const uid_t uid,const String16 & callingPackage)156 virtual int32_t getUidProcessState(const uid_t uid, const String16& callingPackage) 157 { 158 Parcel data, reply; 159 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 160 data.writeInt32(uid); 161 data.writeString16(callingPackage); 162 remote()->transact(GET_UID_PROCESS_STATE_TRANSACTION, data, &reply); 163 // fail on exception 164 if (reply.readExceptionCode() != 0) { 165 return ActivityManager::PROCESS_STATE_UNKNOWN; 166 } 167 return reply.readInt32(); 168 } 169 checkPermission(const String16 & permission,const pid_t pid,const uid_t uid,int32_t * outResult)170 virtual status_t checkPermission(const String16& permission, 171 const pid_t pid, 172 const uid_t uid, 173 int32_t* outResult) { 174 Parcel data, reply; 175 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 176 data.writeString16(permission); 177 data.writeInt32(pid); 178 data.writeInt32(uid); 179 status_t err = remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply); 180 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 181 return err; 182 } 183 *outResult = reply.readInt32(); 184 return NO_ERROR; 185 } 186 logFgsApiBegin(int32_t apiType,int32_t appUid,int32_t appPid)187 virtual status_t logFgsApiBegin(int32_t apiType, int32_t appUid, int32_t appPid) { 188 Parcel data, reply; 189 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 190 data.writeInt32(apiType); 191 data.writeInt32(appUid); 192 data.writeInt32(appPid); 193 status_t err = remote()->transact(LOG_FGS_API_BEGIN_TRANSACTION, data, &reply, 194 IBinder::FLAG_ONEWAY); 195 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 196 ALOGD("%s: FGS Logger Transaction failed, %d", __func__, err); 197 return err; 198 } 199 return NO_ERROR; 200 } 201 logFgsApiEnd(int32_t apiType,int32_t appUid,int32_t appPid)202 virtual status_t logFgsApiEnd(int32_t apiType, int32_t appUid, int32_t appPid) { 203 Parcel data, reply; 204 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 205 data.writeInt32(apiType); 206 data.writeInt32(appUid); 207 data.writeInt32(appPid); 208 status_t err = 209 remote()->transact(LOG_FGS_API_END_TRANSACTION, data, &reply, IBinder::FLAG_ONEWAY); 210 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 211 ALOGD("%s: FGS Logger Transaction failed, %d", __func__, err); 212 return err; 213 } 214 return NO_ERROR; 215 } 216 logFgsApiStateChanged(int32_t apiType,int32_t state,int32_t appUid,int32_t appPid)217 virtual status_t logFgsApiStateChanged(int32_t apiType, int32_t state, int32_t appUid, 218 int32_t appPid) { 219 Parcel data, reply; 220 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 221 data.writeInt32(apiType); 222 data.writeInt32(state); 223 data.writeInt32(appUid); 224 data.writeInt32(appPid); 225 status_t err = remote()->transact(LOG_FGS_API_STATE_CHANGED_TRANSACTION, data, &reply, 226 IBinder::FLAG_ONEWAY); 227 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 228 ALOGD("%s: FGS Logger Transaction failed, %d", __func__, err); 229 return err; 230 } 231 return NO_ERROR; 232 } 233 }; 234 235 // ------------------------------------------------------------------------------------ 236 237 IMPLEMENT_META_INTERFACE(ActivityManager, "android.app.IActivityManager") 238 239 } // namespace android 240