Fix static analysis issue
[platform/core/appfw/pkgmgr-info.git] / src / server / main.cc
1 /*
2  * Copyright (c) 2021 - 2022 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 <gio/gio.h>
18 #include <glib-unix.h>
19 #include <signal.h>
20 #include <sys/signalfd.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <systemd/sd-daemon.h>
24 #include <unistd.h>
25
26 #include <glib.h>
27 #include <dlog.h>
28
29 #include "runner.hh"
30 #include "pkgmgrinfo_debug.h"
31
32 #ifdef LOG_TAG
33 #undef LOG_TAG
34 #endif
35 #define LOG_TAG "PKGMGR_INFO"
36
37 static GMainLoop *main_loop;
38 static GIOChannel *channel;
39 static guint sig_source;
40 static pkgmgr_server::Runner *runner;
41
42 static gboolean __signal_handler(GIOChannel *channel, GIOCondition cond,
43                                  gpointer data) {
44   struct signalfd_siginfo fdsi;
45   ssize_t size;
46   int sfd;
47
48   sfd = g_io_channel_unix_get_fd(channel);
49
50   size = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
51   if (size != sizeof(struct signalfd_siginfo)) {
52     _LOGE("signal read failed [%d]", errno);
53     return TRUE;
54   }
55
56   if (fdsi.ssi_signo == SIGTERM) {
57     _LOGE("Terminated(SIGTERM)");
58     g_main_loop_quit(main_loop);
59   } else {
60     _LOGE("Unknown SIG[%d] received", fdsi.ssi_signo);
61   }
62
63   return TRUE;
64 }
65
66 static int __init_signal_handler(void) {
67   sigset_t mask;
68   int sfd;
69   int ret;
70
71   ret = sigemptyset(&mask);
72   if (ret < 0) {
73     _LOGE("sigemptyset : %d", errno);
74     return -1;
75   }
76
77   ret = sigaddset(&mask, SIGTERM);
78   if (ret < 0) {
79     _LOGE("sigaddset: %d", errno);
80     return -1;
81   }
82
83   ret = sigprocmask(SIG_BLOCK, &mask, NULL);
84   if (ret < 0) {
85     _LOGE("sigprocmask: %d", errno);
86     return -1;
87   }
88
89   sfd = signalfd(-1, &mask, SFD_NONBLOCK);
90   if (sfd < 0) {
91     _LOGE("signalfd: %d", errno);
92     return -1;
93   }
94
95   channel = g_io_channel_unix_new(sfd);
96   g_io_channel_set_close_on_unref(channel, TRUE);
97   g_io_channel_set_encoding(channel, NULL, NULL);
98   g_io_channel_set_buffered(channel, FALSE);
99
100   sig_source = g_io_add_watch(channel, G_IO_IN, __signal_handler, NULL);
101
102   _LOGI("source[%u]", sig_source);
103
104   return 0;
105 }
106
107 static void __finish(void) {
108   if (sig_source > 0)
109     g_source_remove(sig_source);
110
111   if (channel)
112     g_io_channel_unref(channel);
113
114   if (runner)
115     delete runner;
116 }
117
118 static void __pkgmgr_init() {
119   runner = new pkgmgr_server::Runner(5);
120   sd_notify(0, "READY=1");
121 }
122
123 int main() {
124   main_loop = g_main_loop_new(NULL, FALSE);
125   if (!main_loop)
126     return -1;
127
128   __pkgmgr_init();
129
130   if (__init_signal_handler() < 0)
131     _LOGE("Failed to init signal handler");
132
133   g_main_loop_run(main_loop);
134
135   __finish();
136
137   g_main_loop_unref(main_loop);
138
139   return 0;
140 }