From 9ffcacd8427d3b9d9f2c5bd9812dc54ca68354cc Mon Sep 17 00:00:00 2001 From: Jiwoong Im Date: Wed, 20 Jan 2016 17:36:37 +0900 Subject: [PATCH] daemon: add user-memory layer 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 --- common/direct.c | 39 ++++++++++++++++++ common/direct.h | 1 + daemon/CMakeLists.txt | 3 +- daemon/daemon.c | 4 ++ daemon/dbus.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ daemon/dbus.h | 22 ++++++++++ packaging/buxton2.conf | 6 +++ packaging/buxton2.spec | 1 + 8 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 daemon/dbus.c create mode 100644 daemon/dbus.h diff --git a/common/direct.c b/common/direct.c index 1725863..cf582c4 100644 --- a/common/direct.c +++ b/common/direct.c @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -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(); diff --git a/common/direct.h b/common/direct.h index 722f62d..6953906 100644 --- a/common/direct.h +++ b/common/direct.h @@ -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); diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index c2e81b8..7688cf5 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -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 diff --git a/daemon/daemon.c b/daemon/daemon.c index 5daa8f9..b04796f 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -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 index 0000000..6c070e7 --- /dev/null +++ b/daemon/dbus.c @@ -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 +#include +#include +#include + +#include +#include + +#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 index 0000000..43fbe4e --- /dev/null +++ b/daemon/dbus.h @@ -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); diff --git a/packaging/buxton2.conf b/packaging/buxton2.conf index 1438cee..e726601 100644 --- a/packaging/buxton2.conf +++ b/packaging/buxton2.conf @@ -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 diff --git a/packaging/buxton2.spec b/packaging/buxton2.spec index a623b74..7cf6f03 100644 --- a/packaging/buxton2.spec +++ b/packaging/buxton2.spec @@ -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) -- 2.7.4