blob: a3ff1505e842e2fd97ecb81a99874e10ab27f052 [file] [log] [blame]
Saurabh Shah56f610d2012-08-07 15:27:06 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Saurabh Shah86c17292013-02-08 15:24:13 -08003 * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved.
Saurabh Shah56f610d2012-08-07 15:27:06 -07004 *
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 Ahmed52fc4cd2012-09-24 13:38:00 -040029#include <private/android_filesystem_config.h>
Saurabh Shah56f610d2012-08-07 15:27:06 -070030
31#include <IQService.h>
32
33using namespace android;
Saurabh Shah86c17292013-02-08 15:24:13 -080034using namespace qClient;
Saurabh Shah56f610d2012-08-07 15:27:06 -070035
36// ---------------------------------------------------------------------------
37
38namespace qService {
39
40class BpQService : public BpInterface<IQService>
41{
42public:
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 Shah86c17292013-02-08 15:24:13 -080059
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 Sankaran9f59a762013-02-28 10:45:56 -080066
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.Rffef7482013-04-10 14:17:22 -070074
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 }
Saurabh Shah56f610d2012-08-07 15:27:06 -070081};
82
83IMPLEMENT_META_INTERFACE(QService, "android.display.IQService");
84
85// ----------------------------------------------------------------------
86
87static void getProcName(int pid, char *buf, int size);
88
89status_t BnQService::onTransact(
90 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
91{
92 // IPC should be from mediaserver only
93 IPCThreadState* ipc = IPCThreadState::self();
94 const int callerPid = ipc->getCallingPid();
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040095 const int callerUid = ipc->getCallingUid();
Saurabh Shah56f610d2012-08-07 15:27:06 -070096 const size_t MAX_BUF_SIZE = 1024;
Saurabh Shah56f610d2012-08-07 15:27:06 -070097 char callingProcName[MAX_BUF_SIZE] = {0};
98
99 getProcName(callerPid, callingProcName, MAX_BUF_SIZE);
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -0400100
101 const bool permission = (callerUid == AID_MEDIA);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700102
103 switch(code) {
104 case SECURING: {
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -0400105 if(!permission) {
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -0800106 ALOGE("display.qservice SECURING access denied: \
107 pid=%d uid=%d process=%s",
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -0400108 callerPid, callerUid, callingProcName);
109 return PERMISSION_DENIED;
110 }
Saurabh Shah56f610d2012-08-07 15:27:06 -0700111 CHECK_INTERFACE(IQService, data, reply);
112 uint32_t startEnd = data.readInt32();
113 securing(startEnd);
114 return NO_ERROR;
115 } break;
116 case UNSECURING: {
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -0400117 if(!permission) {
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -0800118 ALOGE("display.qservice UNSECURING access denied: \
119 pid=%d uid=%d process=%s",
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -0400120 callerPid, callerUid, callingProcName);
121 return PERMISSION_DENIED;
122 }
Saurabh Shah56f610d2012-08-07 15:27:06 -0700123 CHECK_INTERFACE(IQService, data, reply);
124 uint32_t startEnd = data.readInt32();
125 unsecuring(startEnd);
126 return NO_ERROR;
127 } break;
Saurabh Shah86c17292013-02-08 15:24:13 -0800128 case CONNECT: {
129 CHECK_INTERFACE(IQService, data, reply);
130 if(callerUid != AID_GRAPHICS) {
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -0800131 ALOGE("display.qservice CONNECT access denied: \
132 pid=%d uid=%d process=%s",
Saurabh Shah86c17292013-02-08 15:24:13 -0800133 callerPid, callerUid, callingProcName);
134 return PERMISSION_DENIED;
135 }
136 sp<IQClient> client =
137 interface_cast<IQClient>(data.readStrongBinder());
138 connect(client);
139 return NO_ERROR;
140 } break;
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -0800141 case SCREEN_REFRESH: {
142 CHECK_INTERFACE(IQService, data, reply);
Ping Lifec6c972013-03-18 13:24:07 -0400143 if(callerUid != AID_SYSTEM) {
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -0800144 ALOGE("display.qservice SCREEN_REFRESH access denied: \
145 pid=%d uid=%d process=%s",callerPid,
146 callerUid, callingProcName);
147 return PERMISSION_DENIED;
148 }
149 return screenRefresh();
150 } break;
Arun Kumar K.R4b79e272013-05-21 10:41:02 -0700151 case EXTERNAL_ORIENTATION: {
152 CHECK_INTERFACE(IQService, data, reply);
153 if(callerUid != AID_SYSTEM) {
154 ALOGE("display.qservice EXTERNAL_ORIENTATION access denied: \
155 pid=%d uid=%d process=%s",callerPid,
156 callerUid, callingProcName);
157 return PERMISSION_DENIED;
158 }
159 uint32_t orientation = data.readInt32();
160 setExtOrientation(orientation);
161 return NO_ERROR;
162 } break;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700163 default:
164 return BBinder::onTransact(code, data, reply, flags);
165 }
166}
167
168//Helper
169static void getProcName(int pid, char *buf, int size) {
170 int fd = -1;
171 snprintf(buf, size, "/proc/%d/cmdline", pid);
172 fd = open(buf, O_RDONLY);
173 if (fd < 0) {
174 strcpy(buf, "Unknown");
175 } else {
176 int len = read(fd, buf, size - 1);
177 buf[len] = 0;
178 close(fd);
179 }
180}
181
182}; // namespace qService