Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Saurabh Shah | 86c1729 | 2013-02-08 15:24:13 -0800 | [diff] [blame] | 3 | * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved. |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 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> |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 29 | #include <private/android_filesystem_config.h> |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 30 | |
| 31 | #include <IQService.h> |
| 32 | |
| 33 | using namespace android; |
Saurabh Shah | 86c1729 | 2013-02-08 15:24:13 -0800 | [diff] [blame] | 34 | using namespace qClient; |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 35 | |
| 36 | // --------------------------------------------------------------------------- |
| 37 | |
| 38 | namespace qService { |
| 39 | |
| 40 | class BpQService : public BpInterface<IQService> |
| 41 | { |
| 42 | public: |
| 43 | BpQService(const sp<IBinder>& impl) |
| 44 | : BpInterface<IQService>(impl) {} |
| 45 | |
| 46 | virtual void securing(uint32_t startEnd) { |
| 47 | Parcel data, reply; |
| 48 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 49 | data.writeInt32(startEnd); |
| 50 | remote()->transact(SECURING, data, &reply); |
| 51 | } |
| 52 | |
| 53 | virtual void unsecuring(uint32_t startEnd) { |
| 54 | Parcel data, reply; |
| 55 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 56 | data.writeInt32(startEnd); |
| 57 | remote()->transact(UNSECURING, data, &reply); |
| 58 | } |
Saurabh Shah | 86c1729 | 2013-02-08 15:24:13 -0800 | [diff] [blame] | 59 | |
| 60 | virtual void connect(const sp<IQClient>& client) { |
| 61 | Parcel data, reply; |
| 62 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 63 | data.writeStrongBinder(client->asBinder()); |
| 64 | remote()->transact(CONNECT, data, &reply); |
| 65 | } |
Jeykumar Sankaran | 9f59a76 | 2013-02-28 10:45:56 -0800 | [diff] [blame] | 66 | |
| 67 | virtual status_t screenRefresh() { |
| 68 | Parcel data, reply; |
| 69 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 70 | remote()->transact(SCREEN_REFRESH, data, &reply); |
| 71 | status_t result = reply.readInt32(); |
| 72 | return result; |
| 73 | } |
Arun Kumar K.R | ffef748 | 2013-04-10 14:17:22 -0700 | [diff] [blame] | 74 | |
| 75 | virtual void setExtOrientation(uint32_t orientation) { |
| 76 | Parcel data, reply; |
| 77 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 78 | data.writeInt32(orientation); |
| 79 | remote()->transact(EXTERNAL_ORIENTATION, data, &reply); |
| 80 | } |
Arun Kumar K.R | fb5bfa6 | 2013-07-25 03:10:51 -0700 | [diff] [blame] | 81 | |
| 82 | virtual void setBufferMirrorMode(uint32_t enable) { |
| 83 | Parcel data, reply; |
| 84 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 85 | data.writeInt32(enable); |
| 86 | remote()->transact(BUFFER_MIRRORMODE, data, &reply); |
| 87 | } |
Naseer Ahmed | 58780b9 | 2013-07-29 17:41:40 -0400 | [diff] [blame] | 88 | |
| 89 | virtual status_t vpuCommand(uint32_t command, uint32_t setting) { |
| 90 | Parcel data, reply; |
| 91 | data.writeInterfaceToken(IQService::getInterfaceDescriptor()); |
| 92 | data.writeInt32(setting); |
| 93 | remote()->transact(command, data, &reply); |
| 94 | status_t result = reply.readInt32(); |
| 95 | return result; |
| 96 | } |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | IMPLEMENT_META_INTERFACE(QService, "android.display.IQService"); |
| 100 | |
| 101 | // ---------------------------------------------------------------------- |
| 102 | |
| 103 | static void getProcName(int pid, char *buf, int size); |
| 104 | |
| 105 | status_t BnQService::onTransact( |
| 106 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 107 | { |
| 108 | // IPC should be from mediaserver only |
| 109 | IPCThreadState* ipc = IPCThreadState::self(); |
| 110 | const int callerPid = ipc->getCallingPid(); |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 111 | const int callerUid = ipc->getCallingUid(); |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 112 | const size_t MAX_BUF_SIZE = 1024; |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 113 | char callingProcName[MAX_BUF_SIZE] = {0}; |
| 114 | |
| 115 | getProcName(callerPid, callingProcName, MAX_BUF_SIZE); |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 116 | |
| 117 | const bool permission = (callerUid == AID_MEDIA); |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 118 | |
Naseer Ahmed | 58780b9 | 2013-07-29 17:41:40 -0400 | [diff] [blame] | 119 | if (code > VPU_COMMAND_LIST_START && code < VPU_COMMAND_LIST_END) { |
| 120 | if(callerUid != AID_SYSTEM && callerUid != AID_ROOT) { |
| 121 | ALOGE("display.qservice VPU command access denied: \ |
| 122 | pid=%d uid=%d process=%s",callerPid, |
| 123 | callerUid, callingProcName); |
| 124 | return PERMISSION_DENIED; |
| 125 | } |
| 126 | CHECK_INTERFACE(IQService, data, reply); |
| 127 | int32_t setting = data.readInt32(); |
| 128 | return vpuCommand(code, setting); |
| 129 | } |
| 130 | |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 131 | switch(code) { |
| 132 | case SECURING: { |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 133 | if(!permission) { |
Jeykumar Sankaran | 9f59a76 | 2013-02-28 10:45:56 -0800 | [diff] [blame] | 134 | ALOGE("display.qservice SECURING access denied: \ |
| 135 | pid=%d uid=%d process=%s", |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 136 | callerPid, callerUid, callingProcName); |
| 137 | return PERMISSION_DENIED; |
| 138 | } |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 139 | CHECK_INTERFACE(IQService, data, reply); |
| 140 | uint32_t startEnd = data.readInt32(); |
| 141 | securing(startEnd); |
| 142 | return NO_ERROR; |
| 143 | } break; |
| 144 | case UNSECURING: { |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 145 | if(!permission) { |
Jeykumar Sankaran | 9f59a76 | 2013-02-28 10:45:56 -0800 | [diff] [blame] | 146 | ALOGE("display.qservice UNSECURING access denied: \ |
| 147 | pid=%d uid=%d process=%s", |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 148 | callerPid, callerUid, callingProcName); |
| 149 | return PERMISSION_DENIED; |
| 150 | } |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 151 | CHECK_INTERFACE(IQService, data, reply); |
| 152 | uint32_t startEnd = data.readInt32(); |
| 153 | unsecuring(startEnd); |
| 154 | return NO_ERROR; |
| 155 | } break; |
Saurabh Shah | 86c1729 | 2013-02-08 15:24:13 -0800 | [diff] [blame] | 156 | case CONNECT: { |
| 157 | CHECK_INTERFACE(IQService, data, reply); |
| 158 | if(callerUid != AID_GRAPHICS) { |
Jeykumar Sankaran | 9f59a76 | 2013-02-28 10:45:56 -0800 | [diff] [blame] | 159 | ALOGE("display.qservice CONNECT access denied: \ |
| 160 | pid=%d uid=%d process=%s", |
Saurabh Shah | 86c1729 | 2013-02-08 15:24:13 -0800 | [diff] [blame] | 161 | callerPid, callerUid, callingProcName); |
| 162 | return PERMISSION_DENIED; |
| 163 | } |
| 164 | sp<IQClient> client = |
| 165 | interface_cast<IQClient>(data.readStrongBinder()); |
| 166 | connect(client); |
| 167 | return NO_ERROR; |
| 168 | } break; |
Jeykumar Sankaran | 9f59a76 | 2013-02-28 10:45:56 -0800 | [diff] [blame] | 169 | case SCREEN_REFRESH: { |
| 170 | CHECK_INTERFACE(IQService, data, reply); |
Ping Li | fec6c97 | 2013-03-18 13:24:07 -0400 | [diff] [blame] | 171 | if(callerUid != AID_SYSTEM) { |
Jeykumar Sankaran | 9f59a76 | 2013-02-28 10:45:56 -0800 | [diff] [blame] | 172 | ALOGE("display.qservice SCREEN_REFRESH access denied: \ |
| 173 | pid=%d uid=%d process=%s",callerPid, |
| 174 | callerUid, callingProcName); |
| 175 | return PERMISSION_DENIED; |
| 176 | } |
| 177 | return screenRefresh(); |
| 178 | } break; |
Arun Kumar K.R | 4b79e27 | 2013-05-21 10:41:02 -0700 | [diff] [blame] | 179 | case EXTERNAL_ORIENTATION: { |
| 180 | CHECK_INTERFACE(IQService, data, reply); |
| 181 | if(callerUid != AID_SYSTEM) { |
| 182 | ALOGE("display.qservice EXTERNAL_ORIENTATION access denied: \ |
| 183 | pid=%d uid=%d process=%s",callerPid, |
| 184 | callerUid, callingProcName); |
| 185 | return PERMISSION_DENIED; |
| 186 | } |
| 187 | uint32_t orientation = data.readInt32(); |
| 188 | setExtOrientation(orientation); |
| 189 | return NO_ERROR; |
| 190 | } break; |
Arun Kumar K.R | fb5bfa6 | 2013-07-25 03:10:51 -0700 | [diff] [blame] | 191 | case BUFFER_MIRRORMODE: { |
| 192 | CHECK_INTERFACE(IQService, data, reply); |
| 193 | if(callerUid != AID_SYSTEM) { |
| 194 | ALOGE("display.qservice BUFFER_MIRRORMODE access denied: \ |
| 195 | pid=%d uid=%d process=%s",callerPid, |
| 196 | callerUid, callingProcName); |
| 197 | return PERMISSION_DENIED; |
| 198 | } |
| 199 | uint32_t enable = data.readInt32(); |
| 200 | setBufferMirrorMode(enable); |
| 201 | return NO_ERROR; |
| 202 | } break; |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 203 | default: |
| 204 | return BBinder::onTransact(code, data, reply, flags); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | //Helper |
| 209 | static void getProcName(int pid, char *buf, int size) { |
| 210 | int fd = -1; |
| 211 | snprintf(buf, size, "/proc/%d/cmdline", pid); |
| 212 | fd = open(buf, O_RDONLY); |
| 213 | if (fd < 0) { |
| 214 | strcpy(buf, "Unknown"); |
| 215 | } else { |
| 216 | int len = read(fd, buf, size - 1); |
| 217 | buf[len] = 0; |
| 218 | close(fd); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | }; // namespace qService |