Do not log to syslog when running in the foreground. Addresses #15.

This commit is contained in:
Sam Hocevar 2021-02-19 08:52:46 +01:00
parent 49bf544bad
commit d3dd35327f
4 changed files with 49 additions and 34 deletions

View File

@ -373,7 +373,7 @@ YY_ACTION(void) yy_1_invalid_syntax(yycontext *yy, char *yytext, int yyleng)
{ {
#line 131 #line 131
fprintf(stderr, "rinetd: invalid syntax at line %d: %s\n", logError("rinetd: invalid syntax at line %d: %s\n",
yy->currentLine, yytext); yy->currentLine, yytext);
PARSE_ERROR; /* FIXME */ PARSE_ERROR; /* FIXME */
; ;
@ -1412,8 +1412,7 @@ void parseConfiguration(char const *file)
memset(&ctx, 0, sizeof(yycontext)); memset(&ctx, 0, sizeof(yycontext));
ctx.fp = in; ctx.fp = in;
if (!yyparse(&ctx)) { if (!yyparse(&ctx)) {
syslog(LOG_ERR, "invalid syntax " logError("invalid syntax on file %s, line %d.\n", file, -1);
"on file %s, line %d.\n", file, -1);
exit(1); exit(1);
} }
yyrelease(&ctx); yyrelease(&ctx);

View File

@ -129,7 +129,7 @@ logcommon = "logcommon"
invalid_syntax = < (!eol .)+ > eol invalid_syntax = < (!eol .)+ > eol
{ {
fprintf(stderr, "rinetd: invalid syntax at line %d: %s\n", logError("rinetd: invalid syntax at line %d: %s\n",
yy->currentLine, yytext); yy->currentLine, yytext);
PARSE_ERROR; /* FIXME */ PARSE_ERROR; /* FIXME */
} }
@ -171,8 +171,7 @@ void parseConfiguration(char const *file)
memset(&ctx, 0, sizeof(yycontext)); memset(&ctx, 0, sizeof(yycontext));
ctx.fp = in; ctx.fp = in;
if (!yyparse(&ctx)) { if (!yyparse(&ctx)) {
syslog(LOG_ERR, "invalid syntax " logError("invalid syntax on file %s, line %d.\n", file, -1);
"on file %s, line %d.\n", file, -1);
exit(1); exit(1);
} }
yyrelease(&ctx); yyrelease(&ctx);

View File

@ -28,9 +28,11 @@
# elif HAVE_SYS_TIME_H # elif HAVE_SYS_TIME_H
# include <sys/time.h> # include <sys/time.h>
# endif # endif
# include <syslog.h>
#endif /* _WIN32 */ #endif /* _WIN32 */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
@ -188,7 +190,7 @@ int main(int argc, char *argv[])
registerPID(pidLogFileName ? pidLogFileName : RINETD_PID_FILE); registerPID(pidLogFileName ? pidLogFileName : RINETD_PID_FILE);
} }
syslog(LOG_INFO, "Starting redirections...\n"); logInfo("Starting redirections...\n");
while (1) { while (1) {
selectPass(); selectPass();
} }
@ -196,6 +198,32 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
void logError(char const *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
#if !_WIN32
if (!options.foreground)
vsyslog(LOG_ERR, fmt, ap);
else
#endif
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void logInfo(char const *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
#if !_WIN32
if (!options.foreground)
vsyslog(LOG_INFO, fmt, ap);
else
#endif
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static void clearConfiguration(void) { static void clearConfiguration(void) {
/* Remove references to server information */ /* Remove references to server information */
for (int i = 0; i < coTotal; ++i) { for (int i = 0; i < coTotal; ++i) {
@ -250,7 +278,7 @@ static void readConfiguration(char const *file) {
if (logFile) { if (logFile) {
setvbuf(logFile, NULL, _IONBF, 0); setvbuf(logFile, NULL, _IONBF, 0);
} else { } else {
syslog(LOG_ERR, "could not open %s to append (%m).\n", logError("could not open %s to append (%m).\n",
logFileName); logFileName);
} }
} }
@ -275,7 +303,7 @@ void addServer(char *bindAddress, char *bindPort, int bindProtocol,
si.fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); si.fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (si.fd == INVALID_SOCKET) { if (si.fd == INVALID_SOCKET) {
syslog(LOG_ERR, "couldn't create server socket! (%m)\n"); logError("couldn't create server socket! (%m)\n");
freeaddrinfo(ai); freeaddrinfo(ai);
exit(1); exit(1);
} }
@ -284,7 +312,7 @@ void addServer(char *bindAddress, char *bindPort, int bindProtocol,
setsockopt(si.fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&tmp, sizeof(tmp)); setsockopt(si.fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&tmp, sizeof(tmp));
if (bind(si.fd, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) { if (bind(si.fd, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) {
syslog(LOG_ERR, "couldn't bind to address %s port %s (%m)\n", logError("couldn't bind to address %s port %s (%m)\n",
bindAddress, bindPort); bindAddress, bindPort);
closesocket(si.fd); closesocket(si.fd);
freeaddrinfo(ai); freeaddrinfo(ai);
@ -294,7 +322,7 @@ void addServer(char *bindAddress, char *bindPort, int bindProtocol,
if (bindProtocol == IPPROTO_TCP) { if (bindProtocol == IPPROTO_TCP) {
if (listen(si.fd, RINETD_LISTEN_BACKLOG) == SOCKET_ERROR) { if (listen(si.fd, RINETD_LISTEN_BACKLOG) == SOCKET_ERROR) {
/* Warn -- don't exit. */ /* Warn -- don't exit. */
syslog(LOG_ERR, "couldn't listen to address %s port %s (%m)\n", logError("couldn't listen to address %s port %s (%m)\n",
bindAddress, bindPort); bindAddress, bindPort);
/* XXX: check whether this is correct */ /* XXX: check whether this is correct */
closesocket(si.fd); closesocket(si.fd);
@ -408,7 +436,7 @@ static ConnectionInfo *findAvailableConnection(void)
int oldTotal = coTotal; int oldTotal = coTotal;
setConnectionCount(coTotal * 4 / 3 + 8); setConnectionCount(coTotal * 4 / 3 + 8);
if (coTotal == oldTotal) { if (coTotal == oldTotal) {
syslog(LOG_ERR, "not enough memory to add slots. " logError("not enough memory to add slots. "
"Currently %d slots.\n", coTotal); "Currently %d slots.\n", coTotal);
/* Go back to the previous total number of slots */ /* Go back to the previous total number of slots */
return NULL; return NULL;
@ -645,7 +673,7 @@ static void handleAccept(ServerInfo const *srv)
/* In TCP mode, get remote address using accept(). */ /* In TCP mode, get remote address using accept(). */
nfd = accept(srv->fd, (struct sockaddr *)&addr, &addrlen); nfd = accept(srv->fd, (struct sockaddr *)&addr, &addrlen);
if (nfd == INVALID_SOCKET) { if (nfd == INVALID_SOCKET) {
syslog(LOG_ERR, "accept(%llu): %m\n", (long long unsigned int)srv->fd); logError("accept(%llu): %m\n", (long long unsigned int)srv->fd);
logEvent(NULL, srv, logAcceptFailed); logEvent(NULL, srv, logAcceptFailed);
return; return;
} }
@ -666,7 +694,7 @@ static void handleAccept(ServerInfo const *srv)
if (GetLastError() == WSAEINPROGRESS) { if (GetLastError() == WSAEINPROGRESS) {
return; return;
} }
syslog(LOG_ERR, "recvfrom(%llu): %m\n", (long long unsigned int)srv->fd); logError("recvfrom(%llu): %m\n", (long long unsigned int)srv->fd);
logEvent(NULL, srv, logAcceptFailed); logEvent(NULL, srv, logAcceptFailed);
return; return;
} }
@ -725,7 +753,7 @@ static void handleAccept(ServerInfo const *srv)
struct addrinfo* to = srv->toAddrInfo; struct addrinfo* to = srv->toAddrInfo;
cnx->local.fd = socket(to->ai_family, to->ai_socktype, to->ai_protocol); cnx->local.fd = socket(to->ai_family, to->ai_socktype, to->ai_protocol);
if (cnx->local.fd == INVALID_SOCKET) { if (cnx->local.fd == INVALID_SOCKET) {
syslog(LOG_ERR, "socket(): %m\n"); logError("socket(): %m\n");
if (cnx->remote.protocol == IPPROTO_TCP) if (cnx->remote.protocol == IPPROTO_TCP)
closesocket(cnx->remote.fd); closesocket(cnx->remote.fd);
cnx->remote.fd = INVALID_SOCKET; cnx->remote.fd = INVALID_SOCKET;
@ -741,7 +769,7 @@ static void handleAccept(ServerInfo const *srv)
if (srv->sourceAddrInfo) { if (srv->sourceAddrInfo) {
if (bind(cnx->local.fd, srv->sourceAddrInfo->ai_addr, if (bind(cnx->local.fd, srv->sourceAddrInfo->ai_addr,
srv->sourceAddrInfo->ai_addrlen) == SOCKET_ERROR) { srv->sourceAddrInfo->ai_addrlen) == SOCKET_ERROR) {
syslog(LOG_ERR, "bind(): %m\n"); logError("bind(): %m\n");
} }
} }
@ -861,7 +889,7 @@ RETSIGTYPE plumber(int s)
RETSIGTYPE hup(int s) RETSIGTYPE hup(int s)
{ {
(void)s; (void)s;
syslog(LOG_INFO, "Received SIGHUP, reloading configuration...\n"); logInfo("Received SIGHUP, reloading configuration...\n");
/* Learn the new rules */ /* Learn the new rules */
clearConfiguration(); clearConfiguration();
readConfiguration(options.conf_file); readConfiguration(options.conf_file);
@ -891,8 +919,6 @@ void registerPID(char const *pid_file_name)
FILE *pid_file = fopen(pid_file_name, "w"); FILE *pid_file = fopen(pid_file_name, "w");
if (pid_file == NULL) { if (pid_file == NULL) {
/* non-fatal, non-Linux may lack /var/run... */ /* non-fatal, non-Linux may lack /var/run... */
fprintf(stderr, "rinetd: Couldn't write to "
"%s. PID was not logged.\n", pid_file_name);
goto error; goto error;
} else { } else {
fprintf(pid_file, "%d\n", getpid()); fprintf(pid_file, "%d\n", getpid());
@ -902,8 +928,8 @@ void registerPID(char const *pid_file_name)
} }
return; return;
error: error:
syslog(LOG_ERR, "Couldn't write to " logError("rinetd: Couldn't write to %s. PID was not logged (%m).\n",
"%s. PID was not logged (%m).\n", pid_file_name); pid_file_name);
#else #else
/* add other systems with wherever they register processes */ /* add other systems with wherever they register processes */
(void)pid_file_name; (void)pid_file_name;
@ -942,7 +968,7 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
} }
if (result==logNotAllowed || result==logDenied) if (result==logNotAllowed || result==logDenied)
syslog(LOG_INFO, "%s %s\n" logInfo("%s %s\n"
, addressText , addressText
, logMessages[result]); , logMessages[result]);
if (logFile) { if (logFile) {
@ -1009,7 +1035,7 @@ static int readArgs (int argc, char **argv, RinetdOptions *options)
case 'c': case 'c':
options->conf_file = optarg; options->conf_file = optarg;
if (!options->conf_file) { if (!options->conf_file) {
syslog(LOG_ERR, "Not enough memory to " fprintf(stderr, "Not enough memory to "
"launch rinetd.\n"); "launch rinetd.\n");
exit(1); exit(1);
} }

View File

@ -8,17 +8,6 @@
#pragma once #pragma once
/* Syslog feature */
#if _WIN32
# include <stdio.h>
# define syslog fprintf
# define LOG_ERR stderr
# define LOG_INFO stdout
#else
# include <syslog.h>
#endif /* _WIN32 */
#include <stdint.h> #include <stdint.h>
/* Constants */ /* Constants */
@ -46,6 +35,8 @@ extern FILE *logFile;
/* Functions */ /* Functions */
void logError(char const *fmt, ...);
void logInfo(char const *fmt, ...);
void addServer(char *bindAddress, char *bindPort, int bindProtocol, void addServer(char *bindAddress, char *bindPort, int bindProtocol,
char *connectAddress, char *connectPort, int connectProtocol, char *connectAddress, char *connectPort, int connectProtocol,
int serverTimeout, char *sourceAddress); int serverTimeout, char *sourceAddress);