daemon: add user-memory layer 75/57475/7
authorJiwoong Im <jiwoong.im@samsung.com>
Wed, 20 Jan 2016 08:36:37 +0000 (17:36 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Wed, 27 Jan 2016 06:30:34 +0000 (15:30 +0900)
To store per-user and run-time settings, add user-memory layer.
Buxton2 daemon adds handler for user removed signal to manage user memory db.

Change-Id: Iba1b5c0b9f39d50527afea497a6fdabc9d90f200
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
common/direct.c
common/direct.h
daemon/CMakeLists.txt
daemon/daemon.c
daemon/dbus.c [new file with mode: 0644]
daemon/dbus.h [new file with mode: 0644]
packaging/buxton2.conf
packaging/buxton2.spec

index 1725863..cf582c4 100644 (file)
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <glib.h>
 
@@ -512,6 +513,44 @@ int direct_set_priv(const struct buxton_layer *layer,
        return 0;
 }
 
+int direct_remove_db(const struct buxton_layer *layer)
+{
+       int r;
+       const struct layer *ly;
+       const struct backend *backend;
+       char path[FILENAME_MAX];
+
+       if (!layer) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       ly = conf_get_layer(layer->name);
+       if (!ly)
+               return -1;
+
+       backend = backend_get(ly->backend);
+       assert(backend);
+
+       if (!backend->remove_db) {
+               bxt_err("Remove db : backend '%s' has no remove_db func",
+                               backend->name);
+               return -1;
+       }
+
+       r = get_path(layer->uid, layer->type, ly, path, sizeof(path));
+       if (r == -1)
+               return -1;
+
+       r = backend->remove_db(path);
+       if (r == -1) {
+               bxt_err("remote user memory db failed");
+               return -1;
+       }
+
+       return 0;
+}
+
 void direct_exit(void)
 {
        conf_exit();
index 722f62d..6953906 100644 (file)
@@ -42,3 +42,4 @@ int direct_get_priv(const struct buxton_layer *layer,
 int direct_set_priv(const struct buxton_layer *layer,
                const char *key, enum buxton_priv_type type, const char *priv);
 
+int direct_remove_db(const struct buxton_layer *layer);
index c2e81b8..7688cf5 100644 (file)
@@ -1,6 +1,6 @@
 # buxton2d build
 
-PKG_CHECK_MODULES(D_PKGS REQUIRED libsystemd cynara-client-async)
+PKG_CHECK_MODULES(D_PKGS REQUIRED libsystemd cynara-client-async gio-2.0)
 
 FOREACH(flag ${D_PKGS_CFLAGS})
        SET(DAEMON_CFLAGS "${DAEMON_CFLAGS} ${flag}")
@@ -13,6 +13,7 @@ SET(SRC main.c
        daemon.c
        socks.c
        cynara.c
+       dbus.c
        ../common/common.c
        ../common/config.c
        ../common/backends.c
index 5daa8f9..b04796f 100644 (file)
@@ -38,6 +38,7 @@
 #include "daemon.h"
 #include "socks.h"
 #include "cynara.h"
+#include "dbus.h"
 
 struct bxt_noti {
        char *layer_key;
@@ -835,6 +836,7 @@ static void bxt_exit(struct bxt_daemon *bxtd)
                close(bxtd->sk);
 
        direct_exit();
+       buxton_dbus_exit();
 
        if (bxtd->sigfd != -1)
                close(bxtd->sigfd);
@@ -871,6 +873,8 @@ static int bxt_init(struct bxt_daemon *bxtd, const char *confpath)
 
        buxton_cynara_init();
 
+       buxton_dbus_init();
+
        bxtd->loop = g_main_loop_new(NULL, FALSE);
 
        return 0;
diff --git a/daemon/dbus.c b/daemon/dbus.c
new file mode 100644 (file)
index 0000000..6c070e7
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Buxton
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd.
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "dbus.h"
+#include "common.h"
+#include "log.h"
+#include "direct.h"
+
+#define LOGIND_INTERFACE "org.freedesktop.login1.Manager"
+#define LOGIND_PATH "/org/freedesktop/login1"
+#define LOGIND_SIGNAL_USER_REMOVED "UserRemoved"
+
+static GDBusConnection *conn;
+static guint s_id;
+
+static void __signal_handler(GDBusConnection *connection,
+                                       const gchar *sender_name,
+                                       const gchar *object_path,
+                                       const gchar *interface_name,
+                                       const gchar *signal_name,
+                                       GVariant *parameters,
+                                       gpointer user_data)
+{
+       struct buxton_layer *layer;
+       guint uid;
+
+       if (g_strcmp0(signal_name, LOGIND_SIGNAL_USER_REMOVED))
+               return;
+
+       g_variant_get(parameters, "(uo)", &uid, NULL);
+       layer = layer_create("user-memory");
+       if (layer) {
+               layer->uid = uid;
+               direct_remove_db(layer);
+               layer_free(layer);
+       }
+}
+
+static void __bus_get(GObject *source_object, GAsyncResult *res,
+               gpointer user_data)
+{
+       GError *err = NULL;
+
+       conn = g_bus_get_finish(res, &err);
+       if (!conn) {
+               bxt_err("g_bus_get_sync() is failed. %s", err->message);
+               g_error_free(err);
+               return;
+       }
+
+       s_id = g_dbus_connection_signal_subscribe(conn,
+                                       NULL,
+                                       LOGIND_INTERFACE,
+                                       LOGIND_SIGNAL_USER_REMOVED,
+                                       LOGIND_PATH,
+                                       NULL,
+                                       G_DBUS_SIGNAL_FLAGS_NONE,
+                                       __signal_handler,
+                                       NULL,
+                                       NULL);
+
+       if (s_id == 0) {
+               bxt_err("g_dbus_connection_signal_subscribe() is failed.");
+               g_object_unref(conn);
+       }
+}
+
+void buxton_dbus_init(void)
+{
+       if (!conn)
+               g_bus_get(G_BUS_TYPE_SYSTEM, NULL, __bus_get, NULL);
+}
+
+void buxton_dbus_exit(void)
+{
+       if (s_id) {
+               g_dbus_connection_signal_unsubscribe(conn, s_id);
+               s_id = 0;
+       }
+
+       if (conn) {
+               g_object_unref(conn);
+               conn = NULL;
+       }
+}
+
diff --git a/daemon/dbus.h b/daemon/dbus.h
new file mode 100644 (file)
index 0000000..43fbe4e
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Buxton
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#pragma once
+
+void buxton_dbus_init(void);
+void buxton_dbus_exit(void);
index 1438cee..e726601 100644 (file)
@@ -19,3 +19,9 @@ Type=User
 Backend=gdbm
 Storage=persistent
 Description=Per-user settings
+
+[user-memory]
+Type=User
+Backend=gdbm
+Storage=volatile
+Description=Per-user run-time settings
index a623b74..7cf6f03 100644 (file)
@@ -12,6 +12,7 @@ Source4:        %{name}.tmpfiles.conf
 Source1001:     %{name}.manifest
 BuildRequires:  cmake
 BuildRequires:  gdbm-devel
+BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(libsystemd)
 BuildRequires:  pkgconfig(cynara-client-async)