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
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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
+++ /dev/null
-/*
- * 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);
-}