4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <sys/reboot.h>
23 #include <systemd/sd-daemon.h>
25 #include <libgdbus/dbus-system.h>
27 #include "display/core.h"
31 #include "power/boot.h"
32 #include "power/power-handler.h"
33 #include "device-notifier.h"
35 #define PIDFILE_PATH "/var/run/.deviced.pid"
36 #define WATCHDOG_TIMEOUT 5
38 static GMainLoop *mainloop;
40 static void writepid(char *pidpath)
44 fp = fopen(pidpath, "w");
46 fprintf(fp, "%d", getpid());
51 static void sig_quit(int signo)
53 _D("received SIGTERM signal %d", signo);
56 static void sig_usr1(int signo)
58 _D("received SIGUSR1 signal %d, deviced'll be finished!", signo);
59 if (mainloop && g_main_loop_is_running(mainloop))
60 g_main_loop_quit(mainloop);
63 void watchdog_notify(void)
65 sd_notify(0, "WATCHDOG=1");
68 static void deviced_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
71 ret = booting_finished();
73 _I("notify relaunch");
74 device_notify(DEVICE_NOTIFIER_BOOTING_DONE, &ret);
77 _I("sd_notify(READY=1)");
78 sd_notify(0, "READY=1");
81 static gboolean watchdog_cb(void *data)
84 return G_SOURCE_CONTINUE;
87 static int deviced_main(int argc, char **argv)
90 dbus_handle_h handle = NULL;
92 mainloop = g_main_loop_new(NULL, FALSE);
94 ret = check_power_flag();
98 handle = dbus_handle_get_connection(G_BUS_TYPE_SYSTEM, FALSE);
100 _E("Fail to get dbus connection");
104 ret = dbus_handle_request_bus_name(handle, DEVICED_BUS_NAME, deviced_dbus_name_acquired, NULL);
106 _E("Fail to request bus name");
107 dbus_handle_check_owner_name(NULL, DEVICED_BUS_NAME);
110 signal(SIGTERM, sig_quit);
111 signal(SIGUSR1, sig_usr1);
113 g_timeout_add_seconds_full(G_PRIORITY_HIGH, WATCHDOG_TIMEOUT, watchdog_cb, NULL, NULL);
116 g_main_loop_run(mainloop);
117 g_main_loop_unref(mainloop);
123 int main(int argc, char **argv)
125 writepid(PIDFILE_PATH);
126 return deviced_main(argc, argv);