diff --git a/close.sh b/close.sh new file mode 100644 index 00000000..d9de8efc --- /dev/null +++ b/close.sh @@ -0,0 +1,6 @@ +for fd in $(/usr/bin/env ls /proc/$$/fd); do + if [ "$fd" -gt 10 ]; then + #echo "$fd" + exec {fd}>&- + fi; +done diff --git a/exec/src/run_ast/_spawn_cmd.c b/exec/src/run_ast/_spawn_cmd.c index 47700b06..42476576 100644 --- a/exec/src/run_ast/_spawn_cmd.c +++ b/exec/src/run_ast/_spawn_cmd.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */ -/* Updated: 2024/09/17 21:41:55 by rparodi ### ########.fr */ +/* Updated: 2024/09/18 14:20:32 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -145,8 +145,8 @@ t_error _redirection_heredoc(\ (void)state; if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC) { - (void)((info->stdout.tag == R_FD) && (close_fd(info->stdout.fd.fd), 1)); - info->stdout.tag = R_INHERITED; + (void)((info->stdin.tag == R_FD) && (close_fd(info->stdin.fd.fd), 1)); + info->stdin.tag = R_INHERITED; if (create_pipe(&heredoc_pipe)) return (ERROR); if (_get_heredoc_input(heredoc_pipe.write, \ diff --git a/exec/src/run_ast/run_pipeline.c b/exec/src/run_ast/run_pipeline.c index 2460250a..cfa9779e 100644 --- a/exec/src/run_ast/run_pipeline.c +++ b/exec/src/run_ast/run_pipeline.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:32:37 by maiboyer #+# #+# */ -/* Updated: 2024/09/16 19:22:06 by maiboyer ### ########.fr */ +/* Updated: 2024/09/17 17:09:30 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,6 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, else { vec_pid_push(&pids, cmd_result.process.pid); - close_fd(cmd_pipe.input); if (cmd_result.process.stdout != NULL) cmd_pipe.input = cmd_result.process.stdout; if (cmd_result.process.stdin != NULL) @@ -56,6 +55,8 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, if (cmd_result.process.stderr != NULL) close_fd(cmd_result.process.stderr); } + if (cmd_pipe.input != NULL) + printf("[%i]%s\n", cmd_pipe.input->fd, cmd_pipe.input->name); } i++; } @@ -73,7 +74,6 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, else { vec_pid_push(&pids, cmd_result.process.pid); - close_fd(cmd_pipe.input); if (cmd_result.process.stdout != NULL) close_fd(cmd_result.process.stdout); if (cmd_result.process.stdin != NULL) @@ -85,9 +85,8 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, } if (pids.len != 0) { - if (!(kill(pids.buffer[pids.len - 1], 0) == -1 && errno == ESRCH)) - while (waitpid(pids.buffer[pids.len - 1], &waitpid_status, 0) < 0) - ; + while (waitpid(pids.buffer[pids.len - 1], &waitpid_status, 0) < 0 && errno != ESRCH) + ; while (waitpid(-1, NULL, 0) != -1) ; if (WIFEXITED(waitpid_status)) diff --git a/stdme/src/fs/file_dup_bak.c b/stdme/src/fs/file_dup_bak.c new file mode 100644 index 00000000..1f1eb992 --- /dev/null +++ b/stdme/src/fs/file_dup_bak.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* file_dup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/01 06:59:44 by maiboyer #+# #+# */ +/* Updated: 2024/09/18 14:24:59 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fcntl.h" +#include "me/fs/fs.h" +#include "me/printf/printf.h" +#include "me/str/str.h" +#include "me/string/string.h" +#include "me/types.h" +#include + +static int _get_flags(t_fd *fd) +{ + int out; + out = 0; + if (fd->perms & FD_READ && fd->perms & FD_WRITE) + out = O_RDWR; + else if (fd->perms & FD_READ) + out = O_RDONLY; + else if (fd->perms & FD_WRITE) + out = O_WRONLY; + + return (out); +} + +t_fd *dup_fd(t_fd *fd) +{ + struct s_file_slot *slot; + int tmp; + t_string path; + + if (fd == NULL) + return (NULL); + slot = get_unused_fd_slot(); + if (slot == NULL) + return (NULL); + path = string_new(32); + me_printf_str(&path, "/proc/self/fd/%i", fd->fd); + tmp = open(path.buf, O_CLOEXEC | _get_flags(fd)); + string_free(path); + if (tmp == -1) + return (printf("There was an error while duping the "\ + "fd %d `%s`\n", fd->fd, fd->name), NULL); + slot->ty = SLOT_FD; + slot->slot.fd.fd = tmp; + slot->slot.fd.perms = fd->perms; + slot->slot.fd.type = fd->type; + slot->slot.fd.name = str_clone(fd->name); + return (&slot->slot.fd); +} diff --git a/stdme/src/os/pipe.c b/stdme/src/os/pipe.c index 05ee586c..b1cf0afe 100644 --- a/stdme/src/os/pipe.c +++ b/stdme/src/os/pipe.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */ -/* Updated: 2024/08/14 17:51:38 by maiboyer ### ########.fr */ +/* Updated: 2024/09/18 14:24:44 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/stdme/src/os/process.c b/stdme/src/os/process.c index 7a362bb4..5766d3e1 100644 --- a/stdme/src/os/process.c +++ b/stdme/src/os/process.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */ -/* Updated: 2024/08/11 12:00:25 by maiboyer ### ########.fr */ +/* Updated: 2024/09/17 17:12:21 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,7 +125,7 @@ t_error spawn_process(t_spawn_info info, t_process *process) (spawn_process_exec(info, process), exit(1)); else { - _process_cleanup(info, process, false); + _process_cleanup(info, process, process->pid == -1); if (process->pid == -1) return (ERROR); } diff --git a/test_heredoc.sh b/test_heredoc.sh index 5137a6ee..a2f8af36 100755 --- a/test_heredoc.sh +++ b/test_heredoc.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -make && valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=no --trace-children=yes --read-var-info=yes --read-inline-info=yes ./minishell <<<'cat <