Fix gcov QB build error and Update version (1.60.2)
[platform/core/api/gesture.git] / server / gestured_main.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <dlog.h>
21 #include <glib.h>
22 #include <sys/signalfd.h>
23 #include <signal.h>
24
25 //#include "gestured_data_manager.h"
26 #include "gestured_dbus.h"
27 #include "gestured_error.h"
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32 #define LOG_TAG "GESTURED_SERVER_MAIN"
33
34 #define MAX_ERROR_BUFFER 256
35
36 static GMainLoop *main_loop;
37 static GIOChannel *channel = NULL;
38 static guint source = 0;
39
40 static gboolean signal_handler(GIOChannel *channel, GIOCondition condition, gpointer user_data)
41 {
42         LOGD("signal_handler funaction start");
43
44     struct signalfd_siginfo fd_signal;
45     ssize_t size;
46     int sfd;
47
48     sfd = g_io_channel_unix_get_fd(channel);
49     size = read(sfd, &fd_signal, sizeof(struct signalfd_siginfo));
50     if (size != sizeof(struct signalfd_siginfo)) {
51         LOGE("Failed to read signal");
52         return TRUE;
53     }
54
55     LOGE("sender : %d, signal : %d", fd_signal.ssi_pid, fd_signal.ssi_signo);
56     g_main_loop_quit(main_loop);
57
58     return TRUE;
59 }
60
61 static void register_signal_handler()
62 {
63         LOGD("register_signal_handler funaction start");
64
65     sigset_t mask;
66     int sfd;
67     int ret;
68     char error_buffer[MAX_ERROR_BUFFER];
69
70     ret = sigemptyset(&mask);
71     if (ret < 0) {
72         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
73         LOGE("sigemptyset(): %s", error_buffer);
74     }
75
76     ret = sigaddset(&mask, SIGTERM);
77     if (ret < 0) {
78         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
79         LOGE("sigaddset(): %s", error_buffer);
80     }
81
82     ret = sigprocmask(SIG_BLOCK, &mask, NULL);
83     if (ret < 0) {
84         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
85         LOGE("sigprocmask(): %s", error_buffer);
86     }
87
88     sfd = signalfd(-1, &mask, SFD_NONBLOCK);
89     if (sfd < 0) {
90         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
91         LOGE("signalfd(): %s", error_buffer);
92     }
93
94     channel = g_io_channel_unix_new(sfd);
95     g_io_channel_set_close_on_unref(channel, TRUE);
96     g_io_channel_set_encoding(channel, NULL, NULL);
97     g_io_channel_set_buffered(channel, FALSE);
98     source = g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_OUT, signal_handler, NULL);
99     LOGD("source : %u", source);
100 }
101
102 static void unregister_signal_handler()
103 {
104         LOGD("unregister_signal_handler funaction start");
105
106     if (source > 0)
107         g_source_remove(source);
108     if (channel)
109         g_io_channel_unref(channel);
110 }
111
112 int main(int argc, char** argv)
113 {
114         LOGD("main funaction start");
115
116     int ret;
117     main_loop = g_main_loop_new(NULL, FALSE);
118     if (main_loop == NULL) {
119         LOGE("Failed to create GMainLoop structure");
120         return -1;
121     }
122
123     ret = gestured_dbus_init();
124     if (ret != GESTURED_ERROR_NONE)
125         LOGW("Failed to init dbus");
126
127     register_signal_handler();
128
129     g_main_loop_run(main_loop);
130
131     unregister_signal_handler();
132
133     g_main_loop_unref(main_loop);
134
135     return 0;
136 }