Fixed makefile

This commit is contained in:
elvis
2022-04-04 22:31:14 +02:00
parent a7b28994b6
commit 044b0f527a
7 changed files with 107 additions and 82 deletions

View File

@ -1,9 +1,9 @@
CC = gcc CC = gcc
AR = ar AR = ar
CFLAGS += -std=c99 -Wall -Werror -pedantic -g CFLAGS += -std=c99 -Wall -Werror -pedantic -g
ARFLAGS = rvs ARFLAGS = rvs
INCDIR = ./lib/utils -I ./lib/ini -I ./lib/threadpool -I ./src INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log
INCLUDES = -I . -I $(INCDIR) INCLUDES := $(patsubst %,-I %,$(INCDIR))
LDFLAGS = -L . LDFLAGS = -L .
OPTFLAGS = #-O3 OPTFLAGS = #-O3
LIBS = -lpthread LIBS = -lpthread
@ -11,39 +11,51 @@ SOCKET = ./cs_sock
BUILD_DIR = ./build BUILD_DIR = ./build
SRC_DIR = ./src SRC_DIR = ./src
LIB_DIR = ./lib
OBJ_DIR = ./obj 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 .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 all: $(BUILD_DIR)/server
$(CC) $(CFLAGS) $(INCLUDES) -c lib/ini/ini.c @echo "done!"
@mv ini.o $(OBJ_DIR)/ini.o
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 $(CC) $(CFLAGS) $(LIBS) $(OBJ_SERVER) -o $(BUILD_DIR)/server
clean : clean :
@rm -rf $(BUILD_DIR)/* logs/*.log @rm -rf $(BUILD_DIR)/* logs/*.log

View File

@ -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) { 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;
}

View File

@ -3,7 +3,7 @@
#define _API_FILE #define _API_FILE
#include <fileQueue.h> #include <fileQueue.h>
/* TODO: include lib di logging */ #include <taglialegna.h>
/* TODO: finire tutte le descrizioni */ /* TODO: finire tutte le descrizioni */
// Lista dei client in attesa su una lock // 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); void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append);
// Acquisisci lock di un file // 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 // 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); void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting);

View File

@ -7,24 +7,23 @@
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#define CONFGETINT(var, config, t, q, p, base) \ #define CONFGETINT(var, config, t, q, p, base) \
var = (int) strtol(ini_get(config, t, q), p, base); \ var = (int) strtol(ini_get(config, t, q), p, base); \
if(var<=0) { \ if(var<=0) { \
fprintf(stderr, "ERROR reading config for variable %c\n", t); \ fprintf(stderr, "ERROR reading config for variable %s\n", t); \
ini_free(config); \ ini_free(config); \
return 1; \ return 1; \
} }
#define CONFGETSTR(var, config, t, q, buff) \ #define CONFGETSTR(var, config, t, q, buff) \
var = ini_get(config, t, q); \ buff = ini_get(config, t, q); \
if(var==NULL) { \ if(buff==NULL) { \
fprintf(stderr, "ERROR reading config for variable %c\n", t); \ fprintf(stderr, "ERROR reading config for variable %s\n", t); \
ini_free(config); \ ini_free(config); \
return 1; \ return 1; \
} \ } \
buff = calloc(strlen(var)+1, sizeof(char)); \ var = calloc(strlen(var)+1, sizeof(char)); \
strncpy(buff, var, strlen(var)+1); \ strncpy(var, buff, strlen(buff)+1);
var = buff;
#endif /* _SERVER_STATUS */ #endif /* _SERVER_STATUS */

View File

