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)
52 static Server *_server = NULL;
54 static void __usage_info(const char *exec)
56 printf("Usage: %s [OPTION]... [PLUGIN_PATH]\n", exec);
58 printf(" -T, --testload\t run with plugin load test mode and exit\n");
59 printf(" -h, --help\t\t display this help and exit\n");
63 void tcore_log(enum tcore_log_type type, enum tcore_log_priority priority, const char *tag, const char *fmt, ...)
69 case TCORE_LOG_TYPE_RADIO: {
70 if (priority >= TCORE_LOG_INFO) {
72 vsnprintf(buf, 1023, fmt, ap);
74 __dlog_print(LOG_ID_RADIO, priority, tag, buf);
76 #ifdef TIZEN_DEBUG_ENABLE
78 vsnprintf(buf, 1023, fmt, ap);
80 __dlog_print(LOG_ID_RADIO, priority, tag, buf);
85 case TCORE_LOG_TYPE_TIME_CHECK: {
86 #ifdef TIZEN_DEBUG_ENABLE /* User Mode should not log performance data */
87 float a = 0.00, b = 0.00;
89 FILE *fp = fopen("/proc/uptime", "r");
90 g_return_if_fail(NULL != fp);
92 if(fscanf(fp, "%f %f", &a, &b)){};
94 next = sprintf(buf, "[UPTIME] %f ", a);
99 vsnprintf(buf + next, 1023 - next, fmt, ap);
101 __dlog_print(LOG_ID_RADIO, priority, tag, buf);
110 static void glib_log(const gchar *log_domain, GLogLevelFlags log_level,
111 const gchar *msg, gpointer user_data)
117 __dlog_print (LOG_ID_RADIO, DLOG_ERROR, "GLIB", msg);
120 #ifdef TIZEN_DEBUG_ENABLE
121 static void telephony_signal_handler(int signo)
128 monitor_server_state(_server);
132 tcore_server_exit(_server);
136 warn("*~*~*~* Unhandled Signal: [%d] *~*~*~*", signo);
144 static void __log_uptime()
146 float a = 0.00, b = 0.00;
147 FILE *fp = fopen("/proc/uptime", "r");
148 g_return_if_fail(NULL != fp);
149 info("scanned %d items", fscanf(fp, "%f %f", &a, &b));
150 info("proc uptime = %f idletime = %f\n", a, b);
154 static gboolean __init_plugin(TcorePlugin *plugin)
156 const struct tcore_plugin_define_desc *desc = tcore_plugin_get_description(plugin);
158 if (!desc || !desc->init)
161 if (!desc->init(plugin)) { /* TODO: Remove plugin from server */
162 char *plugin_name = tcore_plugin_get_filename(plugin);
163 if (NULL != plugin_name) {
164 err("plugin(%s) init failed.", plugin_name);
173 static gboolean init_plugins(Server *s)
175 GSList *list = tcore_server_ref_plugins(s);
177 while (list != NULL) {
178 if (G_UNLIKELY(FALSE == __init_plugin(list->data))) {
179 list = g_slist_next(list);
182 list = g_slist_next(list);
188 static void *__load_plugin(const gchar *filename, struct tcore_plugin_define_desc **desc_out)
191 struct tcore_plugin_define_desc *desc = NULL;
192 struct stat stat_buf;
195 handle = dlopen(filename, RTLD_LAZY);
196 if (G_UNLIKELY(NULL == handle)) {
197 err("fail to load '%s': %s", filename, dlerror());
201 desc = dlsym(handle, "plugin_define_desc");
202 if (G_UNLIKELY(NULL == desc)) {
203 err("fail to load symbol: %s", dlerror());
208 dbg("%s plugin", desc->name);
209 dbg(" - path = %s", filename);
210 dbg(" - version = %d", desc->version);
211 dbg(" - priority = %d", desc->priority);
213 memset(&stat_buf, 0x00, sizeof(stat_buf));
214 memset(&file_date, '\0', sizeof(file_date));
216 if (0 == stat(filename, &stat_buf)) {
217 if (NULL != ctime_r(&stat_buf.st_mtime, file_date)) {
218 if (1 < strlen(file_date))
219 file_date[strlen(file_date)-1] = '\0';
220 dbg(" - date = %s", file_date);
224 if (G_LIKELY(desc->load)) {
225 if (G_UNLIKELY(FALSE == desc->load())) {
226 warn("false return from load(). skip this plugin");
232 if (NULL != desc_out)
238 static gboolean load_plugins(Server *s, const char *path, gboolean flag_test_load)
240 const gchar *file = NULL;
241 gchar *filename = NULL;
244 struct tcore_plugin_define_desc *desc = NULL;
249 dir = g_dir_open(path, 0, NULL);
253 while ((file = g_dir_read_name(dir)) != NULL) {
254 if (g_str_has_prefix(file, "lib") == TRUE
255 || g_str_has_suffix(file, ".so") == FALSE)
258 filename = g_build_filename(path, file, NULL);
261 if (G_UNLIKELY((handle = __load_plugin(filename, &desc)) == NULL)) {
266 /* Don't add to server if flag_test_load */
267 if (flag_test_load) {
268 dbg("success to load '%s'", filename);
274 tcore_server_add_plugin(s, tcore_plugin_new(s, desc, filename, handle));
276 dbg("%s added", desc->name);
284 int main(int argc, char *argv[])
286 #ifdef TIZEN_DEBUG_ENABLE
287 struct sigaction sigact;
290 gboolean flag_test_load = FALSE;
291 int opt = 0, opt_index = 0, ret_code = EXIT_SUCCESS;
292 int daemon_load_count = 0;
293 struct option options[] = {
295 { "testload", 0, &flag_test_load, 1 },
298 const char *plugin_path = DEFAULT_PLUGINS_PATH;
299 char *tcore_ver = NULL;
300 struct sysinfo sys_info;
302 TIME_CHECK("Starting Telephony");
305 if (0 == sysinfo(&sys_info))
306 info("uptime: %ld secs", sys_info.uptime);
310 tcore_ver = tcore_util_get_version();
311 info("daemon version: %s", DAEMON_VERSION);
312 info("libtcore version: %s", tcore_ver);
314 info("glib version: %u.%u.%u", glib_major_version, glib_minor_version, glib_micro_version);
316 /* Telephony reset handling*/
317 vconf_get_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT,&daemon_load_count);
319 vconf_set_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT,daemon_load_count);
320 dbg("daemon load count = [%d]", daemon_load_count);
322 #ifdef TIZEN_DEBUG_ENABLE
323 /* Signal Registration */
324 sigact.sa_handler = telephony_signal_handler;
325 sigemptyset(&sigact.sa_mask);
327 if (sigaction(SIGTERM, &sigact, NULL) < 0)
328 warn("sigaction(SIGTERM) failed.");
329 if (sigaction(SIGUSR1, &sigact, NULL) < 0)
330 warn("sigaction(SIGUSR1) failed.");
333 /* Commandline option parser TODO: Replace with GOptionContext */
335 opt = getopt_long(argc, argv, "hT", options, &opt_index);
344 __usage_info(argv[0]);
348 warn("unhandled opt_index.");
354 __usage_info(argv[0]);
359 flag_test_load = TRUE;
362 warn("unhandled opt case.");
368 plugin_path = argv[optind];
370 info("plugin_path: [%s]", plugin_path);
372 #if !GLIB_CHECK_VERSION(2, 35, 0)
375 #if !GLIB_CHECK_VERSION(2, 31, 0)
379 s = tcore_server_new();
380 if (G_UNLIKELY(NULL == s)) {
381 err("server_new failed.");
382 ret_code = EXIT_FAILURE;
387 g_log_set_default_handler(glib_log, s);
390 if (G_UNLIKELY(FALSE == load_plugins(s, (const char *)plugin_path, flag_test_load))) {
391 err("load_plugins failed.");
392 ret_code = EXIT_FAILURE;
396 TIME_CHECK("Loading Plugins Complete");
398 if (flag_test_load) {
399 ret_code = EXIT_SUCCESS;
403 /* Initialize Plugins */
404 if (G_UNLIKELY(FALSE == init_plugins(s))) {
405 err("init_plugins failed.");
406 ret_code = EXIT_FAILURE;
410 info("server mainloop start");
411 TIME_CHECK("Initializing Plugins Complete. Starting Daemon");
413 if (G_UNLIKELY(TCORE_RETURN_SUCCESS != tcore_server_run(s))) {
414 err("server_run failed.");
415 ret_code = EXIT_FAILURE;
419 tcore_server_free(s); _server = NULL;