gysiang / minishell

0 stars 1 forks source link

Close fd. #235

Closed gysiang closed 4 months ago

gysiang commented 4 months ago

@axellee1994 try this to see if it still leaks. before exit it will show a lot of leaks but upon exit, all the leaks are cleared and only 3 fd left.

int redirect_input(t_shell minishell, t_token curr) { int fd; int type; char *file_name; int dup_fd;

fd = -1;
type = curr->type;
file_name = curr->next->token;
if (check_redirect_file(curr->prev))
    file_name = curr->next->next->token;
if (type == T_LESSER_THAN)
    fd = open_input(file_name);
else if (type == T_LEFT_SHIFT)
    fd = here_doc(minishell, file_name, 1);
if (fd == -1)
{
    minishell->last_return = 1;
    exit(1);
    return (-1);
}
if (fd > 0)
{
    minishell->input_fd = dup(fd);
    safe_close(&fd);
}
return (1);

}

int redirect_output(t_shell minishell, t_token curr) { int fd; int type; char *file_name; int dup_fd;

if (!curr || !curr->next)
    return (-1);
file_name = curr->next->token;
type = curr->type;
fd = open_output(file_name, type);
if (fd == -1)
{
    minishell->last_return = minishell_error_msg(file_name, errno);
    exit(1);
    return (-1);
}
else if (fd > 0)
{
    minishell->output_fd = dup(fd);
    safe_close(&fd);
}
return (1);

}

gysiang commented 4 months ago

These are the messages from valgrind

minishell$ cat << EOF

