--- /dev/null
+#include <stdlib.h>
+#include <uuid/uuid.h>
+#include <sqlite3.h>
+#include <string.h>
+#include <dlog.h>
+#include <unistd.h>
+#include <tzplatform_config.h>
+
+#include "eventsystem_daemon.h"
+
+#define DBPATH tzplatform_mkpath(TZ_SYS_DB, ".cion.db")
+
+#define CREATE_CION_TABLE " \
+PRAGMA user_version = 50; \
+PRAGMA journal_mode = PERSIST; \
+PRAGMA foreign_keys = ON; \
+BEGIN EXCLUSIVE TRANSACTION; \
+CREATE TABLE IF NOT EXISTS cion_info ( \
+ appid TEXT NOT NULL, \
+ uuid TEXT NOT NULL, \
+ PRIMARY KEY (appid) \
+); \
+COMMIT TRANSACTION; "
+
+static int __check_table_exist(sqlite3 *db) {
+ int ret;
+ const char *val;
+ sqlite3_stmt *stmt = NULL;
+ const char query[] =
+ "SELECT name FROM sqlite_master WHERE type='table'"
+ " ORDER BY name ASC";
+
+ ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ _E("prepare error: %s", sqlite3_errmsg(db));
+ ret = -1;
+ goto out;
+ }
+
+ ret = sqlite3_step(stmt);
+ if (ret != SQLITE_ROW) {
+ _E("fail to get row");
+ ret = -1;
+ goto out;
+ }
+
+ val = (const char*)sqlite3_column_text(stmt, 0);
+ if (val == NULL) {
+ _E("name is NULL");
+ ret = -1;
+ goto out;
+ }
+
+ if (strcmp("cion", val) != 0) {
+ ret = -1;
+ goto out;
+ }
+
+ ret = 0;
+
+out:
+ sqlite3_finalize(stmt);
+
+ return ret;
+}
+
+static int __create_table(sqlite3 *db) {
+ int ret;
+ char *errmsg = NULL;
+
+ ret = sqlite3_exec(db, CREATE_CION_TABLE,
+ NULL, NULL, &errmsg);
+ if (ret != SQLITE_OK) {
+ _E("create table fail : %s", errmsg);
+ sqlite3_free(errmsg);
+ return -1;
+ }
+
+ return 0;
+}
+
+int esd_cion_db_init(void) {
+ sqlite3 *db = NULL;
+
+ if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
+ sqlite3_close_v2(db);
+ unlink(DBPATH);
+
+ if (sqlite3_open_v2(DBPATH, &db,
+ SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
+ NULL) != SQLITE_OK) {
+ _E("Fail to create db");
+ unlink(DBPATH);
+ return -1;
+ }
+ }
+
+ if (__check_table_exist(db) < 0) {
+ if (__create_table(db) < 0) {
+ sqlite3_close_v2(db);
+ _E("Fail to create table");
+ return -1;
+ }
+ }
+
+ sqlite3_close_v2(db);
+
+ return 0;
+}
+
+sqlite3 *esd_cion_db_open() {
+ sqlite3 *db;
+
+ if (access(DBPATH, R_OK | W_OK) != 0)
+ return NULL;
+
+ if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
+ return NULL;
+
+ return db;
+}
+
+int esd_cion_db_close(sqlite3 **db) {
+ if (db == NULL || *db == NULL)
+ return 0;
+
+ if (sqlite3_close(*db) != SQLITE_OK) {
+ _E("Failed to close db");
+ return 0;
+ }
+
+ *db = NULL;
+
+ return 0;
+}
+
+static const char *__esd_cion_generate_uuid() {
+ uuid_t uu;
+ char _uuid[37];
+ char *uuid;
+
+ uuid_generate_random(uu);
+ uuid_unparse(uu, _uuid);
+
+ uuid = strdup(_uuid);
+
+ return uuid;
+}
+
+int esd_cion_get_uuid_with_generate(const char *appid, char **uuid) {
+ int ret;
+ sqlite3 *db;
+ char *query = NULL;
+ sqlite3_stmt *stmt = NULL;
+ const char *_uuid;
+
+ db = esd_cion_db_open();
+ if (!db) {
+ _E("db open fail");
+ return -1;
+ }
+
+ query = sqlite3_mprintf("SELECT uuid FROM cion_info "
+ "WHERE appid = %Q", appid);
+
+ ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ ret = -1;
+ goto out;
+ }
+
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_ROW) {
+ ret = 0;
+ *uuid = strdup((char*)sqlite3_column_text(stmt, 0));
+ } else {
+ sqlite3_free(query);
+ sqlite3_finalize(stmt);
+ stmt = NULL;
+
+ _uuid = __esd_cion_generate_uuid();
+ query = sqlite3_mprintf("INSERT INTO cion_info (appid, uuid) "
+ "VALUES (%Q, %Q) ", appid, _uuid);
+
+ ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ ret = -1;
+ goto out;
+ }
+
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_OK || ret == SQLITE_DONE) {
+ ret = 0;
+ *uuid = strdup(_uuid);
+ } else {
+ ret = -1;
+ }
+ }
+
+out:
+ sqlite3_free(query);
+ sqlite3_finalize(stmt);
+ esd_cion_db_close(&db);
+
+ return ret;
+}
" <arg type='b' name='trusted' direction='in'/>"
" <arg type='i' name='ret' direction='out'/>"
" </method>"
+" <method name='GetUuid'>"
+" <arg type='i' name='ret' direction='out'/>"
+" <arg type='s' name='uuid' direction='out'/>"
+" </method>"
" </interface>"
"</node>";
-static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size)
+static int __esd_get_appid_by_pid_for_uid(int pid, uid_t uid, char *app_id, int buf_size)
{
int retval = ES_R_OK;
int ret = 0;
return retval;
}
+static int __esd_get_appid_by_pid(int pid, char *app_id, int buf_size)
+{
+ int ret;
+ int fd;
+ char buf[128] = { 0, };
+
+ ret = aul_app_get_appid_bypid(pid, app_id, buf_size);
+ if (ret != AUL_R_OK) {
+ snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
+
+ fd = open(buf, O_RDONLY);
+ if (fd < 0)
+ return ES_R_ERROR;
+
+ ret = read(fd, app_id, sizeof(app_id) - 1);
+ close(fd);
+
+ if (ret <= 0)
+ return ES_R_ERROR;
+
+ app_id[ret] = '\0';
+ ret = ES_R_OK;
+ }
+
+ return ret;
+}
+
static int check_user_event_sender_valid(const char *event_name, const char *app_id)
{
char *valid_name = NULL;
_D("event_sender_pid(%d), event_name(%s)", event_sender_pid, event_name);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
+ if (__esd_get_appid_by_pid_for_uid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
result = ES_R_ERROR;
} else {
if (check_user_event_sender_valid(event_name, app_id) < 0) {
sender_pid = __get_sender_pid(connection, sender);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
+ if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
result = ES_R_ERROR;
} else {
if (check_user_event_sender_valid(event_name, app_id) < 0) {
sender_pid = __get_sender_pid(connection, sender);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
+ if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
result = ES_R_ERROR;
} else {
builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
if (destination_name && destination_name[0] != '\0') {
sender_pid = __get_sender_pid(connection, sender);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
+ if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
result = ES_R_ERROR;
} else {
ret = __esd_trusted_busname_add_item(sender_uid, app_id, destination_name,
if (privilege_name) {
sender_pid = __get_sender_pid(connection, sender);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
+ if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
result = ES_R_ERROR;
} else {
ret = cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client);
sender_pid = __get_sender_pid(connection, sender);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id,
+ if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id,
sizeof(app_id)) < 0) {
_E("failed to get appid by pid");
result = ES_R_ERROR;
sender_pid = __get_sender_pid(connection, sender);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id,
+ if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id,
sizeof(app_id)) < 0) {
result = ES_R_ERROR;
_E("failed to get appid by pid");
sender_pid = __get_sender_pid(connection, sender);
sender_uid = (uid_t)__get_sender_uid(connection, sender);
- if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id,
+ if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id,
sizeof(app_id)) < 0) {
_E("failed to get appid by pid");
result = ES_R_ERROR;
g_dbus_method_invocation_return_value(invocation, param);
}
+static void get_uuid_method_call(GDBusConnection *connection,
+ const gchar *sender, GVariant *parameters,
+ GDBusMethodInvocation *invocation)
+{
+ GVariant *param = NULL;
+ int result = ES_R_OK;
+ char *uuid;
+ char app_id[128] = { 0, };
+ int sender_pid;
+
+ sender_pid = __get_sender_pid(connection, sender);
+ if (__esd_get_appid_by_pid(sender_pid, app_id, sizeof(app_id)) < 0) {
+ _E("failed to get appid by pid");
+ result = ES_R_ERROR;
+ goto out;
+ }
+
+ if (esd_cion_get_uuid_with_generate(app_id, &uuid) == 0) {
+ param = g_variant_new("(is)", result, uuid);
+ free(uuid);
+ } else {
+ result = ES_R_ERROR;
+ }
+
+out:
+ if (param == NULL)
+ param = g_variant_new("(is)", result, "");
+
+ g_dbus_method_invocation_return_value(invocation, param);
+}
+
static void handle_method_call(GDBusConnection *connection,
const gchar *sender, const gchar *object_path,
const gchar *interface_name, const gchar *method_name,
keep_last_data_method_call(connection, sender, parameters, invocation);
} else if (g_strcmp0(method_name, "CheckLastData") == 0) {
check_last_data_method_call(connection, sender, parameters, invocation);
- } else if (g_strcmp0(method_name, "LaunchOnEventFromUserEvent") == 0) {
+ } else if (g_strcmp0(method_name, "LaunchOnEventFromUserEvent") == 0) {
launch_on_event_from_userevent(connection, sender, parameters, invocation);
+ } else if (g_strcmp0(method_name, "GetUuid") == 0) {
+ get_uuid_method_call(connection, sender, parameters, invocation);
}
}
{
bundle *b;
- _I("name acquired(%s)", name);
-
__esd_check_trusted_events(connection, "ListNames");
__esd_check_trusted_events(connection, "ListActivatableNames");
static void __esd_on_name_lost(GDBusConnection *connection,
const gchar *name, gpointer user_data)
{
- _E("name lost(%s)", name);
}
static int __esd_before_loop(void)
return ES_R_ERROR;
}
+ if (esd_cion_db_init() < 0)
+ _E("db init failed!");
+
g_main_loop_run(mainloop);
_E("shutdown");