Free all memory when quitting, and quit when SIGINT is received.

This commit is contained in:
Sam Hocevar 2016-02-06 18:40:02 +01:00
parent 830b7e5fd1
commit 65273124f3

View File

@ -153,6 +153,7 @@ static void refuse(ConnectionInfo *cnx, int logCode);
static int readArgs (int argc, char **argv, RinetdOptions *options); static int readArgs (int argc, char **argv, RinetdOptions *options);
static int getConfLine(FILE *in, char *line, int space, int *lnum); static int getConfLine(FILE *in, char *line, int space, int *lnum);
static int patternBad(char const *pattern); static int patternBad(char const *pattern);
static void clearConfiguration(void);
static void readConfiguration(void); static void readConfiguration(void);
static void registerPID(void); static void registerPID(void);
@ -162,7 +163,7 @@ static struct tm *get_gmtoff(int *tz);
/* Signal handlers */ /* Signal handlers */
static RETSIGTYPE plumber(int s); static RETSIGTYPE plumber(int s);
static RETSIGTYPE hup(int s); static RETSIGTYPE hup(int s);
static RETSIGTYPE term(int s); static RETSIGTYPE quit(int s);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -205,7 +206,8 @@ int main(int argc, char *argv[])
signal(SIGHUP, hup); signal(SIGHUP, hup);
#endif #endif
#endif /* WIN32 */ #endif /* WIN32 */
signal(SIGTERM, term); signal(SIGINT, quit);
signal(SIGTERM, quit);
readConfiguration(); readConfiguration();
registerPID(); registerPID();
syslog(LOG_INFO, "Starting redirections..."); syslog(LOG_INFO, "Starting redirections...");
@ -222,10 +224,7 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
static void readConfiguration(void) static void clearConfiguration(void) {
{
FILE *in;
char line[16384];
/* Close existing server sockets. */ /* Close existing server sockets. */
for (int i = 0; i < seTotal; ++i) { for (int i = 0; i < seTotal; ++i) {
ServerInfo *srv = &seInfo[i]; ServerInfo *srv = &seInfo[i];
@ -252,12 +251,16 @@ static void readConfiguration(void)
logFileName = NULL; logFileName = NULL;
free(pidLogFileName); free(pidLogFileName);
pidLogFileName = NULL; pidLogFileName = NULL;
}
static void readConfiguration(void) {
/* Parse the configuration file. */ /* Parse the configuration file. */
in = fopen(options.conf_file, "r"); FILE *in = fopen(options.conf_file, "r");
if (!in) { if (!in) {
goto lowMemory; goto lowMemory;
} }
for (int lnum = 0; ; ) { for (int lnum = 0; ; ) {
char line[16384];
if (!getConfLine(in, line, sizeof(line), &lnum)) { if (!getConfLine(in, line, sizeof(line), &lnum)) {
break; break;
} }
@ -945,6 +948,7 @@ RETSIGTYPE hup(int s)
(void)s; (void)s;
syslog(LOG_INFO, "Received SIGHUP, reloading configuration..."); syslog(LOG_INFO, "Received SIGHUP, reloading configuration...");
/* Learn the new rules */ /* Learn the new rules */
clearConfiguration();
readConfiguration(); readConfiguration();
#ifndef HAVE_SIGACTION #ifndef HAVE_SIGACTION
/* And reinstall the signal handler */ /* And reinstall the signal handler */
@ -953,13 +957,16 @@ RETSIGTYPE hup(int s)
} }
#endif /* WIN32 */ #endif /* WIN32 */
RETSIGTYPE term(int s) RETSIGTYPE quit(int s)
{ {
(void)s; (void)s;
/* Obey the request, but first flush the log */ /* Obey the request, but first flush the log */
if (logFile) { if (logFile) {
fclose(logFile); fclose(logFile);
} }
/* ...and get rid of memory allocations */
setConnectionCount(0);
clearConfiguration();
exit(0); exit(0);
} }
@ -1082,7 +1089,7 @@ static int readArgs (int argc, char **argv, RinetdOptions *options)
} }
switch (c) { switch (c) {
case 'c': case 'c':
options->conf_file = strdup(optarg); options->conf_file = optarg;
if (!options->conf_file) { if (!options->conf_file) {
syslog(LOG_ERR, "Not enough memory to " syslog(LOG_ERR, "Not enough memory to "
"launch rinetd.\n"); "launch rinetd.\n");
@ -1090,7 +1097,7 @@ static int readArgs (int argc, char **argv, RinetdOptions *options)
} }
break; break;
case 'f': case 'f':
options->foreground=1; options->foreground = 1;
break; break;
case 'h': case 'h':
printf("Usage: rinetd [OPTION]\n" printf("Usage: rinetd [OPTION]\n"