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