2 * Copyright (c) 2021 - 2022 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.
18 #include <glib-unix.h>
20 #include <sys/signalfd.h>
22 #include <sys/types.h>
23 #include <systemd/sd-daemon.h>
30 #include "pkgmgrinfo_debug.h"
35 #define LOG_TAG "PKGMGR_INFO"
37 static GMainLoop *main_loop;
38 static GIOChannel *channel;
39 static guint sig_source;
40 static pkgmgr_server::Runner *runner;
42 static gboolean __signal_handler(GIOChannel *channel, GIOCondition cond,
44 struct signalfd_siginfo fdsi;
48 sfd = g_io_channel_unix_get_fd(channel);
50 size = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
51 if (size != sizeof(struct signalfd_siginfo)) {
52 _LOGE("signal read failed [%d]", errno);
56 if (fdsi.ssi_signo == SIGTERM) {
57 _LOGE("Terminated(SIGTERM)");
58 g_main_loop_quit(main_loop);
60 _LOGE("Unknown SIG[%d] received", fdsi.ssi_signo);
66 static int __init_signal_handler(void) {
71 ret = sigemptyset(&mask);
73 _LOGE("sigemptyset : %d", errno);
77 ret = sigaddset(&mask, SIGTERM);
79 _LOGE("sigaddset: %d", errno);
83 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
85 _LOGE("sigprocmask: %d", errno);
89 sfd = signalfd(-1, &mask, SFD_NONBLOCK);
91 _LOGE("signalfd: %d", errno);
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);
100 sig_source = g_io_add_watch(channel, G_IO_IN, __signal_handler, NULL);
102 _LOGI("source[%u]", sig_source);
107 static void __finish(void) {
109 g_source_remove(sig_source);
112 g_io_channel_unref(channel);
118 static void __pkgmgr_init() {
119 runner = new pkgmgr_server::Runner(5);
120 sd_notify(0, "READY=1");
124 main_loop = g_main_loop_new(NULL, FALSE);
130 if (__init_signal_handler() < 0)
131 _LOGE("Failed to init signal handler");
133 g_main_loop_run(main_loop);
137 g_main_loop_unref(main_loop);