Fixed fd leaking when second cmd doesn't exist in pipe
This commit is contained in:
parent
45a55df528
commit
ea1908e644
6 changed files with 87 additions and 31 deletions
2
Makefile
2
Makefile
|
|
@ -6,7 +6,7 @@
|
||||||
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||||
# Updated: 2024/08/05 14:52:59 by maiboyer ### ########.fr #
|
# Updated: 2024/08/11 11:56:54 by maiboyer ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
SRC_FILES = \
|
SRC_FILES = \
|
||||||
|
builtins/_debug \
|
||||||
builtins/cd \
|
builtins/cd \
|
||||||
builtins/echo \
|
builtins/echo \
|
||||||
builtins/env \
|
builtins/env \
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/10 18:43:56 by maiboyer #+# #+# */
|
/* Created: 2024/08/10 18:43:56 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/08/11 11:20:22 by maiboyer ### ########.fr */
|
/* Updated: 2024/08/11 12:01:18 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -39,5 +39,6 @@ t_error builtin_exit__(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co
|
||||||
t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code);
|
t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code);
|
||||||
t_error builtin_pwd___(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code);
|
t_error builtin_pwd___(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code);
|
||||||
t_error builtin_unset_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code);
|
t_error builtin_unset_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code);
|
||||||
|
t_error builtin_debug_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code);
|
||||||
|
|
||||||
#endif /* BUILTINS_H */
|
#endif /* BUILTINS_H */
|
||||||
|
|
|
||||||
52
exec/src/builtins/_debug.c
Normal file
52
exec/src/builtins/_debug.c
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* _debug.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/08/11 12:11:43 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "exec/builtins.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"
|
||||||
|
|
||||||
|
#define DEBUG_USAGE \
|
||||||
|
"Usage:\n" \
|
||||||
|
" - print_fd: print the opened file descritors informations"
|
||||||
|
|
||||||
|
static t_error _debug_fd(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||||
|
{
|
||||||
|
const t_fd_array *fds = get_fd_arrays();
|
||||||
|
t_usize i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < FILE_SLOT_LEN)
|
||||||
|
{
|
||||||
|
if (fds->storage[i].ty == SLOT_FD)
|
||||||
|
me_printf_fd(info.stderr, " FD[%i] => %s\n", fds->storage[i].slot.fd.fd, fds->storage[i].slot.fd.name);
|
||||||
|
if (fds->storage[i].ty == SLOT_FILE)
|
||||||
|
me_printf_fd(info.stderr, "FILE[%p] => %s\n", fds->storage[i].slot.file.ptr, fds->storage[i].slot.file.name);
|
||||||
|
if (fds->storage[i].ty == SLOT_DIR)
|
||||||
|
me_printf_fd(info.stderr, " DIR[%p] => %s\n", fds->storage[i].slot.dir.ptr, fds->storage[i].slot.dir.name);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
*exit_code = 0;
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error builtin_debug_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||||
|
{
|
||||||
|
if (info.args.len != 2)
|
||||||
|
return (me_printf_fd(info.stdout, DEBUG_USAGE), *exit_code = 1, ERROR);
|
||||||
|
if (str_compare(info.args.buffer[1], "print_fd"))
|
||||||
|
return (_debug_fd(state, info, exit_code));
|
||||||
|
*exit_code = 2;
|
||||||
|
return (ERROR);
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
|
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/08/11 11:41:48 by maiboyer ### ########.fr */
|
/* Updated: 2024/08/11 12:19:11 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -597,7 +597,7 @@ t_error run_program(t_ast_program *self, t_state *state, t_program_result *out)
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FUNCTIONS
|
// FUNCTIONS
|
||||||
|
|
@ -636,18 +636,30 @@ void _ffree_func(struct s_ffree_state *state)
|
||||||
hmap_env_free(state->state->env);
|
hmap_env_free(state->state->env);
|
||||||
hmap_env_free(state->state->tmp_var);
|
hmap_env_free(state->state->tmp_var);
|
||||||
close_fd(state->cmd_pipe.input);
|
close_fd(state->cmd_pipe.input);
|
||||||
me_exit(127);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _is_builtin(t_const_str argv0);
|
bool _is_builtin(t_const_str argv0)
|
||||||
|
{
|
||||||
|
t_usize i;
|
||||||
|
const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", "_debug", NULL};
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (argv0 == NULL)
|
||||||
|
return (false);
|
||||||
|
while (value[i] != NULL)
|
||||||
|
if (str_compare(argv0, value[i++]))
|
||||||
|
return (true);
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
t_error _handle_builtin(t_spawn_info info, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out)
|
t_error _handle_builtin(t_spawn_info info, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out)
|
||||||
{
|
{
|
||||||
t_usize i;
|
t_usize i;
|
||||||
const t_const_str argv0 = info.binary_path;
|
const t_const_str argv0 = info.binary_path;
|
||||||
const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", NULL};
|
const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", "_debug", NULL};
|
||||||
const t_builtin_func funcs[] = {builtin_cd____, builtin_echo__, builtin_env___, builtin_exit__,
|
const t_builtin_func funcs[] = {builtin_cd____, builtin_echo__, builtin_env___,
|
||||||
builtin_export, builtin_pwd___, builtin_unset_, NULL};
|
builtin_exit__, builtin_export, builtin_pwd___,
|
||||||
|
builtin_unset_, builtin_debug_, NULL};
|
||||||
t_builtin_func actual_func;
|
t_builtin_func actual_func;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
@ -715,13 +727,18 @@ t_error _handle_builtin(t_spawn_info info, t_state *state, t_cmd_pipe cmd_pipe,
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (actual_func(state, binfo, &exit_code))
|
if (actual_func(state, binfo, &exit_code))
|
||||||
return (out->exit = 126, ERROR);
|
out->exit = 126;
|
||||||
out->exit = exit_code;
|
else
|
||||||
|
out->exit = exit_code;
|
||||||
}
|
}
|
||||||
|
if (binfo.stdin)
|
||||||
|
close_fd(binfo.stdin);
|
||||||
|
if (binfo.stdout)
|
||||||
|
close_fd(binfo.stdout);
|
||||||
|
if (binfo.stderr)
|
||||||
|
close_fd(binfo.stderr);
|
||||||
vec_str_free(info.arguments);
|
vec_str_free(info.arguments);
|
||||||
str_free(info.binary_path);
|
str_free(info.binary_path);
|
||||||
// we need to check if we have to fork !
|
|
||||||
|
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -837,7 +854,7 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state
|
||||||
signal(SIGINT, SIG_IGN);
|
signal(SIGINT, SIG_IGN);
|
||||||
signal(SIGQUIT, SIG_IGN);
|
signal(SIGQUIT, SIG_IGN);
|
||||||
if (spawn_process(info, &out->process))
|
if (spawn_process(info, &out->process))
|
||||||
return (ERROR);
|
return (close_fd(cmd_pipe.input), out->exit = 127, ERROR);
|
||||||
int status;
|
int status;
|
||||||
if (waitpid(out->process.pid, &status, 0) == -1)
|
if (waitpid(out->process.pid, &status, 0) == -1)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
|
|
@ -848,21 +865,6 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _is_builtin(t_const_str argv0)
|
|
||||||
{
|
|
||||||
t_usize i;
|
|
||||||
const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", NULL};
|
|
||||||
const t_str funcs[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", NULL};
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (argv0 == NULL)
|
|
||||||
return (false);
|
|
||||||
while (value[i] != NULL)
|
|
||||||
if (str_compare(argv0, value[i++]))
|
|
||||||
return (true);
|
|
||||||
return (false);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out)
|
t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out)
|
||||||
{
|
{
|
||||||
t_vec_str args;
|
t_vec_str args;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
|
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/08/10 18:41:56 by maiboyer ### ########.fr */
|
/* Updated: 2024/08/11 12:00:25 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -44,7 +44,7 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
||||||
vec_str_push(&info.arguments, NULL);
|
vec_str_push(&info.arguments, NULL);
|
||||||
vec_str_push(&info.environement, NULL);
|
vec_str_push(&info.environement, NULL);
|
||||||
execve(info.binary_path, info.arguments.buffer, info.environement.buffer);
|
execve(info.binary_path, info.arguments.buffer, info.environement.buffer);
|
||||||
return (NO_ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
|
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue