diff --git a/lib/api/api.c b/lib/api/api.c index 4be63a7..812ec5d 100644 --- a/lib/api/api.c +++ b/lib/api/api.c @@ -34,36 +34,16 @@ // #define ME "52" // not used #define MESE "55" // server error - - - -#define printOpenedFiles(str) \ - - -/* #define printOpenedFiles(str) \ */ -/* fprintf(stdout, "\nInside of function: %s\n", __func__); \ */ -/* fprintf(stdout, "\t[%s]\n", str); \ */ -/* fprintf(stdout, "\tcreatedAndlocked: %s\n", openedFiles->createdAndLocked); \ */ -/* fprintf(stdout, "\tf: %p\n", (void *)openedFiles->f); \ */ -/* fprintf(stdout, "\tnumOfFiles: %d\n", openedFiles->numOfFiles); \ */ -/* fprintf(stdout, "\tout: %p\n", (void *)openedFiles->out); \ */ -/* fprintf(stdout, "\tvalidrDir: %d\n", openedFiles->validrDir); \ */ -/* fprintf(stdout, "\trDir: %p\n", (void *)openedFiles->rDir); \ */ -/* fprintf(stdout, "\tvalidwDir: %d\n", openedFiles->validwDir); \ */ -/* fprintf(stdout, "\twDir: %p\n", (void *)openedFiles->wDir); \ */ -/* fflush(stdout); \ */ - - - - - - - +#define debughere(str) \ + fprintf(stdout, "\nIn function: %s\n", __func__); \ + fprintf(stdout, "\t%s\n", str); \ + fflush(stdout); \ // ----------------------------------------------------------------------------- /* structs */ typedef struct files_s { char *filename; + int locked; struct files_s *next; } files_t; @@ -108,9 +88,9 @@ int closeEveryFile(); // closes and frees memory int removeOpenFile(const char *pathname); // adds element -int addOpenFile(const char *pathname); +int addOpenFile(const char *pathname, const int flags); // checks if the file is open already -int isOpen(const char *pathname); +int isOpen(const char *pathname, const int flags); // reads everything the server sent into the variable passed to the function // expected is 1 if a file could be sent (if no server errors occurs) int reciveData(response_t *res, int expected); @@ -124,9 +104,6 @@ int createOpenedFiles(void); // ----------------------------------------------------------------------------- int openConnection(const char* sockname, int msec, const struct timespec abstime) { - - printOpenedFiles("before"); - if(!sockname){ errno = EINVAL; return -1; @@ -179,14 +156,10 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime } strncpy(socketName, sockname, sizeof(socketName)-1); - - printOpenedFiles("after"); return 0; } int closeConnection(const char* sockname) { - printOpenedFiles("before"); - if (!sockname || strncmp(socketName, sockname, sizeof(socketName)) != 0) { errno = EINVAL; return -1; @@ -207,9 +180,6 @@ int closeConnection(const char* sockname) { } int openFile(const char* pathname, int flags) { - - printOpenedFiles("before"); - if(!pathname){ errno = EINVAL; return -1; @@ -226,12 +196,12 @@ int openFile(const char* pathname, int flags) { errno = ENOTCONN; return -1; } - if(isOpen(pathname)) { // already open - return -1; + if(isOpen(pathname, flags)) { // already open + return 0; } // invio al server la stringa "openFile|$(pathname)|$(flags)" - long len = strlen("openFile")+1+strlen(pathname)+1+((int)log10(flags)+1)+1; + long len = strlen("openFile")+1+strlen(pathname)+1+((int)log10(flags?flags:1)+1)+1; char *cmd = malloc(len); if(!cmd) { perror("malloc"); @@ -304,7 +274,7 @@ int openFile(const char* pathname, int flags) { } } - if(addOpenFile(pathname) == -1) { + if(addOpenFile(pathname, flags) == -1) { perror("addOpenFile"); return -1; } @@ -327,15 +297,10 @@ int openFile(const char* pathname, int flags) { free(res); free(cmd); - - printOpenedFiles("after"); return 0; } int readFile(const char* pathname, void** buf, size_t* size) { - - printOpenedFiles("before"); - if(!pathname) { errno = EINVAL; return -1; @@ -353,7 +318,7 @@ int readFile(const char* pathname, void** buf, size_t* size) { return -1; } - if(isOpen(pathname) != 1) { // if it's not open => error + if(isOpen(pathname, 0) != 1) { // if it's not open => error errno = EPERM; return -1; } @@ -454,9 +419,6 @@ int readFile(const char* pathname, void** buf, size_t* size) { } int readNFiles(int N, const char* dirname) { - - printOpenedFiles("before"); - if(!dirname) { errno = EINVAL; return -1; @@ -537,7 +499,6 @@ int readNFiles(int N, const char* dirname) { } int writeFile(const char* pathname, const char* dirname) { - printOpenedFiles("before"); if(!pathname) { errno = EINVAL; return -1; @@ -600,6 +561,7 @@ int writeFile(const char* pathname, const char* dirname) { } if (close(fdi) == -1) { + perror("closing file"); free(content); return -1; } @@ -719,14 +681,10 @@ int writeFile(const char* pathname, const char* dirname) { free(cmd); free(content); - - printOpenedFiles("after"); return 0; } int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname) { - - printOpenedFiles("before"); if(!pathname || !buf) { errno = EINVAL; return -1; @@ -744,7 +702,7 @@ int appendToFile(const char* pathname, void* buf, size_t size, const char* dirna return -1; } - if (isOpen(pathname) != 1) { + if (isOpen(pathname, 0) != 1) { errno = EPERM; return -1; } @@ -845,7 +803,6 @@ int appendToFile(const char* pathname, void* buf, size_t size, const char* dirna } int lockFile(const char* pathname) { - printOpenedFiles("before"); if(!pathname){ errno = EINVAL; return -1; @@ -864,11 +821,15 @@ int lockFile(const char* pathname) { } int existing = 0; - if(isOpen(pathname)){ // file is already open + if(isOpen(pathname, 0)){ // file is already open existing = 1; goto _unlockFile; } + if(isOpen(pathname, O_LOCK)){ // file is already locked + return 0; + } + if (openFile(pathname, O_LOCK) == 0) // open the file with the lock return 0; @@ -926,7 +887,7 @@ _unlockFile: { } if(!existing) { - if(addOpenFile(pathname) == -1) { + if(addOpenFile(pathname, O_LOCK) == -1) { perror("addOpenFile"); freeResponse(res); free(res); @@ -943,7 +904,6 @@ _unlockFile: { } int unlockFile(const char* pathname) { - printOpenedFiles("before"); if(!pathname){ errno = EINVAL; return -1; @@ -960,7 +920,7 @@ int unlockFile(const char* pathname) { errno = ENOTCONN; return -1; } - if(isOpen(pathname) != 1) { // not open + if(isOpen(pathname, 0) != 1) { // not open or not locked errno = EPERM; return -1; } @@ -1017,7 +977,6 @@ int unlockFile(const char* pathname) { } int closeFile(const char *pathname) { - printOpenedFiles("before"); if(!pathname){ errno = EINVAL; return -1; @@ -1035,7 +994,7 @@ int closeFile(const char *pathname) { return -1; } - if(isOpen(pathname) != 1) { // not open + if(isOpen(pathname, 0) != 1) { // not open errno = EPERM; return -1; } @@ -1095,12 +1054,10 @@ int closeFile(const char *pathname) { free(res); free(cmd); - printOpenedFiles("after"); return 0; } int removeFile(const char* pathname) { - printOpenedFiles("before"); if(!pathname){ errno = EINVAL; return -1; @@ -1118,7 +1075,7 @@ int removeFile(const char* pathname) { return -1; } - if (isOpen(pathname) != 1) { // not open + if (isOpen(pathname, 0) != 1) { // not open errno = EPERM; return -1; } @@ -1175,14 +1132,12 @@ int removeFile(const char* pathname) { } free(cmd); - printOpenedFiles("after"); return 0; } // ----------------------------------------------------------------------------- int setDirectory(char* Dir, int rw) { - printOpenedFiles("before"); if (!Dir) { errno = EINVAL; return -1; @@ -1205,7 +1160,6 @@ int setDirectory(char* Dir, int rw) { openedFiles->rDir = malloc(strlen(Dir)+1); strncpy(openedFiles->rDir, Dir, strlen(Dir)+1); } - printOpenedFiles("after"); return 0; } @@ -1219,14 +1173,12 @@ void printInfo(int p, FILE *stream) { } openedFiles->print = (p)? 1: 0; openedFiles->out = stream; - printOpenedFiles("after"); return; } // ----------------------------------------------------------------------------- int reciveData(response_t *res, int expected) { - printOpenedFiles("before"); if(!res) { errno = EINVAL; return -1; @@ -1474,7 +1426,6 @@ _nofile: } int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf) { - printOpenedFiles("before"); if(!dirname || strcmp(dirname, "") || !rf || n<0) { errno = EINVAL; return -1; @@ -1530,7 +1481,6 @@ int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf) { } free(basepath); - printOpenedFiles("after"); return 0; } @@ -1551,7 +1501,7 @@ void freeResponse(response_t *res) { return; } -int isOpen(const char *pathname) { +int isOpen(const char *pathname, const int flags) { if(!pathname) { errno = EINVAL; return -1; @@ -1563,7 +1513,7 @@ int isOpen(const char *pathname) { files_t *tmp = openedFiles->f; while(tmp) { - if(strcmp(tmp->filename, pathname) == 0) + if(strcmp(tmp->filename, pathname) == 0 && (tmp->locked==(flags&O_LOCK))) return 1; tmp = tmp->next; } @@ -1571,8 +1521,7 @@ int isOpen(const char *pathname) { return 0; } -int addOpenFile(const char *pathname) { - printOpenedFiles("before"); +int addOpenFile(const char *pathname, const int flags) { if(!pathname) { errno = EINVAL; return -1; @@ -1585,6 +1534,7 @@ int addOpenFile(const char *pathname) { } size_t len = strlen(pathname)+1; + new->locked = flags&O_LOCK; new->filename = calloc(len, sizeof(char)); if(!new->filename) { perror("calloc"); @@ -1607,12 +1557,10 @@ int addOpenFile(const char *pathname) { } tail->next = new; openedFiles->numOfFiles++; - printOpenedFiles("after"); return 0; } int removeOpenFile(const char *pathname) { - printOpenedFiles("before"); if(!pathname) { errno = EINVAL; return -1; @@ -1652,7 +1600,6 @@ int removeOpenFile(const char *pathname) { } int closeEveryFile() { - printOpenedFiles("before"); if (openedFiles == NULL) return 0; @@ -1687,7 +1634,8 @@ int closeEveryFile() { openedFiles->wDir = NULL; } - printOpenedFiles("after"); + free(openedFiles); + openedFiles = NULL; return 0; } @@ -1706,7 +1654,5 @@ int createOpenedFiles(void) { openedFiles->wDir = NULL; openedFiles->print = 0; openedFiles->out = NULL; - - printOpenedFiles("after"); return 0; } diff --git a/lib/threadpool/apiFile.c b/lib/threadpool/apiFile.c index cc10240..06e502d 100644 --- a/lib/threadpool/apiFile.c +++ b/lib/threadpool/apiFile.c @@ -306,7 +306,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) { size_t m = sizeof(tmp_buf); if(!filepath || !q || !taglia) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore del server\n", fd_c, filepath); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; @@ -315,7 +315,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) { fileT *f = NULL; f = find(q, filepath); if(!f) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" ma il file non esiste\n", fd_c, filepath); errno = ENOENT; serror(MESE, fd_c, taglia, tmp_buf); return; diff --git a/lib/threadpool/fileQueue.c b/lib/threadpool/fileQueue.c index 82890ca..b8efee5 100644 --- a/lib/threadpool/fileQueue.c +++ b/lib/threadpool/fileQueue.c @@ -751,6 +751,7 @@ fileT* find(queueT *q, char *filepath) { if (writeFileT(res, (tmp->data)->data, (tmp->data)->size) == -1) { perror("writeFileT res"); + destroyFile(res); goto _end_find_in_queue; } diff --git a/lib/utils/strsep_gnu.c b/lib/utils/strsep_gnu.c index dc8487e..3d5a787 100644 --- a/lib/utils/strsep_gnu.c +++ b/lib/utils/strsep_gnu.c @@ -27,7 +27,7 @@ strsep_gnu (char **stringp, const char *delim) if (begin == NULL) return NULL; /* Find the end of the token. */ - end = begin + strcspn (begin, delim); + end = begin + strcspn(begin, delim); if (*end) { /* Terminate the token and set *STRINGP past NUL character. */ diff --git a/src/client.c b/src/client.c index 8d83a0d..98a3b12 100644 --- a/src/client.c +++ b/src/client.c @@ -196,6 +196,10 @@ int main(int argc, char* argv[]) { goto _cleanup; } + if(cmds) { + destroyCommandList(cmds); + } + return 0; _cleanup: @@ -217,9 +221,9 @@ void destroyCommandList(cmd_t *l) { while (l) { tmp = l; - free(l->arg); l = l->next; + free(tmp->arg); free(tmp); } } @@ -322,8 +326,10 @@ int execute(cmd_t *l, int print) { if(!ok) printf("errore\n"); } - if(!ok) + if(!ok) { + closeConnection(tmp->arg); return 0; // no socket to connect, nothing to do + } // we only read the first -f, no error reported if more than one is // specified tmp = NULL; @@ -398,7 +404,7 @@ int execute(cmd_t *l, int print) { case 'D': if(Dir) free(Dir); - Dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1); + Dir = calloc(strnlen(tmp->arg, MAXARGLENGTH)+1, sizeof(char)); strncpy(Dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH)); if (setDirectory(Dir, 1) == -1) { @@ -417,7 +423,7 @@ int execute(cmd_t *l, int print) { case 'd': if(dir) free(dir); - dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1); + dir = calloc(strnlen(tmp->arg, MAXARGLENGTH)+1, sizeof(char)); strncpy(dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH)); if(setDirectory(dir, 0) == -1) { @@ -569,9 +575,10 @@ int cmd_W(char *filelist, char *Dir, int print) { perror("malloc"); return -1; } - strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); + memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1); + strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)+1); - char *token; + char *token = NULL; char *string = tofree; if (print == 1) { @@ -631,13 +638,14 @@ int cmd_r(char *filelist, char *dir, int print) { // we copy filelist because we are nice char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); + memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1); if(!tofree) { perror("malloc"); return -1; } strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); - char *token; + char *token = NULL; char *string = tofree; if (print) { @@ -663,7 +671,7 @@ int cmd_r(char *filelist, char *dir, int print) { void *buf = NULL; size_t size = -1; - printInfo(0, stdout); + // printInfo(0, stdout); // read the content of the file if (ok && readFile(token, &buf, &size) == -1) { ok = 0; diff --git a/src/server.c b/src/server.c index 05c8a92..0ae10e7 100644 --- a/src/server.c +++ b/src/server.c @@ -250,6 +250,7 @@ int main(int argc, char *argv[]) { FD_CLR(i, &set); close(i); + free(connfd); continue; } @@ -280,7 +281,7 @@ int main(int argc, char *argv[]) { free(connfd); goto _cleanup; } - args->connfd = connfd; + args->connfd = *connfd; args->quit = &quit; args->request_pipe = request_pipe[1]; args->q = queue; @@ -293,13 +294,13 @@ int main(int argc, char *argv[]) { int r = addToThreadPool(pool, threadF, args); if (r == 0) { numberOfConnections++; + free(connfd); continue; // aggiunto con successo } if (r < 0) // errore interno fprintf(stderr, "ERROR FATAL adding to the thread pool\n"); else // coda dei pendenti piena fprintf(stderr, "ERROR SERVER TOO BUSY\n"); - free(args); close(*connfd); free(connfd); continue; @@ -329,6 +330,7 @@ int main(int argc, char *argv[]) { } break; } + free(connfd); continue; } if (i == signal_pipe[0]) { // controllo se devo terminare @@ -382,8 +384,7 @@ int main(int argc, char *argv[]) { free(connfd); goto _cleanup; } - *connfd = i; - args->connfd = connfd; + args->connfd = i; args->quit = &quit; args->request_pipe = request_pipe[1]; args->q = queue; @@ -396,13 +397,13 @@ int main(int argc, char *argv[]) { int r = addToThreadPool(pool, threadF, args); if (r == 0) { numberOfConnections++; + free(connfd); continue; // aggiunto con successo } if (r < 0) // errore interno fprintf(stderr, "ERROR FATAL adding to the thread pool\n"); else // coda dei pendenti piena fprintf(stderr, "ERROR SERVER TOO BUSY\n"); - free(args); close(*connfd); free(connfd); continue; @@ -436,6 +437,12 @@ int main(int argc, char *argv[]) { return 0; _cleanup: + if(queue) { + destroyQueue(queue); + } + if(taglia) { + taglia_del(taglia); + } unlink(socketName); free(socketName); return -1; diff --git a/src/serverWorker.c b/src/serverWorker.c index e878941..5a39907 100644 --- a/src/serverWorker.c +++ b/src/serverWorker.c @@ -27,7 +27,7 @@ void threadF(void *arg) { } threadT *argl = (threadT *) arg; - int connfd = *argl->connfd; + long connfd = argl->connfd; volatile int *quit = argl->quit; int request_pipe = argl->request_pipe; queueT *q = argl->q; @@ -94,7 +94,7 @@ void threadF(void *arg) { // log chiusura connessione int n = 0; char buf[1024]; - n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %d.\n", connfd); + n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %ld.\n", connfd); if( n<0 ) { perror("snprintf"); goto _cleanup; @@ -117,17 +117,19 @@ void threadF(void *arg) { // log int m = 0; char buf[1024]; - m = snprintf(buf, sizeof(buf), "Servito una richiesta del client %d.\n", connfd); + m = snprintf(buf, sizeof(buf), "Servito una richiesta del client %ld.\n", connfd); if( m<0 ) { perror("snprintf"); goto _cleanup; } if( taglia_log(taglia, buf) < 0 ) goto _cleanup; + + free(arg); return; _cleanup: - // nothing to do because no memory needs to be freed + free(arg); return; } @@ -138,12 +140,13 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p return -1; } - char *string = calloc(1, len); + char *string = calloc(1, len+1); if(string == NULL) { perror("calloc"); return -1; } strncpy(string, command, len); + char *tofree = string; char *token = NULL; char *token2 = NULL; @@ -237,9 +240,11 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p _parser_cleanup: free(command); + free(tofree); return -1; _parser_end: free(command); + free(tofree); return 0; } diff --git a/src/serverWorker.h b/src/serverWorker.h index bd2c42e..c56b6c4 100644 --- a/src/serverWorker.h +++ b/src/serverWorker.h @@ -13,7 +13,7 @@ typedef struct struct_thread { volatile int *quit; int request_pipe; - long *connfd; + long connfd; queueT *q; // puntatore alla queue dei file taglia_t *taglia; // puntatore alla struct del file di log threadpool_t *pool; // puntatore alla threadpool