diff --git a/config.ini b/config.ini index 024c6c7..1e94259 100644 --- a/config.ini +++ b/config.ini @@ -6,4 +6,13 @@ pending = 20 [files] MaxFiles = 20 -MaxSize = 10000000 \ No newline at end of file +MaxSize = 10000000 + +[log] + +logFile = ./logs/l.log + +[socket] + +name = ./socket +backlog = 100 \ No newline at end of file diff --git a/lib/log/taglialegna.c b/lib/log/taglialegna.c index 62af1fc..98141c1 100644 --- a/lib/log/taglialegna.c +++ b/lib/log/taglialegna.c @@ -5,6 +5,7 @@ #include #include +#include taglia_t *taglia_init(char *file, int n, ...) { int max_files; diff --git a/lib/threadpool/apiFile.h b/lib/threadpool/apiFile.h index a4cd416..b96894e 100644 --- a/lib/threadpool/apiFile.h +++ b/lib/threadpool/apiFile.h @@ -11,7 +11,7 @@ typedef struct struct_waiting { long fd; // client in attesa char *file; // file su cui si vuole acquisire la lock struct struct_waiting *next; // puntatore al prossimo elemento della lista -} waitingT; +} waiting_t; /** * Apri o crea un nuovo file @@ -36,25 +36,25 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, logT *logFileT void lockFile(char *filepath, queueT *q, long fd_c, logT *logFileT, pthread_mutex_t *lock, waitingT **waiting); // Rilascia una Lock di un file -void unlockFile(char *filepath, queueT *q, long fd_c, logT *logFileT, pthread_mutex_t *lock, waitingT **waiting); +void unlockFile(char *filepath, queueT *q, long fd_c, logT *logFileT, pthread_mutex_t *lock, waiting_t **waiting); // Chiudi un file -void closeFile(char *filepath, queueT *q, long fd_c, logT *logFileT, pthread_mutex_t *lock, waitingT **waiting); +void closeFile(char *filepath, queueT *q, long fd_c, logT *logFileT, pthread_mutex_t *lock, waiting_t **waiting); // Rimuovi un file -void removeFile(char *filepath, queueT *q, long fd_c, logT *logFileT, pthread_mutex_t *lock, waitingT **waiting); +void removeFile(char *filepath, queueT *q, long fd_c, logT *logFileT, pthread_mutex_t *lock, waiting_t **waiting); // Funzione ausiliaria che invia un file al client int sendFile(fileT *f, long fd_c, logT *logFileT); // Aggiunge una coppia client/file alla coda in attesa di ottenere una lock -int addWaiting(waitingT **waiting, char *file, int fd); +int addWaiting(waiting_t **waiting, char *file, int fd); // Ottiene il primo client in attesa su una lock di un determinato file -int removeFirstWaiting(waitingT **waiting, char *file); +int removeFirstWaiting(waiting_t **waiting, char *file); // Distrugge la lista d'attesa e ne libera la memoria -void clearWaiting(waitingT **waiting); +void clearWaiting(waiting_t **waiting); #endif // _API_FILE diff --git a/lib/utils/conn.h b/lib/utils/conn.h index e427094..0447020 100644 --- a/lib/utils/conn.h +++ b/lib/utils/conn.h @@ -13,9 +13,6 @@ #if !defined(BUFSIZE) #define BUFSIZE 256 #endif -#if !defined(SOCKNAME) -#define SOCKNAME "./cs_sock" -#endif #if !defined(MAXBACKLOG) #define MAXBACKLOG 32 #endif diff --git a/lib/utils/serverUtil.h b/lib/utils/serverUtil.h index b9ad88d..4742012 100644 --- a/lib/utils/serverUtil.h +++ b/lib/utils/serverUtil.h @@ -4,6 +4,7 @@ #include #include +#include #include #define CONFGETINT(var, config, t, q, p, base) \ @@ -14,5 +15,16 @@ return 1; \ } +#define CONFGETSTR(var, config, t, q, buff) \ + var = ini_get(config, t, q); \ + if(var==NULL) { \ + fprintf(stderr, "ERROR reading config for variable %c\n", t); \ + ini_free(config); \ + return 1; \ + } \ + buff = calloc(strlen(var)+1, sizeof(char)); \ + strncpy(buff, var, strlen(var)+1); \ + var = buff; + #endif /* _SERVER_STATUS */ diff --git a/lib/utils/util.h b/lib/utils/util.h index 642ced2..aed20e0 100644 --- a/lib/utils/util.h +++ b/lib/utils/util.h @@ -1,7 +1,6 @@ #ifndef _UTIL_H #define _UTIL_H - #include #include #include diff --git a/src/server.c b/src/server.c index acd4450..ec44a9e 100644 --- a/src/server.c +++ b/src/server.c @@ -15,6 +15,7 @@ #include #include #include +#include /** @@ -61,6 +62,9 @@ int main(int argc, char *argv[]) { int pendingSize; CONFGETINT(pendingSize, config, "threadpool", "pending", NULL, 10); int maxFiles; CONFGETINT(maxFiles, config, "files", "MaxFiles", NULL, 10); int maxSize; CONFGETINT(maxSize, config, "files", "MaxSize", NULL, 10); + char *logFile, *buf; CONFGETSTR(logFile, config, "log", "logFile", buf); + char *socketName, *buf; CONFGETSTR(socketName, config, "socket", "name", buf); + int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10); ini_free(config); sigset_t mask; @@ -84,16 +88,26 @@ int main(int argc, char *argv[]) { // remove("mysock"); maybe necessary??? - printf("File Server ready."); - fflush(stdout); + // creo la struttura per il log + taglia_t *taglia = taglia_init(logFile); + free(logFile); // free del nome del file + if(taglia==NULL) { + perror("taglia_init"); + goto _cleanup; + } + + // pipes di comunicazione fra thread main e sigHandler thread/threadF threads int signal_pipe[2]; + int request_pipe[2]; if (pipe(signal_pipe) == -1) { perror("pipe"); goto _cleanup; } - - // todo logging + statistiche + if (pipe(request_pipe) == -1) { + perror("pipe"); + goto _cleanup; + } @@ -105,30 +119,56 @@ int main(int argc, char *argv[]) { goto _cleanup; } + // scrivo sul log + char buf[2048]; + int n; + n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %l\n", (long) sighandler_thread); + if( n<0 || m signal_pipe[0]) ? listenfd : signal_pipe[0]; + fdmax = (fdmax > request_pipe[0]) ? fdmax : request_pipe[0]; + + + // scrivo sul log + char buf[2048]; + int n; + n = snprintf(buf, sizeof(buf), "File Server ready.\n\tMaxFiles: %d\n\tMaxSize: %d\n", maxFiles, maxSize); + if( n<0 || m