diff --git a/Makefile b/Makefile index 73ff9f7..3a43ea3 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -CC = gcc -AR = ar -CFLAGS += -std=c99 -Wall -Werror -pedantic -g -ARFLAGS = rvs -INCDIR = ./lib/utils -I ./lib/ini -I ./lib/threadpool -I ./src -INCLUDES = -I . -I $(INCDIR) +CC = gcc +AR = ar +CFLAGS += -std=c99 -Wall -Werror -pedantic -g +ARFLAGS = rvs +INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log +INCLUDES := $(patsubst %,-I %,$(INCDIR)) LDFLAGS = -L . OPTFLAGS = #-O3 LIBS = -lpthread @@ -11,39 +11,51 @@ SOCKET = ./cs_sock BUILD_DIR = ./build SRC_DIR = ./src +LIB_DIR = ./lib OBJ_DIR = ./obj -OBJ_SERVER = obj/threadpool.o obj/ini.o obj/serverWorker.o obj/server.o +OBJ_SRC := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(wildcard $(SRC_DIR)/*.c)))) +OBJ_LIBS := $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(wildcard $(LIB_DIR)/*/*.c)))) +OBJ_SERVER := $(OBJ_SRC) $(OBJ_LIBS) -.DEFAULT_GOAL = server +VPATH := $(SRC_DIR) $(wildcard $(LIB_DIR)/*) + +.DEFAULT_GOAL := all -# aggiungere qui altri targets -TARGETS = server .PHONY: all clean cleanall -.SUFFIXES: .c .h -obj/threadpool.o: lib/threadpool/threadpool.c - $(CC) $(CFLAGS) $(INCLUDES) -c lib/threadpool/threadpool.c - @mv threadpool.o $(OBJ_DIR)/threadpool.o -obj/ini.o: lib/ini/ini.c - $(CC) $(CFLAGS) $(INCLUDES) -c lib/ini/ini.c - @mv ini.o $(OBJ_DIR)/ini.o +all: $(BUILD_DIR)/server + @echo "done!" -obj/serverWorker.o: src/serverWorker.c - $(CC) $(CFLAGS) $(INCLUDES) -c src/serverWorker.c - @mv serverWorker.o $(OBJ_DIR)/serverWorker.o -obj/server.o: src/server.c - $(CC) $(CFLAGS) $(INCLUDES) -c src/server.c - @mv server.o $(OBJ_DIR)/server.o -server: $(OBJ_SERVER) +$(OBJ_DIR)/fileQueue.o: fileQueue.c + $(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +$(OBJ_DIR)/apiFile.o: apiFile.c + $(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +$(OBJ_DIR)/taglialegna.o: taglialegna.c + $(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +$(OBJ_DIR)/threadpool.o: threadpool.c + $(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +$(OBJ_DIR)/ini.o: ini.c + $(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +$(OBJ_DIR)/serverWorker.o: serverWorker.c + $(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +$(OBJ_DIR)/server.o: server.c + $(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@ + +$(BUILD_DIR)/server: $(OBJ_SERVER) $(CC) $(CFLAGS) $(LIBS) $(OBJ_SERVER) -o $(BUILD_DIR)/server - clean : @rm -rf $(BUILD_DIR)/* logs/*.log diff --git a/lib/threadpool/apiFile.c b/lib/threadpool/apiFile.c index 981f04f..ff4585c 100644 --- a/lib/threadpool/apiFile.c +++ b/lib/threadpool/apiFile.c @@ -268,7 +268,10 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) { } -void readNFiles(char *numStr, queueT *q, long fd_c, taglia_t *taglia); // TODO +void readNFiles(char *numStr, queueT *q, long fd_c, taglia_t *taglia){ + // TODO + return; +} void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append) { @@ -405,26 +408,42 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl } -void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting); +void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { + return; +} -void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting); +void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { + return; +} -void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting); +void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { + return; +} -void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting); +void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { + return; +} -int sendFile(fileT *f, long fd_c, taglia_t *taglia); +int sendFile(fileT *f, long fd_c, taglia_t *taglia) { + return; +} -int addWaiting(waitingT **waiting, char *file, int fd); +int addWaiting(waitingT **waiting, char *file, int fd) { + return; +} -int removeFirstWaiting(waitingT **waiting, char *file); +int removeFirstWaiting(waitingT **waiting, char *file) { + return; +} -void clearWaiting(waitingT **waiting); +void clearWaiting(waitingT **waiting) { + return; +} diff --git a/lib/threadpool/apiFile.h b/lib/threadpool/apiFile.h index c1085aa..437b9e8 100644 --- a/lib/threadpool/apiFile.h +++ b/lib/threadpool/apiFile.h @@ -3,7 +3,7 @@ #define _API_FILE #include -/* TODO: include lib di logging */ +#include /* TODO: finire tutte le descrizioni */ // Lista dei client in attesa su una lock @@ -33,7 +33,7 @@ void readNFiles(char *numStr, queueT *q, long fd_c, taglia_t *taglia); void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append); // Acquisisci lock di un file -void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting); +void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); // Rilascia una Lock di un file void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); diff --git a/lib/utils/serverUtil.h b/lib/utils/serverUtil.h index 4742012..e8577e2 100644 --- a/lib/utils/serverUtil.h +++ b/lib/utils/serverUtil.h @@ -7,24 +7,23 @@ #include #include -#define CONFGETINT(var, config, t, q, p, base) \ - var = (int) strtol(ini_get(config, t, q), p, base); \ - if(var<=0) { \ - fprintf(stderr, "ERROR reading config for variable %c\n", t); \ - ini_free(config); \ - return 1; \ +#define CONFGETINT(var, config, t, q, p, base) \ + var = (int) strtol(ini_get(config, t, q), p, base); \ + if(var<=0) { \ + fprintf(stderr, "ERROR reading config for variable %s\n", t); \ + ini_free(config); \ + 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; +#define CONFGETSTR(var, config, t, q, buff) \ + buff = ini_get(config, t, q); \ + if(buff==NULL) { \ + fprintf(stderr, "ERROR reading config for variable %s\n", t); \ + ini_free(config); \ + return 1; \ + } \ + var = calloc(strlen(var)+1, sizeof(char)); \ + strncpy(var, buff, strlen(buff)+1); #endif /* _SERVER_STATUS */ diff --git a/src/server.c b/src/server.c index ae93a35..687ce77 100644 --- a/src/server.c +++ b/src/server.c @@ -62,8 +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); + const char *buff; + char *logFile; CONFGETSTR(logFile, config, "log", "logFile", buff); + char *socketName; CONFGETSTR(socketName, config, "socket", "name", buff); int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10); ini_free(config); @@ -89,12 +90,15 @@ int main(int argc, char *argv[]) { // creo la struttura per il log - taglia_t *taglia = taglia_init(logFile); + taglia_t *taglia = taglia_init(logFile, 0); free(logFile); // free del nome del file if(taglia==NULL) { perror("taglia_init"); goto _cleanup; } + // risorse per il logfile usate nel main + char buf[2048]; + int n; // pipes di comunicazione fra thread main e sigHandler thread/threadF threads @@ -120,10 +124,8 @@ int main(int argc, char *argv[]) { } // 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 || mconnfd = connfd; args->quit = &quit; args->request_pipe = request_pipe[1]; - args->q = &queue; - args->taglia = &taglia; + args->q = queue; + args->taglia = taglia; args->pool = pool; args->lock = &lock; args->waiting = &waiting; @@ -307,7 +301,7 @@ int main(int argc, char *argv[]) { } if (i == request_pipe[0]) { // un worker ha finito di servire un client int pdr; // ottengo il descrittore della pipe - if (readn(requestPipe[0], &pdr, sizeof(int)) == -1) { + if (readn(request_pipe[0], &pdr, sizeof(int)) == -1) { perror("readn"); break; } @@ -325,7 +319,7 @@ int main(int argc, char *argv[]) { default: // client served but not disconnected FD_SET(pdr, &set); if (pdr > fdmax) { - fdmax = fdr; + fdmax = pdr; } break; } @@ -379,8 +373,8 @@ int main(int argc, char *argv[]) { args->connfd = connfd; args->quit = &quit; args->request_pipe = request_pipe[1]; - args->q = &queue; - args->taglia = &taglia; + args->q = queue; + args->taglia = taglia; args->pool = pool; args->lock = &lock; args->waiting = &waiting; @@ -406,10 +400,10 @@ int main(int argc, char *argv[]) { destroyThreadPool(pool, 0); // notifico che i thread dovranno uscire clearWaiting(&waiting); - taglia_stats(taglia); // print stats + taglia_stats(taglia, stdout); // print stats // print all files in storage during shutdown - if (printQueue(queue) == -1) { + if (printQueue(stdout, queue) == -1) { perror("printQueue"); return 1; } @@ -436,7 +430,7 @@ static void *sigHandler(void *arg) { sigset_t *set = ((sigHandler_t*)arg)->set; int fd_pipe = ((sigHandler_t*)arg)->signal_pipe; - if(pthread_sigmask(SIG_SETMASK, &set, NULL)) { + if(pthread_sigmask(SIG_SETMASK, set, NULL)) { fprintf(stderr, "ERROR setting mask\n"); return (void*) 1; } @@ -444,7 +438,7 @@ static void *sigHandler(void *arg) { while (1) { int sig; int code; - int r = sigwait(&set, &sig); + int r = sigwait(set, &sig); if (r != 0) { errno = r; diff --git a/src/serverWorker.c b/src/serverWorker.c index 654360a..2e981c3 100644 --- a/src/serverWorker.c +++ b/src/serverWorker.c @@ -7,8 +7,9 @@ #include #include -#include +#include #include +#include // funzione eseguita dal Worker thread del pool void threadF(void *arg) { diff --git a/src/serverWorker.h b/src/serverWorker.h index 71b73f7..457068b 100644 --- a/src/serverWorker.h +++ b/src/serverWorker.h @@ -8,14 +8,14 @@ // struttura dati che contiene gli argomenti da passare ai worker threads typedef struct struct_thread { - int connfd; - int *quit; + volatile int *quit; int request_pipe; + 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 pthread_mutex_t *lock; - waitingT **waiting; // puntatore ai client in attesa di ottenere la lock su un file + waiting_t **waiting; // puntatore ai client in attesa di ottenere la lock su un file } threadT; // funzione eseguita dal generico Worker del pool di thread