Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / src / log.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <syslog.h>
33 #include <execinfo.h>
34 #include <dlfcn.h>
35
36 #include "connman.h"
37
38 static const char *program_exec;
39 static const char *program_path;
40
41 #if defined TIZEN_EXT
42 #include <sys/stat.h>
43
44 #define LOG_FILE_PATH "/opt/usr/data/network/connman.log"
45 #define MAX_LOG_SIZE    1 * 1024 * 1024
46 #define MAX_LOG_COUNT   3
47
48 #define openlog __connman_log_open
49 #define closelog __connman_log_close
50 #define vsyslog __connman_log
51 #define syslog __connman_log_s
52
53 static FILE *log_file = NULL;
54
55 void __connman_log_open(const char *ident, int option, int facility)
56 {
57         if (!log_file)
58                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
59 }
60
61 void __connman_log_close(void)
62 {
63         fclose(log_file);
64         log_file = NULL;
65 }
66
67 static void __connman_log_update_file_revision(int rev)
68 {
69         int next_log_rev = 0;
70         char *log_file = NULL;
71         char *next_log_file = NULL;
72
73         next_log_rev = rev + 1;
74
75         log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
76         next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
77
78         if (next_log_rev >= MAX_LOG_COUNT)
79                 remove(next_log_file);
80
81         if (access(next_log_file, F_OK) == 0)
82                 __connman_log_update_file_revision(next_log_rev);
83
84         if (rename(log_file, next_log_file) != 0)
85                 remove(log_file);
86
87         g_free(log_file);
88         g_free(next_log_file);
89 }
90
91 static void __connman_log_make_backup(void)
92 {
93         const int rev = 0;
94         char *backup = NULL;
95
96         backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
97
98         if (access(backup, F_OK) == 0)
99                 __connman_log_update_file_revision(rev);
100
101         if (rename(LOG_FILE_PATH, backup) != 0)
102                 remove(LOG_FILE_PATH);
103
104         g_free(backup);
105 }
106
107 static void __connman_log_get_local_time(char *strtime, const int size)
108 {
109         time_t buf;
110         struct tm *local_ptm;
111
112         time(&buf);
113         buf = time(NULL);
114         local_ptm = localtime(&buf);
115
116         strftime(strtime, size, "%m/%d %H:%M:%S", local_ptm);
117 }
118
119 void __connman_log(const int log_priority, const char *format, va_list ap)
120 {
121         int log_size = 0;
122         struct stat buf;
123         char str[256];
124         char strtime[40];
125
126         if (!log_file)
127                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
128
129         if (!log_file)
130                 return;
131
132         fstat(fileno(log_file), &buf);
133         log_size = buf.st_size;
134
135         if (log_size >= MAX_LOG_SIZE) {
136                 fclose(log_file);
137                 log_file = NULL;
138
139                 __connman_log_make_backup();
140
141                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
142
143                 if (!log_file)
144                         return;
145         }
146
147         __connman_log_get_local_time(strtime, sizeof(strtime));
148
149         if (vsnprintf(str, sizeof(str), format, ap) > 0)
150                 fprintf(log_file, "%s %s\n", strtime, str);
151 }
152
153 void __connman_log_s(int log_priority, const char *format, ...)
154 {
155         va_list ap;
156
157         va_start(ap, format);
158
159         vsyslog(LOG_DEBUG, format, ap);
160
161         va_end(ap);
162 }
163 #endif
164
165 /**
166  * connman_info:
167  * @format: format string
168  * @Varargs: list of arguments
169  *
170  * Output general information
171  */
172 void connman_info(const char *format, ...)
173 {
174         va_list ap;
175
176         va_start(ap, format);
177
178         vsyslog(LOG_INFO, format, ap);
179
180         va_end(ap);
181 }
182
183 /**
184  * connman_warn:
185  * @format: format string
186  * @Varargs: list of arguments
187  *
188  * Output warning messages
189  */
190 void connman_warn(const char *format, ...)
191 {
192         va_list ap;
193
194         va_start(ap, format);
195
196         vsyslog(LOG_WARNING, format, ap);
197
198         va_end(ap);
199 }
200
201 /**
202  * connman_error:
203  * @format: format string
204  * @varargs: list of arguments
205  *
206  * Output error messages
207  */
208 void connman_error(const char *format, ...)
209 {
210         va_list ap;
211
212         va_start(ap, format);
213
214         vsyslog(LOG_ERR, format, ap);
215
216         va_end(ap);
217 }
218
219 /**
220  * connman_debug:
221  * @format: format string
222  * @varargs: list of arguments
223  *
224  * Output debug message
225  */
226 void connman_debug(const char *format, ...)
227 {
228         va_list ap;
229
230         va_start(ap, format);
231
232         vsyslog(LOG_DEBUG, format, ap);
233
234         va_end(ap);
235 }
236
237 static void print_backtrace(unsigned int offset)
238 {
239         void *frames[99];
240         size_t n_ptrs;
241         unsigned int i;
242         int outfd[2], infd[2];
243         int pathlen;
244         pid_t pid;
245
246         if (!program_exec)
247                 return;
248
249         pathlen = strlen(program_path);
250
251         n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
252         if (n_ptrs < offset)
253                 return;
254
255         if (pipe(outfd) < 0)
256                 return;
257
258         if (pipe(infd) < 0) {
259                 close(outfd[0]);
260                 close(outfd[1]);
261                 return;
262         }
263
264         pid = fork();
265         if (pid < 0) {
266                 close(outfd[0]);
267                 close(outfd[1]);
268                 close(infd[0]);
269                 close(infd[1]);
270                 return;
271         }
272
273         if (pid == 0) {
274                 close(outfd[1]);
275                 close(infd[0]);
276
277                 dup2(outfd[0], STDIN_FILENO);
278                 dup2(infd[1], STDOUT_FILENO);
279
280                 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
281
282                 exit(EXIT_FAILURE);
283         }
284
285         close(outfd[0]);
286         close(infd[1]);
287
288         connman_error("++++++++ backtrace ++++++++");
289
290         for (i = offset; i < n_ptrs - 1; i++) {
291                 Dl_info info;
292                 char addr[20], buf[PATH_MAX * 2];
293                 int len, written;
294                 char *ptr, *pos;
295
296                 dladdr(frames[i], &info);
297
298                 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
299                 if (len < 0)
300                         break;
301
302                 written = write(outfd[1], addr, len);
303                 if (written < 0)
304                         break;
305
306                 len = read(infd[0], buf, sizeof(buf) - 1);
307                 if (len < 0)
308                         break;
309
310                 buf[len] = '\0';
311
312                 pos = strchr(buf, '\n');
313                 *pos++ = '\0';
314
315                 if (strcmp(buf, "??") == 0) {
316                         connman_error("#%-2u %p in %s", i - offset,
317                                                 frames[i], info.dli_fname);
318                         continue;
319                 }
320
321                 ptr = strchr(pos, '\n');
322                 *ptr++ = '\0';
323
324                 if (strncmp(pos, program_path, pathlen) == 0)
325                         pos += pathlen + 1;
326
327                 connman_error("#%-2u %p in %s() at %s", i - offset,
328                                                 frames[i], buf, pos);
329         }
330
331         connman_error("+++++++++++++++++++++++++++");
332
333         kill(pid, SIGTERM);
334
335         close(outfd[1]);
336         close(infd[0]);
337 }
338
339 static void signal_handler(int signo)
340 {
341         connman_error("Aborting (signal %d) [%s]", signo, program_exec);
342
343         print_backtrace(2);
344
345         exit(EXIT_FAILURE);
346 }
347
348 static void signal_setup(sighandler_t handler)
349 {
350         struct sigaction sa;
351         sigset_t mask;
352
353         sigemptyset(&mask);
354         sa.sa_handler = handler;
355         sa.sa_mask = mask;
356         sa.sa_flags = 0;
357         sigaction(SIGBUS, &sa, NULL);
358         sigaction(SIGILL, &sa, NULL);
359         sigaction(SIGFPE, &sa, NULL);
360         sigaction(SIGSEGV, &sa, NULL);
361         sigaction(SIGABRT, &sa, NULL);
362         sigaction(SIGPIPE, &sa, NULL);
363 }
364
365 extern struct connman_debug_desc __start___debug[];
366 extern struct connman_debug_desc __stop___debug[];
367
368 static gchar **enabled = NULL;
369
370 static bool is_enabled(struct connman_debug_desc *desc)
371 {
372         int i;
373
374         if (!enabled)
375                 return false;
376
377         for (i = 0; enabled[i]; i++) {
378                 if (desc->name && g_pattern_match_simple(enabled[i],
379                                                         desc->name))
380                         return true;
381                 if (desc->file && g_pattern_match_simple(enabled[i],
382                                                         desc->file))
383                         return true;
384         }
385
386         return false;
387 }
388
389 void __connman_log_enable(struct connman_debug_desc *start,
390                                         struct connman_debug_desc *stop)
391 {
392         struct connman_debug_desc *desc;
393         const char *name = NULL, *file = NULL;
394
395         if (!start || !stop)
396                 return;
397
398         for (desc = start; desc < stop; desc++) {
399                 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
400                         file = desc->file;
401                         name = desc->name;
402                         continue;
403                 }
404
405                 if (file || name) {
406                         if (g_strcmp0(desc->file, file) == 0) {
407                                 if (!desc->name)
408                                         desc->name = name;
409                         } else
410                                 file = NULL;
411                 }
412
413                 if (is_enabled(desc))
414                         desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
415         }
416 }
417
418 int __connman_log_init(const char *program, const char *debug,
419                 gboolean detach, gboolean backtrace,
420                 const char *program_name, const char *program_version)
421 {
422         static char path[PATH_MAX];
423         int option = LOG_NDELAY | LOG_PID;
424
425         program_exec = program;
426         program_path = getcwd(path, sizeof(path));
427
428         if (debug)
429                 enabled = g_strsplit_set(debug, ":, ", 0);
430
431         __connman_log_enable(__start___debug, __stop___debug);
432
433         if (!detach)
434                 option |= LOG_PERROR;
435
436         if (backtrace)
437                 signal_setup(signal_handler);
438
439         openlog(basename(program), option, LOG_DAEMON);
440
441         syslog(LOG_INFO, "%s version %s", program_name, program_version);
442
443         return 0;
444 }
445
446 void __connman_log_cleanup(gboolean backtrace)
447 {
448         syslog(LOG_INFO, "Exit");
449
450         closelog();
451
452         if (backtrace)
453                 signal_setup(SIG_DFL);
454
455         g_strfreev(enabled);
456 }