dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / src / log.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 /**
42  * connman_info:
43  * @format: format string
44  * @Varargs: list of arguments
45  *
46  * Output general information
47  */
48 void connman_info(const char *format, ...)
49 {
50         va_list ap;
51
52         va_start(ap, format);
53
54         vsyslog(LOG_INFO, format, ap);
55
56         va_end(ap);
57 }
58
59 /**
60  * connman_warn:
61  * @format: format string
62  * @Varargs: list of arguments
63  *
64  * Output warning messages
65  */
66 void connman_warn(const char *format, ...)
67 {
68         va_list ap;
69
70         va_start(ap, format);
71
72         vsyslog(LOG_WARNING, format, ap);
73
74         va_end(ap);
75 }
76
77 /**
78  * connman_error:
79  * @format: format string
80  * @varargs: list of arguments
81  *
82  * Output error messages
83  */
84 void connman_error(const char *format, ...)
85 {
86         va_list ap;
87
88         va_start(ap, format);
89
90         vsyslog(LOG_ERR, format, ap);
91
92         va_end(ap);
93 }
94
95 /**
96  * connman_debug:
97  * @format: format string
98  * @varargs: list of arguments
99  *
100  * Output debug message
101  */
102 void connman_debug(const char *format, ...)
103 {
104         va_list ap;
105
106         va_start(ap, format);
107
108         vsyslog(LOG_DEBUG, format, ap);
109
110         va_end(ap);
111 }
112
113 static void print_backtrace(unsigned int offset)
114 {
115         void *frames[99];
116         size_t n_ptrs;
117         unsigned int i;
118         int outfd[2], infd[2];
119         int pathlen;
120         pid_t pid;
121
122         if (program_exec == NULL)
123                 return;
124
125         pathlen = strlen(program_path);
126
127         n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
128         if (n_ptrs < offset)
129                 return;
130
131         if (pipe(outfd) < 0)
132                 return;
133
134         if (pipe(infd) < 0) {
135                 close(outfd[0]);
136                 close(outfd[1]);
137                 return;
138         }
139
140         pid = fork();
141         if (pid < 0) {
142                 close(outfd[0]);
143                 close(outfd[1]);
144                 close(infd[0]);
145                 close(infd[1]);
146                 return;
147         }
148
149         if (pid == 0) {
150                 close(outfd[1]);
151                 close(infd[0]);
152
153                 dup2(outfd[0], STDIN_FILENO);
154                 dup2(infd[1], STDOUT_FILENO);
155
156                 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
157
158                 exit(EXIT_FAILURE);
159         }
160
161         close(outfd[0]);
162         close(infd[1]);
163
164         connman_error("++++++++ backtrace ++++++++");
165
166         for (i = offset; i < n_ptrs - 1; i++) {
167                 Dl_info info;
168                 char addr[20], buf[PATH_MAX * 2];
169                 int len, written;
170                 char *ptr, *pos;
171
172                 dladdr(frames[i], &info);
173
174                 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
175                 if (len < 0)
176                         break;
177
178                 written = write(outfd[1], addr, len);
179                 if (written < 0)
180                         break;
181
182                 len = read(infd[0], buf, sizeof(buf));
183                 if (len < 0)
184                         break;
185
186                 buf[len] = '\0';
187
188                 pos = strchr(buf, '\n');
189                 *pos++ = '\0';
190
191                 if (strcmp(buf, "??") == 0) {
192                         connman_error("#%-2u %p in %s", i - offset,
193                                                 frames[i], info.dli_fname);
194                         continue;
195                 }
196
197                 ptr = strchr(pos, '\n');
198                 *ptr++ = '\0';
199
200                 if (strncmp(pos, program_path, pathlen) == 0)
201                         pos += pathlen + 1;
202
203                 connman_error("#%-2u %p in %s() at %s", i - offset,
204                                                 frames[i], buf, pos);
205         }
206
207         connman_error("+++++++++++++++++++++++++++");
208
209         kill(pid, SIGTERM);
210
211         close(outfd[1]);
212         close(infd[0]);
213 }
214
215 static void signal_handler(int signo)
216 {
217         connman_error("Aborting (signal %d) [%s]", signo, program_exec);
218
219         print_backtrace(2);
220
221         exit(EXIT_FAILURE);
222 }
223
224 static void signal_setup(sighandler_t handler)
225 {
226         struct sigaction sa;
227         sigset_t mask;
228
229         sigemptyset(&mask);
230         sa.sa_handler = handler;
231         sa.sa_mask = mask;
232         sa.sa_flags = 0;
233         sigaction(SIGBUS, &sa, NULL);
234         sigaction(SIGILL, &sa, NULL);
235         sigaction(SIGFPE, &sa, NULL);
236         sigaction(SIGSEGV, &sa, NULL);
237         sigaction(SIGABRT, &sa, NULL);
238         sigaction(SIGPIPE, &sa, NULL);
239 }
240
241 extern struct connman_debug_desc __start___debug[];
242 extern struct connman_debug_desc __stop___debug[];
243
244 static gchar **enabled = NULL;
245
246 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
247 {
248         int i;
249
250         if (enabled == NULL)
251                 return FALSE;
252
253         for (i = 0; enabled[i] != NULL; i++) {
254                 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
255                                                         desc->name) == TRUE)
256                         return TRUE;
257                 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
258                                                         desc->file) == TRUE)
259                         return TRUE;
260         }
261
262         return FALSE;
263 }
264
265 void __connman_log_enable(struct connman_debug_desc *start,
266                                         struct connman_debug_desc *stop)
267 {
268         struct connman_debug_desc *desc;
269         const char *name = NULL, *file = NULL;
270
271         if (start == NULL || stop == NULL)
272                 return;
273
274         for (desc = start; desc < stop; desc++) {
275                 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
276                         file = desc->file;
277                         name = desc->name;
278                         continue;
279                 }
280
281                 if (file != NULL || name != NULL) {
282                         if (g_strcmp0(desc->file, file) == 0) {
283                                 if (desc->name == NULL)
284                                         desc->name = name;
285                         } else
286                                 file = NULL;
287                 }
288
289                 if (is_enabled(desc) == TRUE)
290                         desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
291         }
292 }
293
294 int __connman_log_init(const char *program, const char *debug,
295                                                 connman_bool_t detach)
296 {
297         static char path[PATH_MAX];
298         int option = LOG_NDELAY | LOG_PID;
299
300         program_exec = program;
301         program_path = getcwd(path, sizeof(path));
302
303         if (debug != NULL)
304                 enabled = g_strsplit_set(debug, ":, ", 0);
305
306         __connman_log_enable(__start___debug, __stop___debug);
307
308         if (detach == FALSE)
309                 option |= LOG_PERROR;
310
311         signal_setup(signal_handler);
312
313         openlog(basename(program), option, LOG_DAEMON);
314
315         syslog(LOG_INFO, "Connection Manager version %s", VERSION);
316
317         return 0;
318 }
319
320 void __connman_log_cleanup(void)
321 {
322         syslog(LOG_INFO, "Exit");
323
324         closelog();
325
326         signal_setup(SIG_DFL);
327
328         g_strfreev(enabled);
329 }