From 008596161453e64354973268125058b49aee60d3 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 13 Dec 2013 21:07:48 +0800 Subject: [PATCH] Server - Fixed leaked file handles in both itemdb and modbus - Also improved logging in coke handler --- src/server/common.h | 4 ++++ src/server/handler_coke.c | 15 ++++++++++----- src/server/itemdb.c | 1 + src/server/server.c | 3 --- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/server/common.h b/src/server/common.h index 7ad01c5..74c42d9 100644 --- a/src/server/common.h +++ b/src/server/common.h @@ -96,8 +96,12 @@ extern int DispenseDonate(int ActualUser, int User, int Ammount, const char *Rea extern int DispenseUpdateItem(int User, tItem *Item, const char *NewName, int NewPrice); // --- Logging --- +// to syslog extern void Log_Error(const char *Format, ...); extern void Log_Info(const char *Format, ...); +// To stdout +#define Debug_Notice(msg, v...) fprintf(stderr, "%08llun: "msg"\n", (unsigned long long)time(NULL) ,##v) +#define Debug_Debug(msg, v...) fprintf(stderr, "%08llud: "msg"\n", (unsigned long long)time(NULL) ,##v) // --- Config Database --- extern void Config_ParseFile(const char *Filename); diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index 6908aec..ff051c8 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -107,9 +107,9 @@ int Coke_DoDispense(int UNUSED(User), int Item) if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod ) { int delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime); - printf("Wait %i seconds?\n", delay); + Debug_Debug("Waiting for %i seconds (rate limit)", delay); sleep( delay ); - printf("wait done\n"); + Debug_Debug("wait done"); } gtCoke_LastDispenseTime = time(NULL); @@ -133,8 +133,11 @@ int Coke_int_ConnectToPLC(void) return 1; } } - printf("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress); - fprintf(stderr, "Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress); + else { + // Preven resource leaks + modbus_close(gCoke_Modbus); + } + Debug_Notice("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress); if( modbus_connect(gCoke_Modbus) ) { @@ -227,7 +230,8 @@ int Coke_int_DropSlot(int Slot) if( res == 0 ) { // Oops!, no drink - printf("Drink dispense failed, bit lowered too quickly\n"); + Log_Error("Drink dispense failed, bit lowered too quickly"); + Debug_Notice("Drink dispense failed, bit lowered too quickly"); return 1; } @@ -255,6 +259,7 @@ int _WriteBit(int BitNum, uint8_t Value) return -1; if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 ) return 0; + // Error case if( Coke_int_ConnectToPLC() ) return -1; if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 ) diff --git a/src/server/itemdb.c b/src/server/itemdb.c index 632eb50..06c8257 100644 --- a/src/server/itemdb.c +++ b/src/server/itemdb.c @@ -223,6 +223,7 @@ void Items_ReadFromFile(void) free(gaItems); gaItems = NULL; } + fclose(fp); // Replace with new giNumItems = numItems; diff --git a/src/server/server.c b/src/server/server.c index 6077b9b..e76c459 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -28,9 +28,6 @@ #define PIDFILE "/var/run/dispsrv.pid" -#define Debug_Notice(msg, v...) printf("%08llun: "msg"\n", (unsigned long long)time(NULL) ,##v) -#define Debug_Debug(msg, v...) printf("%08llud: "msg"\n", (unsigned long long)time(NULL) ,##v) - // Statistics #define MAX_CONNECTION_QUEUE 5 #define INPUT_BUFFER_SIZE 256 -- 2.20.1