blob: 420d59c5d8d6b9567f1f40b445ba708830f97950 [file] [log] [blame]
Saurabh Shah56f610d2012-08-07 15:27:06 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, The Linux Foundation. All rights reserved.
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 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;
34
35// ---------------------------------------------------------------------------
36
37namespace qService {
38
39class BpQService : public BpInterface<IQService>
40{
41public:
42 BpQService(const sp<IBinder>& impl)
43 : BpInterface<IQService>(impl) {}
44
45 virtual void securing(uint32_t startEnd) {
46 Parcel data, reply;
47 data.writeInterfaceToken(IQService::getInterfaceDescriptor());
48 data.writeInt32(startEnd);
49 remote()->transact(SECURING, data, &reply);
50 }
51
52 virtual void unsecuring(uint32_t startEnd) {
53 Parcel data, reply;
54 data.writeInterfaceToken(IQService::getInterfaceDescriptor());
55 data.writeInt32(startEnd);
56 remote()->transact(UNSECURING, data, &reply);
57 }
58};
59
60IMPLEMENT_META_INTERFACE(QService, "android.display.IQService");
61
62// ----------------------------------------------------------------------
63
64static void getProcName(int pid, char *buf, int size);
65
66status_t BnQService::onTransact(
67 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
68{
69 // IPC should be from mediaserver only
70 IPCThreadState* ipc = IPCThreadState::self();
71 const int callerPid = ipc->getCallingPid();
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040072 const int callerUid = ipc->getCallingUid();
Saurabh Shah56f610d2012-08-07 15:27:06 -070073 const size_t MAX_BUF_SIZE = 1024;
Saurabh Shah56f610d2012-08-07 15:27:06 -070074 char callingProcName[MAX_BUF_SIZE] = {0};
75
76 getProcName(callerPid, callingProcName, MAX_BUF_SIZE);
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040077
78 const bool permission = (callerUid == AID_MEDIA);
Saurabh Shah56f610d2012-08-07 15:27:06 -070079
80 switch(code) {
81 case SECURING: {
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040082 if(!permission) {
83 ALOGE("display.qservice SECURING access denied: pid=%d uid=%d process=%s",
84 callerPid, callerUid, callingProcName);
85 return PERMISSION_DENIED;
86 }
Saurabh Shah56f610d2012-08-07 15:27:06 -070087 CHECK_INTERFACE(IQService, data, reply);
88 uint32_t startEnd = data.readInt32();
89 securing(startEnd);
90 return NO_ERROR;
91 } break;
92 case UNSECURING: {
Naseer Ahmed52fc4cd2012-09-24 13:38:00 -040093 if(!permission) {
94 ALOGE("display.qservice UNSECURING access denied: pid=%d uid=%d process=%s",
95 callerPid, callerUid, callingProcName);
96 return PERMISSION_DENIED;
97 }
Saurabh Shah56f610d2012-08-07 15:27:06 -070098 CHECK_INTERFACE(IQService, data, reply);
99 uint32_t startEnd = data.readInt32();
100 unsecuring(startEnd);
101 return NO_ERROR;
102 } break;
103 default:
104 return BBinder::onTransact(code, data, reply, flags);
105 }
106}
107
108//Helper
109static void getProcName(int pid, char *buf, int size) {
110 int fd = -1;
111 snprintf(buf, size, "/proc/%d/cmdline", pid);
112 fd = open(buf, O_RDONLY);
113 if (fd < 0) {
114 strcpy(buf, "Unknown");
115 } else {
116 int len = read(fd, buf, size - 1);
117 buf[len] = 0;
118 close(fd);
119 }
120}
121
122}; // namespace qService