@ -62,8 +62,9 @@ int main(int argc, char *argv[]) {
int pendingSize; CONFGETINT(pendingSize, config, "threadpool", "pending", NULL, 10); int pendingSize; CONFGETINT(pendingSize, config, "threadpool", "pending", NULL, 10);
int maxFiles; CONFGETINT(maxFiles, config, "files", "MaxFiles", NULL, 10); int maxFiles; CONFGETINT(maxFiles, config, "files", "MaxFiles", NULL, 10);
int maxSize; CONFGETINT(maxSize, config, "files", "MaxSize", NULL, 10); int maxSize; CONFGETINT(maxSize, config, "files", "MaxSize", NULL, 10);
char *logFile, *buf; CONFGETSTR(logFile, config, "log", "logFile", buf); const char *buff;
char *socketName, *buf; CONFGETSTR(socketName, config, "socket", "name", buf); 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); int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10);
ini_free(config); ini_free(config);
@ -89,12 +90,15 @@ int main(int argc, char *argv[]) {
// creo la struttura per il log // 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 free(logFile); // free del nome del file
if(taglia==NULL) { if(taglia==NULL) {
perror("taglia_init"); perror("taglia_init");
goto _cleanup; 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 // pipes di comunicazione fra thread main e sigHandler thread/threadF threads
@ -120,10 +124,8 @@ int main(int argc, char *argv[]) {
} }
// scrivo sul log // scrivo sul log
char buf[2048]; n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %ld\n", (long) sighandler_thread);
int n; if( n<0 ) {
n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %l\n", (long) sighandler_thread);
if( n<0 || m<n ) {
perror("snprintf"); perror("snprintf");
goto _cleanup; goto _cleanup;
} }
@ -160,8 +162,6 @@ int main(int argc, char *argv[]) {
} }
// scrivo sul log // scrivo sul log
char buf[2048];
int n;
n = snprintf(buf, sizeof(buf), "Creato socket: %s\n", socketName); n = snprintf(buf, sizeof(buf), "Creato socket: %s\n", socketName);
if( n<0 ) { if( n<0 ) {
perror("snprintf"); perror("snprintf");
@ -187,8 +187,6 @@ int main(int argc, char *argv[]) {
} }
// scrivo sul log // scrivo sul log
char buf[2048];
int n;
n = snprintf(buf, sizeof(buf), "Creato threadpool di dimensione %d e lending size %d\n", threadsInPool, pendingSize); n = snprintf(buf, sizeof(buf), "Creato threadpool di dimensione %d e lending size %d\n", threadsInPool, pendingSize);
if( n<0 ) { if( n<0 ) {
perror("snprintf"); perror("snprintf");
@ -211,8 +209,6 @@ int main(int argc, char *argv[]) {
// scrivo sul log // 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); n = snprintf(buf, sizeof(buf), "File Server ready.\n\tMaxFiles: %d\n\tMaxSize: %d\n", maxFiles, maxSize);
if( n<0 ) { if( n<0 ) {
perror("snprintf"); perror("snprintf");
@ -264,9 +260,7 @@ int main(int argc, char *argv[]) {
// scrivo sul log // scrivo sul log
char buf[512]; n = snprintf(buf, sizeof(buf), "New client: %ld\n", (long) *connfd);
int n;
n = snprintf(buf, sizeof(buf), "New client: %l\n", *connfd);
if( n<0 ) { if( n<0 ) {
perror("snprintf"); perror("snprintf");
goto _cleanup; goto _cleanup;
@ -284,8 +278,8 @@ int main(int argc, char *argv[]) {
args->connfd = connfd; args->connfd = connfd;
args->quit = &quit; args->quit = &quit;
args->request_pipe = request_pipe[1]; args->request_pipe = request_pipe[1];
args->q = &queue; args->q = queue;
args->taglia = &taglia; args->taglia = taglia;
args->pool = pool; args->pool = pool;
args->lock = &lock; args->lock = &lock;
args->waiting = &waiting; 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 if (i == request_pipe[0]) { // un worker ha finito di servire un client
int pdr; // ottengo il descrittore della pipe 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"); perror("readn");
break; break;
} }
@ -325,7 +319,7 @@ int main(int argc, char *argv[]) {
default: // client served but not disconnected default: // client served but not disconnected
FD_SET(pdr, &set); FD_SET(pdr, &set);
if (pdr > fdmax) { if (pdr > fdmax) {
fdmax = fdr; fdmax = pdr;
} }
break; break;
} }
@ -379,8 +373,8 @@ int main(int argc, char *argv[]) {
args->connfd = connfd; args->connfd = connfd;
args->quit = &quit; args->quit = &quit;
args->request_pipe = request_pipe[1]; args->request_pipe = request_pipe[1];
args->q = &queue; args->q = queue;
args->taglia = &taglia; args->taglia = taglia;
args->pool = pool; args->pool = pool;
args->lock = &lock; args->lock = &lock;
args->waiting = &waiting; args->waiting = &waiting;
@ -406,10 +400,10 @@ int main(int argc, char *argv[]) {
destroyThreadPool(pool, 0); // notifico che i thread dovranno uscire destroyThreadPool(pool, 0); // notifico che i thread dovranno uscire
clearWaiting(&waiting); clearWaiting(&waiting);
taglia_stats(taglia); // print stats taglia_stats(taglia, stdout); // print stats
// print all files in storage during shutdown // print all files in storage during shutdown
if (printQueue(queue) == -1) { if (printQueue(stdout, queue) == -1) {
perror("printQueue"); perror("printQueue");
return 1; return 1;
} }
@ -436,7 +430,7 @@ static void *sigHandler(void *arg) {
sigset_t *set = ((sigHandler_t*)arg)->set; sigset_t *set = ((sigHandler_t*)arg)->set;
int fd_pipe = ((sigHandler_t*)arg)->signal_pipe; 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"); fprintf(stderr, "ERROR setting mask\n");
return (void*) 1; return (void*) 1;
} }
@ -444,7 +438,7 @@ static void *sigHandler(void *arg) {
while (1) { while (1) {
int sig; int sig;
int code; int code;
int r = sigwait(&set, &sig); int r = sigwait(set, &sig);
if (r != 0) { if (r != 0) {
errno = r; errno = r;

View File

@ -7,8 +7,9 @@
#include <conn.h> #include <conn.h>
#include <message.h> #include <message.h>
#include <serverQueue.h> #include <fileQueue.h>
#include <apiFile.h> #include <apiFile.h>
#include <taglialegna.h>
// funzione eseguita dal Worker thread del pool // funzione eseguita dal Worker thread del pool
void threadF(void *arg) { void threadF(void *arg) {

View File

@ -8,14 +8,14 @@
// struttura dati che contiene gli argomenti da passare ai worker threads // struttura dati che contiene gli argomenti da passare ai worker threads
typedef struct struct_thread { typedef struct struct_thread {
int connfd; volatile int *quit;
int *quit;
int request_pipe; int request_pipe;
long *connfd;
queueT *q; // puntatore alla queue dei file queueT *q; // puntatore alla queue dei file
taglia_t *taglia; // puntatore alla struct del file di log taglia_t *taglia; // puntatore alla struct del file di log
threadpool_t *pool; // puntatore alla threadpool threadpool_t *pool; // puntatore alla threadpool
pthread_mutex_t *lock; 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; } threadT;
// funzione eseguita dal generico Worker del pool di thread // funzione eseguita dal generico Worker del pool di thread