Skip to content

Commit

Permalink
* debug.c (debugerrno): New function.
Browse files Browse the repository at this point in the history
(debugerrnox): New function.
* radsecproxy.c: Use debugerrno() and debugerrnox() when appropriate.
  • Loading branch information
Linus Nordberg committed May 28, 2010
1 parent 9b88622 commit 17f018e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
28 changes: 28 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sys/time.h>
#include <syslog.h>
#include <errno.h>
#include <assert.h>
#include "debug.h"
#include "util.h"

Expand Down Expand Up @@ -178,6 +179,33 @@ void debugx(int status, uint8_t level, char *format, ...) {
exit(status);
}

void debugerrno(int err, uint8_t level, char *format, ...) {
if (level >= debug_level) {
va_list ap;
size_t len = strlen(format);
char *tmp = malloc(len + 1024 + 2);
assert(tmp);
strcpy(tmp, format);
tmp[len++] = ':';
tmp[len++] = ' ';
if (strerror_r(err, tmp + len, 1024))
tmp = format;
va_start(ap, format);
debug_logit(level, tmp, ap);
va_end(ap);
}
}

void debugerrnox(int err, uint8_t level, char *format, ...) {
if (level >= debug_level) {
va_list ap;
va_start(ap, format);
debugerrno(err, level, format, ap);
va_end(ap);
}
exit(err);
}

/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */
2 changes: 2 additions & 0 deletions debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void debug_timestamp_on();
uint8_t debug_get_level();
void debug(uint8_t level, char *format, ...);
void debugx(int status, uint8_t level, char *format, ...);
void debugerrno(int err, uint8_t level, char *format, ...);
void debugerrnox(int err, uint8_t level, char *format, ...);
int debug_set_destination(char *dest);
void debug_reopen_log();

Expand Down
27 changes: 14 additions & 13 deletions radsecproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <regex.h>
#include <libgen.h>
#include <pthread.h>
#include <errno.h>
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/err.h>
Expand Down Expand Up @@ -370,24 +371,24 @@ int addserver(struct clsrvconf *conf) {
goto errexit;
}
if (pthread_mutex_init(conf->servers->requests[i].lock, NULL)) {
debug(DBG_ERR, "mutex init failed");
debugerrno(errno, DBG_ERR, "mutex init failed");
free(conf->servers->requests[i].lock);
conf->servers->requests[i].lock = NULL;
goto errexit;
}
}
if (pthread_mutex_init(&conf->servers->lock, NULL)) {
debug(DBG_ERR, "mutex init failed");
debugerrno(errno, DBG_ERR, "mutex init failed");
goto errexit;
}
conf->servers->newrq = 0;
if (pthread_mutex_init(&conf->servers->newrq_mutex, NULL)) {
debug(DBG_ERR, "mutex init failed");
debugerrno(errno, DBG_ERR, "mutex init failed");
pthread_mutex_destroy(&conf->servers->lock);
goto errexit;
}
if (pthread_cond_init(&conf->servers->newrq_cond, NULL)) {
debug(DBG_ERR, "mutex init failed");
debugerrno(errno, DBG_ERR, "mutex init failed");
pthread_mutex_destroy(&conf->servers->newrq_mutex);
pthread_mutex_destroy(&conf->servers->lock);
goto errexit;
Expand Down Expand Up @@ -1762,7 +1763,7 @@ void *clientwr(void *arg) {
}
server->connectionok = 1;
if (pthread_create(&clientrdth, NULL, conf->pdef->clientconnreader, (void *)server)) {
debug(DBG_ERR, "clientwr: pthread_create failed");
debugerrno(errno, DBG_ERR, "clientwr: pthread_create failed");
goto errexit;
}
} else
Expand Down Expand Up @@ -1895,7 +1896,7 @@ void createlistener(uint8_t type, char *arg) {
for (res = hp->addrinfo; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
debug(DBG_WARN, "createlistener: socket failed");
debugerrno(errno, DBG_WARN, "createlistener: socket failed");
continue;
}
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Expand All @@ -1907,7 +1908,7 @@ void createlistener(uint8_t type, char *arg) {
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
#endif
if (bind(s, res->ai_addr, res->ai_addrlen)) {
debug(DBG_WARN, "createlistener: bind failed");
debugerrno(errno, DBG_WARN, "createlistener: bind failed");
close(s);
s = -1;
continue;
Expand All @@ -1918,7 +1919,7 @@ void createlistener(uint8_t type, char *arg) {
debugx(1, DBG_ERR, "malloc failed");
*sp = s;
if (pthread_create(&th, NULL, protodefs[type]->listener, (void *)sp))
debugx(1, DBG_ERR, "pthread_create failed");
debugerrnox(errno, DBG_ERR, "pthread_create failed");
pthread_detach(th);
}
if (!sp)
Expand Down Expand Up @@ -2067,7 +2068,7 @@ struct realm *addrealm(struct list *realmlist, char *value, char **servers, char
memset(realm, 0, sizeof(struct realm));

if (pthread_mutex_init(&realm->mutex, NULL)) {
debug(DBG_ERR, "mutex init failed");
debugerrno(errno, DBG_ERR, "mutex init failed");
free(realm);
realm = NULL;
goto exit;
Expand Down Expand Up @@ -2158,7 +2159,7 @@ struct list *createsubrealmservers(struct realm *realm, struct list *srvconfs) {
srvconf->servers->dynamiclookuparg = stringcopy(realm->name, 0);
srvconf->servers->dynstartup = 1;
if (pthread_create(&clientth, NULL, clientwr, (void *)(srvconf->servers))) {
debug(DBG_ERR, "pthread_create failed");
debugerrno(errno, DBG_ERR, "pthread_create failed");
freeserver(srvconf->servers, 1);
srvconf->servers = NULL;
} else
Expand Down Expand Up @@ -2220,12 +2221,12 @@ int dynamicconfig(struct server *server) {
debug(DBG_DBG, "dynamicconfig: need dynamic server config for %s", server->dynamiclookuparg);

if (pipe(fd) > 0) {
debug(DBG_ERR, "dynamicconfig: pipe error");
debugerrno(errno, DBG_ERR, "dynamicconfig: pipe error");
goto errexit;
}
pid = fork();
if (pid < 0) {
debug(DBG_ERR, "dynamicconfig: fork error");
debugerrno(errno, DBG_ERR, "dynamicconfig: fork error");
close(fd[0]);
close(fd[1]);
goto errexit;
Expand All @@ -2250,7 +2251,7 @@ int dynamicconfig(struct server *server) {
freegconf(&cf);

if (waitpid(pid, &status, 0) < 0) {
debug(DBG_ERR, "dynamicconfig: wait error");
debugerrno(errno, DBG_ERR, "dynamicconfig: wait error");
goto errexit;
}

Expand Down

0 comments on commit 17f018e

Please sign in to comment.