Added log utility and improved server

This commit is contained in:
elvis
2022-03-22 19:35:06 +01:00
parent b11640aff0
commit c1977248d7
7 changed files with 111 additions and 23 deletions

View File

@ -7,3 +7,12 @@ pending = 20
MaxFiles = 20
MaxSize = 10000000
[log]
logFile = ./logs/l.log
[socket]
name = ./socket
backlog = 100

View File

@ -5,6 +5,7 @@
#include <time.h>
#include <taglialegna.h>
#include <fileQueue.h>
taglia_t *taglia_init(char *file, int n, ...) {
int max_files;

View File

@ -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

View File

@ -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

View File

@ -4,6 +4,7 @@
#include <ini.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#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 */

View File

@ -1,7 +1,6 @@
#ifndef _UTIL_H
#define _UTIL_H
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>

View File

@ -15,6 +15,7 @@
#include <ini.h>
#include <serverUtil.h>
#include <fileQueue.h>
#include <taglialegna.h>
/**
@ -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<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
// creo socket
int listenfd;
if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
goto _cleanup;
}
// thread per i segnali
struct sockaddr_un serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strncpy(serv_addr.sun_path, SOCKNAME, strlen(SOCKNAME)+1);
strncpy(serv_addr.sun_path, socketName, strlen(socketName)+1);
if (bind(listenfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1) {
perror("bind");
goto _cleanup;
}
if (listen(listenfd, MAXBACKLOG) == -1) {
if (listen(listenfd, maxBacklog) == -1) {
perror("listen");
goto _cleanup;
}
// scrivo sul log
char buf[2048];
int n;
n = snprintf(buf, sizeof(buf), "Creato socket: %s\n", socketName);
if( n<0 || m<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
// creo la queue
queueT *queue = createQueue(maxFiles, maxSize);
// creo la lista dei client in attesa a una lock
waiting_t *waiting;
threadpool_t *pool = NULL;
pool = createThreadPool(threadsInPool, pendingSize);
@ -137,15 +177,43 @@ int main(int argc, char *argv[]) {
goto _cleanup;
}
// 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);
if( n<0 || m<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
fd_set set, tmpset;
FD_ZERO(&set);
FD_ZERO(&tmpset);
FD_SET(listenfd, &set); // aggiungo il listener fd al master set
FD_SET(signal_pipe[0], &set); // aggiungo il descrittore di lettura della signal_pipe
FD_SET(request_pipe[0], &set); // aggiungo il descrittore di lettura della request_pipe
// tengo traccia del file descriptor con id piu' grande
int fdmax = (listenfd > 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<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
printf("File Server ready.");
fflush(stdout);
@ -165,13 +233,15 @@ int main(int argc, char *argv[]) {
if (FD_ISSET(i, &tmpset)) {
long* connfd = malloc(sizeof(long));
if (!connfd) {
// TODO logging utility
perror("ERROR FATAL malloc");
goto _cleanup;
}
if (i == listenfd) { // e' una nuova richiesta di connessione
if(stopNewConnections) { // non vogliamo nuove connessioni
// TODO log
// scrivo sul log
if( taglia_log(taglia, "Nuova connessione rifiutata, server in terminazione\n") < 0)
goto _cleanup;
FD_CLR(i, &set);
close(i);
continue;
@ -262,7 +332,7 @@ int main(int argc, char *argv[]) {
// aspetto la terminazione de signal handler thread
pthread_join(sighandler_thread, NULL);
unlink(SOCKNAME);
unlink(socketName);
printf("File Storage Server terminato.\n");
fflush(stdout);
@ -270,7 +340,7 @@ int main(int argc, char *argv[]) {
_cleanup:
unlink(SOCKNAME);
unlink(socketName);
return -1;
}