display: Create QService binder interface.

Add QService binder interface to enable communication
to display by the mediaserver for Securing/Unsecuring start and end
notifications.

Create separate lib for external.
Clear reserved field before applying format.

Change-Id: I463c9c6deac7587bd0c4e0b84513b5d0b5dd7e98
diff --git a/libqservice/IQService.cpp b/libqservice/IQService.cpp
new file mode 100644
index 0000000..5f71732
--- /dev/null
+++ b/libqservice/IQService.cpp
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2012, The Linux Foundation. All rights reserved.
+ *
+ * Not a Contribution, Apache license notifications and license are
+ * retained for attribution purposes only.
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fcntl.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <binder/Parcel.h>
+#include <binder/IBinder.h>
+#include <binder/IInterface.h>
+#include <binder/IPCThreadState.h>
+#include <utils/Errors.h>
+
+#include <IQService.h>
+
+using namespace android;
+
+// ---------------------------------------------------------------------------
+
+namespace qService {
+
+class BpQService : public BpInterface<IQService>
+{
+public:
+    BpQService(const sp<IBinder>& impl)
+        : BpInterface<IQService>(impl) {}
+
+    virtual void securing(uint32_t startEnd) {
+        Parcel data, reply;
+        data.writeInterfaceToken(IQService::getInterfaceDescriptor());
+        data.writeInt32(startEnd);
+        remote()->transact(SECURING, data, &reply);
+    }
+
+    virtual void unsecuring(uint32_t startEnd) {
+        Parcel data, reply;
+        data.writeInterfaceToken(IQService::getInterfaceDescriptor());
+        data.writeInt32(startEnd);
+        remote()->transact(UNSECURING, data, &reply);
+    }
+};
+
+IMPLEMENT_META_INTERFACE(QService, "android.display.IQService");
+
+// ----------------------------------------------------------------------
+
+static void getProcName(int pid, char *buf, int size);
+
+status_t BnQService::onTransact(
+    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
+{
+    // IPC should be from mediaserver only
+    IPCThreadState* ipc = IPCThreadState::self();
+    const int callerPid = ipc->getCallingPid();
+    const size_t MAX_BUF_SIZE = 1024;
+    const char *mediaServer = "/system/bin/mediaserver";
+    char callingProcName[MAX_BUF_SIZE] = {0};
+
+    getProcName(callerPid, callingProcName, MAX_BUF_SIZE);
+    if(strcmp(callingProcName, mediaServer) != 0 ) { //Some rogue process
+        ALOGE("No Permission:can't access display.qservice pid=%d process=%s",
+                callerPid, callingProcName);
+        return PERMISSION_DENIED;
+    }
+
+    switch(code) {
+        case SECURING: {
+            CHECK_INTERFACE(IQService, data, reply);
+            uint32_t startEnd = data.readInt32();
+            securing(startEnd);
+            return NO_ERROR;
+        } break;
+        case UNSECURING: {
+            CHECK_INTERFACE(IQService, data, reply);
+            uint32_t startEnd = data.readInt32();
+            unsecuring(startEnd);
+            return NO_ERROR;
+        } break;
+        default:
+            return BBinder::onTransact(code, data, reply, flags);
+    }
+}
+
+//Helper
+static void getProcName(int pid, char *buf, int size) {
+    int fd = -1;
+    snprintf(buf, size, "/proc/%d/cmdline", pid);
+    fd = open(buf, O_RDONLY);
+    if (fd < 0) {
+        strcpy(buf, "Unknown");
+    } else {
+        int len = read(fd, buf, size - 1);
+        buf[len] = 0;
+        close(fd);
+    }
+}
+
+}; // namespace qService