blob: f63e4116daa083fc03c3cd552dc583c1e5a21b50 [file] [log] [blame]
Saurabh Shah56f610d2012-08-07 15:27:06 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Naseer Ahmedce077232016-03-14 19:58:14 -04003 * Copyright (C) 2012-2016, 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#include <IQService.h>
31
Naseer Ahmed4957c522013-11-12 18:07:15 -050032#define QSERVICE_DEBUG 0
33
Saurabh Shah56f610d2012-08-07 15:27:06 -070034using namespace android;
Saurabh Shah86c17292013-02-08 15:24:13 -080035using namespace qClient;
Saurabh Shah56f610d2012-08-07 15:27:06 -070036
37// ---------------------------------------------------------------------------
38
39namespace qService {
40
41class BpQService : public BpInterface<IQService>
42{
43public:
44 BpQService(const sp<IBinder>& impl)
45 : BpInterface<IQService>(impl) {}
46
Saurabh Shah86c17292013-02-08 15:24:13 -080047 virtual void connect(const sp<IQClient>& client) {
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -040048 ALOGD_IF(QSERVICE_DEBUG, "%s: connect HWC client", __FUNCTION__);
Saurabh Shah86c17292013-02-08 15:24:13 -080049 Parcel data, reply;
50 data.writeInterfaceToken(IQService::getInterfaceDescriptor());
Ajay Dudani6c515122015-04-08 19:29:29 -070051 data.writeStrongBinder(IInterface::asBinder(client));
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -040052 remote()->transact(CONNECT_HWC_CLIENT, data, &reply);
Saurabh Shah86c17292013-02-08 15:24:13 -080053 }
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -080054
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -040055 virtual void connect(const sp<IQHDMIClient>& client) {
56 ALOGD_IF(QSERVICE_DEBUG, "%s: connect HDMI client", __FUNCTION__);
57 Parcel data, reply;
58 data.writeInterfaceToken(IQService::getInterfaceDescriptor());
Ajay Dudani6c515122015-04-08 19:29:29 -070059 data.writeStrongBinder(IInterface::asBinder(client));
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -040060 remote()->transact(CONNECT_HDMI_CLIENT, data, &reply);
61 }
62
63
Naseer Ahmed4957c522013-11-12 18:07:15 -050064 virtual android::status_t dispatch(uint32_t command, const Parcel* inParcel,
65 Parcel* outParcel) {
66 ALOGD_IF(QSERVICE_DEBUG, "%s: dispatch in:%p", __FUNCTION__, inParcel);
Arun Kumar K.Rf15adc02014-01-21 21:26:25 -080067 status_t err = (status_t) android::FAILED_TRANSACTION;
Naseer Ahmed4957c522013-11-12 18:07:15 -050068 Parcel data;
69 Parcel *reply = outParcel;
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -080070 data.writeInterfaceToken(IQService::getInterfaceDescriptor());
Naseer Ahmed4957c522013-11-12 18:07:15 -050071 if (inParcel && inParcel->dataSize() > 0)
72 data.appendFrom(inParcel, 0, inParcel->dataSize());
73 err = remote()->transact(command, data, reply);
74 return err;
Naseer Ahmed58780b92013-07-29 17:41:40 -040075 }
Saurabh Shah56f610d2012-08-07 15:27:06 -070076};
77
78IMPLEMENT_META_INTERFACE(QService, "android.display.IQService");
79
80// ----------------------------------------------------------------------
81
82static void getProcName(int pid, char *buf, int size);
83
84status_t BnQService::onTransact(
85 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
86{
Naseer Ahmed4957c522013-11-12 18:07:15 -050087 ALOGD_IF(QSERVICE_DEBUG, "%s: code: %d", __FUNCTION__, code);
88 // IPC should be from certain processes only
Saurabh Shah56f610d2012-08-07 15:27:06 -070089 IPCThreadState* ipc = IPCThreadState::self();
90 const int callerPid = ipc->getCallingPid();
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040091 const int callerUid = ipc->getCallingUid();
Arun Kumar K.Rf15adc02014-01-21 21:26:25 -080092 const int MAX_BUF_SIZE = 1024;
Saurabh Shah56f610d2012-08-07 15:27:06 -070093 char callingProcName[MAX_BUF_SIZE] = {0};
94
95 getProcName(callerPid, callingProcName, MAX_BUF_SIZE);
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040096
Naseer Ahmed4957c522013-11-12 18:07:15 -050097 const bool permission = (callerUid == AID_MEDIA ||
98 callerUid == AID_GRAPHICS ||
99 callerUid == AID_ROOT ||
Naseer Ahmedce077232016-03-14 19:58:14 -0400100 callerUid == AID_CAMERASERVER ||
Naseer Ahmed4957c522013-11-12 18:07:15 -0500101 callerUid == AID_SYSTEM);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700102
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -0400103 if (code == CONNECT_HWC_CLIENT) {
Naseer Ahmed4957c522013-11-12 18:07:15 -0500104 CHECK_INTERFACE(IQService, data, reply);
105 if(callerUid != AID_GRAPHICS) {
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -0400106 ALOGE("display.qservice CONNECT_HWC_CLIENT access denied: \
Naseer Ahmed4957c522013-11-12 18:07:15 -0500107 pid=%d uid=%d process=%s",
108 callerPid, callerUid, callingProcName);
109 return PERMISSION_DENIED;
110 }
111 sp<IQClient> client =
112 interface_cast<IQClient>(data.readStrongBinder());
113 connect(client);
114 return NO_ERROR;
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -0400115 } else if(code == CONNECT_HDMI_CLIENT) {
116 CHECK_INTERFACE(IQService, data, reply);
117 if(callerUid != AID_SYSTEM && callerUid != AID_ROOT) {
118 ALOGE("display.qservice CONNECT_HDMI_CLIENT access denied: \
119 pid=%d uid=%d process=%s",
120 callerPid, callerUid, callingProcName);
121 return PERMISSION_DENIED;
122 }
123 sp<IQHDMIClient> client =
124 interface_cast<IQHDMIClient>(data.readStrongBinder());
125 connect(client);
126 return NO_ERROR;
Naseer Ahmed4957c522013-11-12 18:07:15 -0500127 } else if (code > COMMAND_LIST_START && code < COMMAND_LIST_END) {
128 if(!permission) {
129 ALOGE("display.qservice access denied: command=%d\
130 pid=%d uid=%d process=%s", code, callerPid,
Naseer Ahmed58780b92013-07-29 17:41:40 -0400131 callerUid, callingProcName);
132 return PERMISSION_DENIED;
133 }
134 CHECK_INTERFACE(IQService, data, reply);
Naseer Ahmed4957c522013-11-12 18:07:15 -0500135 dispatch(code, &data, reply);
136 return NO_ERROR;
137 } else {
138 return BBinder::onTransact(code, data, reply, flags);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700139 }
140}
141
142//Helper
143static void getProcName(int pid, char *buf, int size) {
144 int fd = -1;
145 snprintf(buf, size, "/proc/%d/cmdline", pid);
146 fd = open(buf, O_RDONLY);
147 if (fd < 0) {
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800148 strlcpy(buf, "Unknown", size);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700149 } else {
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530150 ssize_t len = read(fd, buf, size - 1);
151 if (len >= 0)
152 buf[len] = 0;
153
Saurabh Shah56f610d2012-08-07 15:27:06 -0700154 close(fd);
155 }
156}
157
158}; // namespace qService