2012-05-17 20:14:22 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static pthread_t main_thread;
|
2012-05-31 13:31:22 +01:00
|
|
|
static int global_debug;
|
2012-05-17 20:14:22 +01:00
|
|
|
|
|
|
|
void error_init()
|
|
|
|
{
|
|
|
|
main_thread = pthread_self();
|
|
|
|
}
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
void error(int consult_errno, int fatal, int close_socket, pthread_mutex_t* unlock, const char* format, ...)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
fprintf(stderr, "*** ");
|
|
|
|
|
|
|
|
va_start(argptr, format);
|
|
|
|
vfprintf(stderr, format, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
|
|
|
|
if (consult_errno) {
|
|
|
|
fprintf(stderr, " (errno=%d, %s)", errno, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
if (close_socket) { close(close_socket); }
|
|
|
|
if (unlock) { pthread_mutex_unlock(unlock); }
|
2012-05-29 00:59:12 +01:00
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2012-06-07 14:25:30 +01:00
|
|
|
if (fatal || pthread_equal(pthread_self(), main_thread)) {
|
2012-05-17 20:14:22 +01:00
|
|
|
exit(1);
|
2012-06-07 14:25:30 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Killing Thread\n");
|
2012-05-17 20:14:22 +01:00
|
|
|
pthread_exit((void*) 1);
|
2012-06-07 14:25:30 +01:00
|
|
|
}
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void* xrealloc(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
void* p = realloc(ptr, size);
|
|
|
|
if (p == NULL)
|
|
|
|
SERVER_ERROR("couldn't xrealloc %d bytes", size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void* p = xrealloc(NULL, size);
|
|
|
|
memset(p, 0, size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-05-31 13:31:22 +01:00
|
|
|
|
|
|
|
void set_debug(int value) {
|
|
|
|
global_debug = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
# include <sys/times.h>
|
|
|
|
# include <stdarg.h>
|
|
|
|
|
|
|
|
void debug(const char *msg, ...) {
|
2012-06-07 11:44:19 +01:00
|
|
|
va_list argp;
|
|
|
|
va_start( argp, msg );
|
2012-05-31 13:31:22 +01:00
|
|
|
|
|
|
|
if ( global_debug ) {
|
2012-06-07 11:44:19 +01:00
|
|
|
fprintf(stderr, "%08x %4d: ", (int) pthread_self(), (int) clock() );
|
|
|
|
vfprintf(stderr, msg, argp);
|
|
|
|
fprintf(stderr, "\n");
|
2012-05-31 13:31:22 +01:00
|
|
|
}
|
2012-06-07 11:44:19 +01:00
|
|
|
|
|
|
|
va_end( argp );
|
2012-05-31 13:31:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|