perror for server.c

This commit is contained in:
elvis
2022-05-05 21:18:16 +02:00
parent 1a20f374f1
commit 1779fed379

View File

@ -73,7 +73,7 @@ int main(int argc, char *argv[]) {
memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));
s.sa_handler = SIG_IGN; s.sa_handler = SIG_IGN;
if ( (sigaction(SIGPIPE,&s,NULL)) == -1 ) { if ( (sigaction(SIGPIPE,&s,NULL)) == -1 ) {
perror("sigaction"); perror("main: sigaction");
goto _cleanup_beforesigthread; goto _cleanup_beforesigthread;
} }
@ -83,7 +83,7 @@ int main(int argc, char *argv[]) {
taglia = taglia_init(logFile, 0); taglia = taglia_init(logFile, 0);
free(logFile); // free the name of the file free(logFile); // free the name of the file
if(taglia==NULL) { if(taglia==NULL) {
perror("taglia_init"); perror("main: taglia_init");
goto _cleanup_beforesigthread; goto _cleanup_beforesigthread;
} }
// buffer for loggin // buffer for loggin
@ -95,11 +95,11 @@ int main(int argc, char *argv[]) {
int signal_pipe[2]; int signal_pipe[2];
int request_pipe[2]; int request_pipe[2];
if (pipe(signal_pipe) == -1) { if (pipe(signal_pipe) == -1) {
perror("pipe"); perror("main: pipe, signal_pipe");
goto _cleanup_beforesigthread; goto _cleanup_beforesigthread;
} }
if (pipe(request_pipe) == -1) { if (pipe(request_pipe) == -1) {
perror("pipe"); perror("main: pipe, request_pipe");
goto _cleanup_beforesigthread; goto _cleanup_beforesigthread;
} }
@ -115,7 +115,7 @@ int main(int argc, char *argv[]) {
// write to logfile // write to logfile
n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %ld\n", (long) sighandler_thread); n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %ld\n", (long) sighandler_thread);
if( n<0 ) { if( n<0 ) {
perror("snprintf"); perror("main: snprintf");
goto _cleanup; goto _cleanup;
} }
if(taglia_log(taglia, buf) < 0) if(taglia_log(taglia, buf) < 0)
@ -125,14 +125,14 @@ int main(int argc, char *argv[]) {
// lock for operations on files // lock for operations on files
pthread_mutex_t lock; pthread_mutex_t lock;
if (pthread_mutex_init(&lock, NULL) != 0) { if (pthread_mutex_init(&lock, NULL) != 0) {
perror("pthread_mutex_init lock"); perror("main: pthread_mutex_init");
goto _cleanup; goto _cleanup;
} }
// create socket // create socket
int listenfd; int listenfd;
if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket"); perror("main: socket");
goto _cleanup; goto _cleanup;
} }
@ -142,18 +142,18 @@ int main(int argc, char *argv[]) {
strncpy(serv_addr.sun_path, socketName, strlen(socketName)+1); strncpy(serv_addr.sun_path, socketName, strlen(socketName)+1);
if (bind(listenfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1) { if (bind(listenfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1) {
perror("bind"); perror("main: bind");
goto _cleanup; goto _cleanup;
} }
if (listen(listenfd, maxBacklog) == -1) { if (listen(listenfd, maxBacklog) == -1) {
perror("listen"); perror("main: listen");
goto _cleanup; goto _cleanup;
} }
// write to logfile // write to logfile
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("main: snprintf");
goto _cleanup; goto _cleanup;
} }
if( taglia_log(taglia, buf) < 0) if( taglia_log(taglia, buf) < 0)
@ -178,7 +178,7 @@ int main(int argc, char *argv[]) {
// write to logfile // write to logfile
n = snprintf(buf, sizeof(buf), "Creato threadpool di dimensione %d e pending size %d\n", threadsInPool, pendingSize); n = snprintf(buf, sizeof(buf), "Creato threadpool di dimensione %d e pending size %d\n", threadsInPool, pendingSize);
if(n<0) { if(n<0) {
perror("snprintf"); perror("main: snprintf");
goto _cleanup; goto _cleanup;
} }
if(taglia_log(taglia, buf) < 0) if(taglia_log(taglia, buf) < 0)
@ -203,7 +203,7 @@ int main(int argc, char *argv[]) {
// write to logfile // write to logfile
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("main: snprintf");
goto _cleanup; goto _cleanup;
} }
if( taglia_log(taglia, buf) < 0) if( taglia_log(taglia, buf) < 0)
@ -221,7 +221,7 @@ int main(int argc, char *argv[]) {
// copy the set in the tmp variable for the select // copy the set in the tmp variable for the select
tmpset = set; tmpset = set;
if (select(fdmax+1, &tmpset, NULL, NULL, NULL) == -1) { if (select(fdmax+1, &tmpset, NULL, NULL, NULL) == -1) {
perror("select"); perror("main: select");
goto _cleanup; goto _cleanup;
} }
// search for the ready fd // search for the ready fd
@ -229,7 +229,7 @@ int main(int argc, char *argv[]) {
if (FD_ISSET(i, &tmpset)) { if (FD_ISSET(i, &tmpset)) {
long* connfd = malloc(sizeof(long)); long* connfd = malloc(sizeof(long));
if (!connfd) { if (!connfd) {
perror("malloc"); perror("main: malloc");
goto _cleanup; goto _cleanup;
} }
if (i == listenfd) { // new request for connection if (i == listenfd) { // new request for connection
@ -248,7 +248,7 @@ int main(int argc, char *argv[]) {
// accept new connection // accept new connection
if ((*connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL)) == -1) { if ((*connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL)) == -1) {
perror("accept"); perror("main: accept");
free(connfd); free(connfd);
goto _cleanup; goto _cleanup;
} }
@ -256,7 +256,7 @@ int main(int argc, char *argv[]) {
// write to logfile // write to logfile
n = snprintf(buf, sizeof(buf), "Nuovo client: %ld\n", *connfd); n = snprintf(buf, sizeof(buf), "Nuovo client: %ld\n", *connfd);
if( n<0 ) { if( n<0 ) {
perror("snprintf"); perror("main: snprintf");
free(connfd); free(connfd);
goto _cleanup; goto _cleanup;
} }
@ -268,7 +268,7 @@ int main(int argc, char *argv[]) {
// create args to pass to the worker // create args to pass to the worker
threadT* args = calloc(1, sizeof(threadT)); threadT* args = calloc(1, sizeof(threadT));
if(!args) { if(!args) {
perror("calloc"); perror("main: calloc");
free(connfd); free(connfd);
goto _cleanup; goto _cleanup;
} }
@ -299,7 +299,7 @@ int main(int argc, char *argv[]) {
if (i == request_pipe[0]) { // worker finished task if (i == request_pipe[0]) { // worker finished task
long pdr; // get pipe descriptor long pdr; // get pipe descriptor
if (readn(request_pipe[0], &pdr, sizeof(long)) == -1) { if (readn(request_pipe[0], &pdr, sizeof(long)) == -1) {
perror("readn"); perror("main: readn");
free(connfd); free(connfd);
break; break;
} }
@ -327,7 +327,7 @@ int main(int argc, char *argv[]) {
if (i == signal_pipe[0]) { // check if we need to terminate if (i == signal_pipe[0]) { // check if we need to terminate
int code; int code;
if (readn(signal_pipe[0], &code, sizeof(int)) == -1) { if (readn(signal_pipe[0], &code, sizeof(int)) == -1) {
perror("readn"); perror("main: readn");
free(connfd); free(connfd);
break; break;
} }
@ -372,7 +372,7 @@ int main(int argc, char *argv[]) {
// create args for worker thread // create args for worker thread
threadT* args = calloc(1, sizeof(threadT)); threadT* args = calloc(1, sizeof(threadT));
if(!args) { if(!args) {
perror("calloc"); perror("main: calloc");
free(connfd); free(connfd);
goto _cleanup; goto _cleanup;
} }
@ -410,7 +410,7 @@ int main(int argc, char *argv[]) {
// print all files in storage during shutdown // print all files in storage during shutdown
if (printQueue(stdout, queue) == -1) { if (printQueue(stdout, queue) == -1) {
perror("printQueue"); perror("main: printQueue");
return 1; return 1;
} }
destroyQueue(queue); destroyQueue(queue);
@ -461,7 +461,7 @@ static void *sigHandler(void *arg) {
if (r != 0) { if (r != 0) {
errno = r; errno = r;
perror("sigwait"); perror("sigHandler: sigwait");
return NULL; return NULL;
} }
@ -470,7 +470,7 @@ static void *sigHandler(void *arg) {
code = 0; code = 0;
// notify main thread to stop new connections // notify main thread to stop new connections
if (writen(fd_pipe, &code, sizeof(int)) == -1) { if (writen(fd_pipe, &code, sizeof(int)) == -1) {
perror("writen"); perror("sigHandler: writen");
} }
break; break;
case SIGINT: case SIGINT:
@ -478,7 +478,7 @@ static void *sigHandler(void *arg) {
code = 1; code = 1;
// notify main thread to stop immediatly // notify main thread to stop immediatly
if (writen(fd_pipe, &code, sizeof(int)) == -1) { if (writen(fd_pipe, &code, sizeof(int)) == -1) {
perror("writen"); perror("sigHandler: writen");
} }
return NULL; return NULL;
default: default: