fixed -W, working on -r

This commit is contained in:
elvis
2022-04-25 20:24:55 +02:00
parent d8a0220ade
commit e67d3b5580
8 changed files with 72 additions and 105 deletions

View File

@ -196,6 +196,10 @@ int main(int argc, char* argv[]) {
goto _cleanup;
}
if(cmds) {
destroyCommandList(cmds);
}
return 0;
_cleanup:
@ -217,9 +221,9 @@ void destroyCommandList(cmd_t *l) {
while (l) {
tmp = l;
free(l->arg);
l = l->next;
free(tmp->arg);
free(tmp);
}
}
@ -322,8 +326,10 @@ int execute(cmd_t *l, int print) {
if(!ok)
printf("errore\n");
}
if(!ok)
if(!ok) {
closeConnection(tmp->arg);
return 0; // no socket to connect, nothing to do
}
// we only read the first -f, no error reported if more than one is
// specified
tmp = NULL;
@ -398,7 +404,7 @@ int execute(cmd_t *l, int print) {
case 'D':
if(Dir)
free(Dir);
Dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1);
Dir = calloc(strnlen(tmp->arg, MAXARGLENGTH)+1, sizeof(char));
strncpy(Dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
if (setDirectory(Dir, 1) == -1) {
@ -417,7 +423,7 @@ int execute(cmd_t *l, int print) {
case 'd':
if(dir)
free(dir);
dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1);
dir = calloc(strnlen(tmp->arg, MAXARGLENGTH)+1, sizeof(char));
strncpy(dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
if(setDirectory(dir, 0) == -1) {
@ -569,9 +575,10 @@ int cmd_W(char *filelist, char *Dir, int print) {
perror("malloc");
return -1;
}
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)+1);
char *token;
char *token = NULL;
char *string = tofree;
if (print == 1) {
@ -631,13 +638,14 @@ int cmd_r(char *filelist, char *dir, int print) {
// we copy filelist because we are nice
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
if(!tofree) {
perror("malloc");
return -1;
}
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
char *token;
char *token = NULL;
char *string = tofree;
if (print) {
@ -663,7 +671,7 @@ int cmd_r(char *filelist, char *dir, int print) {
void *buf = NULL;
size_t size = -1;
printInfo(0, stdout);
// printInfo(0, stdout);
// read the content of the file
if (ok && readFile(token, &buf, &size) == -1) {
ok = 0;

View File

@ -250,6 +250,7 @@ int main(int argc, char *argv[]) {
FD_CLR(i, &set);
close(i);
free(connfd);
continue;
}
@ -280,7 +281,7 @@ int main(int argc, char *argv[]) {
free(connfd);
goto _cleanup;
}
args->connfd = connfd;
args->connfd = *connfd;
args->quit = &quit;
args->request_pipe = request_pipe[1];
args->q = queue;
@ -293,13 +294,13 @@ int main(int argc, char *argv[]) {
int r = addToThreadPool(pool, threadF, args);
if (r == 0) {
numberOfConnections++;
free(connfd);
continue; // aggiunto con successo
}
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");
free(args);
close(*connfd);
free(connfd);
continue;
@ -329,6 +330,7 @@ int main(int argc, char *argv[]) {
}
break;
}
free(connfd);
continue;
}
if (i == signal_pipe[0]) { // controllo se devo terminare
@ -382,8 +384,7 @@ int main(int argc, char *argv[]) {
free(connfd);
goto _cleanup;
}
*connfd = i;
args->connfd = connfd;
args->connfd = i;
args->quit = &quit;
args->request_pipe = request_pipe[1];
args->q = queue;
@ -396,13 +397,13 @@ int main(int argc, char *argv[]) {
int r = addToThreadPool(pool, threadF, args);
if (r == 0) {
numberOfConnections++;
free(connfd);
continue; // aggiunto con successo
}
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");
free(args);
close(*connfd);
free(connfd);
continue;
@ -436,6 +437,12 @@ int main(int argc, char *argv[]) {
return 0;
_cleanup:
if(queue) {
destroyQueue(queue);
}
if(taglia) {
taglia_del(taglia);
}
unlink(socketName);
free(socketName);
return -1;

View File

@ -27,7 +27,7 @@ void threadF(void *arg) {
}
threadT *argl = (threadT *) arg;
int connfd = *argl->connfd;
long connfd = argl->connfd;
volatile int *quit = argl->quit;
int request_pipe = argl->request_pipe;
queueT *q = argl->q;
@ -94,7 +94,7 @@ void threadF(void *arg) {
// log chiusura connessione
int n = 0;
char buf[1024];
n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %d.\n", connfd);
n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %ld.\n", connfd);
if( n<0 ) {
perror("snprintf");
goto _cleanup;
@ -117,17 +117,19 @@ void threadF(void *arg) {
// log
int m = 0;
char buf[1024];
m = snprintf(buf, sizeof(buf), "Servito una richiesta del client %d.\n", connfd);
m = snprintf(buf, sizeof(buf), "Servito una richiesta del client %ld.\n", connfd);
if( m<0 ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0 )
goto _cleanup;
free(arg);
return;
_cleanup:
// nothing to do because no memory needs to be freed
free(arg);
return;
}
@ -138,12 +140,13 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
return -1;
}
char *string = calloc(1, len);
char *string = calloc(1, len+1);
if(string == NULL) {
perror("calloc");
return -1;
}
strncpy(string, command, len);
char *tofree = string;
char *token = NULL;
char *token2 = NULL;
@ -237,9 +240,11 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
_parser_cleanup:
free(command);
free(tofree);
return -1;
_parser_end:
free(command);
free(tofree);
return 0;
}

View File

@ -13,7 +13,7 @@
typedef struct struct_thread {
volatile int *quit;
int request_pipe;
long *connfd;
long connfd;
queueT *q; // puntatore alla queue dei file
taglia_t *taglia; // puntatore alla struct del file di log
threadpool_t *pool; // puntatore alla threadpool