2 * Copyright 2016 Samsung Electronics Co., Ltd
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.
20 #include <sys/types.h>
23 #include <sys/signalfd.h>
27 #include <systemd/sd-daemon.h>
29 #include <glib-object.h>
36 #include "service_common.h"
37 #include "shortcut_service.h"
38 #include "notification_service.h"
39 #include "badge_service.h"
41 static GMainLoop *main_loop;
42 static GIOChannel *channel;
45 static void lang_key_changed_cb(keynode_t *node, void *first)
50 lang = vconf_get_str(VCONFKEY_LANGSET);
52 setenv("LANG", lang, 1);
53 setenv("LC_MESSAGES", lang, 1);
54 r = setlocale(LC_ALL, "");
56 r = setlocale(LC_ALL, lang);
58 DBG("setlocale = %s", r);
60 DBG("setlocale [%s]", r);
65 static inline int app_create(void)
69 ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_key_changed_cb, NULL);
71 DBG("VCONFKEY_LANGSET notify key chenaged [%d]", ret);
73 lang_key_changed_cb(NULL, NULL);
75 ret = shortcut_service_init();
77 WARN("shortcut [%d]", ret);
79 ret = notification_service_init();
81 WARN("notification [%d]", ret);
83 ret = badge_service_init();
85 WARN("badge [%d]", ret);
87 service_common_init();
88 INFO("Successfully initialized");
93 static inline int app_terminate(void)
97 ret = badge_service_fini();
99 DBG("badge [%d]", ret);
101 ret = notification_service_fini();
103 DBG("notification [%d]", ret);
105 ret = shortcut_service_fini();
107 DBG("shortcut [%d]", ret);
112 static gboolean __signal_handler(GIOChannel *channel,
113 GIOCondition cond, gpointer data)
115 struct signalfd_siginfo fdsi;
119 sfd = g_io_channel_unix_get_fd(channel);
121 size = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
122 if (size != sizeof(struct signalfd_siginfo)) {
123 ERR("signal read failed [%d]", errno);
127 if (fdsi.ssi_signo == SIGTERM) {
128 ERR("Terminated(SIGTERM)");
129 g_main_loop_quit(main_loop);
131 ERR("Unknown SIG[%d] received", fdsi.ssi_signo);
137 static int __init_signal_handler(void)
143 ret = sigemptyset(&mask);
145 ERR("sigemptyset : %d", errno);
149 ret = sigaddset(&mask, SIGTERM);
151 ERR("sigaddset: %d", errno);
155 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
157 ERR("sigprocmask: %d", errno);
161 sfd = signalfd(-1, &mask, SFD_NONBLOCK);
163 ERR("signalfd: %d", errno);
167 channel = g_io_channel_unix_new(sfd);
168 g_io_channel_set_close_on_unref(channel, TRUE);
169 g_io_channel_set_encoding(channel, NULL, NULL);
170 g_io_channel_set_buffered(channel, FALSE);
172 source = g_io_add_watch(channel, G_IO_IN, __signal_handler, NULL);
174 INFO("source[%u]", source);
182 g_source_remove(source);
184 g_io_channel_unref(channel);
189 int main(int argc, char *argv[])
191 int restart_count = 0;
193 main_loop = g_main_loop_new(NULL, FALSE);
197 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
201 vconf_get_int(VCONFKEY_MASTER_RESTART_COUNT, &restart_count);
204 sd_notify(0, "READY=1");
207 vconf_set_int(VCONFKEY_MASTER_RESTART_COUNT, restart_count);
209 if (__init_signal_handler() < 0)
210 ERR("Failed to init signal handler");
212 g_main_loop_run(main_loop);
216 g_main_loop_unref(main_loop);