Home | History | Annotate | Download | only in libqservice
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  * Copyright (C) 2012-2016, 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>
     29 #include <private/android_filesystem_config.h>
     30 #include <IQService.h>
     31 
     32 #define QSERVICE_DEBUG 0
     33 
     34 using namespace android;
     35 using namespace qClient;
     36 
     37 // ---------------------------------------------------------------------------
     38 
     39 namespace qService {
     40 
     41 class BpQService : public BpInterface<IQService>
     42 {
     43 public:
     44     BpQService(const sp<IBinder>& impl)
     45         : BpInterface<IQService>(impl) {}
     46 
     47     virtual void connect(const sp<IQClient>& client) {
     48         ALOGD_IF(QSERVICE_DEBUG, "%s: connect HWC client", __FUNCTION__);
     49         Parcel data, reply;
     50         data.writeInterfaceToken(IQService::getInterfaceDescriptor());
     51         data.writeStrongBinder(IInterface::asBinder(client));
     52         remote()->transact(CONNECT_HWC_CLIENT, data, &reply);
     53     }
     54 
     55     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());
     59         data.writeStrongBinder(IInterface::asBinder(client));
     60         remote()->transact(CONNECT_HDMI_CLIENT, data, &reply);
     61     }
     62 
     63 
     64     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);
     67         status_t err = (status_t) android::FAILED_TRANSACTION;
     68         Parcel data;
     69         Parcel *reply = outParcel;
     70         data.writeInterfaceToken(IQService::getInterfaceDescriptor());
     71         if (inParcel && inParcel->dataSize() > 0)
     72             data.appendFrom(inParcel, 0, inParcel->dataSize());
     73         err = remote()->transact(command, data, reply);
     74         return err;
     75     }
     76 };
     77 
     78 IMPLEMENT_META_INTERFACE(QService, "android.display.IQService");
     79 
     80 // ----------------------------------------------------------------------
     81 
     82 status_t BnQService::onTransact(
     83     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
     84 {
     85     ALOGD_IF(QSERVICE_DEBUG, "%s: code: %d", __FUNCTION__, code);
     86     // IPC should be from certain processes only
     87     IPCThreadState* ipc = IPCThreadState::self();
     88     const int callerPid = ipc->getCallingPid();
     89     const int callerUid = ipc->getCallingUid();
     90 
     91     const bool permission = (callerUid == AID_MEDIA ||
     92             callerUid == AID_GRAPHICS ||
     93             callerUid == AID_ROOT ||
     94             callerUid == AID_CAMERASERVER ||
     95             callerUid == AID_AUDIO ||
     96             callerUid == AID_SYSTEM);
     97 
     98     if (code == CONNECT_HWC_CLIENT) {
     99         CHECK_INTERFACE(IQService, data, reply);
    100         if(callerUid != AID_GRAPHICS) {
    101             ALOGE("display.qservice CONNECT_HWC_CLIENT access denied: pid=%d uid=%d",
    102                    callerPid, callerUid);
    103             return PERMISSION_DENIED;
    104         }
    105         sp<IQClient> client =
    106                 interface_cast<IQClient>(data.readStrongBinder());
    107         connect(client);
    108         return NO_ERROR;
    109     } else if(code == CONNECT_HDMI_CLIENT) {
    110         CHECK_INTERFACE(IQService, data, reply);
    111         if(callerUid != AID_SYSTEM && callerUid != AID_ROOT) {
    112             ALOGE("display.qservice CONNECT_HDMI_CLIENT access denied: pid=%d uid=%d",
    113                    callerPid, callerUid);
    114             return PERMISSION_DENIED;
    115         }
    116         sp<IQHDMIClient> client =
    117                 interface_cast<IQHDMIClient>(data.readStrongBinder());
    118         connect(client);
    119         return NO_ERROR;
    120     } else if (code > COMMAND_LIST_START && code < COMMAND_LIST_END) {
    121         if(!permission) {
    122             ALOGE("display.qservice access denied: command=%d pid=%d uid=%d",
    123                    code, callerPid, callerUid);
    124             return PERMISSION_DENIED;
    125         }
    126         CHECK_INTERFACE(IQService, data, reply);
    127         dispatch(code, &data, reply);
    128         return NO_ERROR;
    129     } else {
    130         return BBinder::onTransact(code, data, reply, flags);
    131     }
    132 }
    133 
    134 }; // namespace qService
    135