From: SangYoun Kwak Date: Wed, 11 Jun 2025 01:39:58 +0000 (+0900) Subject: core: Separate main logic to storaged_init/exit function for common usage X-Git-Tag: accepted/tizen/unified/20250612.143602^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Faccepted%2Ftizen_unified_x;p=platform%2Fcore%2Fsystem%2Fstoraged.git core: Separate main logic to storaged_init/exit function for common usage To make the init/exit sequences of the main function can be used by other source files such as plugin library, the init/exit logics are separated as 'storaged_init' and 'storaged_exit' respectively. Accordingly, main.c file which includes main function is renamed to storaged.c. Also, to make them callable, header storaged.h is added. Change-Id: I7f5512ae13a84095e94b9d56f9970ce09b1f9f0b Signed-off-by: SangYoun Kwak --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 6577dd9..e0d38d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF) SET(STORAGED_APPS ${CMAKE_SOURCE_DIR}/apps) SET(SRCS - src/core/main.c + src/core/storaged.c src/core/modules.c src/shared/common.c ) diff --git a/src/core/main.c b/src/core/main.c deleted file mode 100644 index ad5e948..0000000 --- a/src/core/main.c +++ /dev/null @@ -1,135 +0,0 @@ -/* - * storaged - * - * Copyright (c) 2012 - 2013 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 -#include -#include - -#include "log.h" -#include "modules.h" -#include "storaged_common.h" - -#define WATCHDOG_TIMEOUT 15 /* Seconds */ -#define STORAGED_DIR_PATH "/run/storaged" - -static GMainLoop *loop; -static dbus_handle_h g_handle = NULL; - -static gboolean handle_signal(gpointer data) -{ - long signo = (long) data; - - _D("Received signal(%ld), Storaged'll be finished.", signo); - - if (loop && g_main_loop_is_running(loop)) - g_main_loop_quit(loop); - - return G_SOURCE_REMOVE; -} - -void watchdog_notify(void) -{ - int ret = aw_notify(); - if (ret < 0) - _E("Failed to aw_notify:%d", ret); -} - -static gboolean watchdog_cb(gpointer data) -{ - watchdog_notify(); - return G_SOURCE_CONTINUE; -} - -static void dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) -{ - _I("sd_notify(READY=1)"); - sd_notify(0, "READY=1"); -} - -static void dir_init(void) -{ - int ret; - - ret = remove_directory(STORAGED_DIR_PATH); - if (ret < 0) - _E("Failed to remove directory."); - ret = mkdir(STORAGED_DIR_PATH, 0644); - if (ret < 0) - _E("Failed to make directory: %d", errno); - -} - -int main(int argc, char **argv) -{ - guint timer; - int ret; - - g_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, TRUE); - if (!g_handle) - _E("Failed to get dbus connection."); - - loop = g_main_loop_new(NULL, TRUE); - if (!loop) { - _E("Failed to make main loop."); - return -ENOMEM; - } - - dir_init(); - modules_init((void *)&g_handle); - - ret = gdbus_request_name(g_handle, STORAGED_BUS_NAME, dbus_name_acquired, NULL); - if (ret <= 0) { - _E("Failed to request bus name."); - gdbus_check_name_owner(NULL, STORAGED_BUS_NAME); - } - - g_unix_signal_add(SIGTERM, handle_signal, (gpointer) SIGTERM); - g_unix_signal_add(SIGUSR1, handle_signal, (gpointer) SIGUSR1); - - timer = g_timeout_add(WATCHDOG_TIMEOUT * 1000, watchdog_cb, NULL); - if (timer > 0) { - ret = aw_register(WATCHDOG_TIMEOUT * 6); - if (ret < 0) - _E("Failed to aw_register."); - } - - g_main_loop_run(loop); - - modules_deinit(NULL); - - if (!g_handle) - return 0; - - ret = gdbus_free_connection(g_handle); - if (ret < 0) { - _E("Failed to free dbus connection %d", ret); - g_handle = NULL; - return ret; - } - - g_handle = NULL; - - return 0; -} diff --git a/src/core/storaged.c b/src/core/storaged.c new file mode 100644 index 0000000..4a1835f --- /dev/null +++ b/src/core/storaged.c @@ -0,0 +1,152 @@ +/* + * storaged + * + * Copyright (c) 2012 - 2013 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 +#include +#include + +#include "log.h" +#include "storaged.h" +#include "modules.h" +#include "storaged_common.h" + +#define WATCHDOG_TIMEOUT 15 /* Seconds */ +#define STORAGED_DIR_PATH "/run/storaged" + +static GMainLoop *loop; +static dbus_handle_h g_handle = NULL; + +static gboolean handle_signal(gpointer data) +{ + long signo = (long) data; + + _D("Received signal(%ld), Storaged'll be finished.", signo); + + if (loop && g_main_loop_is_running(loop)) + g_main_loop_quit(loop); + + return G_SOURCE_REMOVE; +} + +void watchdog_notify(void) +{ + int ret = aw_notify(); + if (ret < 0) + _E("Failed to aw_notify:%d", ret); +} + +static gboolean watchdog_cb(gpointer data) +{ + watchdog_notify(); + return G_SOURCE_CONTINUE; +} + +static void dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) +{ + _I("sd_notify(READY=1)"); + sd_notify(0, "READY=1"); +} + +static void dir_init(void) +{ + int ret; + + ret = remove_directory(STORAGED_DIR_PATH); + if (ret < 0) + _E("Failed to remove directory."); + ret = mkdir(STORAGED_DIR_PATH, 0644); + if (ret < 0) + _E("Failed to make directory: %d", errno); + +} + +int storaged_init(void *data) +{ + guint timer; + int ret; + + g_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, TRUE); + if (!g_handle) + _E("Failed to get dbus connection."); + + dir_init(); + modules_init((void *)&g_handle); + + ret = gdbus_request_name(g_handle, STORAGED_BUS_NAME, dbus_name_acquired, NULL); + if (ret <= 0) { + _E("Failed to request bus name."); + gdbus_check_name_owner(NULL, STORAGED_BUS_NAME); + } + + g_unix_signal_add(SIGTERM, handle_signal, (gpointer) SIGTERM); + g_unix_signal_add(SIGUSR1, handle_signal, (gpointer) SIGUSR1); + + timer = g_timeout_add(WATCHDOG_TIMEOUT * 1000, watchdog_cb, NULL); + if (timer > 0) { + ret = aw_register(WATCHDOG_TIMEOUT * 6); + if (ret < 0) + _E("Failed to aw_register."); + } + + return 0; +} + +int storaged_exit(void *data) +{ + int ret = 0; + + modules_deinit(NULL); + + if (!g_handle) + return 0; + + ret = gdbus_free_connection(g_handle); + if (ret < 0) { + _E("Failed to free dbus connection %d", ret); + g_handle = NULL; + return ret; + } + + g_handle = NULL; + + return 0; +} + +int main(int argc, char **argv) +{ + loop = g_main_loop_new(NULL, TRUE); + if (!loop) { + _E("Failed to make main loop."); + return -ENOMEM; + } + + storaged_init(NULL); + + g_main_loop_run(loop); + + storaged_exit(NULL); + + return 0; +} diff --git a/src/core/storaged.h b/src/core/storaged.h new file mode 100644 index 0000000..13fa3bf --- /dev/null +++ b/src/core/storaged.h @@ -0,0 +1,25 @@ +/* + * storaged + * + * Copyright (c) 2012 - 2013 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. + */ + +#ifndef __STORAGED_H__ +#define __STORAGED_H__ + +int storaged_init(void *data); +int storaged_exit(void *data); + +#endif /* __STORAGED_H__ */