Skip to content

Commit ff41723

Browse files
committed
Improve error reporting. Fixes #15.
- Add an error message when the configuration file could not be found. - Log to stderr when running in the foreground.
1 parent d3dd353 commit ff41723

3 files changed

Lines changed: 67 additions & 45 deletions

File tree

src/parse.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \
3131
}
3232
#define PARSE_ERROR exit(1);
33+
#define MEMORY_ERROR { \
34+
logError("could not allocate memory when parsing configuration.\n"); \
35+
exit(1); \
36+
}
3337

3438
#if defined __clang__
3539
#pragma clang diagnostic ignored "-Wunused-parameter"
@@ -357,7 +361,7 @@ YY_ACTION(void) yy_1_sol(yycontext *yy, char *yytext, int yyleng)
357361
#define yythunkpos yy->__thunkpos
358362
yyprintf((stderr, "do yy_1_sol\n"));
359363
{
360-
#line 157
364+
#line 161
361365
++yy->currentLine; ;
362366
}
363367
#undef yythunkpos
@@ -371,9 +375,9 @@ YY_ACTION(void) yy_1_invalid_syntax(yycontext *yy, char *yytext, int yyleng)
371375
#define yythunkpos yy->__thunkpos
372376
yyprintf((stderr, "do yy_1_invalid_syntax\n"));
373377
{
374-
#line 131
378+
#line 135
375379

376-
logError("rinetd: invalid syntax at line %d: %s\n",
380+
logError("invalid syntax at line %d: %s\n",
377381
yy->currentLine, yytext);
378382
PARSE_ERROR; /* FIXME */
379383
;
@@ -389,7 +393,7 @@ YY_ACTION(void) yy_1_logcommon(yycontext *yy, char *yytext, int yyleng)
389393
#define yythunkpos yy->__thunkpos
390394
yyprintf((stderr, "do yy_1_logcommon\n"));
391395
{
392-
#line 126
396+
#line 130
393397

394398
logFormatCommon = 1;
395399
;
@@ -405,11 +409,11 @@ YY_ACTION(void) yy_1_pidlogfile(yycontext *yy, char *yytext, int yyleng)
405409
#define yythunkpos yy->__thunkpos
406410
yyprintf((stderr, "do yy_1_pidlogfile\n"));
407411
{
408-
#line 118
412+
#line 122
409413

410414
pidLogFileName = strdup(yytext);
411415
if (!pidLogFileName) {
412-
PARSE_ERROR;
416+
MEMORY_ERROR;
413417
}
414418
;
415419
}
@@ -424,11 +428,11 @@ YY_ACTION(void) yy_1_logfile(yycontext *yy, char *yytext, int yyleng)
424428
#define yythunkpos yy->__thunkpos
425429
yyprintf((stderr, "do yy_1_logfile\n"));
426430
{
427-
#line 110
431+
#line 114
428432

429433
logFileName = strdup(yytext);
430434
if (!logFileName) {
431-
PARSE_ERROR;
435+
MEMORY_ERROR;
432436
}
433437
;
434438
}
@@ -443,7 +447,7 @@ YY_ACTION(void) yy_1_auth_key(yycontext *yy, char *yytext, int yyleng)
443447
#define yythunkpos yy->__thunkpos
444448
yyprintf((stderr, "do yy_1_auth_key\n"));
445449
{
446-
#line 107
450+
#line 111
447451
yy->isAuthAllow = (yytext[0] == 'a'); ;
448452
}
449453
#undef yythunkpos
@@ -457,16 +461,16 @@ YY_ACTION(void) yy_1_auth_rule(yycontext *yy, char *yytext, int yyleng)
457461
#define yythunkpos yy->__thunkpos
458462
yyprintf((stderr, "do yy_1_auth_rule\n"));
459463
{
460-
#line 85
464+
#line 89
461465

462466
allRules = (Rule *)
463467
realloc(allRules, sizeof(Rule) * (allRulesCount + 1));
464468
if (!allRules) {
465-
PARSE_ERROR;
469+
MEMORY_ERROR;
466470
}
467471
allRules[allRulesCount].pattern = strdup(yytext);
468472
if (!allRules[allRulesCount].pattern) {
469-
PARSE_ERROR;
473+
MEMORY_ERROR;
470474
}
471475
allRules[allRulesCount].type = yy->isAuthAllow ? allowRule : denyRule;
472476
if (seTotal > 0) {
@@ -491,7 +495,7 @@ YY_ACTION(void) yy_3_proto(yycontext *yy, char *yytext, int yyleng)
491495
#define yythunkpos yy->__thunkpos
492496
yyprintf((stderr, "do yy_3_proto\n"));
493497
{
494-
#line 82
498+
#line 86
495499
yy->tmpProto = IPPROTO_TCP; ;
496500
}
497501
#undef yythunkpos
@@ -505,7 +509,7 @@ YY_ACTION(void) yy_2_proto(yycontext *yy, char *yytext, int yyleng)
505509
#define yythunkpos yy->__thunkpos
506510
yyprintf((stderr, "do yy_2_proto\n"));
507511
{
508-
#line 81
512+
#line 85
509513
yy->tmpProto = IPPROTO_UDP; ;
510514
}
511515
#undef yythunkpos
@@ -519,7 +523,7 @@ YY_ACTION(void) yy_1_proto(yycontext *yy, char *yytext, int yyleng)
519523
#define yythunkpos yy->__thunkpos
520524
yyprintf((stderr, "do yy_1_proto\n"));
521525
{
522-
#line 80
526+
#line 84
523527
yy->tmpProto = IPPROTO_TCP; ;
524528
}
525529
#undef yythunkpos
@@ -533,7 +537,7 @@ YY_ACTION(void) yy_1_port(yycontext *yy, char *yytext, int yyleng)
533537
#define yythunkpos yy->__thunkpos
534538
yyprintf((stderr, "do yy_1_port\n"));
535539
{
536-
#line 79
540+
#line 83
537541
yy->tmpPort = strdup(yytext); ;
538542
}
539543
#undef yythunkpos
@@ -547,7 +551,7 @@ YY_ACTION(void) yy_1_option_source(yycontext *yy, char *yytext, int yyleng)
547551
#define yythunkpos yy->__thunkpos
548552
yyprintf((stderr, "do yy_1_option_source\n"));
549553
{
550-
#line 75
554+
#line 79
551555
yy->sourceAddress = strdup(yytext); ;
552556
}
553557
#undef yythunkpos
@@ -561,7 +565,7 @@ YY_ACTION(void) yy_1_option_timeout(yycontext *yy, char *yytext, int yyleng)
561565
#define yythunkpos yy->__thunkpos
562566
yyprintf((stderr, "do yy_1_option_timeout\n"));
563567
{
564-
#line 74
568+
#line 78
565569
yy->serverTimeout = atoi(yytext); ;
566570
}
567571
#undef yythunkpos
@@ -575,7 +579,7 @@ YY_ACTION(void) yy_1_connect_port(yycontext *yy, char *yytext, int yyleng)
575579
#define yythunkpos yy->__thunkpos
576580
yyprintf((stderr, "do yy_1_connect_port\n"));
577581
{
578-
#line 69
582+
#line 73
579583
yy->connectPort = yy->tmpPort; yy->connectProto = yy->tmpProto; ;
580584
}
581585
#undef yythunkpos
@@ -589,7 +593,7 @@ YY_ACTION(void) yy_1_bind_port(yycontext *yy, char *yytext, int yyleng)
589593
#define yythunkpos yy->__thunkpos
590594
yyprintf((stderr, "do yy_1_bind_port\n"));
591595
{
592-
#line 68
596+
#line 72
593597
yy->bindPort = yy->tmpPort; yy->bindProto = yy->tmpProto; ;
594598
}
595599
#undef yythunkpos
@@ -603,7 +607,7 @@ YY_ACTION(void) yy_1_connect_address(yycontext *yy, char *yytext, int yyleng)
603607
#define yythunkpos yy->__thunkpos
604608
yyprintf((stderr, "do yy_1_connect_address\n"));
605609
{
606-
#line 67
610+
#line 71
607611
yy->connectAddress = strdup(yytext); ;
608612
}
609613
#undef yythunkpos
@@ -617,7 +621,7 @@ YY_ACTION(void) yy_1_bind_address(yycontext *yy, char *yytext, int yyleng)
617621
#define yythunkpos yy->__thunkpos
618622
yyprintf((stderr, "do yy_1_bind_address\n"));
619623
{
620-
#line 66
624+
#line 70
621625
yy->bindAddress = strdup(yytext); ;
622626
}
623627
#undef yythunkpos
@@ -631,7 +635,7 @@ YY_ACTION(void) yy_1_server_rule(yycontext *yy, char *yytext, int yyleng)
631635
#define yythunkpos yy->__thunkpos
632636
yyprintf((stderr, "do yy_1_server_rule\n"));
633637
{
634-
#line 51
638+
#line 55
635639

636640
addServer(yy->bindAddress, yy->bindPort, yy->bindProto,
637641
yy->connectAddress, yy->connectPort, yy->connectProto,
@@ -1398,21 +1402,22 @@ YY_PARSE(yycontext *) YYRELEASE(yycontext *yyctx)
13981402
}
13991403

14001404
#endif
1401-
#line 161 "parse.peg"
1405+
#line 165 "parse.peg"
14021406

14031407

14041408
void parseConfiguration(char const *file)
14051409
{
14061410
FILE *in = fopen(file, "r");
14071411
if (!in) {
1408-
PARSE_ERROR;
1412+
logError("could not open configuration file %s.\n", file);
1413+
exit(1);
14091414
}
14101415

14111416
yycontext ctx;
14121417
memset(&ctx, 0, sizeof(yycontext));
14131418
ctx.fp = in;
14141419
if (!yyparse(&ctx)) {
1415-
logError("invalid syntax on file %s, line %d.\n", file, -1);
1420+
logError("invalid syntax in file %s, line %d.\n", file, -1);
14161421
exit(1);
14171422
}
14181423
yyrelease(&ctx);

src/parse.peg

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \
3232
}
3333
#define PARSE_ERROR exit(1);
34+
#define MEMORY_ERROR { \
35+
logError("could not allocate memory when parsing configuration.\n"); \
36+
exit(1); \
37+
}
3438

3539
#if defined __clang__
3640
#pragma clang diagnostic ignored "-Wunused-parameter"
@@ -86,11 +90,11 @@ auth-rule = auth-key - < pattern >
8690
allRules = (Rule *)
8791
realloc(allRules, sizeof(Rule) * (allRulesCount + 1));
8892
if (!allRules) {
89-
PARSE_ERROR;
93+
MEMORY_ERROR;
9094
}
9195
allRules[allRulesCount].pattern = strdup(yytext);
9296
if (!allRules[allRulesCount].pattern) {
93-
PARSE_ERROR;
97+
MEMORY_ERROR;
9498
}
9599
allRules[allRulesCount].type = yy->isAuthAllow ? allowRule : denyRule;
96100
if (seTotal > 0) {
@@ -110,15 +114,15 @@ logfile = "logfile" - < filename >
110114
{
111115
logFileName = strdup(yytext);
112116
if (!logFileName) {
113-
PARSE_ERROR;
117+
MEMORY_ERROR;
114118
}
115119
}
116120

117121
pidlogfile = "pidlogfile" - < filename >
118122
{
119123
pidLogFileName = strdup(yytext);
120124
if (!pidLogFileName) {
121-
PARSE_ERROR;
125+
MEMORY_ERROR;
122126
}
123127
}
124128

@@ -129,7 +133,7 @@ logcommon = "logcommon"
129133

130134
invalid_syntax = < (!eol .)+ > eol
131135
{
132-
logError("rinetd: invalid syntax at line %d: %s\n",
136+
logError("invalid syntax at line %d: %s\n",
133137
yy->currentLine, yytext);
134138
PARSE_ERROR; /* FIXME */
135139
}
@@ -164,14 +168,15 @@ void parseConfiguration(char const *file)
164168
{
165169
FILE *in = fopen(file, "r");
166170
if (!in) {
167-
PARSE_ERROR;
171+
logError("could not open configuration file %s.\n", file);
172+
exit(1);
168173
}
169174

170175
yycontext ctx;
171176
memset(&ctx, 0, sizeof(yycontext));
172177
ctx.fp = in;
173178
if (!yyparse(&ctx)) {
174-
logError("invalid syntax on file %s, line %d.\n", file, -1);
179+
logError("invalid syntax in file %s, line %d.\n", file, -1);
175180
exit(1);
176181
}
177182
yyrelease(&ctx);

src/rinetd.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ enum {
108108
logDenied,
109109
};
110110

111-
RinetdOptions options = {
111+
static RinetdOptions options = {
112112
RINETD_CONFIG_FILE,
113113
0,
114114
};
115115

116+
static int forked = 0;
117+
116118
static void selectPass(void);
117119
static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socket);
118120
static void handleRead(ConnectionInfo *cnx, Socket *socket, Socket *other_socket);
@@ -160,15 +162,19 @@ int main(int argc, char *argv[])
160162

161163
readArgs(argc, argv, &options);
162164

165+
if (!options.foreground) {
163166
#if HAVE_DAEMON && !DEBUG
164-
if (!options.foreground && daemon(0, 0) != 0) {
165-
exit(0);
166-
}
167+
if (daemon(0, 0) != 0) {
168+
exit(0);
169+
}
170+
forked = 1;
167171
#elif HAVE_FORK && !DEBUG
168-
if (!options.foreground && fork() != 0) {
169-
exit(0);
170-
}
172+
if (fork() != 0) {
173+
exit(0);
174+
}
175+
forked = 1;
171176
#endif
177+
}
172178

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

193-
logInfo("Starting redirections...\n");
199+
logInfo("starting redirections...\n");
194200
while (1) {
195201
selectPass();
196202
}
@@ -203,11 +209,14 @@ void logError(char const *fmt, ...)
203209
va_list ap;
204210
va_start(ap, fmt);
205211
#if !_WIN32
206-
if (!options.foreground)
212+
if (forked)
207213
vsyslog(LOG_ERR, fmt, ap);
208214
else
209215
#endif
216+
{
217+
fprintf(stderr, "rinetd error: ");
210218
vfprintf(stderr, fmt, ap);
219+
}
211220
va_end(ap);
212221
}
213222

@@ -216,11 +225,14 @@ void logInfo(char const *fmt, ...)
216225
va_list ap;
217226
va_start(ap, fmt);
218227
#if !_WIN32
219-
if (!options.foreground)
228+
if (forked)
220229
vsyslog(LOG_INFO, fmt, ap);
221230
else
222231
#endif
232+
{
233+
fprintf(stderr, "rinetd: ");
223234
vfprintf(stderr, fmt, ap);
235+
}
224236
va_end(ap);
225237
}
226238

@@ -889,7 +901,7 @@ RETSIGTYPE plumber(int s)
889901
RETSIGTYPE hup(int s)
890902
{
891903
(void)s;
892-
logInfo("Received SIGHUP, reloading configuration...\n");
904+
logInfo("received SIGHUP, reloading configuration...\n");
893905
/* Learn the new rules */
894906
clearConfiguration();
895907
readConfiguration(options.conf_file);
@@ -928,7 +940,7 @@ void registerPID(char const *pid_file_name)
928940
}
929941
return;
930942
error:
931-
logError("rinetd: Couldn't write to %s. PID was not logged (%m).\n",
943+
logError("couldn't write to %s. PID was not logged (%m).\n",
932944
pid_file_name);
933945
#else
934946
/* add other systems with wherever they register processes */

0 commit comments

Comments
 (0)