Skip to content

Commit

Permalink
Improve error reporting. Fixes #15.
Browse files Browse the repository at this point in the history
 - Add an error message when the configuration file could not be found.
 - Log to stderr when running in the foreground.
  • Loading branch information
samhocevar committed Feb 19, 2021
1 parent d3dd353 commit ff41723
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 45 deletions.
57 changes: 31 additions & 26 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \
}
#define PARSE_ERROR exit(1);
#define MEMORY_ERROR { \
logError("could not allocate memory when parsing configuration.\n"); \
exit(1); \
}

#if defined __clang__
#pragma clang diagnostic ignored "-Wunused-parameter"
Expand Down Expand Up @@ -357,7 +361,7 @@ YY_ACTION(void) yy_1_sol(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_sol\n"));
{
#line 157
#line 161
++yy->currentLine; ;
}
#undef yythunkpos
Expand All @@ -371,9 +375,9 @@ YY_ACTION(void) yy_1_invalid_syntax(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_invalid_syntax\n"));
{
#line 131
#line 135

logError("rinetd: invalid syntax at line %d: %s\n",
logError("invalid syntax at line %d: %s\n",
yy->currentLine, yytext);
PARSE_ERROR; /* FIXME */
;
Expand All @@ -389,7 +393,7 @@ YY_ACTION(void) yy_1_logcommon(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_logcommon\n"));
{
#line 126
#line 130

logFormatCommon = 1;
;
Expand All @@ -405,11 +409,11 @@ YY_ACTION(void) yy_1_pidlogfile(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_pidlogfile\n"));
{
#line 118
#line 122

pidLogFileName = strdup(yytext);
if (!pidLogFileName) {
PARSE_ERROR;
MEMORY_ERROR;
}
;
}
Expand All @@ -424,11 +428,11 @@ YY_ACTION(void) yy_1_logfile(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_logfile\n"));
{
#line 110
#line 114

logFileName = strdup(yytext);
if (!logFileName) {
PARSE_ERROR;
MEMORY_ERROR;
}
;
}
Expand All @@ -443,7 +447,7 @@ YY_ACTION(void) yy_1_auth_key(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_auth_key\n"));
{
#line 107
#line 111
yy->isAuthAllow = (yytext[0] == 'a'); ;
}
#undef yythunkpos
Expand All @@ -457,16 +461,16 @@ YY_ACTION(void) yy_1_auth_rule(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_auth_rule\n"));
{
#line 85
#line 89

allRules = (Rule *)
realloc(allRules, sizeof(Rule) * (allRulesCount + 1));
if (!allRules) {
PARSE_ERROR;
MEMORY_ERROR;
}
allRules[allRulesCount].pattern = strdup(yytext);
if (!allRules[allRulesCount].pattern) {
PARSE_ERROR;
MEMORY_ERROR;
}
allRules[allRulesCount].type = yy->isAuthAllow ? allowRule : denyRule;
if (seTotal > 0) {
Expand All @@ -491,7 +495,7 @@ YY_ACTION(void) yy_3_proto(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_3_proto\n"));
{
#line 82
#line 86
yy->tmpProto = IPPROTO_TCP; ;
}
#undef yythunkpos
Expand All @@ -505,7 +509,7 @@ YY_ACTION(void) yy_2_proto(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_2_proto\n"));
{
#line 81
#line 85
yy->tmpProto = IPPROTO_UDP; ;
}
#undef yythunkpos
Expand All @@ -519,7 +523,7 @@ YY_ACTION(void) yy_1_proto(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_proto\n"));
{
#line 80
#line 84
yy->tmpProto = IPPROTO_TCP; ;
}
#undef yythunkpos
Expand All @@ -533,7 +537,7 @@ YY_ACTION(void) yy_1_port(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_port\n"));
{
#line 79
#line 83
yy->tmpPort = strdup(yytext); ;
}
#undef yythunkpos
Expand All @@ -547,7 +551,7 @@ YY_ACTION(void) yy_1_option_source(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_option_source\n"));
{
#line 75
#line 79
yy->sourceAddress = strdup(yytext); ;
}
#undef yythunkpos
Expand All @@ -561,7 +565,7 @@ YY_ACTION(void) yy_1_option_timeout(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_option_timeout\n"));
{
#line 74
#line 78
yy->serverTimeout = atoi(yytext); ;
}
#undef yythunkpos
Expand All @@ -575,7 +579,7 @@ YY_ACTION(void) yy_1_connect_port(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_connect_port\n"));
{
#line 69
#line 73
yy->connectPort = yy->tmpPort; yy->connectProto = yy->tmpProto; ;
}
#undef yythunkpos
Expand All @@ -589,7 +593,7 @@ YY_ACTION(void) yy_1_bind_port(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_bind_port\n"));
{
#line 68
#line 72
yy->bindPort = yy->tmpPort; yy->bindProto = yy->tmpProto; ;
}
#undef yythunkpos
Expand All @@ -603,7 +607,7 @@ YY_ACTION(void) yy_1_connect_address(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_connect_address\n"));
{
#line 67
#line 71
yy->connectAddress = strdup(yytext); ;
}
#undef yythunkpos
Expand All @@ -617,7 +621,7 @@ YY_ACTION(void) yy_1_bind_address(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_bind_address\n"));
{
#line 66
#line 70
yy->bindAddress = strdup(yytext); ;
}
#undef yythunkpos
Expand All @@ -631,7 +635,7 @@ YY_ACTION(void) yy_1_server_rule(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_server_rule\n"));
{
#line 51
#line 55

addServer(yy->bindAddress, yy->bindPort, yy->bindProto,
yy->connectAddress, yy->connectPort, yy->connectProto,
Expand Down Expand Up @@ -1398,21 +1402,22 @@ YY_PARSE(yycontext *) YYRELEASE(yycontext *yyctx)
}

#endif
#line 161 "parse.peg"
#line 165 "parse.peg"


void parseConfiguration(char const *file)
{
FILE *in = fopen(file, "r");
if (!in) {
PARSE_ERROR;
logError("could not open configuration file %s.\n", file);
exit(1);
}

yycontext ctx;
memset(&ctx, 0, sizeof(yycontext));
ctx.fp = in;
if (!yyparse(&ctx)) {
logError("invalid syntax on file %s, line %d.\n", file, -1);
logError("invalid syntax in file %s, line %d.\n", file, -1);
exit(1);
}
yyrelease(&ctx);
Expand Down
19 changes: 12 additions & 7 deletions src/parse.peg
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \
}
#define PARSE_ERROR exit(1);
#define MEMORY_ERROR { \
logError("could not allocate memory when parsing configuration.\n"); \
exit(1); \
}

#if defined __clang__
#pragma clang diagnostic ignored "-Wunused-parameter"
Expand Down Expand Up @@ -86,11 +90,11 @@ auth-rule = auth-key - < pattern >
allRules = (Rule *)
realloc(allRules, sizeof(Rule) * (allRulesCount + 1));
if (!allRules) {
PARSE_ERROR;
MEMORY_ERROR;
}
allRules[allRulesCount].pattern = strdup(yytext);
if (!allRules[allRulesCount].pattern) {
PARSE_ERROR;
MEMORY_ERROR;
}
allRules[allRulesCount].type = yy->isAuthAllow ? allowRule : denyRule;
if (seTotal > 0) {
Expand All @@ -110,15 +114,15 @@ logfile = "logfile" - < filename >
{
logFileName = strdup(yytext);
if (!logFileName) {
PARSE_ERROR;
MEMORY_ERROR;
}
}

pidlogfile = "pidlogfile" - < filename >
{
pidLogFileName = strdup(yytext);
if (!pidLogFileName) {
PARSE_ERROR;
MEMORY_ERROR;
}
}

Expand All @@ -129,7 +133,7 @@ logcommon = "logcommon"

invalid_syntax = < (!eol .)+ > eol
{
logError("rinetd: invalid syntax at line %d: %s\n",
logError("invalid syntax at line %d: %s\n",
yy->currentLine, yytext);
PARSE_ERROR; /* FIXME */
}
Expand Down Expand Up @@ -164,14 +168,15 @@ void parseConfiguration(char const *file)
{
FILE *in = fopen(file, "r");
if (!in) {
PARSE_ERROR;
logError("could not open configuration file %s.\n", file);
exit(1);
}

yycontext ctx;
memset(&ctx, 0, sizeof(yycontext));
ctx.fp = in;
if (!yyparse(&ctx)) {
logError("invalid syntax on file %s, line %d.\n", file, -1);
logError("invalid syntax in file %s, line %d.\n", file, -1);
exit(1);
}
yyrelease(&ctx);
Expand Down
36 changes: 24 additions & 12 deletions src/rinetd.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ enum {
logDenied,
};

RinetdOptions options = {
static RinetdOptions options = {
RINETD_CONFIG_FILE,
0,
};

static int forked = 0;

static void selectPass(void);
static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socket);
static void handleRead(ConnectionInfo *cnx, Socket *socket, Socket *other_socket);
Expand Down Expand Up @@ -160,15 +162,19 @@ int main(int argc, char *argv[])

readArgs(argc, argv, &options);

if (!options.foreground) {
#if HAVE_DAEMON && !DEBUG
if (!options.foreground && daemon(0, 0) != 0) {
exit(0);
}
if (daemon(0, 0) != 0) {
exit(0);
}
forked = 1;
#elif HAVE_FORK && !DEBUG
if (!options.foreground && fork() != 0) {
exit(0);
}
if (fork() != 0) {
exit(0);
}
forked = 1;
#endif
}

#if HAVE_SIGACTION
struct sigaction act;
Expand All @@ -190,7 +196,7 @@ int main(int argc, char *argv[])
registerPID(pidLogFileName ? pidLogFileName : RINETD_PID_FILE);
}

logInfo("Starting redirections...\n");
logInfo("starting redirections...\n");
while (1) {
selectPass();
}
Expand All @@ -203,11 +209,14 @@ void logError(char const *fmt, ...)
va_list ap;
va_start(ap, fmt);
#if !_WIN32
if (!options.foreground)
if (forked)
vsyslog(LOG_ERR, fmt, ap);
else
#endif
{
fprintf(stderr, "rinetd error: ");
vfprintf(stderr, fmt, ap);
}
va_end(ap);
}

Expand All @@ -216,11 +225,14 @@ void logInfo(char const *fmt, ...)
va_list ap;
va_start(ap, fmt);
#if !_WIN32
if (!options.foreground)
if (forked)
vsyslog(LOG_INFO, fmt, ap);
else
#endif
{
fprintf(stderr, "rinetd: ");
vfprintf(stderr, fmt, ap);
}
va_end(ap);
}

Expand Down Expand Up @@ -889,7 +901,7 @@ RETSIGTYPE plumber(int s)
RETSIGTYPE hup(int s)
{
(void)s;
logInfo("Received SIGHUP, reloading configuration...\n");
logInfo("received SIGHUP, reloading configuration...\n");
/* Learn the new rules */
clearConfiguration();
readConfiguration(options.conf_file);
Expand Down Expand Up @@ -928,7 +940,7 @@ void registerPID(char const *pid_file_name)
}
return;
error:
logError("rinetd: Couldn't write to %s. PID was not logged (%m).\n",
logError("couldn't write to %s. PID was not logged (%m).\n",
pid_file_name);
#else
/* add other systems with wherever they register processes */
Expand Down

0 comments on commit ff41723

Please sign in to comment.