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 "notification_ex_service.h"
40 #include "badge_service.h"
42 static GMainLoop *main_loop;
43 static GIOChannel *channel;
46 static void lang_key_changed_cb(keynode_t *node, void *first)
51 lang = vconf_get_str(VCONFKEY_LANGSET);
53 setenv("LANG", lang, 1);
54 setenv("LC_MESSAGES", lang, 1);
55 r = setlocale(LC_ALL, "");
57 r = setlocale(LC_ALL, lang);
59 DBG("setlocale = %s", r);
61 DBG("setlocale [%s]", r);
66 static inline int app_create(void)
70 ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_key_changed_cb, NULL);
72 DBG("VCONFKEY_LANGSET notify key chenaged [%d]", ret);
74 lang_key_changed_cb(NULL, NULL);
76 ret = notification_ex_service_init();
78 WARN("notification_ex [%d]", ret);
80 service_common_set_connection(
81 notification_ex_service_get_gdbus_connection());
83 ret = shortcut_service_init();
85 WARN("shortcut [%d]", ret);
87 ret = notification_service_init();
89 WARN("notification [%d]", ret);
91 ret = badge_service_init();
93 WARN("badge [%d]", ret);
95 service_common_init();
96 INFO("Successfully initialized");
101 static inline int app_terminate(void)
105 ret = badge_service_fini();
107 DBG("badge [%d]", ret);
109 ret = notification_service_fini();
111 DBG("notification [%d]", ret);
113 ret = shortcut_service_fini();
115 DBG("shortcut [%d]", ret);
120 static gboolean __signal_handler(GIOChannel *channel,
121 GIOCondition cond, gpointer data)
123 struct signalfd_siginfo fdsi;
127 sfd = g_io_channel_unix_get_fd(channel);
129 size = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
130 if (size != sizeof(struct signalfd_siginfo)) {
131 ERR("signal read failed [%d]", errno);
135 if (fdsi.ssi_signo == SIGTERM) {
136 ERR("Terminated(SIGTERM)");
137 g_main_loop_quit(main_loop);
139 ERR("Unknown SIG[%d] received", fdsi.ssi_signo);
145 static int __init_signal_handler(void)
151 ret = sigemptyset(&mask);
153 ERR("sigemptyset : %d", errno);
157 ret = sigaddset(&mask, SIGTERM);
159 ERR("sigaddset: %d", errno);
163 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
165 ERR("sigprocmask: %d", errno);
169 sfd = signalfd(-1, &mask, SFD_NONBLOCK);
171 ERR("signalfd: %d", errno);
175 channel = g_io_channel_unix_new(sfd);
176 g_io_channel_set_close_on_unref(channel, TRUE);
177 g_io_channel_set_encoding(channel, NULL, NULL);
178 g_io_channel_set_buffered(channel, FALSE);
180 source = g_io_add_watch(channel, G_IO_IN, __signal_handler, NULL);
182 INFO("source[%u]", source);
190 g_source_remove(source);
192 g_io_channel_unref(channel);
197 int main(int argc, char *argv[])
199 int restart_count = 0;
201 main_loop = g_main_loop_new(NULL, FALSE);
205 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
209 vconf_get_int(VCONFKEY_MASTER_RESTART_COUNT, &restart_count);
212 sd_notify(0, "READY=1");
215 vconf_set_int(VCONFKEY_MASTER_RESTART_COUNT, restart_count);
217 if (__init_signal_handler() < 0)
218 ERR("Failed to init signal handler");
220 g_main_loop_run(main_loop);
224 g_main_loop_unref(main_loop);