Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 29 | #include <private/android_filesystem_config.h> |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 30 | |
| 31 | #include <IQService.h> |
| 32 | |
| 33 | using namespace android; |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | namespace qService { |
| 38 | |
| 39 | class BpQService : public BpInterface<IQService> |
| 40 | { |
| 41 | public: |
| 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 | |
| 60 | IMPLEMENT_META_INTERFACE(QService, "android.display.IQService"); |
| 61 | |
| 62 | // ---------------------------------------------------------------------- |
| 63 | |
| 64 | static void getProcName(int pid, char *buf, int size); |
| 65 | |
| 66 | status_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 Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 72 | const int callerUid = ipc->getCallingUid(); |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 73 | const size_t MAX_BUF_SIZE = 1024; |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 74 | char callingProcName[MAX_BUF_SIZE] = {0}; |
| 75 | |
| 76 | getProcName(callerPid, callingProcName, MAX_BUF_SIZE); |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 77 | |
| 78 | const bool permission = (callerUid == AID_MEDIA); |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 79 | |
| 80 | switch(code) { |
| 81 | case SECURING: { |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 82 | 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 Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 87 | CHECK_INTERFACE(IQService, data, reply); |
| 88 | uint32_t startEnd = data.readInt32(); |
| 89 | securing(startEnd); |
| 90 | return NO_ERROR; |
| 91 | } break; |
| 92 | case UNSECURING: { |
Naseer Ahmed | 52fc4cd | 2012-09-24 13:38:00 -0400 | [diff] [blame] | 93 | 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 Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 98 | 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 |
| 109 | static 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 |