summaryrefslogtreecommitdiffstats
path: root/mod_log_sql.c
diff options
context:
space:
mode:
Diffstat (limited to 'mod_log_sql.c')
-rw-r--r--mod_log_sql.c76
1 files changed, 52 insertions, 24 deletions
diff --git a/mod_log_sql.c b/mod_log_sql.c
index ecdfcf0..db9b3a3 100644
--- a/mod_log_sql.c
+++ b/mod_log_sql.c
@@ -1,4 +1,4 @@
1/* $Id: mod_log_sql.c,v 1.14 2002/05/24 20:52:39 helios Exp $ */ 1/* $Id: mod_log_sql.c,v 1.15 2002/06/27 20:09:17 helios Exp $ */
2 2
3/* --------* 3/* --------*
4 * DEFINES * 4 * DEFINES *
@@ -574,11 +574,13 @@ void preserve_entry(request_rec *r, const char *query)
574/* */ 574/* */
575/* Parms: request record, SQL insert statement */ 575/* Parms: request record, SQL insert statement */
576/* Returns: 0 (OK) on success */ 576/* Returns: 0 (OK) on success */
577/* mysql return code on error */ 577/* -1 if have no log handle */
578/* actual MySQL return code on error */
578/*-----------------------------------------------------*/ 579/*-----------------------------------------------------*/
579int safe_mysql_query(request_rec *r, const char *query) 580int safe_mysql_query(request_rec *r, const char *query)
580{ 581{
581 int retval; 582 int retval;
583 unsigned int real_error;
582 struct timespec delay, remainder; 584 struct timespec delay, remainder;
583 int ret; 585 int ret;
584 void (*handler) (int); 586 void (*handler) (int);
@@ -591,19 +593,22 @@ int safe_mysql_query(request_rec *r, const char *query)
591 if (mysql_log != NULL) 593 if (mysql_log != NULL)
592 retval = mysql_query(mysql_log, query); 594 retval = mysql_query(mysql_log, query);
593 else 595 else
594 return 1; 596 return -1;
595 597
596 /* If we ran the query and it returned an error, try to be graceful. 598 /* If we ran the query and it returned an error, try to be graceful.
597 * (The module thought it had a valid mysql_log connection but the query 599 * (The module thought it had a valid mysql_log connection but the query
598 * might have failed, so we have to check.) 600 * might have failed, so we have to be extra-safe and check.)
599 */ 601 */
600 if ( retval != 0 ) 602 if ( retval != 0 )
601 { 603 {
602 log_sql_state *cls = get_module_config(r->server->module_config, &mysql_log_module); 604 log_sql_state *cls = get_module_config(r->server->module_config, &mysql_log_module);
605
606 real_error = mysql_errno(mysql_log);
603 607
604 /* Something went wrong, so start by trying to restart the db link. */ 608 /* Something went wrong, so start by trying to restart the db link. */
605 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"mod_log_sql: first attempt failed, API said: %s", MYSQL_ERROR(mysql_log)); 609 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"mod_log_sql: first attempt failed, API said: error %d, %s", real_error, MYSQL_ERROR(mysql_log));
606 610
611 mysql_close(mysql_log);
607 mysql_log = NULL; 612 mysql_log = NULL;
608 open_logdb_link(); 613 open_logdb_link();
609 614
@@ -630,7 +635,8 @@ int safe_mysql_query(request_rec *r, const char *query)
630 /* If this one also failed, log that and append to our local offline file */ 635 /* If this one also failed, log that and append to our local offline file */
631 if ( retval != 0 ) 636 if ( retval != 0 )
632 { 637 {
633 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"mod_log_sql: second attempt failed, API said: %s", MYSQL_ERROR(mysql_log)); 638 real_error = mysql_errno(mysql_log);
639 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"mod_log_sql: second attempt failed, API said: error %d, %s", real_error, MYSQL_ERROR(mysql_log));
634 preserve_entry(r, query); 640 preserve_entry(r, query);
635 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"mod_log_sql: entry preserved in %s", cls->preserve_file); 641 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"mod_log_sql: entry preserved in %s", cls->preserve_file);
636 } else { 642 } else {
@@ -658,7 +664,10 @@ const char *set_log_sql_massvirtual(cmd_parms *parms, void *dummy, int flag)
658 664
659const char *set_log_sql_create(cmd_parms *parms, void *dummy, int flag) 665const char *set_log_sql_create(cmd_parms *parms, void *dummy, int flag)
660{ 666{
661 create_tables = ( flag ? 1 : 0); 667 if (massvirtual != 0)
668 ap_log_error(APLOG_MARK,WARNINGLEVEL,parms->server,"mod_log_sql: do not set LogSQLCreateTables when LogSQLMassVirtualHosting is On. Ignoring.");
669 else
670 create_tables = ( flag ? 1 : 0);
662 return NULL; 671 return NULL;
663} 672}
664 673
@@ -841,6 +850,9 @@ static void log_sql_child_init(server_rec *s, pool *p)
841 int retval; 850 int retval;
842 851
843 retval = open_logdb_link(); 852 retval = open_logdb_link();
853 if (retval == 0)
854 ap_log_error(APLOG_MARK,ERRLEVEL,s,"mod_log_sql: child spawned but unable to open database link");
855
844 #ifdef DEBUG 856 #ifdef DEBUG
845 if (retval > 0) { 857 if (retval > 0) {
846 ap_log_error(APLOG_MARK,DEBUGLEVEL,s,"mod_log_sql: open_logdb_link successful"); 858 ap_log_error(APLOG_MARK,DEBUGLEVEL,s,"mod_log_sql: open_logdb_link successful");
@@ -1052,7 +1064,7 @@ int log_sql_transaction(request_rec *orig)
1052 char *create_notes = NULL; 1064 char *create_notes = NULL;
1053 char *create_hout = NULL; 1065 char *create_hout = NULL;
1054 char *create_hin = NULL; 1066 char *create_hin = NULL;
1055 int create_results_access, create_results_notes, create_results_hout, create_results_hin; 1067 int create_results;
1056 1068
1057 for (r = orig; r->next; r = r->next) { 1069 for (r = orig; r->next; r = r->next) {
1058 continue; 1070 continue;
@@ -1266,10 +1278,10 @@ int log_sql_transaction(request_rec *orig)
1266 create_hin = ap_pstrcat(orig->pool, createprefix, cls->hin_table_name, headers_suffix, NULL); 1278 create_hin = ap_pstrcat(orig->pool, createprefix, cls->hin_table_name, headers_suffix, NULL);
1267 1279
1268 #ifdef DEBUG 1280 #ifdef DEBUG
1269 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"create string: %s", create_access); 1281 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"mod_log_sql: create string: %s", create_access);
1270 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"create string: %s", create_notes); 1282 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"mod_log_sql: create string: %s", create_notes);
1271 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"create string: %s", create_hout); 1283 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"mod_log_sql: create string: %s", create_hout);
1272 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"create string: %s", create_hin); 1284 ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"mod_log_sql: create string: %s", create_hin);
1273 #endif 1285 #endif
1274 1286
1275 } 1287 }
@@ -1278,7 +1290,7 @@ int log_sql_transaction(request_rec *orig)
1278 access_query = ap_pstrcat(r->pool, "insert into ", cls->transfer_table_name, " (", fields, ") values (", values, ")", NULL); 1290 access_query = ap_pstrcat(r->pool, "insert into ", cls->transfer_table_name, " (", fields, ") values (", values, ")", NULL);
1279 1291
1280 #ifdef DEBUG 1292 #ifdef DEBUG
1281 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"insert string: %s", access_query); 1293 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_sql: access string: %s", access_query);
1282 #endif 1294 #endif
1283 1295
1284 1296
@@ -1291,7 +1303,9 @@ int log_sql_transaction(request_rec *orig)
1291 if (mysql_log == NULL) { 1303 if (mysql_log == NULL) {
1292 /* Unable to re-establish a DB link, so assume that it's really 1304 /* Unable to re-establish a DB link, so assume that it's really
1293 * gone and send the entry to the preserve file instead. 1305 * gone and send the entry to the preserve file instead.
1294 * Note that we don't keep logging the db error over and over. */ 1306 * This short-circuits safe_mysql_query during a db outage and therefore
1307 * we don't keep logging the db error over and over.
1308 */
1295 preserve_entry(orig, access_query); 1309 preserve_entry(orig, access_query);
1296 if ( note_query != NULL ) 1310 if ( note_query != NULL )
1297 preserve_entry(orig, note_query); 1311 preserve_entry(orig, note_query);
@@ -1313,21 +1327,35 @@ int log_sql_transaction(request_rec *orig)
1313 1327
1314 /* Make the tables if we're supposed to. */ 1328 /* Make the tables if we're supposed to. */
1315 if ((cls->table_made != 1) && (create_tables != 0)) { 1329 if ((cls->table_made != 1) && (create_tables != 0)) {
1316 create_results_access = safe_mysql_query(orig, create_access); 1330
1317 create_results_notes = safe_mysql_query(orig, create_notes); 1331 /* Assume it will be made successfully...*/
1318 create_results_hin = safe_mysql_query(orig, create_hin); 1332 cls->table_made = 1;
1319 create_results_hout = safe_mysql_query(orig, create_hout);
1320 1333
1321 if ( (create_results_access == 0) && (create_results_notes == 0) && (create_results_hin == 0) && (create_results_hout == 0) ) 1334 if ((create_results = safe_mysql_query(orig, create_access)) != 0) {
1322 cls->table_made = 1; 1335 cls->table_made = 0;
1323 else 1336 ap_log_error(APLOG_MARK,ERRLEVEL,orig->server,"mod_log_sql: failed to create access table, see preserve file");
1324 ap_log_error(APLOG_MARK,ERRLEVEL,orig->server,"mod_log_sql: failed to create all tables, see preserve file"); 1337 }
1338
1339 if ((create_results = safe_mysql_query(orig, create_notes)) != 0) {
1340 cls->table_made = 0;
1341 ap_log_error(APLOG_MARK,ERRLEVEL,orig->server,"mod_log_sql: failed to create notes table, see preserve file");
1342 }
1343
1344 if ((create_results = safe_mysql_query(orig, create_hin)) != 0) {
1345 cls->table_made = 0;
1346 ap_log_error(APLOG_MARK,ERRLEVEL,orig->server,"mod_log_sql: failed to create header_out table, see preserve file");
1347 }
1348
1349 if ((create_results = safe_mysql_query(orig, create_hout)) != 0) {
1350 cls->table_made = 0;
1351 ap_log_error(APLOG_MARK,ERRLEVEL,orig->server,"mod_log_sql: failed to create header_in table, see preserve file");
1352 }
1325 } 1353 }
1326 1354
1327 /* Make the access-table insert */ 1355 /* Make the access-table insert */
1328 safe_mysql_query(orig, access_query); 1356 safe_mysql_query(orig, access_query);
1329 1357
1330 /* If notes are available to log, make the notes-table insert */ 1358 /* Log the optional notes, headers, etc. */
1331 if ( note_query != NULL ) 1359 if ( note_query != NULL )
1332 safe_mysql_query(orig, note_query); 1360 safe_mysql_query(orig, note_query);
1333 1361