libgdbus: Move common gdbus interfaces to libsystem package
[platform/core/system/storaged.git] / src / core / main.c
1 /*
2  * storaged
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 <argos.h>
24 #include <systemd/sd-daemon.h>
25 #include <glib.h>
26 #include <libgdbus/dbus-system.h>
27
28 #include "log.h"
29 #include "modules.h"
30
31 #define WATCHDOG_TIMEOUT        15 /* Seconds */
32
33 static GMainLoop *loop;
34
35 static void sig_quit(int signo)
36 {
37         _D("received SIGTERM signal %d", signo);
38         g_main_loop_quit(loop);
39 }
40
41 static void sig_usr1(int signo)
42 {
43         _D("received SIGUSR1 signal %d, storaged'll be finished!", signo);
44
45         g_main_loop_quit(loop);
46 }
47
48 void watchdog_notify(void)
49 {
50         int ret = aw_notify();
51         if (ret < 0)
52                 _E("aw_notify failed(%d)", ret);
53 }
54
55 static gboolean watchdog_cb(gpointer data)
56 {
57         watchdog_notify();
58         return G_SOURCE_CONTINUE;
59 }
60
61 static gboolean storaged_notify(gpointer data)
62 {
63         _I("sd_notify(READY=1)");
64         sd_notify(0, "READY=1");
65         return G_SOURCE_REMOVE;
66 }
67
68 int main(int argc, char **argv)
69 {
70         int ret;
71         guint timer;
72
73         loop = g_main_loop_new(NULL, TRUE);
74         if (!loop) {
75                 _E("Failed to make main loop");
76                 return -ENOMEM;
77         }
78
79         if (!dbus_handle_init(G_BUS_TYPE_SYSTEM, STORAGED_BUS_NAME, NULL, NULL)) {
80                 _E("Failed to set dbus connection (%s)", STORAGED_BUS_NAME);
81                 return 0;
82         }
83
84         modules_init(NULL);
85         signal(SIGTERM, sig_quit);
86         signal(SIGUSR1, sig_usr1);
87
88         timer = g_timeout_add(WATCHDOG_TIMEOUT * 1000, watchdog_cb, NULL);
89         if (timer > 0) {
90                 ret = aw_register(WATCHDOG_TIMEOUT * 2);
91                 if (ret < 0)
92                         _E("aw_register failed");
93         }
94
95         g_idle_add(storaged_notify, NULL);
96
97         g_main_loop_run(loop);
98
99         modules_deinit(NULL);
100
101         return 0;
102 }