2022-04-11 19:19:17 +02:00
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
|
#include <signal.h>
|
2022-03-09 19:24:49 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
|
|
#include <threadpool.h>
|
|
|
|
|
#include <conn.h>
|
|
|
|
|
#include <util.h>
|
|
|
|
|
#include <serverWorker.h>
|
2022-03-11 11:57:52 +01:00
|
|
|
#include <ini.h>
|
2022-03-15 00:30:04 +01:00
|
|
|
#include <serverUtil.h>
|
|
|
|
|
#include <fileQueue.h>
|
2022-03-22 19:35:06 +01:00
|
|
|
#include <taglialegna.h>
|
2022-03-09 19:24:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @struct sigHandlerArgs_t
|
|
|
|
|
* @brief struttura contenente le informazioni da passare
|
|
|
|
|
* al signal handler thread
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
typedef struct {
|
|
|
|
|
sigset_t *set; // set dei segnali da gestire (mascherati)
|
|
|
|
|
int signal_pipe; // descrittore di scrittura di una pipe senza nome
|
|
|
|
|
} sigHandler_t;
|
|
|
|
|
|
|
|
|
|
// funzione eseguita dal signal handler thread
|
2022-03-16 23:44:53 +01:00
|
|
|
static void *sigHandler(void *arg);
|
2022-03-09 19:24:49 +01:00
|
|
|
|
|
|
|
|
static void usage(const char *argv0) {
|
|
|
|
|
// TODO change this
|
2022-03-27 00:20:34 +01:00
|
|
|
fprintf(stderr, "use: %s <config file location>\n", argv0);
|
2022-03-09 19:24:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void checkargs(int argc, char* argv[]) {
|
|
|
|
|
// TODO change all this
|
|
|
|
|
if (argc != 2) {
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2022-03-11 11:57:52 +01:00
|
|
|
ini_t *config = ini_load(argv[1]);
|
|
|
|
|
if ( config == NULL) {
|
|
|
|
|
fprintf(stderr, "ERROR: unable to read config file\n");
|
2022-03-09 19:24:49 +01:00
|
|
|
usage(argv[0]);
|
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2022-03-11 11:57:52 +01:00
|
|
|
ini_free(config);
|
2022-03-09 19:24:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2022-03-21 01:51:45 +01:00
|
|
|
// read config file
|
2022-03-09 19:24:49 +01:00
|
|
|
checkargs(argc, argv);
|
2022-03-11 11:57:52 +01:00
|
|
|
ini_t *config = ini_load(argv[1]);
|
2022-03-15 00:30:04 +01:00
|
|
|
int threadsInPool; CONFGETINT(threadsInPool, config, "threadpool", "quantity", NULL, 10);
|
2022-03-16 23:44:53 +01:00
|
|
|
int pendingSize; CONFGETINT(pendingSize, config, "threadpool", "pending", NULL, 10);
|
2022-03-15 00:30:04 +01:00
|
|
|
int maxFiles; CONFGETINT(maxFiles, config, "files", "MaxFiles", NULL, 10);
|
|
|
|
|
int maxSize; CONFGETINT(maxSize, config, "files", "MaxSize", NULL, 10);
|
2022-04-04 22:31:14 +02:00
|
|
|
const char *buff;
|
2022-04-11 19:19:17 +02:00
|
|
|
char *logFile=NULL; CONFGETSTR(logFile, config, "log", "logFile", buff);
|
|
|
|
|
char *socketName=NULL; CONFGETSTR(socketName, config, "socket", "name", buff);
|
2022-03-22 19:35:06 +01:00
|
|
|
int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10);
|
2022-03-11 11:57:52 +01:00
|
|
|
ini_free(config);
|
2022-03-09 19:24:49 +01:00
|
|
|
|
|
|
|
|
sigset_t mask;
|
2022-03-21 01:51:45 +01:00
|
|
|
sigfillset(&mask);
|
|
|
|
|
sigdelset(&mask, SIGPIPE); // tolgo soltanto la sigpipe
|
2022-03-09 19:24:49 +01:00
|
|
|
|
2022-03-21 01:51:45 +01:00
|
|
|
if (pthread_sigmask(SIG_SETMASK, &mask, NULL) != 0) {
|
2022-03-09 19:24:49 +01:00
|
|
|
fprintf(stderr, "ERROR setting mask\n");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ignoro SIGPIPE per evitare di essere terminato da una scrittura su un socket
|
|
|
|
|
struct sigaction s;
|
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
|
s.sa_handler = SIG_IGN;
|
|
|
|
|
if ( (sigaction(SIGPIPE,&s,NULL)) == -1 ) {
|
|
|
|
|
perror("sigaction");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 01:51:45 +01:00
|
|
|
// remove("mysock"); maybe necessary???
|
|
|
|
|
|
|
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
// creo la struttura per il log
|
2022-04-04 22:31:14 +02:00
|
|
|
taglia_t *taglia = taglia_init(logFile, 0);
|
2022-03-22 19:35:06 +01:00
|
|
|
free(logFile); // free del nome del file
|
|
|
|
|
if(taglia==NULL) {
|
|
|
|
|
perror("taglia_init");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
2022-04-04 22:31:14 +02:00
|
|
|
// risorse per il logfile usate nel main
|
|
|
|
|
char buf[2048];
|
|
|
|
|
int n;
|
2022-03-22 19:35:06 +01:00
|
|
|
|
2022-03-15 00:30:04 +01:00
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
// pipes di comunicazione fra thread main e sigHandler thread/threadF threads
|
2022-03-09 19:24:49 +01:00
|
|
|
int signal_pipe[2];
|
2022-03-22 19:35:06 +01:00
|
|
|
int request_pipe[2];
|
2022-03-09 19:24:49 +01:00
|
|
|
if (pipe(signal_pipe) == -1) {
|
|
|
|
|
perror("pipe");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
2022-03-22 19:35:06 +01:00
|
|
|
if (pipe(request_pipe) == -1) {
|
|
|
|
|
perror("pipe");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
2022-03-15 00:30:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-16 23:44:53 +01:00
|
|
|
// thread per la gestione delle interruzioni
|
2022-03-09 19:24:49 +01:00
|
|
|
pthread_t sighandler_thread;
|
|
|
|
|
sigHandler_t handlerArgs = { &mask, signal_pipe[1] };
|
|
|
|
|
if (pthread_create(&sighandler_thread, NULL, sigHandler, &handlerArgs) != 0) {
|
|
|
|
|
fprintf(stderr, "ERROR creating signal handler thread\n");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
// scrivo sul log
|
2022-04-04 22:31:14 +02:00
|
|
|
n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %ld\n", (long) sighandler_thread);
|
|
|
|
|
if( n<0 ) {
|
2022-03-22 19:35:06 +01:00
|
|
|
perror("snprintf");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
if( taglia_log(taglia, buf) < 0)
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
|
2022-03-27 00:20:34 +01:00
|
|
|
|
|
|
|
|
// creo lock per operazioni su file
|
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
|
if (pthread_mutex_init(&lock, NULL) != 0) {
|
|
|
|
|
perror("pthread_mutex_init lock");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
// creo socket
|
2022-03-09 19:24:49 +01:00
|
|
|
int listenfd;
|
|
|
|
|
if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
|
perror("socket");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sockaddr_un serv_addr;
|
|
|
|
|
memset(&serv_addr, '0', sizeof(serv_addr));
|
|
|
|
|
serv_addr.sun_family = AF_UNIX;
|
2022-03-22 19:35:06 +01:00
|
|
|
strncpy(serv_addr.sun_path, socketName, strlen(socketName)+1);
|
2022-03-09 19:24:49 +01:00
|
|
|
|
|
|
|
|
if (bind(listenfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1) {
|
|
|
|
|
perror("bind");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
2022-03-22 19:35:06 +01:00
|
|
|
if (listen(listenfd, maxBacklog) == -1) {
|
2022-03-09 19:24:49 +01:00
|
|
|
perror("listen");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
// scrivo sul log
|
|
|
|
|
n = snprintf(buf, sizeof(buf), "Creato socket: %s\n", socketName);
|
2022-03-31 22:26:44 +02:00
|
|
|
if( n<0 ) {
|
2022-03-22 19:35:06 +01:00
|
|
|
perror("snprintf");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
if( taglia_log(taglia, buf) < 0)
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 00:30:04 +01:00
|
|
|
// creo la queue
|
|
|
|
|
queueT *queue = createQueue(maxFiles, maxSize);
|
|
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
// creo la lista dei client in attesa a una lock
|
2022-04-23 17:43:38 +02:00
|
|
|
waiting_t *waiting = NULL;
|
2022-03-22 19:35:06 +01:00
|
|
|
|
2022-03-27 00:20:34 +01:00
|
|
|
// creo la threadpool
|
2022-03-09 19:24:49 +01:00
|
|
|
threadpool_t *pool = NULL;
|
|
|
|
|
|
2022-03-16 23:44:53 +01:00
|
|
|
pool = createThreadPool(threadsInPool, pendingSize);
|
2022-03-09 19:24:49 +01:00
|
|
|
if (!pool) {
|
|
|
|
|
fprintf(stderr, "ERROR creating thread pool\n");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
// scrivo sul log
|
2022-04-23 17:43:38 +02:00
|
|
|
n = snprintf(buf, sizeof(buf), "Creato threadpool di dimensione %d e pending size %d\n", threadsInPool, pendingSize);
|
2022-03-31 22:26:44 +02:00
|
|
|
if( n<0 ) {
|
2022-03-22 19:35:06 +01:00
|
|
|
perror("snprintf");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
if( taglia_log(taglia, buf) < 0)
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
|
2022-03-09 19:24:49 +01:00
|
|
|
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
|
2022-03-22 19:35:06 +01:00
|
|
|
FD_SET(request_pipe[0], &set); // aggiungo il descrittore di lettura della request_pipe
|
2022-03-09 19:24:49 +01:00
|
|
|
|
|
|
|
|
// tengo traccia del file descriptor con id piu' grande
|
|
|
|
|
int fdmax = (listenfd > signal_pipe[0]) ? listenfd : signal_pipe[0];
|
2022-03-22 19:35:06 +01:00
|
|
|
fdmax = (fdmax > request_pipe[0]) ? fdmax : request_pipe[0];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// scrivo sul log
|
|
|
|
|
n = snprintf(buf, sizeof(buf), "File Server ready.\n\tMaxFiles: %d\n\tMaxSize: %d\n", maxFiles, maxSize);
|
2022-03-31 22:26:44 +02:00
|
|
|
if( n<0 ) {
|
2022-03-22 19:35:06 +01:00
|
|
|
perror("snprintf");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
if( taglia_log(taglia, buf) < 0)
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
|
|
|
|
|
printf("File Server ready.");
|
|
|
|
|
fflush(stdout);
|
2022-03-09 19:24:49 +01:00
|
|
|
|
2022-03-12 10:05:58 +01:00
|
|
|
|
|
|
|
|
|
2022-03-16 23:44:53 +01:00
|
|
|
volatile int quit = 0;
|
2022-03-18 20:49:28 +01:00
|
|
|
volatile int stopNewConnections = 0; // true to stop new connections
|
|
|
|
|
volatile int numberOfConnections = 0;
|
2022-03-16 23:44:53 +01:00
|
|
|
|
|
|
|
|
while(!quit) {
|
2022-03-09 19:24:49 +01:00
|
|
|
// copio il set nella variabile temporanea per la select
|
|
|
|
|
tmpset = set;
|
|
|
|
|
if (select(fdmax+1, &tmpset, NULL, NULL, NULL) == -1) {
|
|
|
|
|
perror("select");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
// cerchiamo di capire da quale fd abbiamo ricevuto una richiesta
|
2022-04-24 15:38:30 +02:00
|
|
|
for(long i=0; i <= fdmax; ++i) {
|
2022-03-09 19:24:49 +01:00
|
|
|
if (FD_ISSET(i, &tmpset)) {
|
2022-04-24 15:38:30 +02:00
|
|
|
long* connfd = malloc(sizeof(long));
|
2022-03-12 10:05:58 +01:00
|
|
|
if (!connfd) {
|
|
|
|
|
perror("ERROR FATAL malloc");
|
|
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
2022-03-09 19:24:49 +01:00
|
|
|
if (i == listenfd) { // e' una nuova richiesta di connessione
|
2022-03-16 23:44:53 +01:00
|
|
|
if(stopNewConnections) { // non vogliamo nuove connessioni
|
2022-03-22 19:35:06 +01:00
|
|
|
// scrivo sul log
|
2022-04-25 14:59:30 +02:00
|
|
|
if( taglia_log(taglia, "Nuova connessione rifiutata, server in terminazione\n") < 0) {
|
|
|
|
|
free(connfd);
|
2022-03-22 19:35:06 +01:00
|
|
|
goto _cleanup;
|
2022-04-25 14:59:30 +02:00
|
|
|
}
|
2022-03-22 19:35:06 +01:00
|
|
|
|
2022-03-16 23:44:53 +01:00
|
|
|
FD_CLR(i, &set);
|
|
|
|
|
close(i);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// accetto la connessione nuova
|
2022-03-12 10:05:58 +01:00
|
|
|
if ((*connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL)) == -1) {
|
2022-03-09 19:24:49 +01:00
|
|
|
perror("accept");
|
2022-04-25 14:59:30 +02:00
|
|
|
free(connfd);
|
2022-03-09 19:24:49 +01:00
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-27 00:20:34 +01:00
|
|
|
|
|
|
|
|
// scrivo sul log
|
2022-04-25 14:59:30 +02:00
|
|
|
n = snprintf(buf, sizeof(buf), "New client: %ld\n", *connfd);
|
2022-03-31 22:26:44 +02:00
|
|
|
if( n<0 ) {
|
2022-03-27 00:20:34 +01:00
|
|
|
perror("snprintf");
|
2022-04-25 14:59:30 +02:00
|
|
|
free(connfd);
|
2022-03-27 00:20:34 +01:00
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
2022-04-25 14:59:30 +02:00
|
|
|
if( taglia_log(taglia, buf) < 0) {
|
|
|
|
|
free(connfd);
|
2022-03-27 00:20:34 +01:00
|
|
|
goto _cleanup;
|
2022-04-25 14:59:30 +02:00
|
|
|
}
|
2022-03-27 00:20:34 +01:00
|
|
|
|
2022-03-16 23:44:53 +01:00
|
|
|
// creo gli argomenti da passare al thread
|
2022-03-27 00:20:34 +01:00
|
|
|
threadT* args = calloc(1, sizeof(threadT));
|
|
|
|
|
if(!args) {
|
|
|
|
|
perror("ERROR FATAL calloc");
|
2022-04-25 14:59:30 +02:00
|
|
|
free(connfd);
|
2022-03-27 00:20:34 +01:00
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
|
|
|
|
args->connfd = connfd;
|
|
|
|
|
args->quit = &quit;
|
|
|
|
|
args->request_pipe = request_pipe[1];
|
2022-04-04 22:31:14 +02:00
|
|
|
args->q = queue;
|
|
|
|
|
args->taglia = taglia;
|
2022-03-27 00:20:34 +01:00
|
|
|
args->pool = pool;
|
|
|
|
|
args->lock = &lock;
|
|
|
|
|
args->waiting = &waiting;
|
2022-03-09 19:24:49 +01:00
|
|
|
|
2022-03-16 23:44:53 +01:00
|
|
|
// aggiungo al threadpool
|
2022-03-12 10:05:58 +01:00
|
|
|
int r = addToThreadPool(pool, threadF, args);
|
2022-03-27 00:20:34 +01:00
|
|
|
if (r == 0) {
|
2022-03-16 23:44:53 +01:00
|
|
|
numberOfConnections++;
|
2022-03-09 19:24:49 +01:00
|
|
|
continue; // aggiunto con successo
|
2022-03-27 00:20:34 +01:00
|
|
|
}
|
2022-03-16 23:44:53 +01:00
|
|
|
if (r < 0) // errore interno
|
2022-03-09 19:24:49 +01:00
|
|
|
fprintf(stderr, "ERROR FATAL adding to the thread pool\n");
|
|
|
|
|
else // coda dei pendenti piena
|
|
|
|
|
fprintf(stderr, "ERROR SERVER TOO BUSY\n");
|
|
|
|
|
free(args);
|
2022-03-12 10:05:58 +01:00
|
|
|
close(*connfd);
|
|
|
|
|
free(connfd);
|
2022-03-09 19:24:49 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-27 00:20:34 +01:00
|
|
|
if (i == request_pipe[0]) { // un worker ha finito di servire un client
|
|
|
|
|
int pdr; // ottengo il descrittore della pipe
|
2022-04-04 22:31:14 +02:00
|
|
|
if (readn(request_pipe[0], &pdr, sizeof(int)) == -1) {
|
2022-03-27 00:20:34 +01:00
|
|
|
perror("readn");
|
2022-04-25 14:59:30 +02:00
|
|
|
free(connfd);
|
2022-03-27 00:20:34 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (pdr) {
|
|
|
|
|
case -1: // client disconnected
|
|
|
|
|
--numberOfConnections;
|
|
|
|
|
if (stopNewConnections && numberOfConnections <= 0) {
|
|
|
|
|
quit = 1;
|
|
|
|
|
// termino il signalThread
|
|
|
|
|
pthread_cancel(sighandler_thread);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
default: // client served but not disconnected
|
|
|
|
|
FD_SET(pdr, &set);
|
|
|
|
|
if (pdr > fdmax) {
|
2022-04-04 22:31:14 +02:00
|
|
|
fdmax = pdr;
|
2022-03-27 00:20:34 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-16 23:44:53 +01:00
|
|
|
if (i == signal_pipe[0]) { // controllo se devo terminare
|
|
|
|
|
int code;
|
|
|
|
|
if (readn(signal_pipe[0], &code, sizeof(int)) == -1) {
|
|
|
|
|
perror("readn");
|
2022-04-25 14:59:30 +02:00
|
|
|
free(connfd);
|
2022-03-16 23:44:53 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch (code) {
|
|
|
|
|
case 0: { // stop alle connessioni
|
|
|
|
|
stopNewConnections = 1;
|
2022-03-27 00:20:34 +01:00
|
|
|
|
|
|
|
|
// scrivo sul log
|
2022-04-25 14:59:30 +02:00
|
|
|
if( taglia_log(taglia, "Stop new connections\n") < 0){
|
|
|
|
|
free(connfd);
|
2022-03-27 00:20:34 +01:00
|
|
|
goto _cleanup;
|
2022-04-25 14:59:30 +02:00
|
|
|
}
|
2022-03-16 23:44:53 +01:00
|
|
|
if (numberOfConnections == 0) {
|
|
|
|
|
quit = 1;
|
2022-03-27 00:20:34 +01:00
|
|
|
// termino il signalThread
|
|
|
|
|
pthread_cancel(sighandler_thread);
|
2022-03-16 23:44:53 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 1: { // stop immediato
|
|
|
|
|
quit = 1;
|
2022-03-27 00:20:34 +01:00
|
|
|
// scrivo sul log
|
2022-04-25 14:59:30 +02:00
|
|
|
if( taglia_log(taglia, "Immediate quit\n") < 0) {
|
|
|
|
|
free(connfd);
|
2022-03-27 00:20:34 +01:00
|
|
|
goto _cleanup;
|
2022-04-25 14:59:30 +02:00
|
|
|
}
|
2022-03-16 23:44:53 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
perror("ERROR codice inviato dal sigThread invalido.\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-04-25 14:59:30 +02:00
|
|
|
free(connfd);
|
2022-03-09 19:24:49 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2022-03-16 23:44:53 +01:00
|
|
|
else { // richiesta di un client già connesso
|
|
|
|
|
FD_CLR(i, &set);
|
|
|
|
|
|
|
|
|
|
fdmax = (i>fdmax)?i:fdmax;
|
|
|
|
|
|
|
|
|
|
// creo gli argomenti da passare al thread
|
2022-03-27 00:20:34 +01:00
|
|
|
threadT* args = calloc(1, sizeof(threadT));
|
|
|
|
|
if(!args) {
|
|
|
|
|
perror("ERROR FATAL calloc");
|
2022-04-25 14:59:30 +02:00
|
|
|
free(connfd);
|
2022-03-27 00:20:34 +01:00
|
|
|
goto _cleanup;
|
|
|
|
|
}
|
2022-04-24 01:36:43 +02:00
|
|
|
*connfd = i;
|
2022-03-27 00:20:34 +01:00
|
|
|
args->connfd = connfd;
|
|
|
|
|
args->quit = &quit;
|
|
|
|
|
args->request_pipe = request_pipe[1];
|
2022-04-04 22:31:14 +02:00
|
|
|
args->q = queue;
|
|
|
|
|
args->taglia = taglia;
|
2022-03-27 00:20:34 +01:00
|
|
|
args->pool = pool;
|
|
|
|
|
args->lock = &lock;
|
|
|
|
|
args->waiting = &waiting;
|
|
|
|
|
|
|
|
|
|
// aggiungo al threadpool
|
|
|
|
|
int r = addToThreadPool(pool, threadF, args);
|
|
|
|
|
if (r == 0) {
|
|
|
|
|
numberOfConnections++;
|
|
|
|
|
continue; // aggiunto con successo
|
|
|
|
|
}
|
2022-03-16 23:44:53 +01:00
|
|
|
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");
|
2022-03-27 00:20:34 +01:00
|
|
|
free(args);
|
|
|
|
|
close(*connfd);
|
|
|
|
|
free(connfd);
|
2022-03-16 23:44:53 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-09 19:24:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-24 01:36:43 +02:00
|
|
|
fprintf(stdout, "\n");
|
2022-03-09 19:24:49 +01:00
|
|
|
|
|
|
|
|
destroyThreadPool(pool, 0); // notifico che i thread dovranno uscire
|
2022-04-11 18:38:04 +02:00
|
|
|
clearWaiting(&waiting); // destroy waiting list
|
2022-04-04 22:31:14 +02:00
|
|
|
taglia_stats(taglia, stdout); // print stats
|
2022-03-09 19:24:49 +01:00
|
|
|
|
2022-03-27 00:20:34 +01:00
|
|
|
// print all files in storage during shutdown
|
2022-04-04 22:31:14 +02:00
|
|
|
if (printQueue(stdout, queue) == -1) {
|
2022-03-16 23:44:53 +01:00
|
|
|
perror("printQueue");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
destroyQueue(queue);
|
|
|
|
|
|
2022-03-09 19:24:49 +01:00
|
|
|
// aspetto la terminazione de signal handler thread
|
|
|
|
|
pthread_join(sighandler_thread, NULL);
|
|
|
|
|
|
2022-03-27 00:20:34 +01:00
|
|
|
taglia_del(taglia);
|
|
|
|
|
|
2022-03-22 19:35:06 +01:00
|
|
|
unlink(socketName);
|
2022-03-16 23:44:53 +01:00
|
|
|
|
2022-04-25 14:59:30 +02:00
|
|
|
free(socketName);
|
2022-03-16 23:44:53 +01:00
|
|
|
printf("File Storage Server terminato.\n");
|
|
|
|
|
fflush(stdout);
|
2022-03-09 19:24:49 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
_cleanup:
|
2022-03-22 19:35:06 +01:00
|
|
|
unlink(socketName);
|
2022-04-25 14:59:30 +02:00
|
|
|
free(socketName);
|
2022-03-09 19:24:49 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
2022-03-16 23:44:53 +01:00
|
|
|
|
|
|
|
|
// funzione eseguita dal signal handler thread
|
|
|
|
|
static void *sigHandler(void *arg) {
|
|
|
|
|
sigset_t *set = ((sigHandler_t*)arg)->set;
|
|
|
|
|
int fd_pipe = ((sigHandler_t*)arg)->signal_pipe;
|
|
|
|
|
|
2022-04-04 22:31:14 +02:00
|
|
|
if(pthread_sigmask(SIG_SETMASK, set, NULL)) {
|
2022-03-16 23:44:53 +01:00
|
|
|
fprintf(stderr, "ERROR setting mask\n");
|
|
|
|
|
return (void*) 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
int sig;
|
|
|
|
|
int code;
|
2022-04-04 22:31:14 +02:00
|
|
|
int r = sigwait(set, &sig);
|
2022-03-16 23:44:53 +01:00
|
|
|
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
errno = r;
|
|
|
|
|
perror("FATAL ERROR 'sigwait'");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
|
case SIGHUP:
|
|
|
|
|
code = 0;
|
|
|
|
|
// notifico il thread manager di smettere di accettare nuove connessioni in entrata
|
|
|
|
|
if (writen(fd_pipe, &code, sizeof(int)) == -1) {
|
|
|
|
|
perror("writen");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SIGINT:
|
|
|
|
|
case SIGQUIT:
|
|
|
|
|
code = 1;
|
|
|
|
|
// notifico il thread manager di terminare il server il prima possibile
|
|
|
|
|
if (writen(fd_pipe, &code, sizeof(int)) == -1) {
|
|
|
|
|
perror("writen");
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|