Update logprint
[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) == 0)
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                 dbg("*~*~*~* Signal: [SIGTERM] finished *~*~*~*");
145         } break;
146
147 #if defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
148         case SIGHUP: {
149                 warn("*~*~*~* Ignore Signal: [SIGHUP] *~*~*~*");
150         } break;
151 #endif
152
153         default: {
154                 warn("*~*~*~* Unhandled Signal: [%d] *~*~*~*", signo);
155         } break;
156         } /* end switch */
157
158         return;
159 }
160 #elif defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
161 static void telephony_signal_handler(int signo)
162 {
163         if (!_server)
164                 return;
165
166         switch (signo) {
167         case SIGHUP: {
168                 warn("*~*~*~* Ignore Signal: [SIGHUP] *~*~*~*");
169         } break;
170
171         default: {
172                 warn("*~*~*~* Unhandled Signal: [%d] *~*~*~*", signo);
173         } break;
174         } /* end switch */
175
176         return;
177 }
178 #endif
179
180 static void __log_uptime()
181 {
182         float a = 0.00, b = 0.00;
183         FILE *fp = fopen("/proc/uptime", "r");
184
185         if (NULL == fp) {
186                 err("fopen() failed");
187                 return;
188         }
189         info("scanned %d items", fscanf(fp, "%f %f", &a, &b));
190         info("proc uptime = %f idletime = %f", a, b);
191         fclose(fp);
192 }
193
194 static gboolean __init_plugin(TcorePlugin *plugin)
195 {
196         const struct tcore_plugin_define_desc *desc = tcore_plugin_get_description(plugin);
197
198         if (!desc || !desc->init)
199                 return FALSE;
200
201         if (!desc->init(plugin)) { /* TODO: Remove plugin from server */
202                 char *plugin_name = tcore_plugin_get_filename(plugin);
203                 if (NULL != plugin_name) {
204                         err("plugin(%s) init failed.", plugin_name);
205                         free(plugin_name);
206                 }
207                 return FALSE;
208         }
209
210         return TRUE;
211 }
212
213 static gboolean init_plugins(Server *s)
214 {
215         GSList *list = tcore_server_ref_plugins(s);
216
217         while (list != NULL) {
218                 if (G_UNLIKELY(FALSE == __init_plugin(list->data))) {
219                         list = g_slist_next(list);
220                         continue;
221                 }
222                 list = g_slist_next(list);
223         }
224
225         return TRUE;
226 }
227
228 static void *__load_plugin(const gchar *filename, struct tcore_plugin_define_desc **desc_out)
229 {
230         void *handle = NULL;
231         struct tcore_plugin_define_desc *desc = NULL;
232
233         handle = dlopen(filename, RTLD_LAZY);
234         if (NULL == handle) {
235                 err("dlopen() failed:[%s][%s]", filename, dlerror());
236                 return NULL;
237         }
238
239         desc = dlsym(handle, "plugin_define_desc");
240         if (NULL == desc) {
241                 err("dlsym() failed:[%s]", "plugin_define_desc");
242                 dlclose(handle);
243                 return NULL;
244         }
245
246         dbg("%s (%s) version:[%d] priority:[%d] ", desc->name, filename, desc->version, desc->priority);
247
248         if (G_LIKELY(desc->load)) {
249                 if (G_UNLIKELY(FALSE == desc->load())) {
250                         warn("false return from load(). skip this plugin");
251                         dlclose(handle);
252                         return NULL;
253                 }
254         }
255
256         if (NULL != desc_out)
257                 *desc_out = desc;
258
259         return handle;
260 }
261
262 static gboolean load_plugins(Server *s, const char *path, gboolean flag_test_load)
263 {
264         const gchar *file = NULL;
265         gchar *filename = NULL;
266         GDir *dir = NULL;
267         void *handle = NULL;
268         struct tcore_plugin_define_desc *desc = NULL;
269
270         if (!path || !s)
271                 return FALSE;
272
273         dir = g_dir_open(path, 0, NULL);
274         if (!dir)
275                 return FALSE;
276
277         while ((file = g_dir_read_name(dir)) != NULL) {
278                 if (g_str_has_prefix(file, "lib") == TRUE
279                         || g_str_has_suffix(file, ".so") == FALSE)
280                         continue;
281
282                 filename = g_build_filename(path, file, NULL);
283
284                 /* Load a plugin */
285                 handle = __load_plugin(filename, &desc);
286                 if (NULL == handle) {
287                         g_free(filename);
288                         continue;
289                 }
290
291                 /* Don't add to server if flag_test_load */
292                 if (flag_test_load) {
293                         dbg("success to load '%s'", filename);
294                         dlclose(handle);
295                         g_free(filename);
296                         continue;
297                 }
298
299                 tcore_server_add_plugin(s, tcore_plugin_new(s, desc, filename, handle));
300
301                 dbg("%s added", desc->name);
302                 g_free(filename);
303         }
304         g_dir_close(dir);
305
306         return TRUE;
307 }
308
309 int main(int argc, char *argv[])
310 {
311 #if defined(TIZEN_DEBUG_ENABLE) || defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
312         struct sigaction sigact;
313 #endif
314         Server *s = NULL;
315         gboolean flag_test_load = FALSE;
316         int opt = 0, opt_index = 0, ret_code = EXIT_SUCCESS;
317         int daemon_load_count = 0;
318         struct option options[] = {
319                 { "help", 0, 0, 0 },
320                 { "testload", 0, &flag_test_load, 1 },
321                 { 0, 0, 0, 0 }
322         };
323         const char *plugin_path = DEFAULT_PLUGINS_PATH;
324         char *tcore_ver = NULL;
325         struct sysinfo sys_info;
326
327         TIME_CHECK("Starting Telephony");
328
329         /* System Uptime */
330         if (0 == sysinfo(&sys_info))
331                 info("uptime: %ld secs", sys_info.uptime);
332         __log_uptime();
333
334         /* Version Info */
335         tcore_ver = tcore_util_get_version();
336         info("[version] daemon : %s, libtcore : %s, glib : %u.%u.%u", DAEMON_VERSION, tcore_ver, glib_major_version, glib_minor_version, glib_micro_version);
337         free(tcore_ver);
338
339         /* Telephony reset handling*/
340         vconf_get_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT, &daemon_load_count);
341         daemon_load_count++;
342         vconf_set_int(VCONFKEY_TELEPHONY_DAEMON_LOAD_COUNT, daemon_load_count);
343         info("daemon load count = [%d]", daemon_load_count);
344
345 #ifdef TIZEN_DEBUG_ENABLE
346         /* Signal Registration */
347         sigact.sa_handler = telephony_signal_handler;
348         sigemptyset(&sigact.sa_mask);
349         sigact.sa_flags = 0;
350         if (sigaction(SIGTERM, &sigact, NULL) < 0)
351                 warn("sigaction(SIGTERM) failed.");
352         if (sigaction(SIGUSR1, &sigact, NULL) < 0)
353                 warn("sigaction(SIGUSR1) failed.");
354 #if defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
355         /* SIGHUP should be ignored because cellular dongle ejection makes this signal */
356         if (sigaction(SIGHUP, &sigact, NULL) < 0)
357                 warn("sigaction(SIGHUP) failed.");
358 #endif
359 #elif defined(TIZEN_PROFILE_TV) || defined(TIZEN_PROFILE_IVI)
360         /* Signal Registration */
361         sigact.sa_handler = telephony_signal_handler;
362         sigemptyset(&sigact.sa_mask);
363         sigact.sa_flags = 0;
364         /* SIGHUP should be ignored because cellular dongle ejection makes this signal */
365         if (sigaction(SIGHUP, &sigact, NULL) < 0)
366                 warn("sigaction(SIGHUP) failed.");
367 #endif
368
369         if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
370                 err("Child process won't be auto reaped: [%d]", errno);
371
372         /* Commandline option parser TODO: Replace with GOptionContext */
373         while (TRUE) {
374                 opt = getopt_long(argc, argv, "hT", options, &opt_index);
375
376                 if (-1 == opt)
377                         break;
378
379                 switch (opt) {
380                 case 0: {
381                         switch (opt_index) {
382                         case 0: {
383                                 __usage_info(argv[0]);
384                                 return 0;
385                         } break;
386                         default: {
387                                 warn("unhandled opt_index.");
388                         } break;
389                         } /* end switch */
390                 } break;
391
392                 case 'h': {
393                         __usage_info(argv[0]);
394                         return 0;
395                 } break;
396
397                 case 'T': {
398                         flag_test_load = TRUE;
399                 } break;
400                 default: {
401                         warn("unhandled opt case.");
402                 } break;
403                 } /* end switch */
404         }
405
406         if (optind < argc)
407                 plugin_path = argv[optind];
408
409         info("plugin_path: [%s]", plugin_path);
410
411 #if !GLIB_CHECK_VERSION(2, 35, 0)
412         g_type_init();
413 #endif
414 #if !GLIB_CHECK_VERSION(2, 31, 0)
415         g_thread_init(NULL);
416 #endif
417
418         s = tcore_server_new();
419         if (G_UNLIKELY(NULL == s)) {
420                 err("server_new failed.");
421                 ret_code = EXIT_FAILURE;
422                 goto END;
423         }
424         _server = s;
425
426         g_log_set_default_handler(glib_log, s);
427
428         /* Load Plugins */
429         if (G_UNLIKELY(FALSE == load_plugins(s, (const char *)plugin_path, flag_test_load))) {
430                 err("load_plugins failed.");
431                 ret_code = EXIT_FAILURE;
432                 goto END;
433         }
434
435         TIME_CHECK("Loading Plugins Complete");
436
437         if (flag_test_load) {
438                 ret_code = EXIT_SUCCESS;
439                 goto END;
440         }
441
442         /* Initialize Plugins */
443         if (G_UNLIKELY(FALSE == init_plugins(s))) {
444                 err("init_plugins failed.");
445                 ret_code = EXIT_FAILURE;
446                 goto END;
447         }
448
449         info("server mainloop start");
450         TIME_CHECK("Initializing Plugins Complete. Starting Daemon");
451
452         if (G_UNLIKELY(TCORE_RETURN_SUCCESS != tcore_server_run(s))) {
453                 err("server_run failed.");
454                 ret_code = EXIT_FAILURE;
455         }
456
457 END:
458         tcore_server_free(s); _server = NULL;
459         return ret_code;
460 }