Fix build error in sticker-receiver
[platform/core/uifw/capi-ui-sticker.git] / server / stickerd_main.c
1 /*
2  * Copyright (c) 2019 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 "stickerd_data_manager.h"
26 #include "stickerd_db_manager.h"
27 #include "stickerd_error.h"
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32 #define LOG_TAG "STICKERD_MAIN"
33
34 #define MAX_ERROR_BUFFER 256
35
36 GMainLoop *main_loop = NULL;
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     struct signalfd_siginfo fd_signal;
43     ssize_t size;
44     int sfd;
45
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");
50         return TRUE;
51     }
52
53     LOGE("sender : %d, signal : %d", fd_signal.ssi_pid, fd_signal.ssi_signo);
54     g_main_loop_quit(main_loop);
55
56     return TRUE;
57 }
58
59 static void register_signal_handler()
60 {
61     sigset_t mask;
62     int sfd;
63     int ret;
64     char error_buffer[MAX_ERROR_BUFFER];
65
66     ret = sigemptyset(&mask);
67     if (ret < 0) {
68         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
69         LOGE("sigemptyset(): %s", error_buffer);
70     }
71
72     ret = sigaddset(&mask, SIGTERM);
73     if (ret < 0) {
74         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
75         LOGE("sigaddset(): %s", error_buffer);
76     }
77
78     ret = sigprocmask(SIG_BLOCK, &mask, NULL);
79     if (ret < 0) {
80         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
81         LOGE("sigprocmask(): %s", error_buffer);
82     }
83
84     sfd = signalfd(-1, &mask, SFD_NONBLOCK);
85     if (sfd < 0) {
86         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
87         LOGE("signalfd(): %s", error_buffer);
88     }
89
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);
96 }
97
98 static void unregister_signal_handler()
99 {
100     if (source > 0)
101         g_source_remove(source);
102     if (channel)
103         g_io_channel_unref(channel);
104 }
105
106 int main(int argc, char** argv)
107 {
108     int ret;
109     main_loop = g_main_loop_new(NULL, FALSE);
110     if (main_loop == NULL) {
111         LOGE("Failed to create GMainLoop structure");
112         return -1;
113     }
114
115     ret = stickerd_dbus_init();
116     if (ret != STICKERD_SERVER_ERROR_NONE)
117         LOGW("Failed to init dbus");
118
119     ret = stickerd_db_init();
120     if (ret != STICKERD_SERVER_ERROR_NONE)
121         LOGW("Failed to init database");
122
123     register_signal_handler();
124
125     g_main_loop_run(main_loop);
126
127     unregister_signal_handler();
128
129     g_main_loop_unref(main_loop);
130
131     return 0;
132 }