style and fixed bug allocating too much memory in append file
This commit is contained in:
138
src/server.c
138
src/server.c
@ -9,21 +9,21 @@
|
||||
#include <ctype.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
#include <threadpool.h>
|
||||
#include <conn.h>
|
||||
#include <util.h>
|
||||
#include <serverWorker.h>
|
||||
#include <ini.h>
|
||||
#include <serverUtil.h>
|
||||
#include <fileQueue.h>
|
||||
#include <taglialegna.h>
|
||||
#include "threadpool.h"
|
||||
#include "conn.h"
|
||||
#include "util.h"
|
||||
#include "serverWorker.h"
|
||||
#include "ini.h"
|
||||
#include "serverUtil.h"
|
||||
#include "fileQueue.h"
|
||||
#include "taglialegna.h"
|
||||
|
||||
typedef struct {
|
||||
sigset_t *set; // set of signals to handle (masked)
|
||||
int signal_pipe; // unnamed pipe's descriptor
|
||||
sigset_t *set; /* set of signals to handle (masked) */
|
||||
int signal_pipe; /* unnamed pipe's descriptor */
|
||||
} sigHandler_t;
|
||||
|
||||
// signal handler thread function
|
||||
/* signal handler thread function */
|
||||
static void *sigHandler(void *arg);
|
||||
|
||||
static void usage(const char *argv0) {
|
||||
@ -45,7 +45,7 @@ static void checkargs(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// read config file
|
||||
/* read config file */
|
||||
checkargs(argc, argv);
|
||||
ini_t *config = ini_load(argv[1]);
|
||||
int threadsInPool; CONFGETINT(threadsInPool, config, "threadpool", "quantity", NULL, 10);
|
||||
@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
sigset_t mask;
|
||||
sigfillset(&mask);
|
||||
sigdelset(&mask, SIGPIPE); // only sigpipe is excluded
|
||||
sigdelset(&mask, SIGPIPE); /* only sigpipe is excluded */
|
||||
|
||||
if (pthread_sigmask(SIG_SETMASK, &mask, NULL) != 0) {
|
||||
fprintf(stderr, "Error: setting mask.\n");
|
||||
@ -79,19 +79,19 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
(void) unlink(socketName);
|
||||
|
||||
// create structure for logging
|
||||
/* create structure for logging */
|
||||
taglia = taglia_init(logFile, 0);
|
||||
free(logFile); // free the name of the file
|
||||
free(logFile); /* free the name of the file */
|
||||
if(taglia==NULL) {
|
||||
perror("main: taglia_init");
|
||||
goto _cleanup_beforesigthread;
|
||||
}
|
||||
// buffer for loggin
|
||||
/* buffer for loggin */
|
||||
char buf[2048];
|
||||
int n;
|
||||
|
||||
// pipes for comunication between main thread, sigHandler thread
|
||||
// and worker threads
|
||||
/* pipes for comunication between main thread, sigHandler thread
|
||||
* and worker threads */
|
||||
int signal_pipe[2];
|
||||
int request_pipe[2];
|
||||
if (pipe(signal_pipe) == -1) {
|
||||
@ -104,7 +104,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
|
||||
// thread for handling interruptions
|
||||
/* thread for handling interruptions */
|
||||
pthread_t sighandler_thread;
|
||||
sigHandler_t handlerArgs = { &mask, signal_pipe[1] };
|
||||
if (pthread_create(&sighandler_thread, NULL, sigHandler, &handlerArgs) != 0) {
|
||||
@ -112,7 +112,7 @@ int main(int argc, char *argv[]) {
|
||||
goto _cleanup;
|
||||
}
|
||||
|
||||
// write to logfile
|
||||
/* write to logfile */
|
||||
n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %ld\n", (long) sighandler_thread);
|
||||
if( n<0 ) {
|
||||
perror("main: snprintf");
|
||||
@ -122,14 +122,14 @@ int main(int argc, char *argv[]) {
|
||||
goto _cleanup;
|
||||
|
||||
|
||||
// lock for operations on files
|
||||
/* lock for operations on files */
|
||||
pthread_mutex_t lock;
|
||||
if (pthread_mutex_init(&lock, NULL) != 0) {
|
||||
perror("main: pthread_mutex_init");
|
||||
goto _cleanup;
|
||||
}
|
||||
|
||||
// create socket
|
||||
/* create socket */
|
||||
int listenfd;
|
||||
if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||
perror("main: socket");
|
||||
@ -150,7 +150,7 @@ int main(int argc, char *argv[]) {
|
||||
goto _cleanup;
|
||||
}
|
||||
|
||||
// write to logfile
|
||||
/* write to logfile */
|
||||
n = snprintf(buf, sizeof(buf), "Creato socket: %s\n", socketName);
|
||||
if( n<0 ) {
|
||||
perror("main: snprintf");
|
||||
@ -160,13 +160,13 @@ int main(int argc, char *argv[]) {
|
||||
goto _cleanup;
|
||||
|
||||
|
||||
// create queue
|
||||
/* create queue */
|
||||
queue = createQueue(maxFiles, maxSize);
|
||||
|
||||
// create queue for clients waiting on a lock
|
||||
/* create queue for clients waiting on a lock */
|
||||
waiting_t *waiting = NULL;
|
||||
|
||||
// create threadpool
|
||||
/* create threadpool */
|
||||
threadpool_t *pool = NULL;
|
||||
|
||||
pool = createThreadPool(threadsInPool, pendingSize);
|
||||
@ -175,7 +175,7 @@ int main(int argc, char *argv[]) {
|
||||
goto _cleanup;
|
||||
}
|
||||
|
||||
// write to logfile
|
||||
/* write to logfile */
|
||||
n = snprintf(buf, sizeof(buf), "Creato threadpool di dimensione %d e pending size %d\n", threadsInPool, pendingSize);
|
||||
if(n<0) {
|
||||
perror("main: snprintf");
|
||||
@ -185,22 +185,22 @@ int main(int argc, char *argv[]) {
|
||||
goto _cleanup;
|
||||
|
||||
|
||||
// selector
|
||||
/* selector */
|
||||
fd_set set, tmpset;
|
||||
FD_ZERO(&set);
|
||||
FD_ZERO(&tmpset);
|
||||
|
||||
// add fd for socket, signal pipe and request pipe.
|
||||
/* add fd for socket, signal pipe and request pipe. */
|
||||
FD_SET(listenfd, &set);
|
||||
FD_SET(signal_pipe[0], &set);
|
||||
FD_SET(request_pipe[0], &set);
|
||||
|
||||
// get max file descriptor
|
||||
/* get max file descriptor */
|
||||
int fdmax = (listenfd > signal_pipe[0]) ? listenfd : signal_pipe[0];
|
||||
fdmax = (fdmax > request_pipe[0]) ? fdmax : request_pipe[0];
|
||||
|
||||
|
||||
// write to logfile
|
||||
/* write to logfile */
|
||||
n = snprintf(buf, sizeof(buf), "File Server ready.\n\tMaxFiles: %d\n\tMaxSize: %d\n", maxFiles, maxSize);
|
||||
if( n<0 ) {
|
||||
perror("main: snprintf");
|
||||
@ -218,13 +218,13 @@ int main(int argc, char *argv[]) {
|
||||
volatile int numberOfConnections = 0;
|
||||
|
||||
while(!quit) {
|
||||
// copy the set in the tmp variable for the select
|
||||
/* copy the set in the tmp variable for the select */
|
||||
tmpset = set;
|
||||
if (select(fdmax+1, &tmpset, NULL, NULL, NULL) == -1) {
|
||||
perror("main: select");
|
||||
goto _cleanup;
|
||||
}
|
||||
// search for the ready fd
|
||||
/* search for the ready fd */
|
||||
for(long i=0; i <= fdmax; ++i) {
|
||||
if (FD_ISSET(i, &tmpset)) {
|
||||
long* connfd = malloc(sizeof(long));
|
||||
@ -232,9 +232,9 @@ int main(int argc, char *argv[]) {
|
||||
perror("main: malloc");
|
||||
goto _cleanup;
|
||||
}
|
||||
if (i == listenfd) { // new request for connection
|
||||
if(stopNewConnections) { // no new connections allowed
|
||||
// write to logfile
|
||||
if (i == listenfd) { /* new request for connection */
|
||||
if(stopNewConnections) { /* no new connections allowed */
|
||||
/* write to logfile */
|
||||
if( taglia_log(taglia, "Nuova connessione rifiutata, server in terminazione\n") < 0) {
|
||||
free(connfd);
|
||||
goto _cleanup;
|
||||
@ -246,14 +246,14 @@ int main(int argc, char *argv[]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// accept new connection
|
||||
/* accept new connection */
|
||||
if ((*connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL)) == -1) {
|
||||
perror("main: accept");
|
||||
free(connfd);
|
||||
goto _cleanup;
|
||||
}
|
||||
|
||||
// write to logfile
|
||||
/* write to logfile */
|
||||
n = snprintf(buf, sizeof(buf), "Nuovo client: %ld\n", *connfd);
|
||||
if( n<0 ) {
|
||||
perror("main: snprintf");
|
||||
@ -265,7 +265,7 @@ int main(int argc, char *argv[]) {
|
||||
goto _cleanup;
|
||||
}
|
||||
|
||||
// create args to pass to the worker
|
||||
/* create args to pass to the worker */
|
||||
threadT* args = calloc(1, sizeof(threadT));
|
||||
if(!args) {
|
||||
perror("main: calloc");
|
||||
@ -281,40 +281,40 @@ int main(int argc, char *argv[]) {
|
||||
args->lock = &lock;
|
||||
args->waiting = &waiting;
|
||||
|
||||
// add to threadpool
|
||||
/* add to threadpool */
|
||||
int r = addToThreadPool(pool, threadF, args);
|
||||
if (r == 0) { // no errors
|
||||
if (r == 0) { /* no errors */
|
||||
numberOfConnections++;
|
||||
free(connfd);
|
||||
continue;
|
||||
}
|
||||
if (r < 0) // internal error
|
||||
if (r < 0) /* internal error */
|
||||
fprintf(stderr, "Error: adding to the thread pool.\n");
|
||||
else // pending queue full
|
||||
else /* pending queue full */
|
||||
fprintf(stderr, "Error: server too busy.\n");
|
||||
close(*connfd);
|
||||
free(connfd);
|
||||
continue;
|
||||
}
|
||||
if (i == request_pipe[0]) { // worker finished task
|
||||
long pdr; // get pipe descriptor
|
||||
if (i == request_pipe[0]) { /* worker finished task */
|
||||
long pdr; /* get pipe descriptor */
|
||||
if (readn(request_pipe[0], &pdr, sizeof(long)) == -1) {
|
||||
perror("main: readn");
|
||||
free(connfd);
|
||||
break;
|
||||
}
|
||||
switch (pdr) {
|
||||
case -1: // client disconnected
|
||||
case -1: /* client disconnected */
|
||||
--numberOfConnections;
|
||||
if (stopNewConnections && numberOfConnections <= 0) {
|
||||
// quitting and terminating signalThread
|
||||
/* quitting and terminating signalThread */
|
||||
quit = 1;
|
||||
pthread_cancel(sighandler_thread);
|
||||
break;
|
||||
}
|
||||
free(connfd);
|
||||
continue;
|
||||
default: // client served but not disconnected
|
||||
default: /* client served but not disconnected */
|
||||
FD_SET(pdr, &set);
|
||||
if (pdr > fdmax) {
|
||||
fdmax = pdr;
|
||||
@ -324,7 +324,7 @@ int main(int argc, char *argv[]) {
|
||||
free(connfd);
|
||||
continue;
|
||||
}
|
||||
if (i == signal_pipe[0]) { // check if we need to terminate
|
||||
if (i == signal_pipe[0]) { /* check if we need to terminate */
|
||||
int code;
|
||||
if (readn(signal_pipe[0], &code, sizeof(int)) == -1) {
|
||||
perror("main: readn");
|
||||
@ -332,24 +332,24 @@ int main(int argc, char *argv[]) {
|
||||
break;
|
||||
}
|
||||
switch (code) {
|
||||
case 0: { // stop to new connections
|
||||
case 0: { /* stop to new connections */
|
||||
stopNewConnections = 1;
|
||||
|
||||
// write to logfile
|
||||
/* write to logfile */
|
||||
if(taglia_log(taglia, "Stop new connections\n") < 0){
|
||||
free(connfd);
|
||||
goto _cleanup;
|
||||
}
|
||||
if(numberOfConnections == 0) {
|
||||
quit = 1;
|
||||
// stop signalThread
|
||||
/* stop signalThread */
|
||||
pthread_cancel(sighandler_thread);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: { // immediate stop
|
||||
case 1: { /* immediate stop */
|
||||
quit = 1;
|
||||
// write to logfile
|
||||
/* write to logfile */
|
||||
if( taglia_log(taglia, "Immediate quit\n") < 0) {
|
||||
free(connfd);
|
||||
goto _cleanup;
|
||||
@ -364,12 +364,12 @@ int main(int argc, char *argv[]) {
|
||||
free(connfd);
|
||||
break;
|
||||
}
|
||||
else { // request from an already connected client
|
||||
else { /* request from an already connected client */
|
||||
FD_CLR(i, &set);
|
||||
|
||||
fdmax = (i>fdmax)?i:fdmax;
|
||||
|
||||
// create args for worker thread
|
||||
/* create args for worker thread */
|
||||
threadT* args = calloc(1, sizeof(threadT));
|
||||
if(!args) {
|
||||
perror("main: calloc");
|
||||
@ -385,15 +385,15 @@ int main(int argc, char *argv[]) {
|
||||
args->lock = &lock;
|
||||
args->waiting = &waiting;
|
||||
|
||||
// add to the threadpool
|
||||
/* add to the threadpool */
|
||||
int r = addToThreadPool(pool, threadF, args);
|
||||
if (r == 0) {
|
||||
free(connfd);
|
||||
continue;
|
||||
}
|
||||
if (r < 0) // internal error
|
||||
if (r < 0) /* internal error */
|
||||
fprintf(stderr, "Error: adding to the thread pool.\n");
|
||||
else // pending queue full
|
||||
else /* pending queue full */
|
||||
fprintf(stderr, "Error: server too busy.\n");
|
||||
close(*connfd);
|
||||
free(connfd);
|
||||
@ -404,24 +404,24 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
fprintf(stdout, "\n");
|
||||
|
||||
destroyThreadPool(pool, 0); // notify the threads that need to stop
|
||||
clearWaiting(&waiting); // destroy waiting list
|
||||
taglia_stats(taglia, stdout); // print stats
|
||||
destroyThreadPool(pool, 0); /* notify the threads that need to stop */
|
||||
clearWaiting(&waiting); /* destroy waiting list */
|
||||
taglia_stats(taglia, stdout); /* print stats */
|
||||
|
||||
// print all files in storage during shutdown
|
||||
/* print all files in storage during shutdown */
|
||||
if (printQueue(stdout, queue) == -1) {
|
||||
perror("main: printQueue");
|
||||
return 1;
|
||||
}
|
||||
destroyQueue(queue);
|
||||
|
||||
// wait for sigHandler thread
|
||||
/* wait for sigHandler thread */
|
||||
pthread_join(sighandler_thread, NULL);
|
||||
|
||||
// free log file structure
|
||||
/* free log file structure */
|
||||
taglia_del(taglia);
|
||||
|
||||
// unlink socket
|
||||
/* unlink socket */
|
||||
unlink(socketName);
|
||||
|
||||
free(socketName);
|
||||
@ -444,7 +444,7 @@ _cleanup_beforesigthread:
|
||||
return -1;
|
||||
}
|
||||
|
||||
// signal handler thread function
|
||||
/* signal handler thread function */
|
||||
static void *sigHandler(void *arg) {
|
||||
sigset_t *set = ((sigHandler_t*)arg)->set;
|
||||
int fd_pipe = ((sigHandler_t*)arg)->signal_pipe;
|
||||
@ -468,7 +468,7 @@ static void *sigHandler(void *arg) {
|
||||
switch (sig) {
|
||||
case SIGHUP:
|
||||
code = 0;
|
||||
// notify main thread to stop new connections
|
||||
/* notify main thread to stop new connections */
|
||||
if (writen(fd_pipe, &code, sizeof(int)) == -1) {
|
||||
perror("sigHandler: writen");
|
||||
}
|
||||
@ -476,7 +476,7 @@ static void *sigHandler(void *arg) {
|
||||
case SIGINT:
|
||||
case SIGQUIT:
|
||||
code = 1;
|
||||
// notify main thread to stop immediatly
|
||||
/* notify main thread to stop immediatly */
|
||||
if (writen(fd_pipe, &code, sizeof(int)) == -1) {
|
||||
perror("sigHandler: writen");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user