Fix exit sequence
[platform/core/system/feedbackd.git] / src / core / main.c
1 /*
2  * feedbackd
3  *
4  * Copyright (c) 2016 - 2017 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 <glib.h>
24 #include <libsyscommon/dbus-system.h>
25 #include <systemd/sd-daemon.h>
26
27 #include "core/log.h"
28 #include "core/common.h"
29 #include "haptic/haptic.h"
30
31 static GMainLoop *mainloop = NULL;
32
33 static void sig_quit(int signo)
34 {
35         _D("Received SIGTERM signal(%d)", signo);
36 }
37
38 static void sig_usr1(int signo)
39 {
40         _D("Received SIGUSR1 signal(%d), feedbackd'll be finished.", signo);
41
42         if (mainloop) {
43                 if (g_main_loop_is_running(mainloop))
44                         g_main_loop_quit(mainloop);
45         }
46 }
47
48 static void dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
49 {
50         _I("sd_notify(READY=1)");
51         sd_notify(0, "READY=1");
52 }
53
54 int main(int argc, char **argv)
55 {
56         int ret;
57         dbus_handle_h handle = NULL;
58
59         mainloop = g_main_loop_new(NULL, FALSE);
60
61         handle = dbus_handle_get_connection(G_BUS_TYPE_SYSTEM, FALSE);
62         if (!handle)
63                 _E("Failed to get dbus connection.");;
64
65         ret = haptic_probe();
66         if (ret != 0) {
67                 _E("'Haptic' probe fail.");
68                 return ret;
69         }
70         haptic_init();
71
72         ret = dbus_handle_request_bus_name(handle, VIBRATOR_BUS_NAME, dbus_name_acquired, NULL);
73         if (ret <= 0) {
74                 _E("Failed to request bus name.");
75                 dbus_handle_check_owner_name(NULL, VIBRATOR_BUS_NAME);
76         }
77
78         signal(SIGTERM, sig_quit);
79         signal(SIGUSR1, sig_usr1);
80
81         /* g_main_loop */
82         g_main_loop_run(mainloop);
83
84         _D("'Haptic' deinitialize.");
85         haptic_exit();
86
87         if (mainloop) {
88                 g_main_loop_unref(mainloop);
89                 mainloop = NULL;
90         }
91
92         return 0;
93 }