0ce5b033b0df9d28246e44c19c49ff139e230947
[platform/core/system/deviced.git] / src / core / main.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/reboot.h>
23 #include <systemd/sd-daemon.h>
24 #include <glib.h>
25 #include <libgdbus/dbus-system.h>
26
27 #include "display/core.h"
28 #include "log.h"
29 #include "common.h"
30 #include "devices.h"
31 #include "power/boot.h"
32 #include "power/power-handler.h"
33 #include "device-notifier.h"
34
35 #define PIDFILE_PATH            "/var/run/.deviced.pid"
36 #define WATCHDOG_TIMEOUT        5
37
38 static GMainLoop *mainloop;
39
40 static void writepid(char *pidpath)
41 {
42         FILE *fp;
43
44         fp = fopen(pidpath, "w");
45         if (fp != NULL) {
46                 fprintf(fp, "%d", getpid());
47                 fclose(fp);
48         }
49 }
50
51 static void sig_quit(int signo)
52 {
53         _D("received SIGTERM signal %d", signo);
54 }
55
56 static void sig_usr1(int signo)
57 {
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);
61 }
62
63 void watchdog_notify(void)
64 {
65         sd_notify(0, "WATCHDOG=1");
66 }
67
68 static void deviced_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
69 {
70         int ret;
71         ret = booting_finished();
72         if (ret == 1) {
73                 _I("notify relaunch");
74                 device_notify(DEVICE_NOTIFIER_BOOTING_DONE, &ret);
75         }
76
77         _I("sd_notify(READY=1)");
78         sd_notify(0, "READY=1");
79 }
80
81 static gboolean watchdog_cb(void *data)
82 {
83         watchdog_notify();
84         return G_SOURCE_CONTINUE;
85 }
86
87 static int deviced_main(int argc, char **argv)
88 {
89         int ret;
90         dbus_handle_h handle = NULL;
91
92         mainloop = g_main_loop_new(NULL, FALSE);
93
94         ret = check_power_flag();
95         if (ret)
96                 return 0;
97
98         handle = dbus_handle_get_connection(G_BUS_TYPE_SYSTEM, FALSE);
99         if (!handle)
100                 _E("Fail to get dbus connection");
101
102         devices_init(NULL);
103
104         ret = dbus_handle_request_bus_name(handle, DEVICED_BUS_NAME, deviced_dbus_name_acquired, NULL);
105         if (ret <= 0) {
106                 _E("Fail to request bus name");
107                 dbus_handle_check_owner_name(NULL, DEVICED_BUS_NAME);
108         }
109
110         signal(SIGTERM, sig_quit);
111         signal(SIGUSR1, sig_usr1);
112
113         g_timeout_add_seconds_full(G_PRIORITY_HIGH, WATCHDOG_TIMEOUT, watchdog_cb, NULL, NULL);
114
115         /* g_main_loop */
116         g_main_loop_run(mainloop);
117         g_main_loop_unref(mainloop);
118
119         devices_exit(NULL);
120         return 0;
121 }
122
123 int main(int argc, char **argv)
124 {
125         writepid(PIDFILE_PATH);
126         return deviced_main(argc, argv);
127 }