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