diff options
Diffstat (limited to 'daemon/misc.c')
-rw-r--r-- | daemon/misc.c | 72 |
1 files changed, 54 insertions, 18 deletions
diff --git a/daemon/misc.c b/daemon/misc.c index cda6dc2..a38eda9 100644 --- a/daemon/misc.c +++ b/daemon/misc.c @@ -8,7 +8,7 @@ #include <stdio.h> #include <fcntl.h> -extern int g_debugging; +extern int g_debuglevel; extern int g_daemonized; extern pthread_mutex_t g_mutex; @@ -29,11 +29,14 @@ void ha_messagex(int level, const char* msg, ...) /* Either to syslog or stderr */ if(g_daemonized) - vsyslog(level, msg, ap); + { + if(level < LOG_DEBUG) + vsyslog(level, msg, ap); + } else { - if(g_debugging || level > LOG_INFO) + if(g_debuglevel >= level) vwarnx(msg, ap); } @@ -50,21 +53,24 @@ void ha_message(int level, const char* msg, ...) /* Either to syslog or stderr */ if(g_daemonized) { - char* m = (char*)alloca(strlen(msg) + MAX_MSGLEN + sizeof(kMsgDelimiter)); - - if(m) + if(level < LOG_DEBUG) { - strcpy(m, msg); - strcat(m, kMsgDelimiter); - strerror_r(errno, m + strlen(m), MAX_MSGLEN); - } + char* m = (char*)alloca(strlen(msg) + MAX_MSGLEN + sizeof(kMsgDelimiter)); - vsyslog(LOG_ERR, m ? m : msg, ap); + if(m) + { + strcpy(m, msg); + strcat(m, kMsgDelimiter); + strerror_r(errno, m + strlen(m), MAX_MSGLEN); + } + + vsyslog(LOG_ERR, m ? m : msg, ap); + } } else { - if(g_debugging || level > LOG_INFO) - vwarnx(msg, ap); + if(g_debuglevel >= level) + vwarn(msg, ap); } va_end(ap); @@ -81,7 +87,7 @@ ha_header_t* ha_findheader(ha_request_t* req, const char* name) ASSERT(req && name); - for(i = 0; i < MAX_HEADERS; i++) + for(i = 0; i < HA_MAX_HEADERS; i++) { if(req->headers[i].name) { @@ -99,7 +105,7 @@ const char* ha_getheader(ha_request_t* req, const char* name, const char* prefix ASSERT(req && name); - for(i = 0; i < MAX_HEADERS; i++) + for(i = 0; i < HA_MAX_HEADERS; i++) { if(req->headers[i].name) { @@ -125,7 +131,7 @@ void ha_addheader(ha_response_t* resp, const char* name, const char* data) ASSERT(resp && name && data); - for(i = 0; i < MAX_HEADERS; i++) + for(i = 0; i < HA_MAX_HEADERS; i++) { if(!(resp->headers[i].name)) { @@ -135,7 +141,7 @@ void ha_addheader(ha_response_t* resp, const char* name, const char* data) } } - ha_messagex(LOG_ERR, "too many headers in response. discarding '%s'", name); + ha_messagex(LOG_WARNING, "too many headers in response. discarding '%s'", name); } @@ -145,12 +151,41 @@ void ha_addheader(ha_response_t* resp, const char* name, const char* data) void ha_lock(pthread_mutex_t* mtx) { - int r = pthread_mutex_lock(mtx ? mtx : &g_mutex); + int r; + +#ifdef _DEBUG + int wait = 0; +#endif + + if(!mtx) + mtx = &g_mutex; + +#ifdef _DEBUG + r = pthread_mutex_trylock(mtx); + if(r == EBUSY) + { + wait = 1; + ha_message(LOG_DEBUG, "thread will block: %d", pthread_self()); + r = pthread_mutex_lock(mtx); + } + +#else + r = pthread_mutex_lock(mtx); + +#endif + if(r != 0) { errno = r; ha_message(LOG_CRIT, "threading problem. couldn't lock mutex"); } + +#ifdef _DEBUG + else if(wait) + { + ha_message(LOG_DEBUG, "thread unblocked: %d", pthread_self()); + } +#endif } void ha_unlock(pthread_mutex_t* mtx) @@ -586,3 +621,4 @@ int ha_genrandom(unsigned char* data, size_t len) close(dd); return r == -1 ? HA_FAILED : HA_OK; } + |