core: Separate main logic to deviced_init/exit function for common usage 47/325247/4
authorYunhee Seo <yuni.seo@samsung.com>
Thu, 5 Jun 2025 06:22:25 +0000 (15:22 +0900)
committerYunhee Seo <yuni.seo@samsung.com>
Tue, 10 Jun 2025 05:40:41 +0000 (14:40 +0900)
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
'deviced_init' and 'deviced_exit' respectively.
Accordingly, main.c file(includes main function) is renamed to deviced.c.

Also, to make them callable, header deviced.h is added.

Change-Id: Iaa6c250be3e84d38fa6e8c6bfb516876433d225a
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
CMakeLists.txt
src/core/deviced.c [new file with mode: 0644]
src/core/deviced.h [new file with mode: 0644]
src/core/main.c [deleted file]

index f36ea414550a80d6daba55eedd07eb14c815decf..945829b367620823797fe9bf9d4e4823f0a9d247 100644 (file)
@@ -70,7 +70,7 @@ SET(SRCS
        src/core/resource.c
        src/core/event-handler.c
        src/core/log.c
-       src/core/main.c
+       src/core/deviced.c
        src/core/sig-handler.c
        src/core/udev.c
        src/core/resource-core.c
diff --git a/src/core/deviced.c b/src/core/deviced.c
new file mode 100644 (file)
index 0000000..f4914de
--- /dev/null
@@ -0,0 +1,204 @@
+/*
+ * deviced
+ *
+ * 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 <stdio.h>
+#include <fcntl.h>
+#include <sys/reboot.h>
+#include <systemd/sd-daemon.h>
+#include <glib.h>
+#include <libsyscommon/libgdbus.h>
+#include <libsyscommon/libsystemd.h>
+#include <device/board-internal.h>
+#include <argos.h>
+
+#include "core.h"
+#include "log.h"
+#include "resource.h"
+#include "shared/common.h"
+#include "shared/devices.h"
+#include "power/power.h"
+#include "power/power-boot.h"
+#include "power/power-off.h"
+#include "shared/plugin.h"
+#include "shared/device-notifier.h"
+#include "core/devices.h"
+#include "core/deviced.h"
+
+#define PIDFILE_PATH           "/var/run/.deviced.pid"
+#define WATCHDOG_REFRESH_TIME  5
+#define WATCHDOG_TIMEOUT       90
+
+static GMainLoop *mainloop;
+static dbus_handle_h g_handle = NULL;
+
+static void writepid(char *pidpath)
+{
+       FILE *fp;
+
+       fp = fopen(pidpath, "w");
+       if (fp != NULL) {
+               fprintf(fp, "%d", getpid());
+               fclose(fp);
+       }
+}
+
+static gboolean handle_sigterm(gpointer data)
+{
+       long signo = (long) data;
+       _D("Received SIGTERM signal(%ld).", signo);
+
+       return G_SOURCE_REMOVE;
+}
+
+static gboolean handle_sigusr1(gpointer data)
+{
+       long signo = (long) data;
+       CRITICAL_LOG("Received SIGUSR1 signal(%ld), deviced'll be finished.", signo);
+       if (mainloop && g_main_loop_is_running(mainloop))
+               g_main_loop_quit(mainloop);
+
+       return G_SOURCE_REMOVE;
+}
+
+void watchdog_notify(void)
+{
+       int ret = aw_notify();
+       if (ret < 0)
+               _E("Failed to aw_notifty: %d", ret);
+}
+
+static void deviced_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
+{
+       int ret;
+       ret = check_system_boot_finished();
+       if (ret == 1) {
+               /* Restarted: deviced was terminated */
+               _I("Notify relaunch.");
+               syscommon_notifier_emit_notify_once(DEVICED_NOTIFIER_DELAYED_INIT, &ret);
+       }
+
+       _I("sd_notify(READY=1)");
+       sd_notify(0, "READY=1");
+}
+
+static gboolean watchdog_cb(void *data)
+{
+       watchdog_notify();
+       return G_SOURCE_CONTINUE;
+}
+
+int deviced_init(void *data)
+{
+       int ret;
+       guint timer;
+       char bootmode[32] = "normal";
+       int retval;
+
+       writepid(PIDFILE_PATH);
+
+       CRITICAL_LOG("Initializing deviced.");
+
+       ret = poweroff_check_revived();
+       if (is_poweroff_state(ret)) {
+               /* Restarted: deviced was terminated
+                * in middle of reboot/poweroff - resume procedure
+                */
+               poweroff_request_shutdown(ret);
+               return 0;
+       }
+
+       g_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, TRUE);
+       if (!g_handle)
+               _E("Failed to get dbus connection.");
+
+       load_plugins();
+
+       retval = device_board_get_boot_mode(bootmode, sizeof(bootmode));
+       if (retval < 0)
+               _W("Cannot get boot mode, ret(%d)", retval);
+
+       if (retval == 0) {
+               ret = power_boot_load_silent_boot();
+               if (ret < 0)
+                       _W("Cannot set silent_boot, ret(%d)", ret);
+       }
+
+       CRITICAL_LOG("bootmode=%s", bootmode);
+
+       resource_init();
+       devices_init((void*)&g_handle);
+
+       ret = gdbus_request_name(g_handle, DEVICED_BUS_NAME, deviced_dbus_name_acquired, NULL);
+       if (ret <= 0) {
+               _E("Failed to request bus name.");
+               gdbus_check_name_owner(NULL, DEVICED_BUS_NAME);
+       }
+
+       g_unix_signal_add(SIGTERM, handle_sigterm, (gpointer) SIGTERM);
+       g_unix_signal_add(SIGUSR1, handle_sigusr1, (gpointer) SIGUSR1);
+
+       timer = g_timeout_add_seconds_full(G_PRIORITY_HIGH, WATCHDOG_REFRESH_TIME, watchdog_cb, NULL, NULL);
+       if (timer) {
+               ret = aw_register(WATCHDOG_TIMEOUT);
+               if (ret < 0)
+                       _E("aw_register failed.");
+       }
+
+       CRITICAL_LOG("Starting deviced.");
+
+       return 0;
+}
+
+int deviced_exit(void *data)
+{
+       int ret = 0;
+
+       devices_exit(NULL);
+       resource_exit();
+
+       unload_plugins();
+
+       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)
+{
+       mainloop = g_main_loop_new(NULL, FALSE);
+
+       deviced_init(NULL);
+
+       g_main_loop_run(mainloop);
+       g_main_loop_unref(mainloop);
+
+       deviced_exit(NULL);
+
+       return 0;
+}
diff --git a/src/core/deviced.h b/src/core/deviced.h
new file mode 100644 (file)
index 0000000..6a00bc2
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2025 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 __DEVICED_H__
+#define __DEVICED_H__
+
+int deviced_init(void *data);
+int deviced_exit(void *data);
+
+#endif //__DEVICED_H__
\ No newline at end of file
diff --git a/src/core/main.c b/src/core/main.c
deleted file mode 100644 (file)
index 807ff52..0000000
+++ /dev/null
@@ -1,190 +0,0 @@
-/*
- * deviced
- *
- * 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 <stdio.h>
-#include <fcntl.h>
-#include <sys/reboot.h>
-#include <systemd/sd-daemon.h>
-#include <glib.h>
-#include <libsyscommon/libgdbus.h>
-#include <libsyscommon/libsystemd.h>
-#include <device/board-internal.h>
-#include <argos.h>
-
-#include "core.h"
-#include "log.h"
-#include "resource.h"
-#include "shared/common.h"
-#include "shared/devices.h"
-#include "power/power.h"
-#include "power/power-boot.h"
-#include "power/power-off.h"
-#include "shared/plugin.h"
-#include "shared/device-notifier.h"
-#include "core/devices.h"
-
-#define PIDFILE_PATH           "/var/run/.deviced.pid"
-#define WATCHDOG_REFRESH_TIME  5
-#define WATCHDOG_TIMEOUT       90
-
-static GMainLoop *mainloop;
-static dbus_handle_h g_handle = NULL;
-
-static void writepid(char *pidpath)
-{
-       FILE *fp;
-
-       fp = fopen(pidpath, "w");
-       if (fp != NULL) {
-               fprintf(fp, "%d", getpid());
-               fclose(fp);
-       }
-}
-
-static gboolean handle_sigterm(gpointer data)
-{
-       long signo = (long) data;
-       _D("Received SIGTERM signal(%ld).", signo);
-
-       return G_SOURCE_REMOVE;
-}
-
-static gboolean handle_sigusr1(gpointer data)
-{
-       long signo = (long) data;
-       CRITICAL_LOG("Received SIGUSR1 signal(%ld), deviced'll be finished.", signo);
-       if (mainloop && g_main_loop_is_running(mainloop))
-               g_main_loop_quit(mainloop);
-
-       return G_SOURCE_REMOVE;
-}
-
-void watchdog_notify(void)
-{
-       int ret = aw_notify();
-       if (ret < 0)
-               _E("Failed to aw_notifty: %d", ret);
-}
-
-static void deviced_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
-{
-       int ret;
-       ret = check_system_boot_finished();
-       if (ret == 1) {
-               /* Restarted: deviced was terminated */
-               _I("Notify relaunch.");
-               syscommon_notifier_emit_notify_once(DEVICED_NOTIFIER_DELAYED_INIT, &ret);
-       }
-
-       _I("sd_notify(READY=1)");
-       sd_notify(0, "READY=1");
-}
-
-static gboolean watchdog_cb(void *data)
-{
-       watchdog_notify();
-       return G_SOURCE_CONTINUE;
-}
-
-static int deviced_main(int argc, char **argv)
-{
-       int ret;
-       guint timer;
-       char bootmode[32] = "normal";
-       int retval;
-
-       CRITICAL_LOG("Initializing deviced.");
-       mainloop = g_main_loop_new(NULL, FALSE);
-
-       ret = poweroff_check_revived();
-       if (is_poweroff_state(ret)) {
-               /* Restarted: deviced was terminated
-                * in middle of reboot/poweroff - resume procedure
-                */
-               poweroff_request_shutdown(ret);
-               return 0;
-       }
-
-       g_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, TRUE);
-       if (!g_handle)
-               _E("Failed to get dbus connection.");
-
-       load_plugins();
-
-       retval = device_board_get_boot_mode(bootmode, sizeof(bootmode));
-       if (retval < 0)
-               _W("Cannot get boot mode, ret(%d)", retval);
-
-       if (retval == 0) {
-               ret = power_boot_load_silent_boot();
-               if (ret < 0)
-                       _W("Cannot set silent_boot, ret(%d)", ret);
-       }
-
-       CRITICAL_LOG("bootmode=%s", bootmode);
-
-       resource_init();
-       devices_init((void*)&g_handle);
-
-       ret = gdbus_request_name(g_handle, DEVICED_BUS_NAME, deviced_dbus_name_acquired, NULL);
-       if (ret <= 0) {
-               _E("Failed to request bus name.");
-               gdbus_check_name_owner(NULL, DEVICED_BUS_NAME);
-       }
-
-       g_unix_signal_add(SIGTERM, handle_sigterm, (gpointer) SIGTERM);
-       g_unix_signal_add(SIGUSR1, handle_sigusr1, (gpointer) SIGUSR1);
-
-       timer = g_timeout_add_seconds_full(G_PRIORITY_HIGH, WATCHDOG_REFRESH_TIME, watchdog_cb, NULL, NULL);
-       if (timer) {
-               ret = aw_register(WATCHDOG_TIMEOUT);
-               if (ret < 0)
-                       _E("aw_register failed.");
-       }
-
-       /* g_main_loop */
-       CRITICAL_LOG("Starting deviced.");
-       g_main_loop_run(mainloop);
-       g_main_loop_unref(mainloop);
-
-       devices_exit(NULL);
-       resource_exit();
-
-       unload_plugins();
-
-       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)
-{
-       writepid(PIDFILE_PATH);
-       return deviced_main(argc, argv);
-}