From 7df2461a26b8433ba4ef67800ac7816aaa09afed Mon Sep 17 00:00:00 2001 From: z3APA3A <3APA3A@3proxy.ru> Date: Sun, 27 Dec 2015 19:27:17 +0300 Subject: [PATCH] Unify logging (make it always blocking in exchange for memory) --- src/3proxy.c | 12 ++++-------- src/auth.c | 21 ++++++++++----------- src/common.c | 17 +++++++++++------ src/conf.c | 9 ++------- src/proxy.h | 5 +---- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/3proxy.c b/src/3proxy.c index ee1447a..30a1320 100644 --- a/src/3proxy.c +++ b/src/3proxy.c @@ -19,7 +19,6 @@ #endif FILE * confopen(); -extern unsigned char tmpbuf[1024]; extern unsigned char *strings[]; extern FILE *writable; extern struct counter_header cheader; @@ -65,9 +64,9 @@ void __stdcall CommandHandler( DWORD dwCommand ) Sleep(2000); SetStatus( SERVICE_STOPPED, 0, 0 ); #ifndef NOODBC - pthread_mutex_lock(&odbc_mutex); + pthread_mutex_lock(&log_mutex); close_sql(); - pthread_mutex_unlock(&odbc_mutex); + pthread_mutex_unlock(&log_mutex); #endif break; case SERVICE_CONTROL_PAUSE: @@ -120,9 +119,9 @@ void mysigterm (int sig){ usleep(999*SLEEPTIME); usleep(999*SLEEPTIME); #ifndef NOODBC - pthread_mutex_lock(&odbc_mutex); + pthread_mutex_lock(&log_mutex); close_sql(); - pthread_mutex_unlock(&odbc_mutex); + pthread_mutex_unlock(&log_mutex); #endif conf.timetoexit = 1; } @@ -519,9 +518,6 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int pthread_mutex_init(&hash_mutex, NULL); pthread_mutex_init(&tc_mutex, NULL); pthread_mutex_init(&pwl_mutex, NULL); -#ifndef NOODBC - pthread_mutex_init(&odbc_mutex, NULL); -#endif freeconf(&conf); res = readconfig(fp); diff --git a/src/auth.c b/src/auth.c index 3bbf024..381332e 100644 --- a/src/auth.c +++ b/src/auth.c @@ -1296,46 +1296,45 @@ void sqlerr (char *buf){ fprintf(conf.stdlog, "%s\n", buf); fflush(conf.stdlog); } - pthread_mutex_unlock(&odbc_mutex); + pthread_mutex_unlock(&log_mutex); } void logsql(struct clientparam * param, const unsigned char *s) { - unsigned char buf[4096]; SQLRETURN ret; int len; - len = dobuf(param, buf, s, "\'"); if(param->nolog) return; - pthread_mutex_lock(&odbc_mutex); + pthread_mutex_lock(&log_mutex); + len = dobuf(param, tmpbuf, s, "\'"); if(attempt > 5){ time_t t; t = time(0); if (t - attempt_time < 180){ - sqlerr(buf); + sqlerr(tmpbuf); return; } } if(!hstmt){ if(!init_sql(sqlstring)) { - sqlerr(buf); + sqlerr(tmpbuf); return; } } if(hstmt){ - ret = SQLExecDirect(hstmt, (SQLCHAR *)buf, (SQLINTEGER)len); + ret = SQLExecDirect(hstmt, (SQLCHAR *)tmpbuf, (SQLINTEGER)len); if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){ close_sql(); if(!init_sql(sqlstring)){ - sqlerr(buf); + sqlerr(tmpbuf); return; } if(hstmt) { - ret = SQLExecDirect(hstmt, (SQLCHAR *)buf, (SQLINTEGER)len); + ret = SQLExecDirect(hstmt, (SQLCHAR *)tmpbuf, (SQLINTEGER)len); if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){ - sqlerr(buf); + sqlerr(tmpbuf); return; } attempt = 0; @@ -1343,7 +1342,7 @@ void logsql(struct clientparam * param, const unsigned char *s) { } attempt = 0; } - pthread_mutex_unlock(&odbc_mutex); + pthread_mutex_unlock(&log_mutex); } #endif diff --git a/src/common.c b/src/common.c index 16d81b7..c91dab9 100644 --- a/src/common.c +++ b/src/common.c @@ -603,9 +603,9 @@ void lognone(struct clientparam * param, const unsigned char *s) { } pthread_mutex_t log_mutex; int logmutexinit = 0; +unsigned char tmpbuf[4096]; void logstdout(struct clientparam * param, const unsigned char *s) { - unsigned char buf[4096]; FILE *log; if(!logmutexinit){ @@ -614,8 +614,8 @@ void logstdout(struct clientparam * param, const unsigned char *s) { } pthread_mutex_lock(&log_mutex); log = param->srv->stdlog?param->srv->stdlog:conf.stdlog?conf.stdlog:stdout; - dobuf(param, buf, s, NULL); - if(!param->nolog)if(fprintf(log, "%s\n", buf) < 0) { + dobuf(param, tmpbuf, s, NULL); + if(!param->nolog)if(fprintf(log, "%s\n", tmpbuf) < 0) { perror("printf()"); }; if(log != conf.stdlog)fflush(log); @@ -623,10 +623,15 @@ void logstdout(struct clientparam * param, const unsigned char *s) { } #ifndef _WIN32 void logsyslog(struct clientparam * param, const unsigned char *s) { - unsigned char buf[4096]; - dobuf(param, buf, s, NULL); - if(!param->nolog)syslog(LOG_INFO, "%s", buf); + if(!logmutexinit){ + pthread_mutex_init(&log_mutex, NULL); + logmutexinit = 1; + } + pthread_mutex_lock(&log_mutex); + dobuf(param, tmpbuf, s, NULL) + if(!param->nolog)syslog(LOG_INFO, "%s", tmpbuf); + pthread_mutex_unlock(&log_mutex); } #endif diff --git a/src/conf.c b/src/conf.c index 793421b..896b91f 100644 --- a/src/conf.c +++ b/src/conf.c @@ -16,14 +16,9 @@ pthread_mutex_t pwl_mutex; pthread_mutex_t hash_mutex; pthread_mutex_t config_mutex; -#ifndef NOODBC -pthread_mutex_t odbc_mutex; -#endif - int haveerror = 0; int linenum = 0; -unsigned char tmpbuf[1024]; FILE *writable; struct counter_header cheader = {"3CF", (time_t)0}; struct counter_record crecord; @@ -302,10 +297,10 @@ static int h_log(int argc, unsigned char ** argv){ } #ifndef NOODBC else if(*argv[1]=='&'){ - pthread_mutex_lock(&odbc_mutex); + pthread_mutex_lock(&log_mutex); close_sql(); init_sql((char *)argv[1]+1); - pthread_mutex_unlock(&odbc_mutex); + pthread_mutex_unlock(&log_mutex); conf.logfunc = logsql; } #endif diff --git a/src/proxy.h b/src/proxy.h index 52f5bf1..2e3fe1d 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -309,7 +309,7 @@ struct datatype; struct dictionary; struct node; struct property; - +extern unsigned char tmpbuf[4096]; extern pthread_mutex_t config_mutex; extern pthread_mutex_t bandlim_mutex; extern pthread_mutex_t hash_mutex; @@ -317,9 +317,6 @@ extern pthread_mutex_t tc_mutex; extern pthread_mutex_t pwl_mutex; extern pthread_mutex_t log_mutex; extern int logmutexinit; -#ifndef NOODBC -extern pthread_mutex_t odbc_mutex; -#endif extern struct datatype datatypes[64];