Hello world EOF ==1527932== ==1527932== FILE DESCRIPTORS: 6 open (3 std) at exit. ==1527932== Open file descriptor 5: ==1527932== at 0x49D907D: pipe (pipe.c:29) ==1527932== by 0x10AA04: here_doc (redirect_heredoc.c:80) ==1527932== by 0x10B09F: redirect_input (redirect.c:94) ==1527932== by 0x10DD7B: handle_single_redirection (pipex.c:22) ==1527932== by 0x10DD7B: handle_redirection (pipex.c:45) ==1527932== by 0x10D15B: handle_redir_child_process (execute_5.c:39) ==1527932== by 0x10D15B: execute_command_with_redir (execute_5.c:79) ==1527932== by 0x10D15B: execute_with_redir (execute_5.c:66) ==1527932== by 0x10E082: pipex (pipex.c:124) ==1527932== by 0x10974F: process_command_line (main.c:56) ==1527932== by 0x10974F: process_command_line (main.c:40) ==1527932== by 0x10981C: main_loop (main.c:81) ==1527932== by 0x1094FB: main (main.c:93) ==1527932== ==1527932== Open file descriptor 4: /dev/pts/0 ==1527932== at 0x49D8FEB: dup (syscall-template.S:120) ==1527932== by 0x1096F3: process_command_line (main.c:51) ==1527932== by 0x1096F3: process_command_line (main.c:40) ==1527932== by 0x10981C: main_loop (main.c:81) ==1527932== by 0x1094FB: main (main.c:93) ==1527932== ==1527932== Open file descriptor 3: /dev/pts/0 ==1527932== at 0x49D8FEB: dup (syscall-template.S:120) ==1527932== by 0x1096E6: process_command_line (main.c:50) ==1527932== by 0x1096E6: process_command_line (main.c:40) ==1527932== by 0x10981C: main_loop (main.c:81) ==1527932== by 0x1094FB: main (main.c:93) ==1527932== ==1527932== ==1527932== HEAP SUMMARY: ==1527932== in use at exit: 209,527 bytes in 286 blocks ==1527932== total heap usage: 534 allocs, 248 frees, 230,331 bytes allocated ==1527932== ==1527932== 11 bytes in 3 blocks are still reachable in loss record 6 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x11086D: remove_embedded_quotes (remove_embedded_quotes.c:57) ==1527932== by 0x110B47: parse_token (parser.c:84) ==1527932== by 0x110B47: token_parser (parser.c:101) ==1527932== by 0x10ED8C: token_processor (tokenizer.c:107) ==1527932== by 0x109701: process_command_line (main.c:52) ==1527932== by 0x109701: process_command_line (main.c:40) ==1527932== by 0x10981C: main_loop (main.c:81) ==1527932== by 0x1094FB: main (main.c:93) ==1527932== ==1527932== 24 bytes in 1 blocks are still reachable in loss record 14 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x10A186: ft_using_history (history_free.c:48) ==1527932== by 0x10B45C: initialize_shell (shell.c:63) ==1527932== by 0x1094F2: main (main.c:92) ==1527932== ==1527932== 27 bytes in 2 blocks are still reachable in loss record 17 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x111D56: ft_strdup (in /home/gyong-si/Documents/42/minishell/minishell) ==1527932== by 0x10A237: add_to_history (history_free.c:84) ==1527932== by 0x1097F2: main_loop (main.c:75) ==1527932== by 0x1094FB: main (main.c:93) ==1527932== ==1527932== 40 bytes in 1 blocks are still reachable in loss record 18 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x10E130: create_token (create_token.c:20) ==1527932== by 0x10E130: token_add_back (create_token.c:40) ==1527932== by 0x10E2A0: add_symbol_lst (tokenizer_utils_1.c:42) ==1527932== by 0x10EE39: process_line (tokenizer.c:84) ==1527932== by 0x10EE39: token_processor (tokenizer.c:106) ==1527932== by 0x109701: process_command_line (main.c:52) ==1527932== by 0x109701: process_command_line (main.c:40) ==1527932== by 0x10981C: main_loop (main.c:81) ==1527932== by 0x1094FB: main (main.c:93) ==1527932== ==1527932== 80 bytes in 2 blocks are still reachable in loss record 23 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x10E130: create_token (create_token.c:20) ==1527932== by 0x10E130: token_add_back (create_token.c:40) ==1527932== by 0x10E3B5: add_command_lst (tokenizer_utils_1.c:83) ==1527932== by 0x10EEEF: process_line (tokenizer.c:93) ==1527932== by 0x10EEEF: token_processor (tokenizer.c:106) ==1527932== by 0x109701: process_command_line (main.c:52) ==1527932== by 0x109701: process_command_line (main.c:40) ==1527932== by 0x10981C: main_loop (main.c:81) ==1527932== by 0x1094FB: main (main.c:93) ==1527932== ==1527932== 496 bytes in 1 blocks are still reachable in loss record 30 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x10B3E7: init_shell (shell.c:19) ==1527932== by 0x10B3E7: initialize_shell (shell.c:56) ==1527932== by 0x1094F2: main (main.c:92) ==1527932== ==1527932== 512 bytes in 1 blocks are still reachable in loss record 31 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x111095: ft_calloc (in /home/gyong-si/Documents/42/minishell/minishell) ==1527932== by 0x109BF0: init_env (env_manager.c:85) ==1527932== by 0x10B454: initialize_shell (shell.c:62) ==1527932== by 0x1094F2: main (main.c:92) ==1527932== ==1527932== 800 bytes in 1 blocks are still reachable in loss record 35 of 73 ==1527932== at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x10A1A4: ft_using_history (history_free.c:55) ==1527932== by 0x10B45C: initialize_shell (shell.c:63) ==1527932== by 0x1094F2: main (main.c:92) ==1527932== ==1527932== 3,367 bytes in 51 blocks are still reachable in loss record 49 of 73 ==1527932== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1527932== by 0x111D56: ft_strdup (in /home/gyong-si/Documents/42/minishell/minishell) ==1527932== by 0x109C66: init_env (env_manager.c:95) ==1527932== by 0x10B454: initialize_shell (shell.c:62) ==1527932== by 0x1094F2: main (main.c:92) ==1527932== ==1527932== LEAK SUMMARY: ==1527932== definitely lost: 0 bytes in 0 blocks ==1527932== indirectly lost: 0 bytes in 0 blocks ==1527932== possibly lost: 0 bytes in 0 blocks ==1527932== still reachable: 5,357 bytes in 63 blocks ==1527932== suppressed: 204,170 bytes in 223 blocks ==1527932== ==1527932== For lists of detected and suppressed errors, rerun with: -s ==1527932== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1) Hello world minishell$ exit ==1527929== ==1527929== FILE DESCRIPTORS: 3 open (3 std) at exit. ==1527929== ==1527929== HEAP SUMMARY: ==1527929== in use at exit: 204,187 bytes in 221 blocks ==1527929== total heap usage: 526 allocs, 305 frees, 230,265 bytes allocated ==1527929== ==1527929== LEAK SUMMARY: ==1527929== definitely lost: 0 bytes in 0 blocks ==1527929== indirectly lost: 0 bytes in 0 blocks ==1527929== possibly lost: 0 bytes in 0 blocks ==1527929== still reachable: 0 bytes in 0 blocks ==1527929== suppressed: 204,187 bytes in 221 blocks ==1527929== ==1527929== For lists of detected and suppressed errors, rerun with: -s ==1527929== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)