4 * Copyright (c) 2013 Samsung Electronics Co. Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #ifdef TIZEN_DEBUG_ENABLE
32 #include <sys/sysinfo.h>
42 #ifndef DAEMON_VERSION
43 #define DAEMON_VERSION "unknown"
46 #ifndef DEFAULT_PLUGINS_PATH
47 #define DEFAULT_PLUGINS_PATH "/usr/lib/telephony/plugins/"
50 #define NOTUSED(var) (var = var)
51 #define BUFFER_SIZE 1024
53 static Server *_server = NULL;
55 static void __usage_info(const char *exec)
57 info("Usage: %s [OPTION]... [PLUGIN_PATH]\n", exec);
59 info(" -T, --testload\t run with plugin load test mode and exit\n");
60 info(" -h, --help\t\t display this help and exit\n");
64 void tcore_log(enum tcore_log_type type, enum tcore_log_priority priority, const char *tag, const char *fmt, ...)
67 char buf[BUFFER_SIZE];
68 int log_id = LOG_ID_RADIO;
69 #ifdef TIZEN_PROFILE_TV
74 case TCORE_LOG_TYPE_RADIO: {
75 if (priority >= TCORE_LOG_INFO) {
77 vsnprintf(buf, BUFFER_SIZE-1, fmt, ap);
79 __dlog_print(log_id, priority, tag, "%s", buf);
81 #ifdef TIZEN_DEBUG_ENABLE
83 vsnprintf(buf, BUFFER_SIZE-1, fmt, ap);
85 __dlog_print(log_id, priority, tag, "%s", buf);
90 case TCORE_LOG_TYPE_TIME_CHECK: {
91 #ifdef TIZEN_DEBUG_ENABLE /* User Mode should not log performance data */
92 float a = 0.00, b = 0.00;
94 FILE *fp = fopen("/proc/uptime", "r");
96 err("fopen() failed");
99 if (fscanf(fp, "%f %f", &a, &b) != 1)
100 next = snprintf(buf, BUFFER_SIZE, "[UPTIME] [Not Set] ");
102 next = snprintf(buf, BUFFER_SIZE, "[UPTIME] %f ", a);
108 vsnprintf(buf + next, (BUFFER_SIZE-1) - next, fmt, ap);
110 __dlog_print(log_id, priority, tag, "%s", buf);
119 static void glib_log(const gchar *log_domain, GLogLevelFlags log_level,
120 const gchar *msg, gpointer user_data)
126 __dlog_print(LOG_ID_RADIO, DLOG_ERROR, "GLIB", "%s", msg);
129 #ifdef TIZEN_DEBUG_ENABLE
130 static void telephony_signal_handler(int signo)
137 monitor_server_state(_server);
141 tcore_server_exit(_server);
144 #if defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
146 warn("*~*~*~* Ignore Signal: [SIGHUP] *~*~*~*");
151 warn("*~*~*~* Unhandled Signal: [%d] *~*~*~*", signo);
157 #elif defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
158 static void telephony_signal_handler(int signo)
165 warn("*~*~*~* Ignore Signal: [SIGHUP] *~*~*~*");
169 warn("*~*~*~* Unhandled Signal: [%d] *~*~*~*", signo);
177 static void __log_uptime()
179 float a = 0.00, b = 0.00;
180 FILE *fp = fopen("/proc/uptime", "r");
183 err("fopen() failed");
186 info("scanned %d items", fscanf(fp, "%f %f", &a, &b));
187 info("proc uptime = %f idletime = %f\n", a, b);
191 static gboolean __init_plugin(TcorePlugin *plugin)
193 const struct tcore_plugin_define_desc *desc = tcore_plugin_get_description(plugin);
195 if (!desc || !desc->init)
198 if (!desc->init(plugin)) { /* TODO: Remove plugin from server */
199 char *plugin_name = tcore_plugin_get_filename(plugin);
200 if (NULL != plugin_name) {
201 err("plugin(%s) init failed.", plugin_name);
210 static gboolean init_plugins(Server *s)
212 GSList *list = tcore_server_ref_plugins(s);
214 while (list != NULL) {
215 if (G_UNLIKELY(FALSE == __init_plugin(list->data))) {
216 list = g_slist_next(list);
219 list = g_slist_next(list);
225 static void *__load_plugin(const gchar *filename, struct tcore_plugin_define_desc **desc_out)
228 struct tcore_plugin_define_desc *desc = NULL;
229 struct stat stat_buf;
232 handle = dlopen(filename, RTLD_LAZY);
233 if (NULL == handle) {
234 err("dlopen() failed:[%s]", filename);
238 desc = dlsym(handle, "plugin_define_desc");
240 err("dlsym() failed:[%s]", "plugin_define_desc");
245 dbg("%s plugin", desc->name);
246 dbg(" - path = %s", filename);
247 dbg(" - version = %d", desc->version);
248 dbg(" - priority = %d", desc->priority);
250 memset(&stat_buf, 0x00, sizeof(stat_buf));
251 memset(&file_date, '\0', sizeof(file_date));
253 if (0 == stat(filename, &stat_buf)) {
254 if (NULL != ctime_r(&stat_buf.st_mtime, file_date)) {
255 if (1 < strlen(file_date))
256 file_date[strlen(file_date)-1] = '\0';
257 dbg(" - date = %s", file_date);
261 if (G_LIKELY(desc->load)) {
262 if (G_UNLIKELY(FALSE == desc->load())) {
263 warn("false return from load(). skip this plugin");
269 if (NULL != desc_out)
275 static gboolean load_plugins(Server *s, const char *path, gboolean flag_test_load)
277 const gchar *file = NULL;
278 gchar *filename = NULL;
281 struct tcore_plugin_define_desc *desc = NULL;
286 dir = g_dir_open(path, 0, NULL);
290 while ((file = g_dir_read_name(dir)) != NULL) {
291 if (g_str_has_prefix(file, "lib") == TRUE
292 || g_str_has_suffix(file, ".so") == FALSE)
295 filename = g_build_filename(path, file, NULL);
298 handle = __load_plugin(filename, &desc);
299 if (NULL == handle) {
304 /* Don't add to server if flag_test_load */
305 if (flag_test_load) {
306 dbg("success to load '%s'", filename);
312 tcore_server_add_plugin(s, tcore_plugin_new(s, desc, filename, handle));
314 dbg("%s added", desc->name);
322 int main(int argc, char *argv[])
324 #if defined(TIZEN_DEBUG_ENABLE) || defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
325 struct sigaction sigact;
328 gboolean flag_test_load = FALSE;
329 int opt = 0, opt_index = 0, ret_code = EXIT_SUCCESS;
330 int daemon_load_count = 0;
331 struct option options[] = {
333 { "testload", 0, &flag_test_load, 1 },
336 const char *plugin_path = DEFAULT_PLUGINS_PATH;
337 char *tcore_ver = NULL;
338 struct sysinfo sys_info;
340 TIME_CHECK("Starting Telephony");
343 if (0 == sysinfo(&sys_info))
344 info("uptime: %ld secs", sys_info.uptime);
348 tcore_ver = tcore_util_get_version();
349 info("daemon version: [%s] libtcore version: [%s] glib version: [%u.%u.%u]",
350 DAEMON_VERSION, tcore_ver, glib_major_version, glib_minor_version, glib_micro_version);
352 info("glib version: %u.%u.%u", glib_major_version, glib_minor_version, glib_micro_version);
354 /* Telephony reset handling*/
355 vconf_get_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT, &daemon_load_count);
357 vconf_set_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT, daemon_load_count);
358 info("daemon load count = [%d]", daemon_load_count);
360 #ifdef TIZEN_DEBUG_ENABLE
361 /* Signal Registration */
362 sigact.sa_handler = telephony_signal_handler;
363 sigemptyset(&sigact.sa_mask);
365 if (sigaction(SIGTERM, &sigact, NULL) < 0)
366 warn("sigaction(SIGTERM) failed.");
367 if (sigaction(SIGUSR1, &sigact, NULL) < 0)
368 warn("sigaction(SIGUSR1) failed.");
369 #if defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
370 /* SIGHUP should be ignored because cellular dongle ejection makes this signal */
371 if (sigaction(SIGHUP, &sigact, NULL) < 0)
372 warn("sigaction(SIGHUP) failed.");
374 #elif defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
375 /* Signal Registration */
376 sigact.sa_handler = telephony_signal_handler;
377 sigemptyset(&sigact.sa_mask);
379 /* SIGHUP should be ignored because cellular dongle ejection makes this signal */
380 if (sigaction(SIGHUP, &sigact, NULL) < 0)
381 warn("sigaction(SIGHUP) failed.");
384 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
385 err("Child process won't be auto reaped: [%d]", errno);
387 /* Commandline option parser TODO: Replace with GOptionContext */
389 opt = getopt_long(argc, argv, "hT", options, &opt_index);
398 __usage_info(argv[0]);
402 warn("unhandled opt_index.");
408 __usage_info(argv[0]);
413 flag_test_load = TRUE;
416 warn("unhandled opt case.");
422 plugin_path = argv[optind];
424 info("plugin_path: [%s]", plugin_path);
426 #if !GLIB_CHECK_VERSION(2, 35, 0)
429 #if !GLIB_CHECK_VERSION(2, 31, 0)
433 s = tcore_server_new();
434 if (G_UNLIKELY(NULL == s)) {
435 err("server_new failed.");
436 ret_code = EXIT_FAILURE;
441 g_log_set_default_handler(glib_log, s);
444 if (G_UNLIKELY(FALSE == load_plugins(s, (const char *)plugin_path, flag_test_load))) {
445 err("load_plugins failed.");
446 ret_code = EXIT_FAILURE;
450 TIME_CHECK("Loading Plugins Complete");
452 if (flag_test_load) {
453 ret_code = EXIT_SUCCESS;
457 /* Initialize Plugins */
458 if (G_UNLIKELY(FALSE == init_plugins(s))) {
459 err("init_plugins failed.");
460 ret_code = EXIT_FAILURE;
464 info("server mainloop start");
465 TIME_CHECK("Initializing Plugins Complete. Starting Daemon");
467 if (G_UNLIKELY(TCORE_RETURN_SUCCESS != tcore_server_run(s))) {
468 err("server_run failed.");
469 ret_code = EXIT_FAILURE;
473 tcore_server_free(s); _server = NULL;