2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/signalfd.h>
25 //#include "gestured_data_manager.h"
26 #include "gestured_dbus.h"
27 #include "gestured_error.h"
32 #define LOG_TAG "GESTURED_SERVER_MAIN"
34 #define MAX_ERROR_BUFFER 256
36 static GMainLoop *main_loop;
37 static GIOChannel *channel = NULL;
38 static guint source = 0;
40 static gboolean signal_handler(GIOChannel *channel, GIOCondition condition, gpointer user_data)
42 LOGD("signal_handler funaction start");
44 struct signalfd_siginfo fd_signal;
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");
55 LOGE("sender : %d, signal : %d", fd_signal.ssi_pid, fd_signal.ssi_signo);
56 g_main_loop_quit(main_loop);
61 static void register_signal_handler()
63 LOGD("register_signal_handler funaction start");
68 char error_buffer[MAX_ERROR_BUFFER];
70 ret = sigemptyset(&mask);
72 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
73 LOGE("sigemptyset(): %s", error_buffer);
76 ret = sigaddset(&mask, SIGTERM);
78 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
79 LOGE("sigaddset(): %s", error_buffer);
82 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
84 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
85 LOGE("sigprocmask(): %s", error_buffer);
88 sfd = signalfd(-1, &mask, SFD_NONBLOCK);
90 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
91 LOGE("signalfd(): %s", error_buffer);
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);
102 static void unregister_signal_handler()
104 LOGD("unregister_signal_handler funaction start");
107 g_source_remove(source);
109 g_io_channel_unref(channel);
112 int main(int argc, char** argv)
114 LOGD("main funaction start");
117 main_loop = g_main_loop_new(NULL, FALSE);
118 if (main_loop == NULL) {
119 LOGE("Failed to create GMainLoop structure");
123 ret = gestured_dbus_init();
124 if (ret != GESTURED_ERROR_NONE)
125 LOGW("Failed to init dbus");
127 register_signal_handler();
129 g_main_loop_run(main_loop);
131 unregister_signal_handler();
133 g_main_loop_unref(main_loop);