From 942c2db6ba6648fb271986baa93eb98b77bf3aa2 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 29 Sep 2021 17:53:01 +0200 Subject: [PATCH] Corrected incorrect thread exiting in server.c --- src/model.c | 25 +++++++++++++------------ src/parsing.c | 11 +++++++++-- src/server.c | 10 ++++++++-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/model.c b/src/model.c index 3fdc969..a89af1a 100644 --- a/src/model.c +++ b/src/model.c @@ -96,7 +96,7 @@ int ModelUnload(int id) if (id-1 < loadedModelSize) { memmove(&loadedModel[id-1], &loadedModel[id-1] + sizeof(Model_t*), - loadedModelSize - (id-1)); + loadedModelSize - id); } // Decrement loaded model index @@ -113,13 +113,13 @@ int ModelUnload(int id) int ModelRun(int id) { if (id <= 0 || id > loadedModelSize) - return 0; + return -1; if (!loadedModel[id-1]->scheduler) - return 0; + return -2; if (loadedModel[id-1]->isRunning) - return 0; + return -3; loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; @@ -129,16 +129,19 @@ int ModelRun(int id) SchedInit(loadedModel[id-1]->scheduler); printLog("Model %d launched\n", id); - return 1; + return 0; } int ModelStop(int id) { if (id <= 0 || id > loadedModelSize) { - return 0; + return -1; } if (!loadedModel[id-1]->scheduler) { - return 0; + return -2; + } + if (!loadedModel[id-1]->isRunning) { + return -3; } // Stop model scheduler @@ -150,12 +153,12 @@ int ModelStop(int id) // Disable running bit loadedModel[id-1]->isRunning = false; - return 1; + return 0; } /* -------------------------------------------------------------------------- */ -void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots +void ModelAddToKnown(Model_t **newModel) { // increment index knownModelSize++; @@ -192,7 +195,7 @@ void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots knownModel[knownModelSize-1]->siteNumber = 0; } -void ModelDelete(int id) //XXX +void ModelDelete(int id) //XXX fragmentation { // Free a model structure free(knownModel[id-1]->name); @@ -209,8 +212,6 @@ void ModelDelete(int id) //XXX free(knownModel[id-1]); knownModel[id-1] = NULL; - - knownModelSize--; } void ModelShutdown(void) diff --git a/src/parsing.c b/src/parsing.c index b288db0..9e9e8f0 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -295,6 +295,11 @@ int parseParentFieldPropsXML (xmlDocPtr doc, } currentNode = currentNode->next; } + + free(id); + free(date); + free(author); + return 0; } @@ -353,7 +358,7 @@ int ParseModelIdentityXML(Model_t *model, Parameters_t *params) printLog("Preparsing model %s\n", model->name); // Opening document - xmlDocument = xmlParseFile(model->filename); + xmlDocument = xmlReadFile(model->filename, NULL, 0); if (xmlDocument == NULL) { printErr("Can't parse model file at '%s'.\n", model->filename); @@ -468,6 +473,7 @@ int ParseModelIdentityXML(Model_t *model, Parameters_t *params) free(version); xmlFreeDoc(xmlDocument); free(schemPath); + xmlCleanupParser(); return 0; } @@ -517,7 +523,7 @@ int ParseModelXML(Model_t *model) printLog("Parsing model %s\n", model->name); - xmlDocument = xmlParseFile(model->filename); + xmlDocument = xmlReadFile(model->filename, NULL, 0); if (xmlDocument == NULL) { printErr("Can't parse model file at '%s'.\n", model->filename); @@ -557,6 +563,7 @@ int ParseModelXML(Model_t *model) } xmlFreeDoc(xmlDocument); + xmlCleanupParser(); return 0; } diff --git a/src/server.c b/src/server.c index 400012e..4ee5352 100644 --- a/src/server.c +++ b/src/server.c @@ -45,7 +45,7 @@ void ServerInit(Server_t *server) #define SEND_BUFFER_SIZE 80 * 24 #define RECEIVE_BUFFER_SIZE 80 -void *serverCommunicationInstance(void *server) +void *serverCommunicationInstance(void *serverCom) { ServerCommunication_t *args; char **argv = NULL; @@ -54,7 +54,7 @@ void *serverCommunicationInstance(void *server) int tokenIndex, bytesReceived, clientPort; char clientIP[16]; - args = (ServerCommunication_t*) server; + args = (ServerCommunication_t*) serverCom; // Get ip address from client inet_ntop(AF_INET, @@ -249,6 +249,12 @@ static void *serverMain(void *server) } serverExiting: + for (int i; i < serverSlotIndex; i++) { + serverSlots[i].pleaseStop = true; + usleep(10000); + pthread_cancel(serverSlots[i].id); + pthread_join(serverSlots[i].id, NULL); + } close(args->sockfd); printLog("Server #%lu offline\n", args->id); return NULL;