Adjust log priority for debugging in TV profile
[platform/core/telephony/telephony-daemon.git] / src / main.c
1 /*
2  * telephony-daemon
3  *
4  * Copyright (c) 2013 Samsung Electronics Co. Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #ifdef TIZEN_DEBUG_ENABLE
22 #include "monitor.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <dlfcn.h>
30 #include <getopt.h>
31 #include <sys/stat.h>
32 #include <sys/sysinfo.h>
33
34 #include <glib.h>
35 #include <dlog.h>
36 #include <vconf.h>
37
38 #include <tcore.h>
39 #include <plugin.h>
40 #include <server.h>
41
42 #ifndef DAEMON_VERSION
43 #define DAEMON_VERSION "unknown"
44 #endif
45
46 #ifndef DEFAULT_PLUGINS_PATH
47 #define DEFAULT_PLUGINS_PATH "/usr/lib/telephony/plugins/"
48 #endif
49
50 #define NOTUSED(var) (var = var)
51 #define BUFFER_SIZE 1024
52
53 static Server *_server = NULL;
54
55 static void __usage_info(const char *exec)
56 {
57         info("Usage: %s [OPTION]... [PLUGIN_PATH]\n", exec);
58         info("\n");
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");
61         info("\n");
62 }
63
64 void tcore_log(enum tcore_log_type type, enum tcore_log_priority priority, const char *tag, const char *fmt, ...)
65 {
66         va_list ap;
67         char buf[BUFFER_SIZE];
68         int log_id = LOG_ID_RADIO;
69 #ifdef TIZEN_PROFILE_TV
70         log_id = LOG_ID_MAIN;
71         /* Increase log priority for debugging in TV profile */
72         priority = ((priority == TCORE_LOG_DEBUG) ? TCORE_LOG_INFO : priority);
73 #endif
74
75         switch (type) {
76         case TCORE_LOG_TYPE_RADIO: {
77                 if (priority >= TCORE_LOG_INFO) {
78                         va_start(ap, fmt);
79                         vsnprintf(buf, BUFFER_SIZE-1, fmt, ap);
80                         va_end(ap);
81                         __dlog_print(log_id, priority, tag, "%s", buf);
82                 } else {
83                 #ifdef TIZEN_DEBUG_ENABLE
84                         va_start(ap, fmt);
85                         vsnprintf(buf, BUFFER_SIZE-1, fmt, ap);
86                         va_end(ap);
87                         __dlog_print(log_id, priority, tag, "%s", buf);
88                 #endif
89                 }
90         } break;
91
92         case TCORE_LOG_TYPE_TIME_CHECK: {
93         #ifdef TIZEN_DEBUG_ENABLE /* User Mode should not log performance data */
94                 float a = 0.00, b = 0.00;
95                 int next = 0;
96                 FILE *fp = fopen("/proc/uptime", "r");
97                 if (NULL == fp) {
98                         err("fopen() failed");
99                         return;
100                 }
101                 if (fscanf(fp, "%f %f", &a, &b) != 1)
102                         next = snprintf(buf, BUFFER_SIZE, "[UPTIME] [Not Set] ");
103                 else
104                         next = snprintf(buf, BUFFER_SIZE, "[UPTIME] %f ", a);
105                 fclose(fp);
106                 if (next < 0)
107                         return;
108
109                 va_start(ap, fmt);
110                 vsnprintf(buf + next, (BUFFER_SIZE-1) - next, fmt, ap);
111                 va_end(ap);
112                 __dlog_print(log_id, priority, tag, "%s", buf);
113         #endif
114         } break;
115
116         default:
117         break;
118         }
119 }
120
121 static void glib_log(const gchar *log_domain, GLogLevelFlags log_level,
122                 const gchar *msg, gpointer user_data)
123 {
124         NOTUSED(log_domain);
125         NOTUSED(log_level);
126         NOTUSED(user_data);
127
128         __dlog_print(LOG_ID_RADIO, DLOG_ERROR, "GLIB", "%s", msg);
129 }
130
131 #ifdef TIZEN_DEBUG_ENABLE
132 static void telephony_signal_handler(int signo)
133 {
134         if (!_server)
135                 return;
136
137         switch (signo) {
138         case SIGUSR1: {
139                 monitor_server_state(_server);
140         } break;
141
142         case SIGTERM: {
143                 tcore_server_exit(_server);
144         } break;
145
146 #if defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
147         case SIGHUP: {
148                 warn("*~*~*~* Ignore Signal: [SIGHUP] *~*~*~*");
149         } break;
150 #endif
151
152         default: {
153                 warn("*~*~*~* Unhandled Signal: [%d] *~*~*~*", signo);
154         } break;
155         } /* end switch */
156
157         return;
158 }
159 #elif defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
160 static void telephony_signal_handler(int signo)
161 {
162         if (!_server)
163                 return;
164
165         switch (signo) {
166         case SIGHUP: {
167                 warn("*~*~*~* Ignore Signal: [SIGHUP] *~*~*~*");
168         } break;
169
170         default: {
171                 warn("*~*~*~* Unhandled Signal: [%d] *~*~*~*", signo);
172         } break;
173         } /* end switch */
174
175         return;
176 }
177 #endif
178
179 static void __log_uptime()
180 {
181         float a = 0.00, b = 0.00;
182         FILE *fp = fopen("/proc/uptime", "r");
183
184         if (NULL == fp) {
185                 err("fopen() failed");
186                 return;
187         }
188         info("scanned %d items", fscanf(fp, "%f %f", &a, &b));
189         info("proc uptime = %f idletime = %f\n", a, b);
190         fclose(fp);
191 }
192
193 static gboolean __init_plugin(TcorePlugin *plugin)
194 {
195         const struct tcore_plugin_define_desc *desc = tcore_plugin_get_description(plugin);
196
197         if (!desc || !desc->init)
198                 return FALSE;
199
200         if (!desc->init(plugin)) { /* TODO: Remove plugin from server */
201                 char *plugin_name = tcore_plugin_get_filename(plugin);
202                 if (NULL != plugin_name) {
203                         err("plugin(%s) init failed.", plugin_name);
204                         free(plugin_name);
205                 }
206                 return FALSE;
207         }
208
209         return TRUE;
210 }
211
212 static gboolean init_plugins(Server *s)
213 {
214         GSList *list = tcore_server_ref_plugins(s);
215
216         while (list != NULL) {
217                 if (G_UNLIKELY(FALSE == __init_plugin(list->data))) {
218                         list = g_slist_next(list);
219                         continue;
220                 }
221                 list = g_slist_next(list);
222         }
223
224         return TRUE;
225 }
226
227 static void *__load_plugin(const gchar *filename, struct tcore_plugin_define_desc **desc_out)
228 {
229         void *handle = NULL;
230         struct tcore_plugin_define_desc *desc = NULL;
231         struct stat stat_buf;
232         char file_date[27];
233
234         handle = dlopen(filename, RTLD_LAZY);
235         if (NULL == handle) {
236                 err("dlopen() failed:[%s]", filename);
237                 return NULL;
238         }
239
240         desc = dlsym(handle, "plugin_define_desc");
241         if (NULL == desc) {
242                 err("dlsym() failed:[%s]", "plugin_define_desc");
243                 dlclose(handle);
244                 return NULL;
245         }
246
247         dbg("%s plugin", desc->name);
248         dbg(" - path = %s", filename);
249         dbg(" - version = %d", desc->version);
250         dbg(" - priority = %d", desc->priority);
251
252         memset(&stat_buf, 0x00, sizeof(stat_buf));
253         memset(&file_date, '\0', sizeof(file_date));
254
255         if (0 == stat(filename, &stat_buf)) {
256                 if (NULL != ctime_r(&stat_buf.st_mtime, file_date)) {
257                         if (1 < strlen(file_date))
258                                 file_date[strlen(file_date)-1] = '\0';
259                         dbg(" - date = %s", file_date);
260                 }
261         }
262
263         if (G_LIKELY(desc->load)) {
264                 if (G_UNLIKELY(FALSE == desc->load())) {
265                         warn("false return from load(). skip this plugin");
266                         dlclose(handle);
267                         return NULL;
268                 }
269         }
270
271         if (NULL != desc_out)
272                 *desc_out = desc;
273
274         return handle;
275 }
276
277 static gboolean load_plugins(Server *s, const char *path, gboolean flag_test_load)
278 {
279         const gchar *file = NULL;
280         gchar *filename = NULL;
281         GDir *dir = NULL;
282         void *handle = NULL;
283         struct tcore_plugin_define_desc *desc = NULL;
284
285         if (!path || !s)
286                 return FALSE;
287
288         dir = g_dir_open(path, 0, NULL);
289         if (!dir)
290                 return FALSE;
291
292         while ((file = g_dir_read_name(dir)) != NULL) {
293                 if (g_str_has_prefix(file, "lib") == TRUE
294                         || g_str_has_suffix(file, ".so") == FALSE)
295                         continue;
296
297                 filename = g_build_filename(path, file, NULL);
298
299                 /* Load a plugin */
300                 handle = __load_plugin(filename, &desc);
301                 if (NULL == handle) {
302                         g_free(filename);
303                         continue;
304                 }
305
306                 /* Don't add to server if flag_test_load */
307                 if (flag_test_load) {
308                         dbg("success to load '%s'", filename);
309                         dlclose(handle);
310                         g_free(filename);
311                         continue;
312                 }
313
314                 tcore_server_add_plugin(s, tcore_plugin_new(s, desc, filename, handle));
315
316                 dbg("%s added", desc->name);
317                 g_free(filename);
318         }
319         g_dir_close(dir);
320
321         return TRUE;
322 }
323
324 int main(int argc, char *argv[])
325 {
326 #if defined(TIZEN_DEBUG_ENABLE) || defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
327         struct sigaction sigact;
328 #endif
329         Server *s = NULL;
330         gboolean flag_test_load = FALSE;
331         int opt = 0, opt_index = 0, ret_code = EXIT_SUCCESS;
332         int daemon_load_count = 0;
333         struct option options[] = {
334                 { "help", 0, 0, 0 },
335                 { "testload", 0, &flag_test_load, 1 },
336                 { 0, 0, 0, 0 }
337         };
338         const char *plugin_path = DEFAULT_PLUGINS_PATH;
339         char *tcore_ver = NULL;
340         struct sysinfo sys_info;
341
342         TIME_CHECK("Starting Telephony");
343
344         /* System Uptime */
345         if (0 == sysinfo(&sys_info))
346                 info("uptime: %ld secs", sys_info.uptime);
347         __log_uptime();
348
349         /* Version Info */
350         tcore_ver = tcore_util_get_version();
351         info("daemon version: [%s] libtcore version: [%s] glib version: [%u.%u.%u]",
352                 DAEMON_VERSION, tcore_ver, glib_major_version, glib_minor_version, glib_micro_version);
353         free(tcore_ver);
354         info("glib version: %u.%u.%u", glib_major_version, glib_minor_version, glib_micro_version);
355
356         /* Telephony reset handling*/
357         vconf_get_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT, &daemon_load_count);
358         daemon_load_count++;
359         vconf_set_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT, daemon_load_count);
360         info("daemon load count = [%d]", daemon_load_count);
361
362 #ifdef TIZEN_DEBUG_ENABLE
363         /* Signal Registration */
364         sigact.sa_handler = telephony_signal_handler;
365         sigemptyset(&sigact.sa_mask);
366         sigact.sa_flags = 0;
367         if (sigaction(SIGTERM, &sigact, NULL) < 0)
368                 warn("sigaction(SIGTERM) failed.");
369         if (sigaction(SIGUSR1, &sigact, NULL) < 0)
370                 warn("sigaction(SIGUSR1) failed.");
371 #if defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
372         /* SIGHUP should be ignored because cellular dongle ejection makes this signal */
373         if (sigaction(SIGHUP, &sigact, NULL) < 0)
374                 warn("sigaction(SIGHUP) failed.");
375 #endif
376 #elif defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
377         /* Signal Registration */
378         sigact.sa_handler = telephony_signal_handler;
379         sigemptyset(&sigact.sa_mask);
380         sigact.sa_flags = 0;
381         /* SIGHUP should be ignored because cellular dongle ejection makes this signal */
382         if (sigaction(SIGHUP, &sigact, NULL) < 0)
383                 warn("sigaction(SIGHUP) failed.");
384 #endif
385
386         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
387                 err("Child process won't be auto reaped: [%d]", errno);
388
389         /* Commandline option parser TODO: Replace with GOptionContext */
390         while (TRUE) {
391                 opt = getopt_long(argc, argv, "hT", options, &opt_index);
392
393                 if (-1 == opt)
394                         break;
395
396                 switch (opt) {
397                 case 0: {
398                         switch (opt_index) {
399                         case 0: {
400                                 __usage_info(argv[0]);
401                                 return 0;
402                         } break;
403                         default: {
404                                 warn("unhandled opt_index.");
405                         } break;
406                         } /* end switch */
407                 } break;
408
409                 case 'h': {
410                         __usage_info(argv[0]);
411                         return 0;
412                 } break;
413
414                 case 'T': {
415                         flag_test_load = TRUE;
416                 } break;
417                 default: {
418                         warn("unhandled opt case.");
419                 } break;
420                 } /* end switch */
421         }
422
423         if (optind < argc)
424                 plugin_path = argv[optind];
425
426         info("plugin_path: [%s]", plugin_path);
427
428 #if !GLIB_CHECK_VERSION(2, 35, 0)
429         g_type_init();
430 #endif
431 #if !GLIB_CHECK_VERSION(2, 31, 0)
432         g_thread_init(NULL);
433 #endif
434
435         s = tcore_server_new();
436         if (G_UNLIKELY(NULL == s)) {
437                 err("server_new failed.");
438                 ret_code = EXIT_FAILURE;
439                 goto END;
440         }
441         _server = s;
442
443         g_log_set_default_handler(glib_log, s);
444
445         /* Load Plugins */
446         if (G_UNLIKELY(FALSE == load_plugins(s, (const char *)plugin_path, flag_test_load))) {
447                 err("load_plugins failed.");
448                 ret_code = EXIT_FAILURE;
449                 goto END;
450         }
451
452         TIME_CHECK("Loading Plugins Complete");
453
454         if (flag_test_load) {
455                 ret_code = EXIT_SUCCESS;
456                 goto END;
457         }
458
459         /* Initialize Plugins */
460         if (G_UNLIKELY(FALSE == init_plugins(s))) {
461                 err("init_plugins failed.");
462                 ret_code = EXIT_FAILURE;
463                 goto END;
464         }
465
466         info("server mainloop start");
467         TIME_CHECK("Initializing Plugins Complete. Starting Daemon");
468
469         if (G_UNLIKELY(TCORE_RETURN_SUCCESS != tcore_server_run(s))) {
470                 err("server_run failed.");
471                 ret_code = EXIT_FAILURE;
472         }
473
474 END:
475         tcore_server_free(s); _server = NULL;
476         return ret_code;
477 }