wifi: Fix tethering with kernel 3.5
[profile/ivi/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 #if !defined TIZEN_EXT
114 static void print_backtrace(unsigned int offset)
115 {
116         void *frames[99];
117         size_t n_ptrs;
118         unsigned int i;
119         int outfd[2], infd[2];
120         int pathlen;
121         pid_t pid;
122
123         if (program_exec == NULL)
124                 return;
125
126         pathlen = strlen(program_path);
127
128         n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
129         if (n_ptrs < offset)
130                 return;
131
132         if (pipe(outfd) < 0)
133                 return;
134
135         if (pipe(infd) < 0) {
136                 close(outfd[0]);
137                 close(outfd[1]);
138                 return;
139         }
140
141         pid = fork();
142         if (pid < 0) {
143                 close(outfd[0]);
144                 close(outfd[1]);
145                 close(infd[0]);
146                 close(infd[1]);
147                 return;
148         }
149
150         if (pid == 0) {
151                 close(outfd[1]);
152                 close(infd[0]);
153
154                 dup2(outfd[0], STDIN_FILENO);
155                 dup2(infd[1], STDOUT_FILENO);
156
157                 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
158
159                 exit(EXIT_FAILURE);
160         }
161
162         close(outfd[0]);
163         close(infd[1]);
164
165         connman_error("++++++++ backtrace ++++++++");
166
167         for (i = offset; i < n_ptrs - 1; i++) {
168                 Dl_info info;
169                 char addr[20], buf[PATH_MAX * 2];
170                 int len, written;
171                 char *ptr, *pos;
172
173                 dladdr(frames[i], &info);
174
175                 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
176                 if (len < 0)
177                         break;
178
179                 written = write(outfd[1], addr, len);
180                 if (written < 0)
181                         break;
182
183                 len = read(infd[0], buf, sizeof(buf));
184                 if (len < 0)
185                         break;
186
187                 buf[len] = '\0';
188
189                 pos = strchr(buf, '\n');
190                 *pos++ = '\0';
191
192                 if (strcmp(buf, "??") == 0) {
193                         connman_error("#%-2u %p in %s", i - offset,
194                                                 frames[i], info.dli_fname);
195                         continue;
196                 }
197
198                 ptr = strchr(pos, '\n');
199                 *ptr++ = '\0';
200
201                 if (strncmp(pos, program_path, pathlen) == 0)
202                         pos += pathlen + 1;
203
204                 connman_error("#%-2u %p in %s() at %s", i - offset,
205                                                 frames[i], buf, pos);
206         }
207
208         connman_error("+++++++++++++++++++++++++++");
209
210         kill(pid, SIGTERM);
211
212         close(outfd[1]);
213         close(infd[0]);
214 }
215
216 static void signal_handler(int signo)
217 {
218         connman_error("Aborting (signal %d) [%s]", signo, program_exec);
219
220         print_backtrace(2);
221
222         exit(EXIT_FAILURE);
223 }
224
225 static void signal_setup(sighandler_t handler)
226 {
227         struct sigaction sa;
228         sigset_t mask;
229
230         sigemptyset(&mask);
231         sa.sa_handler = handler;
232         sa.sa_mask = mask;
233         sa.sa_flags = 0;
234         sigaction(SIGBUS, &sa, NULL);
235         sigaction(SIGILL, &sa, NULL);
236         sigaction(SIGFPE, &sa, NULL);
237         sigaction(SIGSEGV, &sa, NULL);
238         sigaction(SIGABRT, &sa, NULL);
239         sigaction(SIGPIPE, &sa, NULL);
240 }
241 #endif
242
243 extern struct connman_debug_desc __start___debug[];
244 extern struct connman_debug_desc __stop___debug[];
245
246 static gchar **enabled = NULL;
247
248 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
249 {
250         int i;
251
252         if (enabled == NULL)
253                 return FALSE;
254
255         for (i = 0; enabled[i] != NULL; i++) {
256                 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
257                                                         desc->name) == TRUE)
258                         return TRUE;
259                 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
260                                                         desc->file) == TRUE)
261                         return TRUE;
262         }
263
264         return FALSE;
265 }
266
267 void __connman_log_enable(struct connman_debug_desc *start,
268                                         struct connman_debug_desc *stop)
269 {
270         struct connman_debug_desc *desc;
271         const char *name = NULL, *file = NULL;
272
273         if (start == NULL || stop == NULL)
274                 return;
275
276         for (desc = start; desc < stop; desc++) {
277                 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
278                         file = desc->file;
279                         name = desc->name;
280                         continue;
281                 }
282
283                 if (file != NULL || name != NULL) {
284                         if (g_strcmp0(desc->file, file) == 0) {
285                                 if (desc->name == NULL)
286                                         desc->name = name;
287                         } else
288                                 file = NULL;
289                 }
290
291                 if (is_enabled(desc) == TRUE)
292                         desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
293         }
294 }
295
296 int __connman_log_init(const char *program, const char *debug,
297                                                 connman_bool_t detach)
298 {
299         static char path[PATH_MAX];
300         int option = LOG_NDELAY | LOG_PID;
301
302         program_exec = program;
303         program_path = getcwd(path, sizeof(path));
304
305         if (debug != NULL)
306                 enabled = g_strsplit_set(debug, ":, ", 0);
307
308         __connman_log_enable(__start___debug, __stop___debug);
309
310         if (detach == FALSE)
311                 option |= LOG_PERROR;
312
313 #if !defined TIZEN_EXT
314         signal_setup(signal_handler);
315 #endif
316
317         openlog(basename(program), option, LOG_DAEMON);
318
319         syslog(LOG_INFO, "Connection Manager version %s", VERSION);
320
321         return 0;
322 }
323
324 void __connman_log_cleanup(void)
325 {
326         syslog(LOG_INFO, "Exit");
327
328         closelog();
329
330 #if !defined TIZEN_EXT
331         signal_setup(SIG_DFL);
332 #endif
333
334         g_strfreev(enabled);
335 }