Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * Copyright (C) 2012, The Linux Foundation. All rights reserved. |
| 4 | * |
| 5 | * Not a Contribution, Apache license notifications and license are |
| 6 | * retained for attribution purposes only. |
| 7 | |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <binder/Parcel.h> |
| 25 | #include <binder/IBinder.h> |
| 26 | #include <binder/IInterface.h> |
| 27 | #include <binder/IPCThreadState.h> |
| 28 | #include <utils/Errors.h> |
| 29 | |
| 30 | #include <IQService.h> |
| 31 | |
| 32 | using namespace android; |
| 33 | |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | namespace qService { |
| 37 | |
| 38 | class BpQService : public BpInterface<IQService> |
| 39 | { |
| 40 | public: |
| 41 | BpQService(const sp<IBinder>& impl) |
| 42 | : BpInterface<IQService>(impl) {} |
| 43 | |
| 44 | virtual void securing(uint32_t startEnd) { |
| 45 | Parcel data, reply; |
| 46 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 47 | data.writeInt32(startEnd); |
| 48 | remote()->transact(SECURING, data, &reply); |
| 49 | } |
| 50 | |
| 51 | virtual void unsecuring(uint32_t startEnd) { |
| 52 | Parcel data, reply; |
| 53 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 54 | data.writeInt32(startEnd); |
| 55 | remote()->transact(UNSECURING, data, &reply); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | IMPLEMENT_META_INTERFACE(QService, "android.display.IQService"); |
| 60 | |
| 61 | // ---------------------------------------------------------------------- |
| 62 | |
| 63 | static void getProcName(int pid, char *buf, int size); |
| 64 | |
| 65 | status_t BnQService::onTransact( |
| 66 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 67 | { |
| 68 | // IPC should be from mediaserver only |
| 69 | IPCThreadState* ipc = IPCThreadState::self(); |
| 70 | const int callerPid = ipc->getCallingPid(); |
| 71 | const size_t MAX_BUF_SIZE = 1024; |
| 72 | const char *mediaServer = "/system/bin/mediaserver"; |
| 73 | char callingProcName[MAX_BUF_SIZE] = {0}; |
| 74 | |
| 75 | getProcName(callerPid, callingProcName, MAX_BUF_SIZE); |
| 76 | if(strcmp(callingProcName, mediaServer) != 0 ) { //Some rogue process |
| 77 | ALOGE("No Permission:can't access display.qservice pid=%d process=%s", |
| 78 | callerPid, callingProcName); |
| 79 | return PERMISSION_DENIED; |
| 80 | } |
| 81 | |
| 82 | switch(code) { |
| 83 | case SECURING: { |
| 84 | CHECK_INTERFACE(IQService, data, reply); |
| 85 | uint32_t startEnd = data.readInt32(); |
| 86 | securing(startEnd); |
| 87 | return NO_ERROR; |
| 88 | } break; |
| 89 | case UNSECURING: { |
| 90 | CHECK_INTERFACE(IQService, data, reply); |
| 91 | uint32_t startEnd = data.readInt32(); |
| 92 | unsecuring(startEnd); |
| 93 | return NO_ERROR; |
| 94 | } break; |
| 95 | default: |
| 96 | return BBinder::onTransact(code, data, reply, flags); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | //Helper |
| 101 | static void getProcName(int pid, char *buf, int size) { |
| 102 | int fd = -1; |
| 103 | snprintf(buf, size, "/proc/%d/cmdline", pid); |
| 104 | fd = open(buf, O_RDONLY); |
| 105 | if (fd < 0) { |
| 106 | strcpy(buf, "Unknown"); |
| 107 | } else { |
| 108 | int len = read(fd, buf, size - 1); |
| 109 | buf[len] = 0; |
| 110 | close(fd); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | }; // namespace qService |