diff --git a/build/modules/configRead.o b/build/modules/configRead.o index 9ebe4bc..b55ba2a 100644 Binary files a/build/modules/configRead.o and b/build/modules/configRead.o differ diff --git a/build/modules/connection.o b/build/modules/connection.o index 719c25f..296d3f8 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/daemon.o b/build/modules/daemon.o index 5336d71..d372df3 100644 Binary files a/build/modules/daemon.o and b/build/modules/daemon.o differ diff --git a/build/modules/dynamicMemory.o b/build/modules/dynamicMemory.o index 9e8a449..ced9d33 100644 Binary files a/build/modules/dynamicMemory.o and b/build/modules/dynamicMemory.o differ diff --git a/build/modules/dynamicVectors.o b/build/modules/dynamicVectors.o index ba7ff14..89bb83c 100644 Binary files a/build/modules/dynamicVectors.o and b/build/modules/dynamicVectors.o differ diff --git a/build/modules/errorHandling.o b/build/modules/errorHandling.o index 965b9e4..4df7f04 100644 Binary files a/build/modules/errorHandling.o and b/build/modules/errorHandling.o differ diff --git a/build/modules/fileManagement.o b/build/modules/fileManagement.o index 056e586..9ec71c5 100644 Binary files a/build/modules/fileManagement.o and b/build/modules/fileManagement.o differ diff --git a/build/modules/ftpCommandElaborate.o b/build/modules/ftpCommandElaborate.o index 7dbf47e..4a4f664 100644 Binary files a/build/modules/ftpCommandElaborate.o and b/build/modules/ftpCommandElaborate.o differ diff --git a/build/modules/ftpData.o b/build/modules/ftpData.o index 8534cd7..f71b256 100644 Binary files a/build/modules/ftpData.o and b/build/modules/ftpData.o differ diff --git a/build/modules/ftpServer.o b/build/modules/ftpServer.o index 57d6186..6dff224 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/modules/logFunctions.o b/build/modules/logFunctions.o index 28f97b6..a8ef6e7 100644 Binary files a/build/modules/logFunctions.o and b/build/modules/logFunctions.o differ diff --git a/build/modules/openSsl.o b/build/modules/openSsl.o index aa4528a..ab3adde 100644 Binary files a/build/modules/openSsl.o and b/build/modules/openSsl.o differ diff --git a/build/modules/signals.o b/build/modules/signals.o index a5f5a54..5106668 100644 Binary files a/build/modules/signals.o and b/build/modules/signals.o differ diff --git a/build/uFTP b/build/uFTP index cd268d4..89bb260 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index 8cfe40f..6457221 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -22,7 +22,6 @@ * THE SOFTWARE. */ - #include #include #include @@ -1175,8 +1174,8 @@ int parseCommandCdup(ftpDataType * data, int socketId) { int returnCode; - FILE_DirectoryToParent(&data->clients[socketId].login.absolutePath.text); - FILE_DirectoryToParent(&data->clients[socketId].login.ftpPath.text); + FILE_DirectoryToParent(&data->clients[socketId].login.absolutePath.text, &data->clients[socketId].memoryTable); + FILE_DirectoryToParent(&data->clients[socketId].login.ftpPath.text, &data->clients[socketId].memoryTable); data->clients[socketId].login.absolutePath.textLen = strlen(data->clients[socketId].login.absolutePath.text); data->clients[socketId].login.ftpPath.textLen = strlen(data->clients[socketId].login.ftpPath.text); diff --git a/ftpData.c b/ftpData.c index c8f2ff2..12c7dab 100644 --- a/ftpData.c +++ b/ftpData.c @@ -74,7 +74,7 @@ void setDynamicStringDataType(dynamicStringDataType *dynamicString, char *theStr if (dynamicString->textLen == 0) { //printf("\nMemory data address before memset call : %lld", memoryTable); - dynamicString->text = (char *) DYNMEM_malloc (((sizeof(char) * stringLen) + 1), &*memoryTable); + dynamicString->text = (char *) DYNMEM_malloc (((sizeof(char) * stringLen) + 1), &*memoryTable, "setDynamicString"); //printf("\nMemory data address after memset call : %lld", memoryTable); memset(dynamicString->text, 0, stringLen + 1); memcpy(dynamicString->text, theString, stringLen); @@ -235,7 +235,7 @@ int writeListDataInfoToSocket(ftpDataType *ftpData, int clientId, int *filesNumb int i, x, returnCode; int fileAndFoldersCount = 0; char **fileList = NULL; - FILE_GetDirectoryInodeList(ftpData->clients[clientId].listPath.text, &fileList, &fileAndFoldersCount, 0); + FILE_GetDirectoryInodeList(ftpData->clients[clientId].listPath.text, &fileList, &fileAndFoldersCount, 0, &*memoryTable); *filesNumber = fileAndFoldersCount; returnCode = socketWorkerPrintf(ftpData, clientId, "sds", "total ", fileAndFoldersCount ,"\r\n"); @@ -287,16 +287,16 @@ int writeListDataInfoToSocket(ftpDataType *ftpData, int clientId, int *filesNumb //printf("\nFILE SIZE : %lld", data.fileSize); - data.owner = FILE_GetOwner(fileList[i]); - data.groupOwner = FILE_GetGroupOwner(fileList[i]); + data.owner = FILE_GetOwner(fileList[i], &*memoryTable); + data.groupOwner = FILE_GetGroupOwner(fileList[i], &*memoryTable); data.fileNameWithPath = fileList[i]; data.fileNameNoPath = FILE_GetFilenameFromPath(fileList[i]); - data.inodePermissionString = FILE_GetListPermissionsString(fileList[i]); + data.inodePermissionString = FILE_GetListPermissionsString(fileList[i], &*memoryTable); data.lastModifiedData = FILE_GetLastModifiedData(fileList[i]); if (strlen(data.fileNameNoPath) > 0) { - data.finalStringPath = (char *) DYNMEM_malloc (strlen(data.fileNameNoPath)+1, &*memoryTable); + data.finalStringPath = (char *) DYNMEM_malloc (strlen(data.fileNameNoPath)+1, &*memoryTable, "dataFinalPath"); strcpy(data.finalStringPath, data.fileNameNoPath); } @@ -306,7 +306,7 @@ int writeListDataInfoToSocket(ftpDataType *ftpData, int clientId, int *filesNumb { int len = 0; data.isLink = 1; - data.linkPath = (char *) DYNMEM_malloc (CLIENT_COMMAND_STRING_SIZE*sizeof(char), &*memoryTable); + data.linkPath = (char *) DYNMEM_malloc (CLIENT_COMMAND_STRING_SIZE*sizeof(char), &*memoryTable, "dataLinkPath"); if ((len = readlink (fileList[i], data.linkPath, CLIENT_COMMAND_STRING_SIZE)) > 0) { data.linkPath[len] = 0; @@ -367,28 +367,28 @@ int writeListDataInfoToSocket(ftpDataType *ftpData, int clientId, int *filesNumb if (data.fileNameWithPath != NULL) - free(data.fileNameWithPath); + DYNMEM_free(data.fileNameWithPath, &*memoryTable); if (data.linkPath != NULL) - free(data.linkPath); + DYNMEM_free(data.linkPath, &*memoryTable); if (data.finalStringPath != NULL) - free(data.finalStringPath); + DYNMEM_free(data.finalStringPath, &*memoryTable); if (data.owner != NULL) - free(data.owner); + DYNMEM_free(data.owner, &*memoryTable); if (data.groupOwner != NULL) - free(data.groupOwner); + DYNMEM_free(data.groupOwner, &*memoryTable); if (data.inodePermissionString != NULL) - free(data.inodePermissionString); + DYNMEM_free(data.inodePermissionString, &*memoryTable); if (returnCode <= 0) { for (x = i+1; x < fileAndFoldersCount; x++) - free (fileList[x]); - free (fileList); + DYNMEM_free (fileList[x], &*memoryTable); + DYNMEM_free (fileList, &*memoryTable); return -1; } @@ -396,7 +396,7 @@ int writeListDataInfoToSocket(ftpDataType *ftpData, int clientId, int *filesNumb if (fileList != NULL) { - free (fileList); + DYNMEM_free (fileList, &*memoryTable); } return 1; @@ -429,7 +429,7 @@ void getListDataInfo(char * thePath, DYNV_VectorGenericDataType *directoryInfo, int i; int fileAndFoldersCount = 0; ftpListDataType data; - FILE_GetDirectoryInodeList(thePath, &data.fileList, &fileAndFoldersCount, 0); + FILE_GetDirectoryInodeList(thePath, &data.fileList, &fileAndFoldersCount, 0, &*memoryTable); //printf("\nNUMBER OF FILES: %d", fileAndFoldersCount); //fflush(0); @@ -477,16 +477,16 @@ void getListDataInfo(char * thePath, DYNV_VectorGenericDataType *directoryInfo, printf("\nFILE SIZE : %lld", data.fileSize); - data.owner = FILE_GetOwner(data.fileList[i]); - data.groupOwner = FILE_GetGroupOwner(data.fileList[i]); + data.owner = FILE_GetOwner(data.fileList[i], &*memoryTable); + data.groupOwner = FILE_GetGroupOwner(data.fileList[i], &*memoryTable); data.fileNameWithPath = data.fileList[i]; data.fileNameNoPath = FILE_GetFilenameFromPath(data.fileList[i]); - data.inodePermissionString = FILE_GetListPermissionsString(data.fileList[i]); + data.inodePermissionString = FILE_GetListPermissionsString(data.fileList[i], &*memoryTable); data.lastModifiedData = FILE_GetLastModifiedData(data.fileList[i]); if (strlen(data.fileNameNoPath) > 0) { - data.finalStringPath = (char *) DYNMEM_malloc (strlen(data.fileNameNoPath)+1, &*memoryTable); + data.finalStringPath = (char *) DYNMEM_malloc (strlen(data.fileNameNoPath)+1, &*memoryTable, "FinalStringPath"); strcpy(data.finalStringPath, data.fileNameNoPath); } @@ -496,7 +496,7 @@ void getListDataInfo(char * thePath, DYNV_VectorGenericDataType *directoryInfo, { int len = 0; data.isLink = 1; - data.linkPath = (char *) DYNMEM_malloc (CLIENT_COMMAND_STRING_SIZE*sizeof(char), &*memoryTable); + data.linkPath = (char *) DYNMEM_malloc (CLIENT_COMMAND_STRING_SIZE*sizeof(char), &*memoryTable, "data.linkPath"); if ((len = readlink (data.fileList[i], data.linkPath, CLIENT_COMMAND_STRING_SIZE)) > 0) { data.linkPath[len] = 0; @@ -529,38 +529,38 @@ void getListDataInfo(char * thePath, DYNV_VectorGenericDataType *directoryInfo, } } -void deleteListDataInfoVector(void *TheElementToDelete) +void deleteListDataInfoVector(DYNV_VectorGenericDataType *theVector) { - ftpListDataType *data = (ftpListDataType *)TheElementToDelete; - if (data->owner != NULL) + int i; + for (i = 0; i < theVector->Size; i++) { - free(data->owner); - } - - if (data->groupOwner != NULL) - { - free(data->groupOwner); - } + ftpListDataType *data = (ftpListDataType *)theVector->Data[i]; - if (data->inodePermissionString != NULL) - { - free(data->inodePermissionString); - } - - if (data->fileNameWithPath != NULL) - { - free(data->fileNameWithPath); - } - - if (data->finalStringPath != NULL) - { - free(data->finalStringPath); - } - - if (data->linkPath != NULL) - { - free(data->linkPath); + if (data->owner != NULL) + { + DYNMEM_free(data->owner, &theVector->memoryTable); + } + if (data->groupOwner != NULL) + { + DYNMEM_free(data->groupOwner, &theVector->memoryTable); + } + if (data->inodePermissionString != NULL) + { + DYNMEM_free(data->inodePermissionString, &theVector->memoryTable); + } + if (data->fileNameWithPath != NULL) + { + DYNMEM_free(data->fileNameWithPath, &theVector->memoryTable); + } + if (data->finalStringPath != NULL) + { + DYNMEM_free(data->finalStringPath, &theVector->memoryTable); + } + if (data->linkPath != NULL) + { + DYNMEM_free(data->linkPath, &theVector->memoryTable); + } } } @@ -632,7 +632,7 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization) { lastToDestroy = ((ftpListDataType *)data->clients[clientId].workerData.directoryInfo.Data[0])->fileList; data->clients[clientId].workerData.directoryInfo.Destroy(&data->clients[clientId].workerData.directoryInfo, deleteListDataInfoVector); - free(lastToDestroy); + DYNMEM_free(lastToDestroy, &data->clients[clientId].workerData.memoryTable); } #ifdef OPENSSL_ENABLED @@ -706,6 +706,9 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) //data->clients[clientId].workerData.ssl = SSL_new(data->ctx); data->clients[clientId].ssl = SSL_new(data->serverCtx); #endif + + + printf("\nclient memory table :%lld", data->clients[clientId].memoryTable); } int compareStringCaseInsensitive(char * stringIn, char * stringRef, int stringLenght) diff --git a/ftpData.h b/ftpData.h index 7a6f5c1..11bf3b9 100644 --- a/ftpData.h +++ b/ftpData.h @@ -273,7 +273,7 @@ int writeListDataInfoToSocket(ftpDataType *data, int clientId, int *filesNumber, int searchInLoginFailsVector(void *loginFailsVector, void *element); void deleteLoginFailsData(void *element); -void deleteListDataInfoVector(void *TheElementToDelete); +void deleteListDataInfoVector(DYNV_VectorGenericDataType *theVector); void resetWorkerData(ftpDataType *data, int clientId, int isInitialization); void resetClientData(ftpDataType *data, int clientId, int isInitialization); int compareStringCaseInsensitive(char *stringIn, char* stringRef, int stringLenght); diff --git a/ftpServer.c b/ftpServer.c index 28eeaa2..fd032a1 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -115,6 +115,11 @@ void workerCleanup(void *socketId) returnCode = close(ftpData.clients[theSocketId].workerData.passiveListeningSocket); resetWorkerData(&ftpData, theSocketId, 0); printf("\nWorker cleaned!"); + printf("\nWorker memory table :%lld", ftpData.clients[theSocketId].workerData.memoryTable); + if (ftpData.clients[theSocketId].workerData.memoryTable != NULL) + printf("\nMemory table element label: %s", ftpData.clients[theSocketId].workerData.memoryTable->theName); + else + printf("\nNo data to print"); } void *connectionWorkerHandle(void * socketId) @@ -466,7 +471,7 @@ void runFtpServer(void) signalHandlerInstall(); /*Read the configuration file */ - configurationRead(&ftpData.ftpParameters); + configurationRead(&ftpData.ftpParameters, &ftpData.generalDynamicMemoryTable); /* apply the reden configuration */ applyConfiguration(&ftpData.ftpParameters); @@ -487,9 +492,26 @@ void runFtpServer(void) //Endless loop ftp process while (1) { - printf("\nUsed memory : %lld", DYNMEM_GetTotalMemory()); + int memCount = 0; + for (memCount = 0; memCount < ftpData.ftpParameters.maxClients; memCount++) + { + if (ftpData.clients[memCount].memoryTable != NULL) + { + printf("\nftpData.clients[%d].memoryTable = %s", memCount, ftpData.clients[memCount].memoryTable->theName); + } + + if (ftpData.clients[memCount].workerData.memoryTable != NULL) + { + printf("\nftpData.clients[%d].workerData.memoryTable = %s", memCount, ftpData.clients[memCount].workerData.memoryTable->theName); + } + + if (ftpData.clients[memCount].workerData.directoryInfo.memoryTable != NULL) + { + printf("\nftpData.clients[%d].workerData.directoryInfo.memoryTable = %s", memCount, ftpData.clients[memCount].workerData.directoryInfo.memoryTable->theName); + } + } /* waits for socket activity, if no activity then checks for client socket timeouts */ if (selectWait(&ftpData) == 0) { @@ -886,8 +908,13 @@ void deallocateMemory(void) DYNMEM_freeAll(&ftpData.clients[i].workerData.memoryTable); } + DYNMEM_freeAll(&ftpData.loginFailsVector.memoryTable); DYNMEM_freeAll(&ftpData.ftpParameters.usersVector.memoryTable); DYNMEM_freeAll(&ftpData.generalDynamicMemoryTable); + + + printf("\n\nUsed memory at end: %lld", DYNMEM_GetTotalMemory()); + //printf("\n ftpData.generalDynamicMemoryTable = %ld", ftpData.generalDynamicMemoryTable); #ifdef OPENSSL_ENABLED SSL_CTX_free(ftpData.serverCtx); diff --git a/library/configRead.c b/library/configRead.c index 0c49eaf..48996cc 100644 --- a/library/configRead.c +++ b/library/configRead.c @@ -40,7 +40,7 @@ /* Private Functions */ static int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_VectorGenericDataType *parametersVector); static int searchParameter(char *name, DYNV_VectorGenericDataType *parametersVector); -static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *parametersVector); +static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *parametersVector, DYNMEM_MemoryTable_DataType ** memoryTable); void destroyConfigurationVectorElement(DYNV_VectorGenericDataType *theVector) { @@ -72,7 +72,7 @@ int searchUser(char *name, DYNV_VectorGenericDataType *usersVector) return returnCode; } -void configurationRead(ftpParameters_DataType *ftpParameters) +void configurationRead(ftpParameters_DataType *ftpParameters, DYNMEM_MemoryTable_DataType **memoryTable) { int returnCode = 0; DYNV_VectorGenericDataType configParameters; @@ -81,12 +81,12 @@ void configurationRead(ftpParameters_DataType *ftpParameters) if (FILE_IsFile(LOCAL_CONFIGURATION_FILENAME) == 1) { printf("\nReading configuration from \n -> %s \n", LOCAL_CONFIGURATION_FILENAME); - returnCode = readConfigurationFile(LOCAL_CONFIGURATION_FILENAME, &configParameters); + returnCode = readConfigurationFile(LOCAL_CONFIGURATION_FILENAME, &configParameters, &*memoryTable); } else if (FILE_IsFile(DEFAULT_CONFIGURATION_FILENAME) == 1) { printf("\nReading configuration from \n -> %s\n", DEFAULT_CONFIGURATION_FILENAME); - returnCode = readConfigurationFile(DEFAULT_CONFIGURATION_FILENAME, &configParameters); + returnCode = readConfigurationFile(DEFAULT_CONFIGURATION_FILENAME, &configParameters, &*memoryTable); } if (returnCode == 1) @@ -142,7 +142,7 @@ void initFtpData(ftpDataType *ftpData) #endif ftpData->connectedClients = 0; - ftpData->clients = (clientDataType *) DYNMEM_malloc((sizeof(clientDataType) * ftpData->ftpParameters.maxClients), &ftpData->generalDynamicMemoryTable); + ftpData->clients = (clientDataType *) DYNMEM_malloc((sizeof(clientDataType) * ftpData->ftpParameters.maxClients), &ftpData->generalDynamicMemoryTable, "ClientData"); //printf("\nDYNMEM_malloc called"); //printf("\nElement location: %ld", (long int) ftpData->generalDynamicMemoryTable); @@ -175,7 +175,7 @@ void initFtpData(ftpDataType *ftpData) } /*Private functions*/ -static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *parametersVector) +static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *parametersVector, DYNMEM_MemoryTable_DataType ** memoryTable) { #define STATE_START 0 #define STATE_NAME 1 @@ -187,7 +187,7 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet int i, state, nameIndex, valueIndex, allowSpacesInValue; char * theFileContent; - theFileSize = FILE_GetStringFromFile(path, &theFileContent); + theFileSize = FILE_GetStringFromFile(path, &theFileContent, &*memoryTable); char name[PARAMETER_SIZE_LIMIT]; char value[PARAMETER_SIZE_LIMIT]; @@ -325,8 +325,8 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet case STATE_STORE: { parameter_DataType parameter; - parameter.name = DYNMEM_malloc(nameIndex+1, ¶metersVector->memoryTable); - parameter.value = DYNMEM_malloc(valueIndex+1, ¶metersVector->memoryTable); + parameter.name = DYNMEM_malloc(nameIndex+1, ¶metersVector->memoryTable, "readConfig"); + parameter.value = DYNMEM_malloc(valueIndex+1, ¶metersVector->memoryTable, "readConfig"); strcpy(parameter.name, name); strcpy(parameter.value, value); parameter.name[nameIndex] = '\0'; @@ -348,8 +348,8 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet valueIndex > 0) { parameter_DataType parameter; - parameter.name = DYNMEM_malloc(nameIndex+1, ¶metersVector->memoryTable); - parameter.value = DYNMEM_malloc(valueIndex+1, ¶metersVector->memoryTable); + parameter.name = DYNMEM_malloc(nameIndex+1, ¶metersVector->memoryTable, "readConfig"); + parameter.value = DYNMEM_malloc(valueIndex+1, ¶metersVector->memoryTable, "readConfig"); strcpy(parameter.name, name); strcpy(parameter.value, value); parameter.name[nameIndex] = '\0'; @@ -364,7 +364,7 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet if (theFileSize > 0) { - free(theFileContent); + DYNMEM_free(theFileContent, &*memoryTable); } return 1; @@ -578,9 +578,9 @@ static int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_Ve userData.ownerShip.groupOwnerString = NULL; userData.ownerShip.userOwnerString = NULL; - userData.name = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchUserIndex])->value) + 1), &ftpParameters->usersVector.memoryTable); - userData.password = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchPasswordIndex])->value) + 1), &ftpParameters->usersVector.memoryTable); - userData.homePath = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchHomeIndex])->value) + 1), &ftpParameters->usersVector.memoryTable); + userData.name = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchUserIndex])->value) + 1), &ftpParameters->usersVector.memoryTable, "userData"); + userData.password = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchPasswordIndex])->value) + 1), &ftpParameters->usersVector.memoryTable, "userData"); + userData.homePath = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchHomeIndex])->value) + 1), &ftpParameters->usersVector.memoryTable, "userData"); strcpy(userData.name, ((parameter_DataType *) parametersVector->Data[searchUserIndex])->value); strcpy(userData.password, ((parameter_DataType *) parametersVector->Data[searchPasswordIndex])->value); @@ -593,8 +593,8 @@ static int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_Ve if (searchUserOwnerIndex != -1 && searchGroupOwnerIndex != -1) { - userData.ownerShip.groupOwnerString = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchGroupOwnerIndex])->value) + 1), &ftpParameters->usersVector.memoryTable); - userData.ownerShip.userOwnerString = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchUserOwnerIndex])->value) + 1), &ftpParameters->usersVector.memoryTable); + userData.ownerShip.groupOwnerString = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchGroupOwnerIndex])->value) + 1), &ftpParameters->usersVector.memoryTable, "userOwnershipData"); + userData.ownerShip.userOwnerString = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchUserOwnerIndex])->value) + 1), &ftpParameters->usersVector.memoryTable, "userOwnershipData"); strcpy(userData.ownerShip.groupOwnerString, ((parameter_DataType *) parametersVector->Data[searchGroupOwnerIndex])->value); strcpy(userData.ownerShip.userOwnerString, ((parameter_DataType *) parametersVector->Data[searchUserOwnerIndex])->value); diff --git a/library/configRead.h b/library/configRead.h index 0d4bc94..55c4306 100644 --- a/library/configRead.h +++ b/library/configRead.h @@ -30,6 +30,7 @@ extern "C" { #endif #include "dynamicVectors.h" +#include "dynamicMemory.h" #include "../ftpData.h" #define DEFAULT_CONFIGURATION_FILENAME "/etc/uftpd.cfg" @@ -39,7 +40,7 @@ extern "C" { /*Public functions */ void initFtpData(ftpDataType *ftpData); int searchUser(char *name, DYNV_VectorGenericDataType *usersVector); -void configurationRead(ftpParameters_DataType *ftpParameters); +void configurationRead(ftpParameters_DataType *ftpParameters, DYNMEM_MemoryTable_DataType **memoryTable); void applyConfiguration(ftpParameters_DataType *ftpParameters); diff --git a/library/dynamicMemory.c b/library/dynamicMemory.c index 49f2b02..d5b9e1a 100644 --- a/library/dynamicMemory.c +++ b/library/dynamicMemory.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dynamicMemory.h" #include "errorHandling.h" @@ -53,7 +54,7 @@ unsigned long long int DYNMEM_DecreaseMemoryCounter(unsigned long long int theSi return theTotalMemory; } -void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead) +void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead, char * theName) { void *memory = NULL; DYNMEM_MemoryTable_DataType *newItem = NULL; @@ -74,6 +75,7 @@ void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead) newItem->size = bytes; newItem->nextElement = NULL; newItem->previousElement = NULL; + strncpy(newItem->theName, theName, 20); if( (*memoryListHead) != NULL) { @@ -180,11 +182,11 @@ void DYNMEM_free(void *f_address, DYNMEM_MemoryTable_DataType ** memoryListHead) if(!found) { - printf("\n\nMemory address : %ld not found\n\n", f_address); + //printf("\n\nMemory address : %ld not found\n\n", f_address); //fflush(0); //Debug TRAP - //char *theData ="c"; - //strcpy(theData, "ciaociaociao"); + char *theData ="c"; + strcpy(theData, "ciaociaociao"); report_error_q("Unable to free memory not previously allocated",__FILE__,__LINE__, 1); // Report this as an error } @@ -209,15 +211,18 @@ void DYNMEM_free(void *f_address, DYNMEM_MemoryTable_DataType ** memoryListHead) void DYNMEM_freeAll(DYNMEM_MemoryTable_DataType **memoryListHead) { - printf("\nDYNMEM_freeAll called"); - printf("\nElement size: %ld", (*memoryListHead)->size); - printf("\nElement address: %ld", (long int) (*memoryListHead)->address); - printf("\nElement nextElement: %ld",(long int) (*memoryListHead)->nextElement); - printf("\nElement previousElement: %ld",(long int) (*memoryListHead)->previousElement); + DYNMEM_MemoryTable_DataType *temp = NULL; while((*memoryListHead) != NULL) { + printf("\nDYNMEM_freeAll called"); + printf("\nElement size: %ld", (*memoryListHead)->size); + printf("\nElement address: %ld", (long int) (*memoryListHead)->address); + printf("\nElement nextElement: %ld",(long int) (*memoryListHead)->nextElement); + printf("\nElement previousElement: %ld",(long int) (*memoryListHead)->previousElement); + + DYNMEM_DecreaseMemoryCounter((*memoryListHead)->size + sizeof(DYNMEM_MemoryTable_DataType)); printf("\nFree table element"); free((*memoryListHead)->address); temp = (*memoryListHead)->nextElement; diff --git a/library/dynamicMemory.h b/library/dynamicMemory.h index c48c99b..8ff8633 100644 --- a/library/dynamicMemory.h +++ b/library/dynamicMemory.h @@ -10,6 +10,7 @@ typedef struct DYNMEM_MemoryTable_DataType { + char theName[20]; void *address; size_t size; struct DYNMEM_MemoryTable_DataType *nextElement; @@ -21,7 +22,7 @@ unsigned long long int DYNMEM_IncreaseMemoryCounter(unsigned long long int theSi unsigned long long int DYNMEM_DecreaseMemoryCounter(unsigned long long int theSize); void DYNMEM_Init(void); -void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType ** memoryListHead); +void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType ** memoryListHead, char * theName); void *DYNMEM_realloc(void *theMemoryAddress, size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead); void DYNMEM_free(void *f_address, DYNMEM_MemoryTable_DataType ** memoryListHead); void DYNMEM_freeAll(DYNMEM_MemoryTable_DataType ** memoryListHead); diff --git a/library/dynamicVectors.c b/library/dynamicVectors.c index 8cdcde8..d54bfa5 100644 --- a/library/dynamicVectors.c +++ b/library/dynamicVectors.c @@ -60,7 +60,7 @@ void DYNV_VectorGeneric_PushBack(DYNV_VectorGenericDataType *TheVectorGeneric, v } else { - TheVectorGeneric->Data = (void **) DYNMEM_malloc (sizeof(void *) * (TheVectorGeneric->Size+1), &TheVectorGeneric->memoryTable); + TheVectorGeneric->Data = (void **) DYNMEM_malloc (sizeof(void *) * (TheVectorGeneric->Size+1), &TheVectorGeneric->memoryTable, "pushback"); } if (TheVectorGeneric->ElementSize != NULL) @@ -69,10 +69,10 @@ void DYNV_VectorGeneric_PushBack(DYNV_VectorGenericDataType *TheVectorGeneric, v } else { - TheVectorGeneric->ElementSize = (int *) DYNMEM_malloc (sizeof(int), &TheVectorGeneric->memoryTable); + TheVectorGeneric->ElementSize = (int *) DYNMEM_malloc (sizeof(int), &TheVectorGeneric->memoryTable, "PushBack"); } - TheVectorGeneric->Data[TheVectorGeneric->Size] = (void *) DYNMEM_malloc(TheElementSize, &TheVectorGeneric->memoryTable); + TheVectorGeneric->Data[TheVectorGeneric->Size] = (void *) DYNMEM_malloc(TheElementSize, &TheVectorGeneric->memoryTable, "pushback"); memcpy(TheVectorGeneric->Data[TheVectorGeneric->Size], TheElementData, TheElementSize); TheVectorGeneric->ElementSize[TheVectorGeneric->Size] = TheElementSize; TheVectorGeneric->Size++; @@ -198,7 +198,7 @@ void DYNV_VectorString_PushBack(DYNV_VectorString_DataType *TheVector, char * Th } else { - TheVector->Data = (char **) DYNMEM_malloc (sizeof(char *) * (TheVector->Size+1), &TheVector->memoryTable); + TheVector->Data = (char **) DYNMEM_malloc (sizeof(char *) * (TheVector->Size+1), &TheVector->memoryTable, "pushback"); } if (TheVector->ElementSize != NULL) @@ -207,10 +207,10 @@ void DYNV_VectorString_PushBack(DYNV_VectorString_DataType *TheVector, char * Th } else { - TheVector->ElementSize = (int *) DYNMEM_malloc (sizeof(int) * 1, &TheVector->memoryTable); + TheVector->ElementSize = (int *) DYNMEM_malloc (sizeof(int) * 1, &TheVector->memoryTable, "pushback"); } - TheVector->Data[TheVector->Size] = (char *) DYNMEM_malloc((StringLenght + 1), &TheVector->memoryTable); + TheVector->Data[TheVector->Size] = (char *) DYNMEM_malloc((StringLenght + 1), &TheVector->memoryTable, "pushback"); for (i = 0; i < StringLenght; i++ ) { diff --git a/library/dynamicVectors.h b/library/dynamicVectors.h index 13a457d..5b40593 100644 --- a/library/dynamicVectors.h +++ b/library/dynamicVectors.h @@ -52,7 +52,7 @@ struct DYNV_VectorGenericDataStruct void (*PushBack)(void *TheVectorGeneric, void * TheElementData, int TheElementSize); void (*PopBack)(void *TheVector, void (*DeleteElement)(void *TheElementToDelete)); void (*SoftPopBack)(void *TheVector); - void (*Destroy)(void *TheVector, void (*DeleteElement)(void *TheElementToDelete)); + void (*Destroy)(void *TheVector, void (*DeleteElement)(struct DYNV_VectorGenericDataStruct *TheElementToDelete)); void (*SoftDestroy)(void *TheVector); void (*DeleteAt)(void *TheVector, int index, void (*DeleteElement)(void *TheElementToDelete)); int (*SearchElement)(void *TheVectorGeneric, void * TheElementData); diff --git a/library/fileManagement.c b/library/fileManagement.c index 38d0db2..6f16f8f 100644 --- a/library/fileManagement.c +++ b/library/fileManagement.c @@ -40,7 +40,6 @@ #include "dynamicMemory.h" static int FILE_CompareString(const void * a, const void * b); - static int FILE_CompareString(const void * a, const void * b) { return strcmp (*(const char **) a, *(const char **) b); @@ -186,14 +185,14 @@ int FILE_IsFile(const char *TheFileName) return 0; } -void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, int * FilesandFolders, int Recursive) +void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, int * FilesandFolders, int Recursive, DYNMEM_MemoryTable_DataType ** memoryTable) { int FileAndFolderIndex = *FilesandFolders; //Allocate the array for the 1st time if (*InodeList == NULL) { - (*InodeList) = (char **) malloc(sizeof(char *) * (1)); + (*InodeList) = (char **) DYNMEM_malloc(sizeof(char *) * (1), &*memoryTable, "InodeList"); } @@ -218,10 +217,10 @@ void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, i //Set the row to needed size int ReallocSize = sizeof(char *) * (FileAndFolderIndex+1)+1; - (*InodeList) = (char ** ) realloc((*InodeList), ReallocSize ); + (*InodeList) = (char ** ) DYNMEM_realloc((*InodeList), ReallocSize, &*memoryTable); int nsize = strlen(dir->d_name) * sizeof(char) + strlen(DirectoryInodeName) * sizeof(char) + 2; //Allocate the path string size - (*InodeList)[FileAndFolderIndex] = (char *) malloc ( nsize ); + (*InodeList)[FileAndFolderIndex] = (char *) DYNMEM_malloc (nsize , &*memoryTable, "InodeList"); strcpy((*InodeList)[FileAndFolderIndex], DirectoryInodeName ); strcat((*InodeList)[FileAndFolderIndex], "/" ); strcat((*InodeList)[FileAndFolderIndex], dir->d_name ); @@ -231,7 +230,7 @@ void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, i if ( Recursive == 1 && FILE_IsDirectory((*InodeList)[*FilesandFolders-1]) == 1 ) { - FILE_GetDirectoryInodeList ( (*InodeList)[FileAndFolderIndex-1], InodeList, FilesandFolders, Recursive ); + FILE_GetDirectoryInodeList ( (*InodeList)[FileAndFolderIndex-1], InodeList, FilesandFolders, Recursive, &*memoryTable); FileAndFolderIndex = (*FilesandFolders); } @@ -245,10 +244,10 @@ void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, i { printf("\nAdding single file to inode list: %s", DirectoryInodeName); int ReallocSize = sizeof(char *) * (FileAndFolderIndex+1)+1; - (*InodeList) = (char ** ) realloc((*InodeList), ReallocSize ); + (*InodeList) = (char ** ) DYNMEM_realloc((*InodeList), ReallocSize, &*memoryTable); int nsize = strlen(DirectoryInodeName) * sizeof(char) + 2; - (*InodeList)[FileAndFolderIndex] = (char *) malloc ( nsize ); + (*InodeList)[FileAndFolderIndex] = (char *) DYNMEM_malloc (nsize, &*memoryTable, "InodeList"); strcpy((*InodeList)[FileAndFolderIndex], DirectoryInodeName ); (*InodeList)[FileAndFolderIndex][strlen(DirectoryInodeName)] = '\0'; (*FilesandFolders)++; @@ -260,7 +259,6 @@ void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, i //No valid path specified, returns zero elements (*FilesandFolders) = 0; } - } int FILE_GetDirectoryInodeCount(char * DirectoryInodeName) @@ -291,7 +289,7 @@ int FILE_GetDirectoryInodeCount(char * DirectoryInodeName) return FileAndFolderIndex; } -int FILE_GetStringFromFile(char * filename, char **file_content) +int FILE_GetStringFromFile(char * filename, char **file_content, DYNMEM_MemoryTable_DataType ** memoryTable) { long long int file_size = 0; int c, count; @@ -321,7 +319,7 @@ int FILE_GetStringFromFile(char * filename, char **file_content) file_size = FILE_GetFileSize(file); count = 0; - *file_content = (char *) malloc(file_size * sizeof(char) + 100); + *file_content = (char *) DYNMEM_malloc((file_size * sizeof(char) + 100), &*memoryTable, "getstringfromfile"); while ((c = fgetc(file)) != EOF) { @@ -531,9 +529,9 @@ char * FILE_GetFilenameFromPath(char * FileName) return TheStr; } -char * FILE_GetListPermissionsString(char *file) { +char * FILE_GetListPermissionsString(char *file, DYNMEM_MemoryTable_DataType ** memoryTable) { struct stat st, stl; - char *modeval = malloc(sizeof(char) * 10 + 1); + char *modeval = DYNMEM_malloc(sizeof(char) * 10 + 1, &*memoryTable, "getperm"); if(stat(file, &st) == 0) { mode_t perm = st.st_mode; @@ -563,7 +561,7 @@ char * FILE_GetListPermissionsString(char *file) { return modeval; } -char * FILE_GetOwner(char *fileName) +char * FILE_GetOwner(char *fileName, DYNMEM_MemoryTable_DataType **memoryTable) { int returnCode = 0; char *toReturn; @@ -576,13 +574,13 @@ char * FILE_GetOwner(char *fileName) if ( (pw = getpwuid(info.st_uid)) == NULL) return NULL; - toReturn = (char *) malloc (strlen(pw->pw_name) + 1); + toReturn = (char *) DYNMEM_malloc (strlen(pw->pw_name) + 1, &*memoryTable, "getowner"); strcpy(toReturn, pw->pw_name); return toReturn; } -char * FILE_GetGroupOwner(char *fileName) +char * FILE_GetGroupOwner(char *fileName, DYNMEM_MemoryTable_DataType **memoryTable) { char *toReturn; struct stat info; @@ -593,7 +591,7 @@ char * FILE_GetGroupOwner(char *fileName) if ((gr = getgrgid(info.st_gid)) == NULL) return NULL; - toReturn = (char *) malloc (strlen(gr->gr_name) + 1); + toReturn = (char *) DYNMEM_malloc (strlen(gr->gr_name) + 1, &*memoryTable, "getowner"); strcpy(toReturn, gr->gr_name); return toReturn; @@ -616,7 +614,7 @@ void FILE_AppendToString(char ** sourceString, char *theString, DYNMEM_MemoryTab (*sourceString)[theNewSize] = '\0'; } -void FILE_DirectoryToParent(char ** sourceString) +void FILE_DirectoryToParent(char ** sourceString, DYNMEM_MemoryTable_DataType ** memoryTable) { //printf("\n"); int i = 0, theLastSlash = -1, strLen = 0; @@ -641,7 +639,7 @@ void FILE_DirectoryToParent(char ** sourceString) { theNewSize = 1; } - *sourceString = realloc(*sourceString, theNewSize+1); + *sourceString = DYNMEM_realloc(*sourceString, theNewSize+1, &*memoryTable); (*sourceString)[theNewSize] = '\0'; } } diff --git a/library/fileManagement.h b/library/fileManagement.h index 8d034ac..b0cd101 100644 --- a/library/fileManagement.h +++ b/library/fileManagement.h @@ -53,19 +53,19 @@ long long int FILE_GetFileSizeFromPath(char *TheFileName); int FILE_IsFile(const char *theFileName); int FILE_IsDirectory (char *directory_path); - void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, int * filesandfolders, int recursive); + void FILE_GetDirectoryInodeList(char * DirectoryInodeName, char *** InodeList, int * filesandfolders, int recursive, DYNMEM_MemoryTable_DataType ** memoryTable); int FILE_GetDirectoryInodeCount(char * DirectoryInodeName); - int FILE_GetStringFromFile(char * filename, char **file_content); + int FILE_GetStringFromFile(char * filename, char **file_content, DYNMEM_MemoryTable_DataType ** memoryTable); void FILE_ReadStringParameters(char * filename, DYNV_VectorGenericDataType *ParametersVector); int FILE_StringParametersLinearySearch(DYNV_VectorGenericDataType *TheVectorGeneric, void * name); int FILE_StringParametersBinarySearch(DYNV_VectorGenericDataType *TheVectorGeneric, void * Needle); char * FILE_GetFilenameFromPath(char * filename); - char * FILE_GetListPermissionsString(char *file); - char * FILE_GetOwner(char *fileName); - char * FILE_GetGroupOwner(char *fileName); + char * FILE_GetListPermissionsString(char *file, DYNMEM_MemoryTable_DataType ** memoryTable); + char * FILE_GetOwner(char *fileName, DYNMEM_MemoryTable_DataType ** memoryTable); + char * FILE_GetGroupOwner(char *fileName, DYNMEM_MemoryTable_DataType ** memoryTable); time_t FILE_GetLastModifiedData(char *path); void FILE_AppendToString(char ** sourceString, char *theString, DYNMEM_MemoryTable_DataType ** memoryTable); - void FILE_DirectoryToParent(char ** sourceString); + void FILE_DirectoryToParent(char ** sourceString, DYNMEM_MemoryTable_DataType ** memoryTable); int FILE_LockFile(int fd); int FILE_doChownFromUidGidString(const char *file_path, const char *user_name, const char *group_name); int FILE_doChownFromUidGid(const char *file_path, uid_t uid, gid_t gid);