From 5fb054d4033ca8531ac163c6f2f71b83d11b6f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 24 Apr 2025 13:47:07 +0200 Subject: [PATCH] fix(redirection): restore std fds on redirection error debug notes: $ cat << eof > /root/outfile hello eof minishell: open: Permission denied minishell: hello: command not found ==42382== ==42382== FILE DESCRIPTORS: 9 open (3 std) at exit. ==42382== Open file descriptor 8: /dev/pts/3 ==42382== at 0x49A8C1B: dup (in /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6) ==42382== by 0x10C9CC: handle_redirections (src/executing/simple_cmd/handle_redirections.c:65) ==42382== by 0x10CBF6: simple_cmd_execute (src/executing/simple_cmd/simple_cmd_execute.c:91) ==42382== by 0x10B10C: cmd_execute (src/executing/cmd/cmd_execute.c:23) ==42382== by 0x10A4DD: execute_command (src/minishell.c:37) ==42382== by 0x10A3A2: main (src/minishell.c:89) ==42382== ==42382== Open file descriptor 7: /dev/pts/3 ==42382== at 0x49A8C1B: dup (in /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6) ==42382== by 0x10C9B9: handle_redirections (src/executing/simple_cmd/handle_redirections.c:64) ==42382== by 0x10CBF6: simple_cmd_execute (src/executing/simple_cmd/simple_cmd_execute.c:91) ==42382== by 0x10B10C: cmd_execute (src/executing/cmd/cmd_execute.c:23) ==42382== by 0x10A4DD: execute_command (src/minishell.c:37) ==42382== by 0x10A3A2: main (src/minishell.c:89) ==42382== ==42382== Open file descriptor 6: /dev/pts/3 ==42382== at 0x49A8C1B: dup (in /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6) ==42382== by 0x10C9CC: handle_redirections (src/executing/simple_cmd/handle_redirections.c:65) ==42382== by 0x10CBF6: simple_cmd_execute (src/executing/simple_cmd/simple_cmd_execute.c:91) ==42382== by 0x10B10C: cmd_execute (src/executing/cmd/cmd_execute.c:23) ==42382== by 0x10A4DD: execute_command (src/minishell.c:37) ==42382== by 0x10A3A2: main (src/minishell.c:89) ==42382== ==42382== Open file descriptor 5: /dev/pts/3 ==42382== at 0x49A8C1B: dup (in /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6) ==42382== by 0x10C9B9: handle_redirections (src/executing/simple_cmd/handle_redirections.c:64) ==42382== by 0x10CBF6: simple_cmd_execute (src/executing/simple_cmd/simple_cmd_execute.c:91) ==42382== by 0x10B10C: cmd_execute (src/executing/cmd/cmd_execute.c:23) ==42382== by 0x10A4DD: execute_command (src/minishell.c:37) ==42382== by 0x10A3A2: main (src/minishell.c:89) ==42382== ==42382== Open file descriptor 4: /dev/pts/3 ==42382== at 0x49A8C1B: dup (in /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6) ==42382== by 0x10C9A6: handle_redirections (src/executing/simple_cmd/handle_redirections.c:63) ==42382== by 0x10CBF6: simple_cmd_execute (src/executing/simple_cmd/simple_cmd_execute.c:91) ==42382== by 0x10B10C: cmd_execute (src/executing/cmd/cmd_execute.c:23) ==42382== by 0x10A4DD: execute_command (src/minishell.c:37) ==42382== by 0x10A3A2: main (src/minishell.c:89) ==42382== ==42382== Open file descriptor 3: /tmp/minishell_here_doc_78Ak8J9TeVu (deleted) ==42382== at 0x49A8C1B: dup (in /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6) ==42382== by 0x10C9A6: handle_redirections (src/executing/simple_cmd/handle_redirections.c:63) ==42382== by 0x10CBF6: simple_cmd_execute (src/executing/simple_cmd/simple_cmd_execute.c:91) ==42382== by 0x10B10C: cmd_execute (src/executing/cmd/cmd_execute.c:23) ==42382== by 0x10A4DD: execute_command (src/minishell.c:37) ==42382== by 0x10A3A2: main (src/minishell.c:89) --- src/executing/simple_cmd/handle_redirections.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/executing/simple_cmd/handle_redirections.c b/src/executing/simple_cmd/handle_redirections.c index 4bd19d6..e6f1f89 100644 --- a/src/executing/simple_cmd/handle_redirections.c +++ b/src/executing/simple_cmd/handle_redirections.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:14:40 by khais #+# #+# */ -/* Updated: 2025/04/24 13:41:23 by khais ### ########.fr */ +/* Updated: 2025/04/24 13:46:22 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,6 +68,7 @@ t_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app) { if (do_redirection(redirections) == NULL) { + restore_std_fds(app); app->last_return_value = 1; return (NULL); }