summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKichan Kwon <k_c.kwon@samsung.com>2016-11-01 14:30:29 +0900
committerKichan Kwon <k_c.kwon@samsung.com>2016-11-07 13:53:56 +0900
commite9c31b7676fffdcb609d3689f358e9199fec4d5e (patch)
tree6582015156c7c411cb051e2225571603b495f65c
parent2100eae3f273cb83341ce0a8dfca806017f02eeb (diff)
downloadresourced-e9c31b7676fffdcb609d3689f358e9199fec4d5e.tar.gz
resourced-e9c31b7676fffdcb609d3689f358e9199fec4d5e.tar.bz2
resourced-e9c31b7676fffdcb609d3689f358e9199fec4d5e.zip
common : each module can restore when resourced is restarted
- After restarting, all information are erased though other apps aren't exited - Therefore, if each module needs, they can run restore function - In addition, when restarted, BOOTING_DONE notifier will be broadcasted Change-Id: I205993417565bc0505c4762b7da0c09e4b85a3a2 Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
-rw-r--r--include/resourced.h11
-rw-r--r--src/common/edbus-handler.c44
-rw-r--r--src/common/edbus-handler.h1
-rw-r--r--src/common/module.c27
-rw-r--r--src/common/module.h3
-rw-r--r--src/resourced/init.c43
-rw-r--r--src/resourced/init.h2
-rw-r--r--src/resourced/main.c12
8 files changed, 94 insertions, 49 deletions
diff --git a/include/resourced.h b/include/resourced.h
index a048166b..4377a917 100644
--- a/include/resourced.h
+++ b/include/resourced.h
@@ -69,6 +69,17 @@ typedef enum {
RESOURCED_CONTINUE = 1, /**< continue */
} resourced_cb_ret;
+/**
+ * runtime resourced directory
+ */
+#define RUNTIME_RESOURCED_DIR "/run/resourced"
+
+/**
+ * pid of resourced is stored in here. It will be used to check
+ * whether resourced is restarted or not.
+ */
+#define RUNTIME_RESOURCED_PID_PATH RUNTIME_RESOURCED_DIR "/main_pid"
+
#ifdef __cplusplus
}
#endif /* __cplusplus */
diff --git a/src/common/edbus-handler.c b/src/common/edbus-handler.c
index d2c92178..337f5b70 100644
--- a/src/common/edbus-handler.c
+++ b/src/common/edbus-handler.c
@@ -640,50 +640,6 @@ static void request_name_cb(void *data, DBusMessage *msg, DBusError *error)
_I("Request Name reply : %d", val);
}
-int check_dbus_active(void)
-{
- int ret = FALSE;
- DBusError err;
- DBusMessage *msg;
- DBusMessageIter iter, sub;
- const char *state;
- char *pa[2];
-
- pa[0] = "org.freedesktop.systemd1.Unit";
- pa[1] = "ActiveState";
-
- _I("%s %s", pa[0], pa[1]);
-
- msg = dbus_method_sync("org.freedesktop.systemd1",
- "/org/freedesktop/systemd1/unit/default_2etarget",
- "org.freedesktop.DBus.Properties",
- "Get", "ss", pa);
- if (!msg)
- return -EBADMSG;
-
- dbus_error_init(&err);
-
- if (!dbus_message_iter_init(msg, &iter) ||
- dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
- goto out;
-
- dbus_message_iter_recurse(&iter, &sub);
-
- if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
- ret = -EBADMSG;
- goto out;
- }
-
- dbus_message_iter_get_basic(&sub, &state);
-
- if (strncmp(state, "active", 6) == 0)
- ret = TRUE;
-out:
- dbus_message_unref(msg);
- dbus_error_free(&err);
- return ret;
-}
-
void edbus_init(void)
{
int retry = 0;
diff --git a/src/common/edbus-handler.h b/src/common/edbus-handler.h
index 5056c779..961c8c4b 100644
--- a/src/common/edbus-handler.h
+++ b/src/common/edbus-handler.h
@@ -227,7 +227,6 @@ resourced_ret_c edbus_add_signals(
const size_t size);
resourced_ret_c dbus_message_reply(DBusMessage *msg);
int register_edbus_interface(struct edbus_object *object);
-int check_dbus_active(void);
int launch_system_app_by_dbus(const char *dest, const char *path,
const char *iface, const char *method, int num, ...);
diff --git a/src/common/module.c b/src/common/module.c
index c9090524..cc759552 100644
--- a/src/common/module.c
+++ b/src/common/module.c
@@ -152,6 +152,33 @@ void modules_exit(void *data)
g_slist_free(modules_list);
}
+void modules_restore(void *data)
+{
+ struct module_ops *module;
+ GSList *iter;
+ int ret;
+
+ gslist_for_each_item(iter, modules_list) {
+ module = (struct module_ops *)iter->data;
+
+ assert(module);
+
+ if (module->initalized != MODULE_INITIALIZED)
+ continue;
+
+ if (!module->restore)
+ continue;
+
+ ret = module->restore(data);
+ if (ret < 0) {
+ _E("Fail to restore [%s] module", module->name);
+ continue;
+ }
+
+ _I("Restored [%s] module", module->name);
+ }
+}
+
void modules_dump(FILE *fp, int mode)
{
GSList *iter;
diff --git a/src/common/module.h b/src/common/module.h
index 2cee7ddf..9294f644 100644
--- a/src/common/module.h
+++ b/src/common/module.h
@@ -68,6 +68,8 @@ enum module_state {
* .name (name of the module, as string)
* .init (must return RESOURCED_ERROR_NONE on success)
* .exit (must return RESOURCED_ERROR_NONE on success)
+ * .restore (optional handler to calls on resourced is
+ * restarted. must return RESOURCED_ERROR_NONE on success))
*
* All rest of callbacks are optional for module to work.
*
@@ -78,6 +80,7 @@ struct module_ops {
const char *name;
int (*init) (void *data);
int (*exit) (void *data);
+ int (*restore) (void *data);
int (*check_runtime_support) (void *data);
int (*control) (void *data);
int (*status) (void *data);
diff --git a/src/resourced/init.c b/src/resourced/init.c
index 295f1100..f464b407 100644
--- a/src/resourced/init.c
+++ b/src/resourced/init.c
@@ -35,11 +35,14 @@
#include "swap-common.h"
#include "trace.h"
#include "version.h"
+#include "file-helper.h"
#include <Ecore.h>
#include <getopt.h>
#include <signal.h>
+static int restarted = false;
+
static void print_root_usage()
{
puts("You must be root to start it.");
@@ -54,6 +57,41 @@ static int assert_root(void)
return RESOURCED_ERROR_NONE;
}
+int resourced_restarted(void)
+{
+ return restarted;
+}
+
+static int resourced_write_state(void)
+{
+ pid_t pid = getpid(), old_pid = 0;
+ int ret;
+
+ assert(pid >= 0);
+
+ ret = mkdir(RUNTIME_RESOURCED_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
+ if (ret < 0 && errno != EEXIST)
+ _E("Failed to create %s dir : %s",
+ RUNTIME_RESOURCED_DIR, strerror(-ret));
+
+ if (access(RUNTIME_RESOURCED_PID_PATH, F_OK) < 0)
+ goto write_pid;
+
+ ret = fread_uint(RUNTIME_RESOURCED_PID_PATH, (u_int32_t*)&old_pid);
+ if (ret < 0) {
+ _E("Failed to read old resourced pid: %s", strerror(-ret));
+ return ret;
+ }
+
+ assert(pid != old_pid);
+ restarted = true;
+
+ _I("resourced is restarted: new_pid(%d), old_pid(%d)", pid, old_pid);
+
+write_pid:
+ return fwrite_uint(RUNTIME_RESOURCED_PID_PATH, (u_int32_t)pid);
+}
+
static void sig_term_handler(int sig)
{
_E("sigterm or sigint received");
@@ -80,6 +118,11 @@ int resourced_init(struct daemon_arg *darg)
"Invalid daemon argument\n");
ret = assert_root();
ret_value_if(ret < 0, RESOURCED_ERROR_FAIL);
+
+ ret = resourced_write_state();
+ if (ret < 0)
+ return ret;
+
ecore_init();
add_signal_handler();
edbus_init();
diff --git a/src/resourced/init.h b/src/resourced/init.h
index dd3a07b8..feaaa4d2 100644
--- a/src/resourced/init.h
+++ b/src/resourced/init.h
@@ -37,6 +37,8 @@ struct daemon_arg {
Ecore_Timer *ecore_quit;
};
+int resourced_restarted(void);
+
int resourced_init(struct daemon_arg *darg);
int resourced_deinit(void);
diff --git a/src/resourced/main.c b/src/resourced/main.c
index e2cca7d0..5ce5f4f7 100644
--- a/src/resourced/main.c
+++ b/src/resourced/main.c
@@ -49,14 +49,18 @@ int main(int argc, char **argv)
"Resourced initialization failed\n");
init_modules_arg(&darg);
modules_check_runtime_support(NULL);
- if (check_dbus_active()) {
- _I("launching all modules (relaunch detected)");
+ if (resourced_restarted()) {
+ _I("Relaunched. Start to initialize and restore");
modules_init(NULL);
- resourced_notify(RESOURCED_NOTIFIER_BOOTING_DONE, NULL);
+ modules_restore(NULL);
} else {
- _I("launch high priority modules");
+ _I("Firstly launched. Start to initialize");
modules_init_right_away(NULL);
}
+
+ if (resourced_restarted())
+ resourced_notify(RESOURCED_NOTIFIER_BOOTING_DONE, NULL);
+
sd_notify(0, "READY=1");
ecore_main_loop_begin();