blob: d2180bb0a2b4391e9f678ba0b6ae0d84b17abd88 [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#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 Ahmed4957c522013-11-12 18:07:15 -050048 ALOGD_IF(QSERVICE_DEBUG, "%s: connect client", __FUNCTION__);
Saurabh Shah86c17292013-02-08 15:24:13 -080049 Parcel data, reply;
50 data.writeInterfaceToken(IQService::getInterfaceDescriptor());
51 data.writeStrongBinder(client->asBinder());
52 remote()->transact(CONNECT, data, &reply);
53 }
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -080054
Naseer Ahmed4957c522013-11-12 18:07:15 -050055 virtual android::status_t dispatch(uint32_t command, const Parcel* inParcel,
56 Parcel* outParcel) {
57 ALOGD_IF(QSERVICE_DEBUG, "%s: dispatch in:%p", __FUNCTION__, inParcel);
58 status_t err = android::FAILED_TRANSACTION;
59 Parcel data;
60 Parcel *reply = outParcel;
Jeykumar Sankaran9f59a762013-02-28 10:45:56 -080061 data.writeInterfaceToken(IQService::getInterfaceDescriptor());
Naseer Ahmed4957c522013-11-12 18:07:15 -050062 if (inParcel && inParcel->dataSize() > 0)
63 data.appendFrom(inParcel, 0, inParcel->dataSize());
64 err = remote()->transact(command, data, reply);
65 return err;
Naseer Ahmed58780b92013-07-29 17:41:40 -040066 }
Saurabh Shah56f610d2012-08-07 15:27:06 -070067};
68
69IMPLEMENT_META_INTERFACE(QService, "android.display.IQService");
70
71// ----------------------------------------------------------------------
72
73static void getProcName(int pid, char *buf, int size);
74
75status_t BnQService::onTransact(
76 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
77{
Naseer Ahmed4957c522013-11-12 18:07:15 -050078 ALOGD_IF(QSERVICE_DEBUG, "%s: code: %d", __FUNCTION__, code);
79 // IPC should be from certain processes only
Saurabh Shah56f610d2012-08-07 15:27:06 -070080 IPCThreadState* ipc = IPCThreadState::self();
81 const int callerPid = ipc->getCallingPid();
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040082 const int callerUid = ipc->getCallingUid();
Saurabh Shah56f610d2012-08-07 15:27:06 -070083 const size_t MAX_BUF_SIZE = 1024;
Saurabh Shah56f610d2012-08-07 15:27:06 -070084 char callingProcName[MAX_BUF_SIZE] = {0};
85
86 getProcName(callerPid, callingProcName, MAX_BUF_SIZE);
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040087
Naseer Ahmed4957c522013-11-12 18:07:15 -050088 const bool permission = (callerUid == AID_MEDIA ||
89 callerUid == AID_GRAPHICS ||
90 callerUid == AID_ROOT ||
91 callerUid == AID_SYSTEM);
Saurabh Shah56f610d2012-08-07 15:27:06 -070092
Naseer Ahmed4957c522013-11-12 18:07:15 -050093 if (code == CONNECT) {
94 CHECK_INTERFACE(IQService, data, reply);
95 if(callerUid != AID_GRAPHICS) {
96 ALOGE("display.qservice CONNECT access denied: \
97 pid=%d uid=%d process=%s",
98 callerPid, callerUid, callingProcName);
99 return PERMISSION_DENIED;
100 }
101 sp<IQClient> client =
102 interface_cast<IQClient>(data.readStrongBinder());
103 connect(client);
104 return NO_ERROR;
105 } else if (code > COMMAND_LIST_START && code < COMMAND_LIST_END) {
106 if(!permission) {
107 ALOGE("display.qservice access denied: command=%d\
108 pid=%d uid=%d process=%s", code, callerPid,
Naseer Ahmed58780b92013-07-29 17:41:40 -0400109 callerUid, callingProcName);
110 return PERMISSION_DENIED;
111 }
112 CHECK_INTERFACE(IQService, data, reply);
Naseer Ahmed4957c522013-11-12 18:07:15 -0500113 dispatch(code, &data, reply);
114 return NO_ERROR;
115 } else {
116 return BBinder::onTransact(code, data, reply, flags);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700117 }
118}
119
120//Helper
121static void getProcName(int pid, char *buf, int size) {
122 int fd = -1;
123 snprintf(buf, size, "/proc/%d/cmdline", pid);
124 fd = open(buf, O_RDONLY);
125 if (fd < 0) {
126 strcpy(buf, "Unknown");
127 } else {
128 int len = read(fd, buf, size - 1);
129 buf[len] = 0;
130 close(fd);
131 }
132}
133
134}; // namespace qService