The program report
launches two accessed
processes. Report basically feeds accessed
a list of filenames and accessed
prints if they have been accessed in x days.
However, my implementation is causing accessed
to freeze somehow. After running reports
, nothing gets printed. When I run ps
, I can see two accessed
programs hanging around, not dying.
At first, I thought the method of reading from stdin was wrong in accessed
, but I manually piped some filenames to it cat filenames.txt | ./accessed
, and it works. So report
program must be wrong.
Relevant parts of report
:
if (!(pid_A1 = fork())) {
char *num_arg = NULL;
size_t arg_sz = 16;
num_arg = (char *) malloc(arg_sz * sizeof(char));
snprintf(num_arg, arg_sz, "%d", cmd_args->num);
close(pipe_RtoA1[1]);
dup2(pipe_RtoA1[0], 0);
close(pipe_RtoA1[0]);
execl("accessed", "accessed", num_arg, (char *)0);
} else if (!(pid_A2 = fork())) {
char *num_arg = NULL;
size_t arg_sz = 16;
num_arg = (char *) malloc(arg_sz * sizeof(char));
snprintf(num_arg, arg_sz, "%d", -(cmd_args->num));
close(pipe_RtoA2[1]);
dup2(pipe_RtoA2[0], 0);
close(pipe_RtoA2[0]);
execl("accessed", "accessed", num_arg, (char *) 0);
} else {
FILE *fRtoA1 = NULL;
FILE *fRtoA2 = NULL;
fRtoA1 = fdopen(pipe_RtoA1[1], "w");
fRtoA2 = fdopen(pipe_RtoA2[1], "w");
close(pipe_RtoA1[0]);
close(pipe_RtoA2[0]);
fprintf(fRtoA1, "%s", file_paths);
fprintf(fRtoA2, "%s", file_paths);
}
Relevant parts of accessed
:
char **process_input()
{
char **file_paths = NULL;
char *input_str = NULL;
size_t len = 0;
size_t file_paths_sz = 10;
unsigned int fcnt = 0;
long read = 0;
char delim[] = " ";
if ((file_paths = (char **) malloc(file_paths_sz * sizeof(char *))) == NULL) {
exit_program("malloc for file_paths failed.");
}
while((read = getline(&input_str, &len, stdin)) != EOF) { //FREEZES HERE
char *tmp_string = NULL;
strip_token(input_str);
tmp_string = strtok(input_str, delim);
*(file_paths + fcnt) = (char *) malloc(sizeof(char) * (strlen(tmp_string) + 1));
strncpy(*(file_paths + fcnt), tmp_string, strlen(tmp_string) + 1);
strip_token(*(file_paths + fcnt));
fcnt++;
while ((tmp_string = strtok(NULL, delim))) {
*(file_paths + fcnt) = (char *) malloc(sizeof(char) * (strlen(tmp_string) + 1));
strncpy(*(file_paths + fcnt), tmp_string, strlen(tmp_string) + 1);
strip_token(*(file_paths + fcnt));
if ((fcnt + 1) == file_paths_sz) {
file_paths_sz *= 2;
file_paths = realloc(file_paths, file_paths_sz * sizeof(char *));
}
fcnt++;
}
}
return file_paths;
}
I attached gdb to the frozen accessed
processes and it seems that it is frozen at the while loop getline
. So I changed the while loop to a single getline
statement and it suddenly works. However, I need to read stdin
until EOF. Any help on the possible sources of errors is very much appreciated. This is causing me much headache.
Aucun commentaire:
Enregistrer un commentaire