summaryrefslogtreecommitdiffstatsabout
path: root/mod_log_sql.c
diff options
context:
space:
mode:
authorEdward Rudd <urkle@outoforder.cc>2004-01-20 16:27:35 (GMT)
committer Edward Rudd <urkle@outoforder.cc>2004-01-20 16:27:35 (GMT)
commit40f0c8fe04858acd724d6221dbf8a357259e5d6b (patch)
treec3effb1a7fa38e47152417830ab1c7480bddf703 /mod_log_sql.c
parent417afc1671669fc9ba79410546c6ddfe242f2f4a (diff)
split out version specific code
code compiles under apache 1.3 and 2.0 updated apache m4 script to detect both verions (two minumums) defaulted install to not activate module in configuration file (use make activate)
Diffstat (limited to 'mod_log_sql.c')
-rw-r--r--mod_log_sql.c331
1 files changed, 145 insertions, 186 deletions
diff --git a/mod_log_sql.c b/mod_log_sql.c
index 64896ab..02d5fd4 100644
--- a/mod_log_sql.c
+++ b/mod_log_sql.c
@@ -1,4 +1,4 @@
1/* $Header: /home/cvs/mod_log_sql/mod_log_sql.c,v 1.6 2004/01/04 03:23:21 urkle Exp $ */ 1/* $Header: /home/cvs/mod_log_sql/mod_log_sql.c,v 1.7 2004/01/20 16:27:34 urkle Exp $ */
2/* --------* 2/* --------*
3 * DEFINES * 3 * DEFINES *
4 * --------*/ 4 * --------*/
@@ -12,24 +12,16 @@
12/* ---------* 12/* ---------*
13 * INCLUDES * 13 * INCLUDES *
14 * ---------*/ 14 * ---------*/
15#include "mysql.h"
16#include "mysqld_error.h"
15 17
16#include "apr_strings.h" 18#if defined(WITH_APACHE20)
17#include "apr_lib.h" 19# include "apache20.h"
18#include "apr_hash.h" 20#elif defined(WITH_APACHE13)
19#include "apr_optional.h" 21# include "apache13.h"
20#define APR_WANT_STRFUNC 22#else
21#include "apr_want.h" 23# error Unsupported Apache version
22#include "apr_tables.h" 24#endif
23
24#include "ap_config.h"
25
26#include "httpd.h"
27#include "http_config.h"
28#include "http_core.h"
29#include "http_log.h"
30#include "http_protocol.h"
31
32#include "util_time.h"
33 25
34#ifdef HAVE_CONFIG_H 26#ifdef HAVE_CONFIG_H
35/* Undefine these to prevent conflicts between Apache ap_config_auto.h and 27/* Undefine these to prevent conflicts between Apache ap_config_auto.h and
@@ -51,8 +43,6 @@
51#include <limits.h> 43#include <limits.h>
52#endif 44#endif
53 45
54#include "mysql.h"
55#include "mysqld_error.h"
56 46
57#ifdef WANT_SSL_LOGGING 47#ifdef WANT_SSL_LOGGING
58#include "mod_ssl.h" 48#include "mod_ssl.h"
@@ -164,8 +154,11 @@ static const char *extract_remote_logname(request_rec *r, char *a)
164 154
165static const char *extract_remote_user(request_rec *r, char *a) 155static const char *extract_remote_user(request_rec *r, char *a)
166{ 156{
157 #ifdef WITH_APACHE13
158 char *rvalue = r->connection->user;
159 #else
167 char *rvalue = r->user; 160 char *rvalue = r->user;
168 161 #endif
169 if (rvalue == NULL) { 162 if (rvalue == NULL) {
170 rvalue = "-"; 163 rvalue = "-";
171 } else if (strlen(rvalue) == 0) { 164 } else if (strlen(rvalue) == 0) {
@@ -184,7 +177,7 @@ static const char *extract_ssl_keysize(request_rec *r, char *a)
184 if (myCtxConfig(scc,ssc) != NULL) { 177 if (myCtxConfig(scc,ssc) != NULL) {
185 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_USEKEYSIZE"); 178 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_USEKEYSIZE");
186 #ifdef DEBUG 179 #ifdef DEBUG
187 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"SSL_KEYSIZE: %s", result); 180 log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"SSL_KEYSIZE: %s", result);
188 #endif 181 #endif
189 if (result != NULL && result[0] == '\0') 182 if (result != NULL && result[0] == '\0')
190 result = NULL; 183 result = NULL;
@@ -203,7 +196,7 @@ static const char *extract_ssl_maxkeysize(request_rec *r, char *a)
203 if (myCtxConfig(scc,ssc) != NULL) { 196 if (myCtxConfig(scc,ssc) != NULL) {
204 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_ALGKEYSIZE"); 197 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_ALGKEYSIZE");
205 #ifdef DEBUG 198 #ifdef DEBUG
206 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"SSL_ALGKEYSIZE: %s", result); 199 log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"SSL_ALGKEYSIZE: %s", result);
207 #endif 200 #endif
208 if (result != NULL && result[0] == '\0') 201 if (result != NULL && result[0] == '\0')
209 result = NULL; 202 result = NULL;
@@ -222,7 +215,7 @@ static const char *extract_ssl_cipher(request_rec *r, char *a)
222 if (myCtxConfig(scc,ssc) != NULL) { 215 if (myCtxConfig(scc,ssc) != NULL) {
223 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER"); 216 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER");
224 #ifdef DEBUG 217 #ifdef DEBUG
225 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"SSL_CIPHER: %s", result); 218 log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"SSL_CIPHER: %s", result);
226 #endif 219 #endif
227 if (result != NULL && result[0] == '\0') 220 if (result != NULL && result[0] == '\0')
228 result = NULL; 221 result = NULL;
@@ -295,93 +288,12 @@ static const char *extract_header_out(request_rec *r, char *a)
295 return table_get(r->err_headers_out, a); 288 return table_get(r->err_headers_out, a);
296} 289}
297*/ 290*/
298static const char *extract_request_time_custom(request_rec *r, char *a,
299 apr_time_exp_t *xt)
300{
301 apr_size_t retcode;
302 char tstr[MAX_STRING_LEN];
303 apr_strftime(tstr, &retcode, sizeof(tstr), a, xt);
304 return apr_pstrdup(r->pool, tstr);
305}
306
307#define DEFAULT_REQUEST_TIME_SIZE 32
308typedef struct {
309 unsigned t;
310 char timestr[DEFAULT_REQUEST_TIME_SIZE];
311 unsigned t_validate;
312} cached_request_time;
313
314#define TIME_CACHE_SIZE 4
315#define TIME_CACHE_MASK 3
316static cached_request_time request_time_cache[TIME_CACHE_SIZE];
317
318static const char *extract_request_time(request_rec *r, char *a)
319{
320 apr_time_exp_t xt;
321 291
322 /* Please read comments in mod_log_config.h for more info about 292#if defined(WITH_APACHE13)
323 * the I_INSIST....COMPLIANCE define 293#include "functions13.h"
324 */ 294#elif defined(WITH_APACHE20)
325 if (a && *a) { /* Custom format */ 295#include "functions20.h"
326#ifdef I_INSIST_ON_EXTRA_CYCLES_FOR_CLF_COMPLIANCE
327 ap_explode_recent_localtime(&xt, apr_time_now());
328#else
329 ap_explode_recent_localtime(&xt, r->request_time);
330#endif
331 return extract_request_time_custom(r, a, &xt);
332 } else { /* CLF format */
333 /* This code uses the same technique as ap_explode_recent_localtime():
334 * optimistic caching with logic to detect and correct race conditions.
335 * See the comments in server/util_time.c for more information.
336 */
337 cached_request_time* cached_time = apr_palloc(r->pool,
338 sizeof(*cached_time));
339#ifdef I_INSIST_ON_EXTRA_CYCLES_FOR_CLF_COMPLIANCE
340 apr_time_t request_time = apr_time_now();
341#else
342 apr_time_t request_time = r->request_time;
343#endif 296#endif
344 unsigned t_seconds = (unsigned)apr_time_sec(request_time);
345 unsigned i = t_seconds & TIME_CACHE_MASK;
346 memcpy(cached_time, &(request_time_cache[i]), sizeof(*cached_time));
347 if ((t_seconds != cached_time->t) ||
348 (t_seconds != cached_time->t_validate)) {
349
350 /* Invalid or old snapshot, so compute the proper time string
351 * and store it in the cache
352 */
353 char sign;
354 int timz;
355
356 ap_explode_recent_localtime(&xt, r->request_time);
357 timz = xt.tm_gmtoff;
358 if (timz < 0) {
359 timz = -timz;
360 sign = '-';
361 }
362 else {
363 sign = '+';
364 }
365 cached_time->t = t_seconds;
366 apr_snprintf(cached_time->timestr, DEFAULT_REQUEST_TIME_SIZE,
367 "[%02d/%s/%d:%02d:%02d:%02d %c%.2d%.2d]",
368 xt.tm_mday, apr_month_snames[xt.tm_mon],
369 xt.tm_year+1900, xt.tm_hour, xt.tm_min, xt.tm_sec,
370 sign, timz / (60*60), timz % (60*60));
371 cached_time->t_validate = t_seconds;
372 memcpy(&(request_time_cache[i]), cached_time,
373 sizeof(*cached_time));
374 }
375 return cached_time->timestr;
376 }
377}
378
379static const char *extract_request_duration(request_rec *r, char *a)
380{
381 apr_time_t duration = apr_time_now() - r->request_time;
382 return apr_psprintf(r->pool, "%" APR_TIME_T_FMT, apr_time_sec(duration));
383}
384
385static const char *extract_virtual_host(request_rec *r, char *a) 297static const char *extract_virtual_host(request_rec *r, char *a)
386{ 298{
387 return apr_pstrdup(r->pool, r->server->server_hostname); 299 return apr_pstrdup(r->pool, r->server->server_hostname);
@@ -456,7 +368,7 @@ static const char *extract_cookie(request_rec *r, char *a)
456 368
457 if (cls->cookie_name != NULL) { 369 if (cls->cookie_name != NULL) {
458 #ifdef DEBUG 370 #ifdef DEBUG
459 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0, r, 371 log_error(APLOG_MARK,APLOG_DEBUG, r->server,
460 "watching for cookie '%s'", cls->cookie_name); 372 "watching for cookie '%s'", cls->cookie_name);
461 #endif 373 #endif
462 374
@@ -464,7 +376,7 @@ static const char *extract_cookie(request_rec *r, char *a)
464 cookiestr = (char *)apr_table_get(r->headers_in, "cookie2"); 376 cookiestr = (char *)apr_table_get(r->headers_in, "cookie2");
465 if (cookiestr != NULL) { 377 if (cookiestr != NULL) {
466 #ifdef DEBUG 378 #ifdef DEBUG
467 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0, r, 379 log_error(APLOG_MARK,APLOG_DEBUG, r->server,
468 "Cookie2: [%s]", cookiestr); 380 "Cookie2: [%s]", cookiestr);
469 #endif 381 #endif
470 /* Does the cookie string contain one with our name? */ 382 /* Does the cookie string contain one with our name? */
@@ -486,7 +398,7 @@ static const char *extract_cookie(request_rec *r, char *a)
486 cookiestr = (char *)apr_table_get(r->headers_in, "cookie"); 398 cookiestr = (char *)apr_table_get(r->headers_in, "cookie");
487 if (cookiestr != NULL) { 399 if (cookiestr != NULL) {
488 #ifdef DEBUG 400 #ifdef DEBUG
489 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r, 401 log_error(APLOG_MARK,APLOG_DEBUG,r->server,
490 "Cookie: [%s]", cookiestr); 402 "Cookie: [%s]", cookiestr);
491 #endif 403 #endif
492 isvalid = strstr(cookiestr, cls->cookie_name); 404 isvalid = strstr(cookiestr, cls->cookie_name);
@@ -503,7 +415,7 @@ static const char *extract_cookie(request_rec *r, char *a)
503 cookiestr = apr_table_get(r->headers_out, "set-cookie"); 415 cookiestr = apr_table_get(r->headers_out, "set-cookie");
504 if (cookiestr != NULL) { 416 if (cookiestr != NULL) {
505 #ifdef DEBUG 417 #ifdef DEBUG
506 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r, 418 log_error(APLOG_MARK,APLOG_DEBUG,r->server,
507 "Set-Cookie: [%s]", cookiestr); 419 "Set-Cookie: [%s]", cookiestr);
508 #endif 420 #endif
509 isvalid = strstr(cookiestr, cls->cookie_name); 421 isvalid = strstr(cookiestr, cls->cookie_name);
@@ -530,15 +442,15 @@ static const char *extract_specific_cookie(request_rec *r, char *a)
530 442
531 if (a != NULL) { 443 if (a != NULL) {
532 #ifdef DEBUG 444 #ifdef DEBUG
533 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0, 445 log_error(APLOG_MARK,APLOG_DEBUG,
534 r,"watching for cookie '%s'", a); 446 r->server,"watching for cookie '%s'", a);
535 #endif 447 #endif
536 448
537 /* Fetch out the cookie header */ 449 /* Fetch out the cookie header */
538 cookiestr = (char *)apr_table_get(r->headers_in, "cookie2"); 450 cookiestr = (char *)apr_table_get(r->headers_in, "cookie2");
539 if (cookiestr != NULL) { 451 if (cookiestr != NULL) {
540 #ifdef DEBUG 452 #ifdef DEBUG
541 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r, 453 log_error(APLOG_MARK,APLOG_DEBUG,r->server,
542 "Cookie2: [%s]", cookiestr); 454 "Cookie2: [%s]", cookiestr);
543 #endif 455 #endif
544 /* Does the cookie string contain one with our name? */ 456 /* Does the cookie string contain one with our name? */
@@ -560,7 +472,7 @@ static const char *extract_specific_cookie(request_rec *r, char *a)
560 cookiestr = (char *)apr_table_get(r->headers_in, "cookie"); 472 cookiestr = (char *)apr_table_get(r->headers_in, "cookie");
561 if (cookiestr != NULL) { 473 if (cookiestr != NULL) {
562 #ifdef DEBUG 474 #ifdef DEBUG
563 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r, 475 log_error(APLOG_MARK,APLOG_DEBUG,r->server,
564 "Cookie: [%s]", cookiestr); 476 "Cookie: [%s]", cookiestr);
565 #endif 477 #endif
566 isvalid = strstr(cookiestr, a); 478 isvalid = strstr(cookiestr, a);
@@ -577,7 +489,7 @@ static const char *extract_specific_cookie(request_rec *r, char *a)
577 cookiestr = apr_table_get(r->headers_out, "set-cookie"); 489 cookiestr = apr_table_get(r->headers_out, "set-cookie");
578 if (cookiestr != NULL) { 490 if (cookiestr != NULL) {
579 #ifdef DEBUG 491 #ifdef DEBUG
580 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r, 492 log_error(APLOG_MARK,APLOG_DEBUG,r->server,
581 "Set-Cookie: [%s]", cookiestr); 493 "Set-Cookie: [%s]", cookiestr);
582 #endif 494 #endif
583 isvalid = strstr(cookiestr, a); 495 isvalid = strstr(cookiestr, a);
@@ -596,11 +508,6 @@ static const char *extract_specific_cookie(request_rec *r, char *a)
596} 508}
597 509
598 510
599static const char *extract_request_timestamp(request_rec *r, char *a)
600{
601 return apr_psprintf(r->pool, "%"APR_TIME_T_FMT, apr_time_sec(apr_time_now()));
602}
603
604/* 511/*
605static const char *extract_note(request_rec *r, char *a) 512static const char *extract_note(request_rec *r, char *a)
606{ 513{
@@ -730,20 +637,20 @@ static int open_logdb_link(server_rec* s)
730 637
731 if (global_config.server_p) { 638 if (global_config.server_p) {
732 #ifdef DEBUG 639 #ifdef DEBUG
733 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'", 640 log_error(APLOG_MARK,APLOG_DEBUG,s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'",
734 global_config.dbhost, global_config.tcpport, global_config.dbname, global_config.dbuser, global_config.socketfile); 641 global_config.dbhost, global_config.tcpport, global_config.dbname, global_config.dbuser, global_config.socketfile);
735 #endif 642 #endif
736 return 1; 643 return 1;
737 } else { 644 } else {
738 #ifdef DEBUG 645 #ifdef DEBUG
739 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,s,"mod_log_sql: database connection error: %s",MYSQL_ERROR(&global_config.server)); 646 log_error(APLOG_MARK,APLOG_DEBUG,s,"mod_log_sql: database connection error: %s",MYSQL_ERROR(&global_config.server));
740 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'", 647 log_error(APLOG_MARK,APLOG_DEBUG,s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'",
741 global_config.dbhost, global_config.tcpport, global_config.dbname, global_config.dbuser, global_config.socketfile); 648 global_config.dbhost, global_config.tcpport, global_config.dbname, global_config.dbuser, global_config.socketfile);
742 #endif 649 #endif
743 return 0; 650 return 0;
744 } 651 }
745 } else { 652 } else {
746 ap_log_error(APLOG_MARK,APLOG_ERR,0,s,"mod_log_sql: insufficient configuration info to establish database link"); 653 log_error(APLOG_MARK,APLOG_ERR,s,"mod_log_sql: insufficient configuration info to establish database link");
747 return 0; 654 return 0;
748 } 655 }
749} 656}
@@ -757,17 +664,30 @@ static int open_logdb_link(server_rec* s)
757 664
758static void preserve_entry(request_rec *r, const char *query) 665static void preserve_entry(request_rec *r, const char *query)
759{ 666{
760 apr_file_t *fp;
761 logsql_state *cls = ap_get_module_config(r->server->module_config, 667 logsql_state *cls = ap_get_module_config(r->server->module_config,
762 &log_sql_module); 668 &log_sql_module);
763 669 #if defined(WITH_APACHE20)
764 if (apr_file_open(&fp, cls->preserve_file,APR_APPEND, APR_OS_DEFAULT, r->pool)!=APR_SUCCESS) { 670 apr_file_t *fp;
765 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: attempted append of local preserve file but failed."); 671 apr_status_t result;
672 result = apr_file_open(&fp, cls->preserve_file,APR_APPEND, APR_OS_DEFAULT, r->pool);
673 #elif defined(WITH_APACHE13)
674 FILE *fp;
675 int result;
676 fp = ap_pfopen(r->pool, cls->preserve_file, "a");
677 result = (fp)?0:1;
678 #endif
679 if (result != APR_SUCCESS) {
680 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: attempted append of local preserve file but failed.");
766 } else { 681 } else {
767 apr_file_printf(fp,"%s;\n", query); 682 #if defined(WITH_APACHE20)
768 apr_file_close(fp); 683 apr_file_printf(fp,"%s;\n", query);
684 apr_file_close(fp);
685 #elif defined(WITH_APACHE13)
686 fprintf(fp,"%s;\n", query);
687 ap_pfclose(r->pool, fp);
688 #endif
769 #ifdef DEBUG 689 #ifdef DEBUG
770 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"mod_log_sql: entry preserved in %s", cls->preserve_file); 690 log_error(APLOG_MARK,APLOG_DEBUG,r->server,"mod_log_sql: entry preserved in %s", cls->preserve_file);
771 #endif 691 #endif
772 } 692 }
773} 693}
@@ -822,24 +742,24 @@ static unsigned int safe_sql_query(request_rec *r, const char *query)
822 } 742 }
823 if (retval) { 743 if (retval) {
824 if (global_config.createtables) { 744 if (global_config.createtables) {
825 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: table doesn't exist...creating now"); 745 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: table doesn't exist...creating now");
826 cls = ap_get_module_config(r->server->module_config, &log_sql_module); 746 cls = ap_get_module_config(r->server->module_config, &log_sql_module);
827 if (safe_create_tables(cls, r)) { 747 if (safe_create_tables(cls, r)) {
828 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: child attempted but failed to create one or more tables for %s, preserving query", ap_get_server_name(r)); 748 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: child attempted but failed to create one or more tables for %s, preserving query", ap_get_server_name(r));
829 preserve_entry(r, query); 749 preserve_entry(r, query);
830 retval = mysql_errno(global_config.server_p); 750 retval = mysql_errno(global_config.server_p);
831 } else { 751 } else {
832 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: tables successfully created - retrying query"); 752 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: tables successfully created - retrying query");
833 if (mysql_query(global_config.server_p, query)) { 753 if (mysql_query(global_config.server_p, query)) {
834 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: giving up, preserving query"); 754 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: giving up, preserving query");
835 preserve_entry(r, query); 755 preserve_entry(r, query);
836 retval = mysql_errno(global_config.server_p); 756 retval = mysql_errno(global_config.server_p);
837 } else 757 } else
838 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: query successful after table creation"); 758 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: query successful after table creation");
839 retval = 0; 759 retval = 0;
840 } 760 }
841 } else { 761 } else {
842 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql, table doesn't exist, creation denied by configuration, preserving query"); 762 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql, table doesn't exist, creation denied by configuration, preserving query");
843 preserve_entry(r, query); 763 preserve_entry(r, query);
844 retval = ER_NO_SUCH_TABLE; 764 retval = ER_NO_SUCH_TABLE;
845 } 765 }
@@ -859,25 +779,25 @@ static unsigned int safe_sql_query(request_rec *r, const char *query)
859 real_error = mysql_errno(global_config.server_p); 779 real_error = mysql_errno(global_config.server_p);
860 } 780 }
861 781
862 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: first attempt failed, API said: error %d, \"%s\"", real_error, MYSQL_ERROR(global_config.server_p)); 782 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: first attempt failed, API said: error %d, \"%s\"", real_error, MYSQL_ERROR(global_config.server_p));
863 mysql_close(global_config.server_p); 783 mysql_close(global_config.server_p);
864 global_config.server_p = NULL; 784 global_config.server_p = NULL;
865 open_logdb_link(r->server); 785 open_logdb_link(r->server);
866 786
867 if (global_config.server_p == NULL) { /* still unable to link */ 787 if (global_config.server_p == NULL) { /* still unable to link */
868 signal(SIGPIPE, handler); 788 signal(SIGPIPE, handler);
869 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: reconnect failed, unable to reach database. SQL logging stopped until child regains a db connection."); 789 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: reconnect failed, unable to reach database. SQL logging stopped until child regains a db connection.");
870 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: log entries are being preserved in %s", cls->preserve_file); 790 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: log entries are being preserved in %s", cls->preserve_file);
871 return 1; 791 return 1;
872 } else 792 } else
873 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: db reconnect successful"); 793 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: db reconnect successful");
874 794
875 /* First sleep for a tiny amount of time. */ 795 /* First sleep for a tiny amount of time. */
876 delay.tv_sec = 0; 796 delay.tv_sec = 0;
877 delay.tv_nsec = 250000000; /* max is 999999999 (nine nines) */ 797 delay.tv_nsec = 250000000; /* max is 999999999 (nine nines) */
878 ret = nanosleep(&delay, &remainder); 798 ret = nanosleep(&delay, &remainder);
879 if (ret && errno != EINTR) 799 if (ret && errno != EINTR)
880 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: nanosleep unsuccessful"); 800 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: nanosleep unsuccessful");
881 801
882 /* Then make our second attempt */ 802 /* Then make our second attempt */
883 retval = mysql_query(global_config.server_p,query); 803 retval = mysql_query(global_config.server_p,query);
@@ -890,11 +810,11 @@ static unsigned int safe_sql_query(request_rec *r, const char *query)
890 real_error = mysql_errno(global_config.server_p); 810 real_error = mysql_errno(global_config.server_p);
891 } 811 }
892 812
893 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: second attempt failed, API said: error %d, \"%s\" -- preserving", real_error, MYSQL_ERROR(global_config.server_p)); 813 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: second attempt failed, API said: error %d, \"%s\" -- preserving", real_error, MYSQL_ERROR(global_config.server_p));
894 preserve_entry(r, query); 814 preserve_entry(r, query);
895 retval = real_error; 815 retval = real_error;
896 } else 816 } else
897 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: second attempt successful"); 817 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: second attempt successful");
898 818
899 /* Restore SIGPIPE to its original handler function */ 819 /* Restore SIGPIPE to its original handler function */
900 signal(SIGPIPE, handler); 820 signal(SIGPIPE, handler);
@@ -974,38 +894,38 @@ static int safe_create_tables(logsql_state *cls, request_rec *r)
974 create_cookies= apr_pstrcat(r->pool, createprefix, cls->cookie_table_name, cookies_suffix, type_suffix, NULL); 894 create_cookies= apr_pstrcat(r->pool, createprefix, cls->cookie_table_name, cookies_suffix, type_suffix, NULL);
975 895
976 #ifdef DEBUG 896 #ifdef DEBUG
977 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"mod_log_sql: create string: %s", create_access); 897 log_error(APLOG_MARK,APLOG_DEBUG,r->server,"mod_log_sql: create string: %s", create_access);
978 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"mod_log_sql: create string: %s", create_notes); 898 log_error(APLOG_MARK,APLOG_DEBUG,r->server,"mod_log_sql: create string: %s", create_notes);
979 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"mod_log_sql: create string: %s", create_hout); 899 log_error(APLOG_MARK,APLOG_DEBUG,r->server,"mod_log_sql: create string: %s", create_hout);
980 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"mod_log_sql: create string: %s", create_hin); 900 log_error(APLOG_MARK,APLOG_DEBUG,r->server,"mod_log_sql: create string: %s", create_hin);
981 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"mod_log_sql: create string: %s", create_cookies); 901 log_error(APLOG_MARK,APLOG_DEBUG,r->server,"mod_log_sql: create string: %s", create_cookies);
982 #endif 902 #endif
983 903
984 /* Assume that things worked unless told otherwise */ 904 /* Assume that things worked unless told otherwise */
985 retval = 0; 905 retval = 0;
986 906
987 if ((create_results = safe_sql_query(r, create_access))) { 907 if ((create_results = safe_sql_query(r, create_access))) {
988 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: failed to create access table"); 908 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: failed to create access table");
989 retval = create_results; 909 retval = create_results;
990 } 910 }
991 911
992 if ((create_results = safe_sql_query(r, create_notes))) { 912 if ((create_results = safe_sql_query(r, create_notes))) {
993 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: failed to create notes table"); 913 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: failed to create notes table");
994 retval = create_results; 914 retval = create_results;
995 } 915 }
996 916
997 if ((create_results = safe_sql_query(r, create_hin))) { 917 if ((create_results = safe_sql_query(r, create_hin))) {
998 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: failed to create header_in table"); 918 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: failed to create header_in table");
999 retval = create_results; 919 retval = create_results;
1000 } 920 }
1001 921
1002 if ((create_results = safe_sql_query(r, create_hout))) { 922 if ((create_results = safe_sql_query(r, create_hout))) {
1003 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: failed to create header_out table"); 923 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: failed to create header_out table");
1004 retval = create_results; 924 retval = create_results;
1005 } 925 }
1006 926
1007 if ((create_results = safe_sql_query(r, create_cookies))) { 927 if ((create_results = safe_sql_query(r, create_cookies))) {
1008 ap_log_error(APLOG_MARK,APLOG_ERR,0,r->server,"mod_log_sql: failed to create cookies table"); 928 log_error(APLOG_MARK,APLOG_ERR,r->server,"mod_log_sql: failed to create cookies table");
1009 retval = create_results; 929 retval = create_results;
1010 } 930 }
1011 931
@@ -1123,21 +1043,18 @@ static const char *set_log_sql_tcp_port(cmd_parms *parms, void *dummy, const cha
1123 * that are defined in the array 'mysql_lgog_module' (at EOF) * 1043 * that are defined in the array 'mysql_lgog_module' (at EOF) *
1124 *------------------------------------------------------------*/ 1044 *------------------------------------------------------------*/
1125 1045
1126 1046#if defined(WITH_APACHE20)
1127/*
1128 * This function is called when an heavy-weight process (such as a child) is
1129 * being run down or destroyed. As with the child-initialisation function,
1130 * any information that needs to be recorded must be in static cells, since
1131 * there's no configuration record.
1132 *
1133 * There is no return value.
1134 */
1135static apr_status_t log_sql_close_link(void *data) 1047static apr_status_t log_sql_close_link(void *data)
1136{ 1048{
1137 mysql_close(global_config.server_p); 1049 mysql_close(global_config.server_p);
1138 return APR_SUCCESS; 1050 return APR_SUCCESS;
1139} 1051}
1140 1052#elif defined(WITH_APACHE13)
1053static void log_sql_child_exit(server_rec *s, apr_pool_t *p)
1054{
1055 mysql_close(global_config.server_p);
1056}
1057#endif
1141/* 1058/*
1142 * This function is called during server initialisation when an heavy-weight 1059 * This function is called during server initialisation when an heavy-weight
1143 * process (such as a child) is being initialised. As with the 1060 * process (such as a child) is being initialised. As with the
@@ -1146,26 +1063,32 @@ static apr_status_t log_sql_close_link(void *data)
1146 * 1063 *
1147 * There is no return value. 1064 * There is no return value.
1148 */ 1065 */
1066#if defined(WITH_APACHE20)
1149static void log_sql_child_init(apr_pool_t *p, server_rec *s) 1067static void log_sql_child_init(apr_pool_t *p, server_rec *s)
1150{ 1068{
1151 apr_pool_cleanup_register(p, NULL, log_sql_close_link, log_sql_close_link); 1069 apr_pool_cleanup_register(p, NULL, log_sql_close_link, log_sql_close_link);
1152} 1070}
1153 1071
1154static int log_sql_open(apr_pool_t *pc, apr_pool_t *p, apr_pool_t *pt, server_rec *s) 1072static int log_sql_open(apr_pool_t *pc, apr_pool_t *p, apr_pool_t *pt, server_rec *s)
1073#elif defined(WITH_APACHE13)
1074static void log_sql_child_init(server_rec *s, apr_pool_t *p)
1075#endif
1155{ 1076{
1156 int retval; 1077 int retval;
1157 /* Open a link to the database */ 1078 /* Open a link to the database */
1158 retval = open_logdb_link(s); 1079 retval = open_logdb_link(s);
1159 if (!retval) 1080 if (!retval)
1160 ap_log_error(APLOG_MARK,APLOG_ERR,0,s,"mod_log_sql: child spawned but unable to open database link"); 1081 log_error(APLOG_MARK,APLOG_ERR,s,"mod_log_sql: child spawned but unable to open database link");
1161 1082
1162 #ifdef DEBUG 1083 #ifdef DEBUG
1163 if ( (retval == 1) || (retval == 2) ) 1084 if ( (retval == 1) || (retval == 2) )
1164 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,s,"mod_log_sql: open_logdb_link successful"); 1085 log_error(APLOG_MARK,APLOG_DEBUG,s,"mod_log_sql: open_logdb_link successful");
1165 if (retval == 3) 1086 if (retval == 3)
1166 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,s,"mod_log_sql: open_logdb_link said that preservation is forced"); 1087 log_error(APLOG_MARK,APLOG_DEBUG,s,"mod_log_sql: open_logdb_link said that preservation is forced");
1167 #endif 1088 #endif
1089#if defined(WITH_APACHE20)
1168 return OK; 1090 return OK;
1091#endif
1169} 1092}
1170/* 1093/*
1171void *log_sql_initializer(server_rec *main_server, apr_pool_t *p) 1094void *log_sql_initializer(server_rec *main_server, apr_pool_t *p)
@@ -1183,6 +1106,23 @@ void *log_sql_initializer(server_rec *main_server, apr_pool_t *p)
1183} 1106}
1184 */ 1107 */
1185 1108
1109#if defined(WITH_APACHE20)
1110static int log_sql_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
1111#elif defined(WITH_APACHE13)
1112static void log_sql_pre_config(server_rec *s, apr_pool_t *p)
1113#endif
1114{
1115 /* Initialize Global configuration */
1116 if (!global_config.socketfile)
1117 global_config.socketfile = "/tmp/mysql.sock";
1118 if (!global_config.tcpport)
1119 global_config.tcpport = 3306;
1120
1121#if defined(WITH_APACHE20)
1122 return OK;
1123#endif
1124}
1125
1186/* 1126/*
1187 * This function gets called to create a per-server configuration 1127 * This function gets called to create a per-server configuration
1188 * record. It will always be called for the main server and 1128 * record. It will always be called for the main server and
@@ -1192,15 +1132,6 @@ void *log_sql_initializer(server_rec *main_server, apr_pool_t *p)
1192 * The return value is a pointer to the created module-specific 1132 * The return value is a pointer to the created module-specific
1193 * structure. 1133 * structure.
1194 */ 1134 */
1195static int log_sql_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
1196{
1197 /* Initialize Global configuration */
1198 memset(&global_config,0,sizeof(global_config_t));
1199 global_config.socketfile = "/tmp/mysql.sock";
1200 global_config.tcpport = 3306;
1201 return OK;
1202}
1203
1204static void *log_sql_make_state(apr_pool_t *p, server_rec *s) 1135static void *log_sql_make_state(apr_pool_t *p, server_rec *s)
1205{ 1136{
1206 logsql_state *cls = (logsql_state *) apr_pcalloc(p, sizeof(logsql_state)); 1137 logsql_state *cls = (logsql_state *) apr_pcalloc(p, sizeof(logsql_state));
@@ -1460,7 +1391,7 @@ static int log_sql_transaction(request_rec *orig)
1460 global_config.insertdelayed?"delayed":"", cls->notes_table_name, itemsets); 1391 global_config.insertdelayed?"delayed":"", cls->notes_table_name, itemsets);
1461 1392
1462 #ifdef DEBUG 1393 #ifdef DEBUG
1463 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,orig->server,"mod_log_sql: note string: %s", note_query); 1394 log_error(APLOG_MARK,APLOG_DEBUG,orig->server,"mod_log_sql: note string: %s", note_query);
1464 #endif 1395 #endif
1465 } 1396 }
1466 1397
@@ -1490,7 +1421,7 @@ static int log_sql_transaction(request_rec *orig)
1490 global_config.insertdelayed?"delayed":"", cls->hout_table_name, itemsets); 1421 global_config.insertdelayed?"delayed":"", cls->hout_table_name, itemsets);
1491 1422
1492 #ifdef DEBUG 1423 #ifdef DEBUG
1493 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,orig->server,"mod_log_sql: header_out string: %s", hout_query); 1424 log_error(APLOG_MARK,APLOG_DEBUG,orig->server,"mod_log_sql: header_out string: %s", hout_query);
1494 #endif 1425 #endif
1495 } 1426 }
1496 1427
@@ -1521,7 +1452,7 @@ static int log_sql_transaction(request_rec *orig)
1521 global_config.insertdelayed?"delayed":"", cls->hin_table_name, itemsets); 1452 global_config.insertdelayed?"delayed":"", cls->hin_table_name, itemsets);
1522 1453
1523 #ifdef DEBUG 1454 #ifdef DEBUG
1524 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,orig->server,"mod_log_sql: header_in string: %s", hin_query); 1455 log_error(APLOG_MARK,APLOG_DEBUG,orig->server,"mod_log_sql: header_in string: %s", hin_query);
1525 #endif 1456 #endif
1526 } 1457 }
1527 1458
@@ -1553,7 +1484,7 @@ static int log_sql_transaction(request_rec *orig)
1553 global_config.insertdelayed?"delayed":"", cls->cookie_table_name, itemsets); 1484 global_config.insertdelayed?"delayed":"", cls->cookie_table_name, itemsets);
1554 1485
1555 #ifdef DEBUG 1486 #ifdef DEBUG
1556 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,orig->server,"mod_log_sql: cookie string: %s", cookie_query); 1487 log_error(APLOG_MARK,APLOG_DEBUG,orig->server,"mod_log_sql: cookie string: %s", cookie_query);
1557 #endif 1488 #endif
1558 } 1489 }
1559 1490
@@ -1563,7 +1494,7 @@ static int log_sql_transaction(request_rec *orig)
1563 global_config.insertdelayed?"delayed":"", cls->transfer_table_name, fields, values); 1494 global_config.insertdelayed?"delayed":"", cls->transfer_table_name, fields, values);
1564 1495
1565 #ifdef DEBUG 1496 #ifdef DEBUG
1566 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,r->server,"mod_log_sql: access string: %s", access_query); 1497 log_error(APLOG_MARK,APLOG_DEBUG,r->server,"mod_log_sql: access string: %s", access_query);
1567 #endif 1498 #endif
1568 1499
1569 /* If the person activated force-preserve, go ahead and push all the entries 1500 /* If the person activated force-preserve, go ahead and push all the entries
@@ -1571,7 +1502,7 @@ static int log_sql_transaction(request_rec *orig)
1571 */ 1502 */
1572 if (global_config.forcepreserve) { 1503 if (global_config.forcepreserve) {
1573 #ifdef DEBUG 1504 #ifdef DEBUG
1574 ap_log_error(APLOG_MARK,APLOG_DEBUG,0,orig->server,"mod_log_sql: preservation forced"); 1505 log_error(APLOG_MARK,APLOG_DEBUG,orig->server,"mod_log_sql: preservation forced");
1575 #endif 1506 #endif
1576 preserve_entry(orig, access_query); 1507 preserve_entry(orig, access_query);
1577 if ( note_query != NULL ) 1508 if ( note_query != NULL )
@@ -1610,7 +1541,7 @@ static int log_sql_transaction(request_rec *orig)
1610 return OK; 1541 return OK;
1611 } else { 1542 } else {
1612 /* Whew, we got the DB link back */ 1543 /* Whew, we got the DB link back */
1613 ap_log_error(APLOG_MARK,APLOG_NOTICE,0,orig->server,"mod_log_sql: child established database connection"); 1544 log_error(APLOG_MARK,APLOG_NOTICE,orig->server,"mod_log_sql: child established database connection");
1614 } 1545 }
1615 } 1546 }
1616 1547
@@ -1745,6 +1676,7 @@ static const command_rec log_sql_cmds[] = {
1745 , 1676 ,
1746 {NULL} 1677 {NULL}
1747}; 1678};
1679#if defined(WITH_APACHE20)
1748/* The configuration array that sets up the hooks into the module. */ 1680/* The configuration array that sets up the hooks into the module. */
1749static void register_hooks(apr_pool_t *p) { 1681static void register_hooks(apr_pool_t *p) {
1750 ap_hook_pre_config(log_sql_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST); 1682 ap_hook_pre_config(log_sql_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
@@ -1762,3 +1694,30 @@ module AP_MODULE_DECLARE_DATA log_sql_module = {
1762 log_sql_cmds, /* command handlers */ 1694 log_sql_cmds, /* command handlers */
1763 register_hooks /* register hooks */ 1695 register_hooks /* register hooks */
1764}; 1696};
1697#elif defined(WITH_APACHE13)
1698/* The configuration array that sets up the hooks into the module. */
1699module log_sql_module = {
1700 STANDARD_MODULE_STUFF,
1701 log_sql_pre_config, /* module initializer */
1702 NULL, /* create per-dir config */
1703 NULL, /* merge per-dir config */
1704 log_sql_make_state, /* create server config */
1705 log_sql_merge_state, /* merge server config */
1706 log_sql_cmds, /* config directive table */
1707 NULL, /* [9] content handlers */
1708 NULL, /* [2] URI-to-filename translation */
1709 NULL, /* [5] check/validate user_id */
1710 NULL, /* [6] check authorization */
1711 NULL, /* [4] check access by host */
1712 NULL, /* [7] MIME type checker/setter */
1713 NULL, /* [8] fixups */
1714 log_sql_transaction, /* [10] logger */
1715 NULL /* [3] header parser */
1716#if MODULE_MAGIC_NUMBER >= 19970728 /* 1.3-dev or later support these additionals... */
1717 ,log_sql_child_init, /* child process initializer */
1718 log_sql_child_exit, /* process exit/cleanup */
1719 NULL /* [1] post read-request */
1720#endif
1721
1722};
1723#endif