/
trace.c
291 lines (237 loc) · 6.34 KB
/
trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "trace.h"
#include "buffer.h"
#include "util.h"
#include "bogotime.h"
#include "access.h"
#include "socket.h"
#include "connection-protected.h"
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
#endif
typedef struct {
cherokee_buffer_t modules;
cherokee_boolean_t use_syslog;
cherokee_boolean_t print_time;
cherokee_boolean_t print_thread;
cherokee_access_t *from_filter;
} cherokee_trace_t;
static cherokee_trace_t trace = {
CHEROKEE_BUF_INIT,
false,
false,
false,
NULL
};
/* cherokee_trace_do_trace() calls complex functions that might do
* some tracing as well, and that would generate a loop. This flag is
* used to disable the tracing while a previous trace call is ongoin.
*/
static cherokee_boolean_t disabled = false;
ret_t
cherokee_trace_init (void)
{
const char *env;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
/* Read the environment variable
*/
env = getenv (TRACE_ENV);
if (env == NULL)
return ret_ok;
/* Set it up
*/
cherokee_buffer_fake (&tmp, env, strlen(env));
cherokee_trace_set_modules (&tmp);
return ret_ok;
}
ret_t
cherokee_trace_set_modules (cherokee_buffer_t *modules)
{
ret_t ret;
char *p;
char *end;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
/* Store a copy of the modules
*/
cherokee_buffer_clean (&trace.modules);
if (trace.from_filter != NULL) {
cherokee_access_free (trace.from_filter);
}
trace.from_filter = NULL;
if (cherokee_buffer_case_cmp_str (modules, "none") != 0) {
cherokee_buffer_add_buffer (&trace.modules, modules);
}
/* Check the special properties
*/
trace.use_syslog = (strstr (modules->buf, "syslog") != NULL);
trace.print_time = (strstr (modules->buf, "time") != NULL);
trace.print_thread = (strstr (modules->buf, "thread") != NULL);
/* Even a more special 'from' property
*/
p = strstr (modules->buf, "from=");
if (p != NULL) {
p += 5;
end = strchr(p, ',');
if (end == NULL) {
end = p + strlen(p);
}
if (end > p) {
ret = cherokee_access_new (&trace.from_filter);
if (ret != ret_ok) return ret_error;
cherokee_buffer_add (&tmp, p, end-p);
ret = cherokee_access_add (trace.from_filter, tmp.buf);
if (ret != ret_ok) {
ret = ret_error;
goto out;
}
}
}
ret = ret_ok;
out:
cherokee_buffer_mrproper (&tmp);
return ret;
}
void
cherokee_trace_do_trace (const char *entry, const char *file, int line, const char *func, const char *fmt, ...)
{
ret_t ret;
char *p;
char *lentry;
char *lentry_end;
va_list args;
cherokee_connection_t *conn;
cherokee_buffer_t *trace_modules = &trace.modules;
cherokee_boolean_t do_log = false;
cherokee_buffer_t entries = CHEROKEE_BUF_INIT;
/* Prevents loops
*/
if (disabled) {
return;
}
disabled = true;
/* Return ASAP if nothing is being traced
*/
if (cherokee_buffer_is_empty (&trace.modules)) {
goto out;
}
/* Check the connection source, if possible
*/
if (trace.from_filter != NULL) {
conn = CONN (CHEROKEE_THREAD_PROP_GET (thread_connection_ptr));
/* No conn, no trace entry
*/
if (conn == NULL) {
goto out;
}
if (conn->socket.socket < 0) {
goto out;
}
/* Skip the trace if the conn doesn't match
*/
ret = cherokee_access_ip_match (trace.from_filter, &conn->socket);
if (ret != ret_ok) {
goto out;
}
}
/* Also, check for 'all'
*/
p = strstr (trace_modules->buf, "all");
if (p == NULL) {
/* Parse the In-code module string
*/
cherokee_buffer_add (&entries, entry, strlen(entry));
for (lentry = entries.buf;;) {
lentry_end = strchr (lentry, ',');
if (lentry_end) *lentry_end = '\0';
/* Check the type
*/
p = strstr (trace_modules->buf, lentry);
if (p) {
char *tmp = p + strlen(lentry);
if ((*tmp == '\0') || (*tmp == ',') || (*tmp == ' '))
do_log = true;
}
if ((lentry_end == NULL) || (do_log))
break;
lentry = lentry_end + 1;
}
/* Return if trace entry didn't match with the configured list
*/
if (! do_log) {
goto out;
}
}
/* Format the message and log it:
* 'entries' is not needed at this stage, reuse it
*/
cherokee_buffer_clean (&entries);
if (trace.print_thread) {
int len;
char tmp[32+1];
static int longest_len = 0;
len = snprintf (tmp, 32+1, "%llX", (unsigned long long) CHEROKEE_THREAD_SELF);
longest_len = MAX (longest_len, len);
cherokee_buffer_add_str (&entries, "{0x");
cherokee_buffer_add_char_n (&entries, '0', longest_len - len);
cherokee_buffer_add (&entries, tmp, len);
cherokee_buffer_add_str (&entries, "} ");
}
if (trace.print_time) {
cherokee_buffer_add_char (&entries, '[');
cherokee_buf_add_bogonow (&entries, true);
cherokee_buffer_add_str (&entries, "] ");
}
cherokee_buffer_add_va (&entries, "%18s:%04d (%30s): ", file, line, func);
va_start (args, fmt);
cherokee_buffer_add_va_list (&entries, (char *)fmt, args);
va_end (args);
if (trace.use_syslog) {
cherokee_syslog (LOG_DEBUG, &entries);
} else {
#ifdef HAVE_FLOCKFILE
flockfile (stdout);
#endif
fprintf (stdout, "%s", entries.buf);
#ifdef HAVE_FUNLOCKFILE
funlockfile (stdout);
#endif
}
out:
cherokee_buffer_mrproper (&entries);
disabled = false;
}
ret_t
cherokee_trace_get_trace (cherokee_buffer_t **buf_ref)
{
if (buf_ref == NULL)
return ret_not_found;
*buf_ref = &trace.modules;
return ret_ok;
}
int
cherokee_trace_is_tracing (void)
{
return (! cherokee_buffer_is_empty (&trace.modules));
}