2 * Copyright (c) 2019 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 "stickerd_data_manager.h"
26 #include "stickerd_db_manager.h"
27 #include "stickerd_error.h"
32 #define LOG_TAG "STICKERD_MAIN"
34 #define MAX_ERROR_BUFFER 256
36 GMainLoop *main_loop = NULL;
37 static GIOChannel *channel = NULL;
38 static guint source = 0;
40 static gboolean signal_handler(GIOChannel *channel, GIOCondition condition, gpointer user_data)
42 struct signalfd_siginfo fd_signal;
46 sfd = g_io_channel_unix_get_fd(channel);
47 size = read(sfd, &fd_signal, sizeof(struct signalfd_siginfo));
48 if (size != sizeof(struct signalfd_siginfo)) {
49 LOGE("Failed to read signal");
53 LOGE("sender : %d, signal : %d", fd_signal.ssi_pid, fd_signal.ssi_signo);
54 g_main_loop_quit(main_loop);
59 static void register_signal_handler()
64 char error_buffer[MAX_ERROR_BUFFER];
66 ret = sigemptyset(&mask);
68 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
69 LOGE("sigemptyset(): %s", error_buffer);
72 ret = sigaddset(&mask, SIGTERM);
74 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
75 LOGE("sigaddset(): %s", error_buffer);
78 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
80 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
81 LOGE("sigprocmask(): %s", error_buffer);
84 sfd = signalfd(-1, &mask, SFD_NONBLOCK);
86 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
87 LOGE("signalfd(): %s", error_buffer);
90 channel = g_io_channel_unix_new(sfd);
91 g_io_channel_set_close_on_unref(channel, TRUE);
92 g_io_channel_set_encoding(channel, NULL, NULL);
93 g_io_channel_set_buffered(channel, FALSE);
94 source = g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_OUT, signal_handler, NULL);
95 LOGD("source : %u", source);
98 static void unregister_signal_handler()
101 g_source_remove(source);
103 g_io_channel_unref(channel);
106 int main(int argc, char** argv)
109 main_loop = g_main_loop_new(NULL, FALSE);
110 if (main_loop == NULL) {
111 LOGE("Failed to create GMainLoop structure");
115 ret = stickerd_dbus_init();
116 if (ret != STICKERD_SERVER_ERROR_NONE)
117 LOGW("Failed to init dbus");
119 ret = stickerd_db_init();
120 if (ret != STICKERD_SERVER_ERROR_NONE)
121 LOGW("Failed to init database");
123 register_signal_handler();
125 g_main_loop_run(main_loop);
127 unregister_signal_handler();
129 g_main_loop_unref(main_loop);