Permalink
Browse files

Enhances the CHEROKEE_TRACE mechanism by adding a new special 'from'

keyword. It allows to restrict the tracing to an IP or Subnet rather
than over all the server operations.

For instance: "CHEROKEE_TRACE=cache,from=10.0.0.0/8" will trace all
the cache related operation generated by connections opened from the
10.0.0.0/8 network. "CHEROKEE_TRACE=all,from=::1" enables every single
trace entries if the connection came from the localhost IPv6 IP.

git-svn-id: svn://cherokee-project.com/cherokee/trunk@6641 5dc97367-97f1-0310-9951-d761b3857238
  • Loading branch information...
1 parent 235add8 commit 4d7cf6425233fb9308785e67e8c4243c8168a007 @alobbs alobbs committed May 19, 2011
Showing with 155 additions and 55 deletions.
  1. +25 −12 cherokee/thread.c
  2. +3 −0 cherokee/threading.c
  3. +1 −0 cherokee/threading.h
  4. +125 −42 cherokee/trace.c
  5. +1 −1 cherokee/trace.h
View
@@ -174,6 +174,7 @@ cherokee_thread_new (cherokee_thread_t **thd,
/* Thread Local Storage
*/
CHEROKEE_THREAD_PROP_SET (thread_error_writer_ptr, NULL);
+ CHEROKEE_THREAD_PROP_SET (thread_connection_ptr, NULL);
/* Event poll object
*/
@@ -535,13 +536,19 @@ process_polling_connections (cherokee_thread_t *thd)
list_for_each_safe (i, tmp, LIST(&thd->polling_list)) {
conn = CONN(i);
- /* Thread's error logger
+ /* Thread's properties
*/
- if (CONN_VSRV(conn) &&
- CONN_VSRV(conn)->error_writer)
- {
- CHEROKEE_THREAD_PROP_SET (thread_error_writer_ptr,
- CONN_VSRV(conn)->error_writer);
+ if (CONN_VSRV(conn)) {
+ /* Current connection
+ */
+ CHEROKEE_THREAD_PROP_SET (thread_connection_ptr, conn);
+
+ /* Error writer
+ */
+ if (CONN_VSRV(conn)->error_writer) {
+ CHEROKEE_THREAD_PROP_SET (thread_error_writer_ptr,
+ CONN_VSRV(conn)->error_writer);
+ }
}
/* Has it been too much without any work?
@@ -668,13 +675,19 @@ process_active_connections (cherokee_thread_t *thd)
thd, conn, conn->phase, cherokee_connection_get_phase_str (conn),
conn->socket.socket, (conn->socket.status == socket_reading)? "read" : (conn->socket.status == socket_writing)? "writing" : "closed");
- /* Thread's error logger
+ /* Thread's properties
*/
- if (CONN_VSRV(conn) &&
- CONN_VSRV(conn)->error_writer)
- {
- CHEROKEE_THREAD_PROP_SET (thread_error_writer_ptr,
- CONN_VSRV(conn)->error_writer);
+ if (CONN_VSRV(conn)) {
+ /* Current connection
+ */
+ CHEROKEE_THREAD_PROP_SET (thread_connection_ptr, conn);
+
+ /* Error writer
+ */
+ if (CONN_VSRV(conn)->error_writer) {
+ CHEROKEE_THREAD_PROP_SET (thread_error_writer_ptr,
+ CONN_VSRV(conn)->error_writer);
+ }
}
/* Has the connection been too much time w/o any work
View
@@ -35,6 +35,7 @@ pthread_mutexattr_t cherokee_mutexattr_errorcheck;
/* Thread Local Storage variables */
#ifdef HAVE_PTHREAD
pthread_key_t thread_error_writer_ptr = 0;
+pthread_key_t thread_connection_ptr = 0;
#endif
@@ -62,6 +63,7 @@ cherokee_threading_init (void)
#ifdef HAVE_PTHREAD
pthread_key_create (&thread_error_writer_ptr, NULL);
+ pthread_key_create (&thread_connection_ptr, NULL);
#endif
return ret_ok;
@@ -80,6 +82,7 @@ cherokee_threading_free (void)
#ifdef HAVE_PTHREAD
pthread_key_delete (thread_error_writer_ptr);
+ pthread_key_delete (thread_connection_ptr);
#endif
return ret_ok;
View
@@ -46,6 +46,7 @@ extern pthread_mutexattr_t cherokee_mutexattr_errorcheck;
# endif
extern pthread_key_t thread_error_writer_ptr;
+extern pthread_key_t thread_connection_ptr;
/* Global if */
#endif
View
@@ -27,27 +27,39 @@
#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_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
+ 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)
{
@@ -72,10 +84,20 @@ cherokee_trace_init (void)
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);
}
@@ -86,61 +108,122 @@ cherokee_trace_set_modules (cherokee_buffer_t *modules)
trace.print_time = (strstr (modules->buf, "time") != NULL);
trace.print_thread = (strstr (modules->buf, "thread") != NULL);
- return ret_ok;
+ /* 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;
}
-ret_t
+void
cherokee_trace_do_trace (const char *entry, const char *file, int line, const char *func, const char *fmt, ...)
{
- char *p;
- char *lentry;
- char *lentry_end;
- va_list args;
- cherokee_buffer_t *trace_modules = &trace.modules;
- cherokee_boolean_t do_log = false;
- cherokee_buffer_t entries = CHEROKEE_BUF_INIT;
+ 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))
- return ret_ok;
+ if (cherokee_buffer_is_empty (&trace.modules)) {
+ goto out;
+ }
- /* Parse the In-code module string
+ /* Check the connection source, if possible
*/
- cherokee_buffer_add (&entries, entry, strlen(entry));
-
- for (lentry = entries.buf;;) {
- lentry_end = strchr (lentry, ',');
- if (lentry_end) *lentry_end = '\0';
+ if (trace.from_filter != NULL) {
+ conn = CONN (CHEROKEE_THREAD_PROP_GET (thread_connection_ptr));
- /* Check for 'all'
+ /* No conn, no trace entry
*/
- p = strstr (trace_modules->buf, "all");
- if (p) {
- do_log = true;
- break;
+ if (conn == NULL) {
+ goto out;
}
- /* 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 (conn->socket.socket < 0) {
+ goto out;
}
- if ((lentry_end == NULL) || (do_log))
- break;
-
- lentry = lentry_end + 1;
+ /* 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;
+ }
}
- /* Return if trace entry didn't match with the configured list
+ /* Also, check for 'all'
*/
- if (! do_log)
- goto out;
+ 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
@@ -186,7 +269,7 @@ cherokee_trace_do_trace (const char *entry, const char *file, int line, const ch
out:
cherokee_buffer_mrproper (&entries);
- return ret_ok;
+ disabled = false;
}
View
@@ -36,7 +36,7 @@ CHEROKEE_BEGIN_DECLS
ret_t cherokee_trace_init (void);
ret_t cherokee_trace_set_modules (cherokee_buffer_t *modules);
-ret_t cherokee_trace_do_trace (const char *entry, const char *file, int line, const char *func, const char *fmt, ...);
+void cherokee_trace_do_trace (const char *entry, const char *file, int line, const char *func, const char *fmt, ...);
ret_t cherokee_trace_get_trace (cherokee_buffer_t **buf_ref);
int cherokee_trace_is_tracing (void);

0 comments on commit 4d7cf64

Please sign in to comment.