mirror of
https://github.com/darkk/redsocks.git
synced 2025-08-25 19:25:30 +00:00
Emit better errors from config file parser
This commit is contained in:
parent
b322e4a8dd
commit
24a5384e29
2
base.c
2
base.c
@ -301,7 +301,7 @@ static int base_onexit(parser_section *section)
|
||||
}
|
||||
|
||||
if (err)
|
||||
parser_error(section->context, err);
|
||||
parser_error(section->context, "%s", err);
|
||||
|
||||
if (!err)
|
||||
instance.configured = 1;
|
||||
|
4
log.c
4
log.c
@ -23,7 +23,7 @@
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
|
||||
static const char *lowmem = "<Can't print error, not enough memory>";
|
||||
const char *error_lowmem = "<Can't print error, not enough memory>";
|
||||
|
||||
typedef void (*log_func)(const char *file, int line, const char *func, int priority, const char *message, const char *appendix);
|
||||
|
||||
@ -159,7 +159,7 @@ void _log_vwrite(const char *file, int line, const char *func, int do_errno, int
|
||||
message = (const char*)EVBUFFER_DATA(buff);
|
||||
}
|
||||
else
|
||||
message = lowmem;
|
||||
message = error_lowmem;
|
||||
|
||||
log_msg(file, line, func, priority, message, do_errno ? strerror(saved_errno) : NULL);
|
||||
|
||||
|
2
log.h
2
log.h
@ -8,6 +8,8 @@
|
||||
#define log_errno(prio, msg...) _log_write(__FILE__, __LINE__, __func__, 1, prio, ## msg)
|
||||
#define log_error(prio, msg...) _log_write(__FILE__, __LINE__, __func__, 0, prio, ## msg)
|
||||
|
||||
extern const char *error_lowmem;
|
||||
|
||||
int log_preopen(const char *dst, bool log_debug, bool log_info);
|
||||
void log_open();
|
||||
|
||||
|
2
main.c
2
main.c
@ -100,7 +100,7 @@ int main(int argc, char **argv)
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
parser_context* parser = parser_start(f, NULL);
|
||||
parser_context* parser = parser_start(f);
|
||||
if (!parser) {
|
||||
perror("Not enough memory for parser");
|
||||
return EXIT_FAILURE;
|
||||
|
30
parser.c
30
parser.c
@ -38,7 +38,6 @@ struct parser_context_t {
|
||||
parser_section *sections;
|
||||
int line;
|
||||
int error;
|
||||
parser_errhandler errhandler;
|
||||
struct {
|
||||
size_t size;
|
||||
size_t filled;
|
||||
@ -47,22 +46,33 @@ struct parser_context_t {
|
||||
};
|
||||
|
||||
|
||||
void parser_error(parser_context *context, const char *msg)
|
||||
void parser_error(parser_context *context, const char *fmt, ...)
|
||||
{
|
||||
context->error = 1;
|
||||
if (context->errhandler)
|
||||
context->errhandler(msg, context->line);
|
||||
va_list ap;
|
||||
struct evbuffer *buff = evbuffer_new();
|
||||
const char *msg;
|
||||
|
||||
va_start(ap, fmt);
|
||||
if (buff) {
|
||||
evbuffer_add_vprintf(buff, fmt, ap);
|
||||
msg = (const char*)EVBUFFER_DATA(buff);
|
||||
}
|
||||
else
|
||||
msg = error_lowmem;
|
||||
va_end(ap);
|
||||
|
||||
context->error = 1;
|
||||
fprintf(stderr, "file parsing error at line %u: %s\n", context->line, msg);
|
||||
if (buff)
|
||||
evbuffer_free(buff);
|
||||
}
|
||||
|
||||
parser_context* parser_start(FILE *fd, parser_errhandler errhandler)
|
||||
parser_context* parser_start(FILE *fd)
|
||||
{
|
||||
parser_context *ret = calloc(1, sizeof(parser_context));
|
||||
if (!ret)
|
||||
return NULL;
|
||||
ret->fd = fd;
|
||||
ret->errhandler = errhandler;
|
||||
ret->buffer.size = 128; // should be big enough to fetch whole ``line``
|
||||
ret->buffer.data = malloc(ret->buffer.size);
|
||||
if (!ret->buffer.data) {
|
||||
@ -322,9 +332,9 @@ static int vp_in_addr(parser_context *context, void *addr, const char *token)
|
||||
}
|
||||
else {
|
||||
if (err == EAI_SYSTEM)
|
||||
parser_error(context, strerror(errno));
|
||||
parser_error(context, "unable to resolve %s, error %d (%s)", token, errno, strerror(errno));
|
||||
else
|
||||
parser_error(context, gai_strerror(err));
|
||||
parser_error(context, "unable to resolve %s, getaddrinfo error %d (%s)", token, err, gai_strerror(err));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -533,7 +543,7 @@ int parser_run(parser_context *context)
|
||||
parser_error(context, "value can't be parsed");
|
||||
}
|
||||
else {
|
||||
parser_error(context, "assignment with unknown key");
|
||||
parser_error(context, "assignment with unknown key <%s>", key_token);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
10
parser.h
10
parser.h
@ -38,12 +38,14 @@ struct parser_section_t {
|
||||
|
||||
|
||||
|
||||
typedef void (*parser_errhandler)(const char *errmsg, int line);
|
||||
|
||||
parser_context* parser_start(FILE *fd, parser_errhandler errhandler);
|
||||
parser_context* parser_start(FILE *fd);
|
||||
void parser_add_section(parser_context *context, parser_section *section);
|
||||
int parser_run(parser_context *context);
|
||||
void parser_error(parser_context *context, const char *msg);
|
||||
void parser_error(parser_context *context, const char *fmt, ...)
|
||||
#if defined(__GNUC__)
|
||||
__attribute__ (( format (printf, 2, 3) ))
|
||||
#endif
|
||||
;
|
||||
void parser_stop(parser_context *context);
|
||||
|
||||
/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
|
||||
|
@ -187,7 +187,7 @@ static int redsocks_onexit(parser_section *section)
|
||||
}
|
||||
|
||||
if (err)
|
||||
parser_error(section->context, err);
|
||||
parser_error(section->context, "%s", err);
|
||||
|
||||
return err ? -1 : 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user