status_t AudioPolicyService::dump(int fd, const Vector<String16>& args) { if (checkCallingPermission(String16("android.permission.DUMP")) == false) { dumpPermissionDenial(fd); } else { bool locked = tryLock(mLock); if (!locked) { String8 result(kDeadlockedString); write(fd, result.string(), result.size()); } dumpInternals(fd); if (mAudioCommandThread != NULL) { mAudioCommandThread->dump(fd); } if (mTonePlaybackThread != NULL) { mTonePlaybackThread->dump(fd); } if (mpAudioPolicy) { mpAudioPolicy->dump(mpAudioPolicy, fd); } if (locked) mLock.unlock(); } return NO_ERROR; }
bool settingsAllowed() { if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS"); // don't use PermissionCache; this is not a system permission bool ok = checkCallingPermission(sAudioSettings); if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); return ok; }
bool recordingAllowed() { if (getpid_cached == IPCThreadState::self()->getCallingPid()) return true; static const String16 sRecordAudio("android.permission.RECORD_AUDIO"); // don't use PermissionCache; this is not a system permission bool ok = checkCallingPermission(sRecordAudio); if (!ok) ALOGE("Request requires android.permission.RECORD_AUDIO"); return ok; }
/* BnHelloWorld */ status_t BnHelloWorld::onTransact(uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) { LOGE("OnTransact(%u,%u)", code, flags); CHECK_INTERFACE(IHelloWorld, data, reply); switch(code) { case HW_HELLOTHERE: { /** * Checking permissions is always a good idea. * * Note that the native client will also be granted these permissions in two cases * 1) you run the client code as root or system user. * 2) you run the client code as user who was granted this permission. * @see http://github.com/keesj/gomo/wiki/AndroidSecurity for more information **/ if (checkCallingPermission(String16("org.credil.helloworldservice.permissions.CALL_HELLOTHERE")) == false){ return PERMISSION_DENIED; } String16 str = data.readString16(); hellothere(String8(str).string()); ///reply->writeString16(str); return NO_ERROR; } break; case RETURN_INT_SHANQUAN:{ if (checkCallingPermission(String16("org.credil.helloworldservice.permissions.CALL_HELLOTHERE")) == false){ return PERMISSION_DENIED; } String16 str = data.readString16(); helloint(String8(str).string()); int tmp= str.size(); printf("servie str.size():%i\n",tmp); status_t status = reply->writeInt32(tmp); if( status == NO_ERROR) printf("file:%s,line:%i,no error\n",__FILE__,__LINE__); else printf("file:%s,line:%i, error\n",__FILE__,__LINE__); return NO_ERROR; } break; default: return BBinder::onTransact(code, data, reply, flags); } return NO_ERROR; }
static bool checkPermission(const char* permissionString) { #ifndef HAVE_ANDROID_OS return true; #endif if (getpid() == IPCThreadState::self()->getCallingPid()) return true; bool ok = checkCallingPermission(String16(permissionString)); if (!ok) LOGE("Request requires %s", permissionString); return ok; }
static bool checkPermission() { #ifndef HAVE_ANDROID_OS return true; #endif if (getpid() == IPCThreadState::self()->getCallingPid()) return true; bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); return ok; }
status_t CameraService::dump(int fd, const Vector<String16>& args) { static const char* kDeadlockedString = "CameraService may be deadlocked\n"; const size_t SIZE = 256; char buffer[SIZE]; String8 result; if (checkCallingPermission(String16("android.permission.DUMP")) == false) { snprintf(buffer, SIZE, "Permission Denial: " "can't dump CameraService from pid=%d, uid=%d\n", getCallingPid(), getCallingUid()); result.append(buffer); write(fd, result.string(), result.size()); } else { bool locked = tryLock(mServiceLock); // failed to lock - CameraService is probably deadlocked if (!locked) { String8 result(kDeadlockedString); write(fd, result.string(), result.size()); } bool hasClient = false; for (int i = 0; i < mNumberOfCameras; i++) { sp<Client> client = mClient[i].promote(); if (client == 0) continue; hasClient = true; sprintf(buffer, "Client[%d] (%p) PID: %d\n", i, client->getCameraClient()->asBinder().get(), client->mClientPid); result.append(buffer); write(fd, result.string(), result.size()); client->mHardware->dump(fd, args); } if (!hasClient) { result.append("No camera client yet.\n"); write(fd, result.string(), result.size()); } if (locked) mServiceLock.unlock(); // change logging level int n = args.size(); for (int i = 0; i + 1 < n; i++) { if (args[i] == String16("-v")) { String8 levelStr(args[i+1]); int level = atoi(levelStr.string()); sprintf(buffer, "Set Log Level to %d", level); result.append(buffer); setLogLevel(level); } } } return NO_ERROR; }
status_t Service::onTransact(uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) { CHECK_INTERFACE(IHelloWorld, data, reply); if (code == HW_SAYHELLO) { if (checkCallingPermission(String16("cn.bjtu.helloworldservice.permissions.CALL_SAYHELLO")) == false) return PERMISSION_DENIED; String16 str = data.readString16(); sayHello(String8(str).string()); return NO_ERROR; } return BBinder::onTransact(code, data, reply, flags); }
status_t CameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { // Permission checks switch (code) { //!++ #ifdef MTK_CAMERA_BSP_SUPPORT case BnCameraService::GET_PROPERTY: { CHECK_INTERFACE(ICameraService, data, reply); String8 const key = data.readString8(); String8 value; status_t const status = getProperty(key, value); reply->writeString8(value); reply->writeInt32(status); ALOGD("[GET_PROPERTY] - pid=%d, uid=%d (%s)=(%s) \n", getCallingPid(), getCallingUid(), key.string(), value.string()); return OK; } case BnCameraService::SET_PROPERTY: { CHECK_INTERFACE(ICameraService, data, reply); String8 const key = data.readString8(); String8 const value = data.readString8(); status_t const status = setProperty(key, value); reply->writeInt32(status); ALOGD("[SET_PROPERTY] - pid=%d, uid=%d (%s)=(%s) \n", getCallingPid(), getCallingUid(), key.string(), value.string()); return OK; } #endif //MTK_CAMERA_BSP_SUPPORT //!-- case BnCameraService::CONNECT: const int pid = getCallingPid(); const int self_pid = getpid(); if (pid != self_pid) { // we're called from a different process, do the real check if (!checkCallingPermission( String16("android.permission.CAMERA"))) { const int uid = getCallingUid(); ALOGE("Permission Denial: " "can't use the camera pid=%d, uid=%d", pid, uid); return PERMISSION_DENIED; } } break; } return BnCameraService::onTransact(code, data, reply, flags); }
status_t CommonTimeServer::dumpConfigInterface(int fd, const Vector<String16>& /* args */) { AutoMutex _lock(&mLock); const size_t SIZE = 256; char buffer[SIZE]; if (checkCallingPermission(String16("android.permission.DUMP")) == false) { snprintf(buffer, SIZE, "Permission Denial: " "can't dump CommonTimeConfigService from pid=%d, uid=%d\n", IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); write(fd, buffer, strlen(buffer)); } else { char meStr[64]; sockaddrToString(mMasterElectionEP, true, meStr, sizeof(meStr)); dump_printf("Common Time Config Service Status\n" "Bound Interface : %s\n", mBindIfaceValid ? mBindIface.string() : "<unbound>"); dump_printf("Master Election Endpoint : %s\n", meStr); dump_printf("Master Election Group ID : %016" PRIu64 "\n", mSyncGroupID); dump_printf("Master Announce Interval : %d mSec\n", mMasterAnnounceIntervalMs); dump_printf("Client Sync Interval : %d mSec\n", mSyncRequestIntervalMs); dump_printf("Panic Threshold : %d uSec\n", mPanicThresholdUsec); dump_printf("Base ME Prio : 0x%02x\n", static_cast<uint32_t>(mMasterPriority)); dump_printf("Effective ME Prio : 0x%02x\n", static_cast<uint32_t>(effectivePriority())); dump_printf("Auto Disable Allowed : %s\n", mAutoDisable ? "yes" : "no"); dump_printf("Auto Disable Engaged : %s\n", shouldAutoDisable() ? "yes" : "no"); } return NO_ERROR; }
status_t CameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { // Permission checks switch (code) { case BnCameraService::CONNECT: const int pid = getCallingPid(); const int self_pid = getpid(); if (pid != self_pid) { // we're called from a different process, do the real check if (!checkCallingPermission( String16("android.permission.CAMERA"))) { const int uid = getCallingUid(); LOGE("Permission Denial: " "can't use the camera pid=%d, uid=%d", pid, uid); return PERMISSION_DENIED; } } break; } return BnCameraService::onTransact(code, data, reply, flags); }
bool checkCallingPermission(const String16& permission) { return checkCallingPermission(permission, NULL, NULL); }
status_t CommonTimeServer::dumpClockInterface(int fd, const Vector<String16>& /* args */, size_t activeClients) { AutoMutex _lock(&mLock); const size_t SIZE = 256; char buffer[SIZE]; if (checkCallingPermission(String16("android.permission.DUMP")) == false) { snprintf(buffer, SIZE, "Permission Denial: " "can't dump CommonClockService from pid=%d, uid=%d\n", IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); write(fd, buffer, strlen(buffer)); } else { int64_t commonTime; int64_t localTime; bool synced; char maStr[64]; localTime = mLocalClock.getLocalTime(); synced = (OK == mCommonClock.localToCommon(localTime, &commonTime)); sockaddrToString(mMasterEP, mMasterEPValid, maStr, sizeof(maStr)); dump_printf("Common Clock Service Status\nLocal time : %" PRId64 "\n", localTime); if (synced) dump_printf("Common time : %" PRId64 "\n", commonTime); else dump_printf("Common time : %s\n", "not synced"); dump_printf("Timeline ID : %016" PRIu64 "\n", mTimelineID); dump_printf("State : %s\n", stateToString(mState)); dump_printf("Master Addr : %s\n", maStr); if (synced) { int32_t est = (ICommonClock::STATE_MASTER != mState) ? mClockRecovery.getLastErrorEstimate() : 0; dump_printf("Error Est. : %.3f msec\n", static_cast<float>(est) / 1000.0); } else { dump_printf("Error Est. : %s\n", "unknown"); } dump_printf("Syncs TXes : %u\n", mClient_SyncsSentToCurMaster); dump_printf("Syncs RXes : %u (%.2f%%)\n", mClient_SyncRespsRXedFromCurMaster, checked_percentage( mClient_SyncRespsRXedFromCurMaster, mClient_SyncsSentToCurMaster)); dump_printf("RXs Expired : %u (%.2f%%)\n", mClient_ExpiredSyncRespsRXedFromCurMaster, checked_percentage( mClient_ExpiredSyncRespsRXedFromCurMaster, mClient_SyncsSentToCurMaster)); if (!mClient_LastGoodSyncRX) { dump_printf("Last Good RX : %s\n", "unknown"); } else { int64_t localDelta, usecDelta; localDelta = localTime - mClient_LastGoodSyncRX; usecDelta = mCommonClock.localDurationToCommonDuration(localDelta); dump_printf("Last Good RX : %" PRId64 " uSec ago\n", usecDelta); } dump_printf("Active Clients : %zu\n", activeClients); mClient_PacketRTTLog.dumpLog(fd, mCommonClock); mStateChangeLog.dumpLog(fd); mElectionLog.dumpLog(fd); mBadPktLog.dumpLog(fd); } return NO_ERROR; }
status_t CameraService::dump(int fd, const Vector<String16>& args) { String8 result; if (checkCallingPermission(String16("android.permission.DUMP")) == false) { result.appendFormat("Permission Denial: " "can't dump CameraService from pid=%d, uid=%d\n", getCallingPid(), getCallingUid()); write(fd, result.string(), result.size()); } else { bool locked = tryLock(mServiceLock); // failed to lock - CameraService is probably deadlocked if (!locked) { result.append("CameraService may be deadlocked\n"); write(fd, result.string(), result.size()); } bool hasClient = false; if (!mModule) { result = String8::format("No camera module available!\n"); write(fd, result.string(), result.size()); return NO_ERROR; } result = String8::format("Camera module HAL API version: 0x%x\n", mModule->common.hal_api_version); result.appendFormat("Camera module API version: 0x%x\n", mModule->common.module_api_version); result.appendFormat("Camera module name: %s\n", mModule->common.name); result.appendFormat("Camera module author: %s\n", mModule->common.author); result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); write(fd, result.string(), result.size()); for (int i = 0; i < mNumberOfCameras; i++) { result = String8::format("Camera %d static information:\n", i); camera_info info; status_t rc = mModule->get_camera_info(i, &info); if (rc != OK) { result.appendFormat(" Error reading static information!\n"); write(fd, result.string(), result.size()); } else { result.appendFormat(" Facing: %s\n", info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); result.appendFormat(" Orientation: %d\n", info.orientation); int deviceVersion; if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0) { deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; } else { deviceVersion = info.device_version; } result.appendFormat(" Device version: 0x%x\n", deviceVersion); if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { result.appendFormat(" Device static metadata:\n"); write(fd, result.string(), result.size()); dump_indented_camera_metadata(info.static_camera_characteristics, fd, 2, 4); } else { write(fd, result.string(), result.size()); } } sp<Client> client = mClient[i].promote(); if (client == 0) { result = String8::format(" Device is closed, no client instance\n"); write(fd, result.string(), result.size()); continue; } hasClient = true; result = String8::format(" Device is open. Client instance dump:\n"); write(fd, result.string(), result.size()); client->dump(fd, args); } if (!hasClient) { result = String8::format("\nNo active camera clients yet.\n"); write(fd, result.string(), result.size()); } if (locked) mServiceLock.unlock(); // change logging level int n = args.size(); for (int i = 0; i + 1 < n; i++) { String16 verboseOption("-v"); if (args[i] == verboseOption) { String8 levelStr(args[i+1]); int level = atoi(levelStr.string()); result = String8::format("\nSetting log level to %d.\n", level); setLogLevel(level); write(fd, result.string(), result.size()); } } } return NO_ERROR; }
status_t MediaPlayerService::dump(int fd, const Vector<String16>& args) { const size_t SIZE = 256; char buffer[SIZE]; String8 result; if (checkCallingPermission(String16("android.permission.DUMP")) == false) { snprintf(buffer, SIZE, "Permission Denial: " "can't dump MediaPlayerService from pid=%d, uid=%d\n", IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); result.append(buffer); } else { Mutex::Autolock lock(mLock); for (int i = 0, n = mClients.size(); i < n; ++i) { sp<Client> c = mClients[i].promote(); if (c != 0) c->dump(fd, args); } result.append(" Files opened and/or mapped:\n"); snprintf(buffer, SIZE, "/proc/%d/maps", myTid()); FILE *f = fopen(buffer, "r"); if (f) { while (!feof(f)) { fgets(buffer, SIZE, f); if (strstr(buffer, " /sdcard/") || strstr(buffer, " /system/sounds/") || strstr(buffer, " /system/media/")) { result.append(" "); result.append(buffer); } } fclose(f); } else { result.append("couldn't open "); result.append(buffer); result.append("\n"); } snprintf(buffer, SIZE, "/proc/%d/fd", myTid()); DIR *d = opendir(buffer); if (d) { struct dirent *ent; while((ent = readdir(d)) != NULL) { if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name); struct stat s; if (lstat(buffer, &s) == 0) { if ((s.st_mode & S_IFMT) == S_IFLNK) { char linkto[256]; int len = readlink(buffer, linkto, sizeof(linkto)); if(len > 0) { if(len > 255) { linkto[252] = '.'; linkto[253] = '.'; linkto[254] = '.'; linkto[255] = 0; } else { linkto[len] = 0; } if (strstr(linkto, "/sdcard/") == linkto || strstr(linkto, "/system/sounds/") == linkto || strstr(linkto, "/system/media/") == linkto) { result.append(" "); result.append(buffer); result.append(" -> "); result.append(linkto); result.append("\n"); } } } else { result.append(" unexpected type for "); result.append(buffer); result.append("\n"); } } } } closedir(d); } else { result.append("couldn't open "); result.append(buffer); result.append("\n"); } #if defined(__arm__) bool dumpMem = false; for (size_t i = 0; i < args.size(); i++) { if (args[i] == String16("-m")) { dumpMem = true; } } if (dumpMem) { memStatus(fd, args); } #endif } write(fd, result.string(), result.size()); return NO_ERROR; }