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