system-controller: added initial support for application tracking.
authorIsmo Puustinen <ismo.puustinen@intel.com>
Thu, 13 Feb 2014 09:24:11 +0000 (11:24 +0200)
committerKrisztian Litkey <krisztian.litkey@intel.com>
Thu, 8 Jan 2015 16:37:13 +0000 (18:37 +0200)
Change-Id: I8df459fa08e7f15f8137fca2333bd5f650d7e2ef

packaging.in/murphy.lua
src/Makefile.am
src/plugins/system-controller/application-tracker/application-tracker.c [new file with mode: 0644]
src/plugins/system-controller/application-tracker/application-tracker.h [new file with mode: 0644]
src/plugins/system-controller/plugin-system-controller.c

index 923d93b..348a5bc 100644 (file)
@@ -44,6 +44,12 @@ else
     m:info("No domain-control plugin found...")
 end
 
+if m:plugin_exists('glib') then
+    m:load_plugin('glib')
+else
+    m:info("No glib plugin found...")
+end
+
 -- load the AMB plugin
 if m:plugin_exists('amb') then
     m:try_load_plugin('amb')
index 4d9acbd..1a4b420 100644 (file)
@@ -1532,6 +1532,7 @@ SYSTEMCTL_PLUGIN_SOURCES =                                             \
        $(SYSTEMCTL_DIR)/resource-client/scripting-resource-client.c   \
        $(SYSTEMCTL_DIR)/application/application.c                     \
        $(SYSTEMCTL_DIR)/application/scripting-application.c           \
+       $(SYSTEMCTL_DIR)/application-tracker/application-tracker.c     \
        $(SYSTEMCTL_DIR)/user/user.c                                   \
        $(SYSTEMCTL_DIR)/wayland/wayland.c                             \
        $(SYSTEMCTL_DIR)/wayland/output.c                              \
diff --git a/src/plugins/system-controller/application-tracker/application-tracker.c b/src/plugins/system-controller/application-tracker/application-tracker.c
new file mode 100644 (file)
index 0000000..582b187
--- /dev/null
@@ -0,0 +1,201 @@
+/*
+ * Copyright (c) 2013, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <murphy/common.h>
+
+#include <murphy-db/mdb.h>
+#include <murphy-db/mqi.h>
+#include <murphy-db/mql.h>
+
+#include <aul/aul.h>
+
+#define AUL_APPLICATION_TABLE_NAME "aul_applications"
+#define DB_BUF_SIZE 1024
+
+/* IMPORTANT: You need to have glib plugin loaded in order for this to work! */
+
+static int aul_dead_signal(int pid, void *user_data)
+{
+    char buf[DB_BUF_SIZE];
+    int buflen;
+    mql_result_t *r;
+    mqi_handle_t tx;
+
+    mrp_log_info("tracker: dead app %i", pid);
+
+    buflen = snprintf(buf, DB_BUF_SIZE, "DELETE FROM %s WHERE pid = +%i",
+            AUL_APPLICATION_TABLE_NAME, pid);
+
+    if (buflen <= 0 || buflen == DB_BUF_SIZE) {
+        return 0;
+    }
+
+    tx = mqi_begin_transaction();
+
+    mrp_log_info("tracker: '%s'", buf);
+
+    r = mql_exec_string(mql_result_string, buf);
+
+    mql_result_free(r);
+
+    mqi_commit_transaction(tx);
+
+    return 0;
+}
+
+static int add_app_to_db(const char *appid, pid_t pid, const char *category)
+{
+    char buf[DB_BUF_SIZE];
+    int buflen;
+    mql_result_t *r;
+    mqi_handle_t tx;
+
+    buflen = snprintf(buf, DB_BUF_SIZE, "INSERT INTO %s VALUES ('%s', %i, '%s')",
+            AUL_APPLICATION_TABLE_NAME, appid, pid, category);
+
+    if (buflen <= 0 || buflen == DB_BUF_SIZE) {
+        return 0;
+    }
+
+    tx = mqi_begin_transaction();
+
+    mrp_log_info("tracker: '%s'", buf);
+
+    r = mql_exec_string(mql_result_string, buf);
+    mql_result_free(r);
+
+    mqi_commit_transaction(tx);
+
+    return 0;
+}
+
+static int aul_launch_signal(int pid, void *user_data)
+{
+    char appid[512];
+    MRP_UNUSED(user_data);
+
+    mrp_log_info("tracker: launched app %i", pid);
+
+    aul_app_get_appid_bypid(pid, appid, 511);
+
+    add_app_to_db(appid, pid, "<undefined>");
+
+    return 0;
+}
+
+static int aul_iter_app_info(const aul_app_info *ai, void *user_data)
+{
+    MRP_UNUSED(user_data);
+
+    mrp_log_info("tracker: app info pid %i, appid: %s, pkg_name %s", ai->pid,
+            ai->appid ? ai->appid : "NULL", ai->pkg_name);
+
+    add_app_to_db(ai->appid ? ai->appid : "<undefined>", ai->pid, "<undefined>");
+
+    return 0;
+}
+
+static int aul_handler(aul_type type, bundle *b, void *user_data)
+{
+    MRP_UNUSED(type);
+    MRP_UNUSED(b);
+    MRP_UNUSED(user_data);
+
+    mrp_log_info("tracker: aul_handler");
+    return 0;
+}
+
+int mrp_application_tracker_create()
+{
+    mqi_handle_t table;
+    mqi_column_def_t defs[4];
+
+    /* init the database table */
+
+    defs[0].name = "appid";
+    defs[0].type = mqi_varchar;
+    defs[0].length = 64;
+    defs[0].flags = 0;
+
+    defs[1].name = "pid";
+    defs[1].type = mqi_integer;
+    defs[1].flags = 0;
+
+    defs[2].name = "category";
+    defs[2].type = mqi_varchar;
+    defs[2].length = 64;
+    defs[2].flags = 0;
+
+    memset(&defs[3], 0, sizeof(defs[2]));
+
+    table = MQI_CREATE_TABLE(AUL_APPLICATION_TABLE_NAME, MQI_TEMPORARY,
+            defs, NULL);
+
+    if (!table)
+        return -1;
+
+    /* set the application launch/death callbacks */
+
+    if (aul_launch_init(aul_handler, NULL) < 0) {
+        mrp_log_error("failed to init AUL");
+        return -1;
+    }
+
+    if (aul_listen_app_dead_signal(aul_dead_signal, NULL) < 0) {
+        mrp_log_error("failed to listen to application death signals");
+        return -1;
+    }
+
+    if (!aul_listen_app_launch_signal(aul_launch_signal, NULL) < 0) {
+        mrp_log_error("failed to listen to application launch signals");
+        return -1;
+    }
+
+    /* read the existing applications to the database */
+
+    if (!aul_app_get_running_app_info(aul_iter_app_info, NULL) < 0) {
+        mrp_log_error("failed to query running applications");
+        return -1;
+    }
+
+    return 0;
+}
+
+int mrp_application_tracker_destroy()
+{
+    /* TODO: delete the table, deinit AUL */
+    return 0;
+}
\ No newline at end of file
diff --git a/src/plugins/system-controller/application-tracker/application-tracker.h b/src/plugins/system-controller/application-tracker/application-tracker.h
new file mode 100644 (file)
index 0000000..5bbc98e
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2013, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_APPLICATION_TRACKER_H__
+#define __MURPHY_APPLICATION_TRACKER_H__
+
+int mrp_application_tracker_create();
+int mrp_application_tracker_destroy();
+
+#endif
\ No newline at end of file
index 7a8ed34..c04f57f 100644 (file)
@@ -47,6 +47,7 @@
 #include "application/scripting-application.h"
 #include "wayland/scripting-wayland.h"
 #include "user/user.h"
+#include "application-tracker/application-tracker.h"
 
 
 #define DEFAULT_ADDRESS "wsck:127.0.0.1:18081/ico_syc_protocol"
@@ -705,9 +706,10 @@ static int plugin_init(mrp_plugin_t *plugin)
 
         scptr = sc;
 
-       return TRUE;
-    }
+        mrp_application_tracker_create();
 
+        return TRUE;
+    }
 
  fail:
     if (sc != NULL) {
@@ -726,6 +728,8 @@ static void plugin_exit(mrp_plugin_t *plugin)
 
     scptr = NULL;
 
+    mrp_application_tracker_destroy();
+
     transport_destroy(sc);
 
     mrp_free(sc);