Skip to content

Commit 8aa7db4

Browse files
ValerioNeriGitandreabac3
authored andcommitted
log function and searcing BAD File descriptor in memory mapping
1 parent 9854f7f commit 8aa7db4

File tree

15 files changed

+250
-253
lines changed

15 files changed

+250
-253
lines changed

general/include/definitions.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define BUFFER_SIZE 2048
2222
// DOS_PROTECTION is a switcher used in socket.c, when it is true we close the connection immediately without read the whole message, else we read the entire message and we response with error.
2323
int DOS_PROTECTION;
24+
#define VERBOSE_LEVEL 2
2425
#define SEND_BUFFER_SIZE 512
2526
char cwd[BUFFER_SIZE];
2627
#if defined(__unix__) || defined(__APPLE__)

general/include/utils.h

+5
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ int ut_get_line(char *buf, size_t size);
3333

3434
void ut_clone_configs(struct Configs *c, struct Configs *n);
3535

36+
int log_ut(/*int priority, */const char *format, ...);
37+
38+
int vlog_ut(int verbose, const char *format, ...);
39+
40+
3641
#endif //GOPHER_UTILS_H

general/lib/gopher/protocol.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ int protocol_response(char type, char *filename, char *path, const char *host, i
8080
if (sprintf(*result, "%c%s\t%s\t%s\t%d\n", type, filename, path, host, port) < 0) {
8181
return NEED_TO_FREE;
8282
}
83-
printf("protocol_response/result %s\n", *result);
83+
log_ut("protocol_response/result %s\n", *result);
8484
}
8585
return 0;
8686
}
@@ -110,7 +110,7 @@ int print_directory(char *path, int (*socket_send_f)(int, char *), int fd, int p
110110
if (entry->d_name[0] == '.') continue; // don't print hidden files/dirs
111111
char *filename = entry->d_name;
112112

113-
//printf("%s\n", "Fin qui si");
113+
//log_ut("%s\n", "Fin qui si");
114114

115115

116116
char *fullpath = calloc(pathlen + strlen(filename) + 5, sizeof(char));
@@ -132,7 +132,7 @@ int print_directory(char *path, int (*socket_send_f)(int, char *), int fd, int p
132132

133133
// get line to send for gopher
134134
char *response_line;
135-
printf("SONO HOSTNAME");
135+
log_ut("SONO HOSTNAME");
136136
err = protocol_response(code, filename, fullpath, ip_buffer, port, &response_line);
137137

138138
if (err != 0) {

general/lib/io/config_file.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ int conf_parseConfigFile(char *path, struct Configs *config) {
134134

135135
char buff[FILENAME_MAX];
136136
GetCurrentDir(buff, FILENAME_MAX);
137-
//printf("%s", buff);
137+
//log_ut("%s", buff);
138138

139139

140140
Assert((fp = fopen(path, "r")) != NULL, "error: configuration file open failed");
@@ -177,12 +177,12 @@ int conf_parseConfigFile(char *path, struct Configs *config) {
177177

178178
// for all lines
179179
// for (int j = 0; j < idx; j++) {
180-
// printf("%s", StringsArray[j]);
180+
// log_ut("%s", StringsArray[j]);
181181
// }
182182
for (it = 0; it < idx; it++) {
183183
// name of the option
184184
single_word = ut_strtok(StringsArray[it], " ", &saveptr1);
185-
// printf("\n singleword-> %s \n", single_word);
185+
// log_ut("\n singleword-> %s \n", single_word);
186186
// option value evaluation
187187
if (single_word != NULL) {
188188

@@ -210,22 +210,22 @@ int conf_parseConfigFile(char *path, struct Configs *config) {
210210
//config->root_dir = calloc(sizeof(char), strlen(single_word) + 1);
211211
strncpy(config->root_dir, single_word, BUFFER_SIZE);
212212

213-
// printf("%s %s \n", single_word, config->root_dir);
213+
// log_ut("%s %s \n", single_word, config->root_dir);
214214
} else if (strcmp(external_ip, single_word) == 0) {
215215
count_ip++;
216216
// if the option is root_dir
217217
single_word = ut_strtok(NULL, "\"", &saveptr1);
218218
wrong |= (unsigned) Assert_nb(single_word != NULL, "Missing ip value");
219219
//config->root_dir = calloc(sizeof(char), strlen(single_word) + 1);
220-
printf("%s", single_word);
220+
log_ut("%s", single_word);
221221
//wrong |= (unsigned) Assert_nb(REG_NOMATCH != check_ip(single_word), "Wrong ip value");
222222
if (strlen(single_word) > BUFFER_SIZE - 1) {
223223
wrong |= 1;
224224
fprintf(stderr, "error in external_ip");
225225
} else {
226226
strncpy(ip_buffer, single_word, BUFFER_SIZE - 1);
227227
}
228-
// printf("%s %s \n", single_word, config->root_dir);
228+
// log_ut("%s %s \n", single_word, config->root_dir);
229229
} else {
230230

231231
// if the option is unknown (Error)
@@ -242,14 +242,14 @@ int conf_parseConfigFile(char *path, struct Configs *config) {
242242
free(StringsArray[it]);
243243

244244
free(StringsArray);
245-
// printf("\n%d\n", wrong);
245+
// log_ut("\n%d\n", wrong);
246246
if (Assert_nb(count_ip == 1 && count_mode == 1 && count_port == 1 && count_root_dir == 1 && wrong == 0, "Something gone wrong in configuration file") == ASS_CRASH ) {
247247
//free(config->root_dir);
248248
exit(1);
249249
}
250250
configs->hostname = ip_buffer;
251251
config->used_OPTARG = 0;
252-
// printf("\n prima di fare return --> %s\n ", config->root_dir);
252+
// log_ut("\n prima di fare return --> %s\n ", config->root_dir);
253253
return 0;
254254

255255
}

general/lib/socket/socket.c

+32-32
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int SendFile(int write_fd, FILE *read_fd) {
5454
if ((n = fread(buffer, sizeof(char), SEND_BUFFER_SIZE, read_fd)) == -1) {
5555
return -1;
5656
}
57-
// printf("send_buffer: |%s| %zu %zu\n", buffer, n, strlen(buffer));
57+
// log_ut("send_buffer: |%s| %zu %zu\n", buffer, n, strlen(buffer));
5858
if (send(write_fd, buffer, (size_t) n, 0) == -1) {
5959
return -2;
6060
}
@@ -67,7 +67,7 @@ int SendFile(int write_fd, FILE *read_fd) {
6767
FILE *sendFileToClient(int fd) {
6868
//FILE *fp = fopen(pathFilename, "rb");
6969
FILE *fp = fdopen(fd, "rb");
70-
printf("sono qui");
70+
log_ut("sono qui");
7171
if (fp == NULL) {
7272
fprintf(stderr, "Error opening file --> %s", strerror(errno));
7373
exit(EXIT_FAILURE);
@@ -167,7 +167,7 @@ void clean_request(char *path, char *buf, struct ThreadArgs *args) {
167167
* Wrapper for protocol.resolve_selector, control errors and clean the request
168168
* */
169169
void socket_resolve_selector(struct ThreadArgs *args, char *buf, char **path) {
170-
//printf("Selector string: %s\n", buf);
170+
//log_ut("Selector string: %s\n", buf);
171171
//char *path;
172172
int ret_resolve_relector = resolve_selector(args->configs.root_dir, path, buf);
173173
if (ret_resolve_relector == NO_FREE) {
@@ -212,7 +212,7 @@ int socket_read_request(struct ThreadArgs *args, char **buf) {
212212
}
213213
ptr += got_bytes;
214214
if (ptr >= BUFFER_SIZE) {
215-
printf("%s %d\n", "effettuo il drain", ptr);
215+
log_ut("%s %d\n", "effettuo il drain", ptr);
216216

217217
// DOS_PROTECTION is a switcher used in socket.c, when it is true we close the connection immediately without read the whole message, else we read the entire message and we response with error.
218218
if (DOS_PROTECTION) clean_request(NULL, *buf, args);
@@ -247,7 +247,7 @@ void socket_read_request2(struct ThreadArgs *args, char **buf) {
247247
(*buf)[ptr] = 0; // Terminate string
248248
}
249249

250-
// printf("socket/socket_read_request %d %d |%s|\n", (*buf)[got_bytes - 1], (*buf)[got_bytes - 2], *buf);
250+
// log_ut("socket/socket_read_request %d %d |%s|\n", (*buf)[got_bytes - 1], (*buf)[got_bytes - 2], *buf);
251251

252252
for (int i = 2; 0 <= i; i--) {
253253
if (0 != got_bytes && ((*buf)[got_bytes - i] == '\r' || (*buf)[got_bytes - i] == '\n')) {
@@ -258,7 +258,7 @@ void socket_read_request2(struct ThreadArgs *args, char **buf) {
258258

259259
socket_drain_tcp(args->fd);
260260

261-
printf("socket/socket_read_request %ld %ld |%s|\n", got_bytes, strlen(*buf), *buf);
261+
log_ut("socket/socket_read_request %ld %ld |%s|\n", got_bytes, strlen(*buf), *buf);
262262
}
263263

264264

@@ -272,7 +272,7 @@ int socket_drain_tcp(int fd_client) {
272272
if (errno == EINTR) {
273273
continue;
274274
}
275-
printf("%d \t %s\n", drain_recv, "generic error");
275+
log_ut("%d \t %s\n", drain_recv, "generic error");
276276
return -1;
277277
} else if (drain_recv == 0) {
278278
break;
@@ -294,7 +294,7 @@ int socket_drain_tcp2(int fd_client) {
294294

295295
if (drain_recv == EAGAIN) {
296296
} else {
297-
printf("%d \t %s\n", drain_recv, "gemneric error");
297+
log_ut("%d \t %s\n", drain_recv, "gemneric error");
298298
}
299299
return -1;
300300
}
@@ -303,7 +303,7 @@ int socket_drain_tcp2(int fd_client) {
303303

304304
int socket_send_message(int fd, char *message_string) {
305305

306-
// printf("socket_send_message: %d, %s", fd, message_string);
306+
// log_ut("socket_send_message: %d, %s", fd, message_string);
307307
int ret = send(fd, message_string, sizeof(char) * strlen(message_string), 0);
308308
if (0 > ret) {
309309
perror("socket.c/socket_send_message: send failed");
@@ -319,7 +319,7 @@ int socket_send_error_to_client(char *path, char *buf, struct ThreadArgs *args)
319319
clean_request(path, buf, args);
320320
} else {
321321

322-
printf("%s", m);
322+
log_ut("%s", m);
323323
//send(args->fd, m, sizeof(char) * strlen(m), 0);
324324
socket_send_message(args->fd, m);
325325
if (m != NULL) {
@@ -336,7 +336,7 @@ void socket_manage_files(char *path, char *buf, struct ThreadArgs *args) {
336336
if (FILES_NOT_EXIST == type_file || type_file == FILES_NOT_PERMITTED) {
337337
args->type_Request = 1;
338338

339-
printf("socket_manage_files: Il file non esiste");
339+
log_ut("socket_manage_files: Il file non esiste");
340340

341341
socket_send_error_to_client(path, buf, args);
342342
}
@@ -350,12 +350,12 @@ void socket_manage_files(char *path, char *buf, struct ThreadArgs *args) {
350350
args->type_Request = 1;
351351

352352
// it's a directory
353-
printf("%s\n", "Sending Directory");
353+
log_ut("%s\n", "Sending Directory");
354354
// TODO che for exit code of print_directory
355355
print_directory(path, &socket_send_message, args->fd, args->configs.port_number);
356356
} else if (FILES_IS_REG_FILE == type_file) { // FILES_IS_FILE
357357
// it's some kind of files
358-
printf("%s\n", "Sending File");
358+
log_ut("%s\n", "Sending File");
359359

360360
args->type_Request = 0;
361361

@@ -394,7 +394,7 @@ void socket_manage_files(char *path, char *buf, struct ThreadArgs *args) {
394394

395395

396396

397-
printf("End createProcess");
397+
log_ut("End createProcess");
398398
#endif
399399
#if defined(__unix__) || defined(__APPLE__)
400400

@@ -405,7 +405,7 @@ void socket_manage_files(char *path, char *buf, struct ThreadArgs *args) {
405405
memory_mapping_args.mode_concurrency = args->configs.mode_concurrency;
406406
memory_mapping_args.fd = args->fd;
407407

408-
printf("going to linux_memory_mapping\n");
408+
log_ut("going to linux_memory_mapping\n");
409409
int map_size = linux_memory_mapping((void *) &memory_mapping_args);
410410

411411
if (map_size < 0) {
@@ -430,11 +430,11 @@ void socket_manage_files(char *path, char *buf, struct ThreadArgs *args) {
430430
int write_to_log(struct PipeArgs *data) {
431431
FILE *fp_log = fopen("../gopher_log_file.txt", "a");
432432
if (fp_log == NULL) {
433-
printf("%s", "PERCHÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉ");
433+
log_ut("%s", "PERCHÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉ");
434434
return -1;
435435
}
436436
fprintf(fp_log, "%s", "ciaoo");
437-
printf("sono dentro la funzionee");
437+
log_ut("sono dentro la funzionee");
438438
fclose(fp_log);
439439

440440
return 0;
@@ -471,44 +471,44 @@ int vecchiafork(char *path, char *ip_client, int dim_file_to_send) {
471471
close(fd_pipe[1]);
472472
} else if (child == 0) {
473473
close(fd_pipe[1]);
474-
printf("---- child process wrote\n");
474+
write_log("---- child process wrote\n");
475475
//FILE* fp_fileLog = fopen(LOG_PATH, "w");
476476
int fd_log = open(LOG_PATH, O_WRONLY | O_APPEND);
477477
//FILE* fp_filelog= fdopen(fd_log, "a");
478-
printf("---- child process open\n");
478+
write_log("---- child process open\n");
479479
if (fd_log == -1) {
480480
//if (fp_fileLog == NULL){
481-
printf("sono bloccato");
481+
write_log("sono bloccato");
482482
exit(-1);
483483
}
484484
//int n;
485485
struct PipeArgs data;
486486
487487
//ssize_t nread = read(fd_pipe[0], &data, sizeof(data));
488488
ssize_t nread = read(fd_pipe[0], &data, sizeof(data));
489-
printf("%zu", nread);
489+
write_log("%zu", nread);
490490
491491
492-
printf("%zu", nread);
492+
write_log("%zu", nread);
493493
494-
printf("---- child process read\n");
494+
write_log("---- child process read\n");
495495
496496
497-
//printf("\n sono figlio :-> %s\n", data->ip_client);
498-
printf("FileName: %s\n", data.path);
499-
printf("%d Byte \n", data.dim_file);
500-
printf("IP Client: %s\n", data.ip_client);
497+
//write_log("\n sono figlio :-> %s\n", data->ip_client);
498+
write_log("FileName: %s\n", data.path);
499+
write_log("%d Byte \n", data.dim_file);
500+
log_ut("IP Client: %s\n", data.ip_client);
501501
502-
dprintf(fd_log, "FileName: %s\t%d Byte \t IP Client: %s\n", data.path, data.dim_file,
502+
dwrite_log(fd_log, "FileName: %s\t%d Byte \t IP Client: %s\n", data.path, data.dim_file,
503503
data.ip_client);
504-
//int err = fprintf(fp_filelog, "FileName: %s\t%d Byte \t IP Client: %s\n", data->path, data->dim_file, data->ip_client);
505-
perror("dprintf");
504+
//int err = fwrite_log(fp_filelog, "FileName: %s\t%d Byte \t IP Client: %s\n", data->path, data->dim_file, data->ip_client);
505+
perror("dwrite_log");
506506
//write(fd_log, "cia", sizeof("cia"));
507507
508-
//printf("SONO N %d \n", n);
508+
//write_log("SONO N %d \n", n);
509509
close(fd_pipe[0]);
510510
511-
printf("---- child process close\n");
511+
write_log("---- child process close\n");
512512
exit(0);
513513
}
514514
return 0;

general/lib/utils/utils.c

+28-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <zconf.h>
1010

1111
//#include "gopher_server_configuration_linux.h"
12+
#include <stdarg.h>
13+
#include "definitions.h"
1214

1315
#endif
1416
#ifdef __cplusplus
@@ -25,7 +27,7 @@ using namespace std;
2527
#endif
2628
void help() {
2729
char *helpString = "";
28-
printf("%s\n", helpString);
30+
log_ut("%s\n", helpString);
2931
}
3032

3133
/*
@@ -181,17 +183,39 @@ void ut_clone_configs(struct Configs *c, struct Configs *n) {
181183
configs->root_dir = "alterata";
182184
183185
printf("struct n: port:%d mode:%d dir:%s\n", n->port_number, n->mode_concurrency, n->root_dir);
184-
printf("struct c: port:%d mode:%d dir:%s\n", configs->port_number, configs->mode_concurrency, configs->root_dir);
186+
log_ut("struct c: port:%d mode:%d dir:%s\n", configs->port_number, configs->mode_concurrency, configs->root_dir);
185187
*/
186188

187189
}
188190

189191

190192
void ut_get_cwd(){
191193
if (getcwd(cwd, sizeof(cwd)) != NULL) {
192-
printf("Current working dir: %s\n", cwd);
194+
log_ut("Current working dir: %s\n", cwd);
193195
} else {
194196
perror("getcwd() error");
195197
exit(-2);
196198
}
197-
}
199+
}
200+
201+
int vlog_ut(int verbose, const char *format, ...)
202+
{
203+
va_list args;
204+
va_start(args, format);
205+
206+
if(verbose <= VERBOSE_LEVEL)
207+
vprintf(format, args);
208+
209+
va_end(args);
210+
}
211+
212+
int log_ut(/*int priority, */const char *format, ...)
213+
{
214+
va_list args;
215+
va_start(args, format);
216+
217+
if(3 <= VERBOSE_LEVEL)
218+
vprintf(format, args);
219+
220+
va_end(args);
221+
}

0 commit comments

Comments
 (0)