blob: 7a88bdf4ac6bc17c6e6e4414f52accb90405cd69 [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 }
Saurabh Shah56f610d2012-08-07 15:27:06 -070066};
67
68IMPLEMENT_META_INTERFACE(QService, "android.display.IQService");
69
70// ----------------------------------------------------------------------
71
72static void getProcName(int pid, char *buf, int size);
73
74status_t BnQService::onTransact(
75 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
76{
77 // IPC should be from mediaserver only
78 IPCThreadState* ipc = IPCThreadState::self();
79 const int callerPid = ipc->getCallingPid();
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040080 const int callerUid = ipc->getCallingUid();
Saurabh Shah56f610d2012-08-07 15:27:06 -070081 const size_t MAX_BUF_SIZE = 1024;
Saurabh Shah56f610d2012-08-07 15:27:06 -070082 char callingProcName[MAX_BUF_SIZE] = {0};
83
84 getProcName(callerPid, callingProcName, MAX_BUF_SIZE);
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040085
86 const bool permission = (callerUid == AID_MEDIA);
Saurabh Shah56f610d2012-08-07 15:27:06 -070087
88 switch(code) {
89 case SECURING: {
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040090 if(!permission) {
91 ALOGE("display.qservice SECURING access denied: pid=%d uid=%d process=%s",
92 callerPid, callerUid, callingProcName);
93 return PERMISSION_DENIED;
94 }
Saurabh Shah56f610d2012-08-07 15:27:06 -070095 CHECK_INTERFACE(IQService, data, reply);
96 uint32_t startEnd = data.readInt32();
97 securing(startEnd);
98 return NO_ERROR;
99 } break;
100 case UNSECURING: {
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -0400101 if(!permission) {
102 ALOGE("display.qservice UNSECURING access denied: pid=%d uid=%d process=%s",
103 callerPid, callerUid, callingProcName);
104 return PERMISSION_DENIED;
105 }
Saurabh Shah56f610d2012-08-07 15:27:06 -0700106 CHECK_INTERFACE(IQService, data, reply);
107 uint32_t startEnd = data.readInt32();
108 unsecuring(startEnd);
109 return NO_ERROR;
110 } break;
Saurabh Shah86c17292013-02-08 15:24:13 -0800111 case CONNECT: {
112 CHECK_INTERFACE(IQService, data, reply);
113 if(callerUid != AID_GRAPHICS) {
114 ALOGE("display.qservice CONNECT access denied: pid=%d uid=%d process=%s",
115 callerPid, callerUid, callingProcName);
116 return PERMISSION_DENIED;
117 }
118 sp<IQClient> client =
119 interface_cast<IQClient>(data.readStrongBinder());
120 connect(client);
121 return NO_ERROR;
122 } break;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700123 default:
124 return BBinder::onTransact(code, data, reply, flags);
125 }
126}
127
128//Helper
129static void getProcName(int pid, char *buf, int size) {
130 int fd = -1;
131 snprintf(buf, size, "/proc/%d/cmdline", pid);
132 fd = open(buf, O_RDONLY);
133 if (fd < 0) {
134 strcpy(buf, "Unknown");
135 } else {
136 int len = read(fd, buf, size - 1);
137 buf[len] = 0;
138 close(fd);
139 }
140}
141
142}; // namespace qService