mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
fix(exec): correctly exit subprocesses, do not keep multiple shells in parallel
This commit is contained in:
parent
2ea4afda4a
commit
733ae1093a
14 changed files with 81 additions and 46 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/04 19:26:37 by khais #+# #+# */
|
||||
/* Updated: 2025/04/08 16:22:22 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:44:24 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,14 +15,14 @@
|
|||
#include "../connec_cmd/connec_cmd_execute.h"
|
||||
#include "../group_cmd/group_cmd_execute.h"
|
||||
|
||||
void cmd_execute(t_cmd *cmd, t_minishell *app)
|
||||
void cmd_execute(t_cmd *cmd, t_minishell *app, bool should_exit)
|
||||
{
|
||||
if (cmd == NULL)
|
||||
return ;
|
||||
if (cmd->type == FT_SIMPLE)
|
||||
simple_cmd_execute(cmd->value.simple, app);
|
||||
simple_cmd_execute(cmd->value.simple, app, should_exit);
|
||||
if (cmd->type == FT_GROUP)
|
||||
group_cmd_execute(cmd->value.group, app);
|
||||
group_cmd_execute(cmd->value.group, app, should_exit);
|
||||
if (cmd->type == FT_CONNECTION)
|
||||
connec_cmd_execute(cmd->value.connection, app);
|
||||
connec_cmd_execute(cmd->value.connection, app, should_exit);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/04 19:25:39 by khais #+# #+# */
|
||||
/* Updated: 2025/04/04 19:37:46 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:44:37 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,6 +15,6 @@
|
|||
|
||||
# include "../../minishell.h"
|
||||
|
||||
void cmd_execute(t_cmd *cmd, t_minishell *app);
|
||||
void cmd_execute(t_cmd *cmd, t_minishell *app, bool should_exit);
|
||||
|
||||
#endif // CMD_EXECUTE_H
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/07 10:38:55 by khais #+# #+# */
|
||||
/* Updated: 2025/04/11 12:02:02 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:56:20 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,28 +16,31 @@
|
|||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void connec_and_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
||||
static void connec_and_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
|
||||
bool should_exit)
|
||||
{
|
||||
cmd_execute(cmd->first, app);
|
||||
cmd_execute(cmd->first, app, should_exit);
|
||||
if (app->last_return_value == 0)
|
||||
cmd_execute(cmd->second, app);
|
||||
cmd_execute(cmd->second, app, should_exit);
|
||||
}
|
||||
|
||||
static void connec_or_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
||||
static void connec_or_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
|
||||
bool should_exit)
|
||||
{
|
||||
cmd_execute(cmd->first, app);
|
||||
cmd_execute(cmd->first, app, should_exit);
|
||||
if (app->last_return_value != 0)
|
||||
cmd_execute(cmd->second, app);
|
||||
cmd_execute(cmd->second, app, should_exit);
|
||||
}
|
||||
|
||||
void connec_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
||||
void connec_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
|
||||
bool should_exit)
|
||||
{
|
||||
if (cmd == NULL)
|
||||
return ;
|
||||
if (cmd->connector == FT_AND)
|
||||
connec_and_cmd_execute(cmd, app);
|
||||
connec_and_cmd_execute(cmd, app, should_exit);
|
||||
if (cmd->connector == FT_OR)
|
||||
connec_or_cmd_execute(cmd, app);
|
||||
connec_or_cmd_execute(cmd, app, should_exit);
|
||||
if (cmd->connector == FT_PIPE)
|
||||
connec_pipe_cmd_execute(cmd, app);
|
||||
connec_pipe_cmd_execute(cmd, app, should_exit);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/07 10:38:19 by khais #+# #+# */
|
||||
/* Updated: 2025/04/07 10:38:48 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:56:32 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
# include "../../minishell.h"
|
||||
|
||||
void connec_cmd_execute(t_connec_cmd *cmd, t_minishell *app);
|
||||
void connec_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
|
||||
bool should_exit);
|
||||
|
||||
#endif // CONNEC_CMD_EXECUTE_H
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 12:01:29 by khais #+# #+# */
|
||||
/* Updated: 2025/04/11 12:02:26 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:56:55 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -46,7 +46,8 @@ static void close_and_wait(t_minishell *app, int pid1, int pid2, int pipefd[2])
|
|||
do_waitpid(app, pid2);
|
||||
}
|
||||
|
||||
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
||||
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
|
||||
bool should_exit)
|
||||
{
|
||||
int pid1;
|
||||
int pid2;
|
||||
|
|
@ -58,15 +59,19 @@ void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app)
|
|||
if (pid1 == 0)
|
||||
{
|
||||
dup_and_close(pipefd[1], STDOUT_FILENO, pipefd);
|
||||
cmd_execute(cmd->first, app);
|
||||
cmd_execute(cmd->first, app, true);
|
||||
return ;
|
||||
}
|
||||
pid2 = fork();
|
||||
if (pid2 == 0)
|
||||
{
|
||||
dup_and_close(pipefd[0], STDIN_FILENO, pipefd);
|
||||
cmd_execute(cmd->second, app);
|
||||
cmd_execute(cmd->second, app, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
close_and_wait(app, pid1, pid2, pipefd);
|
||||
if (should_exit)
|
||||
exit(app->last_return_value);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 11:56:45 by khais #+# #+# */
|
||||
/* Updated: 2025/04/11 11:57:09 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:56:43 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
# include "../../minishell.h"
|
||||
|
||||
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app);
|
||||
void connec_pipe_cmd_execute(t_connec_cmd *cmd, t_minishell *app,
|
||||
bool should_exit);
|
||||
|
||||
#endif // CONNEC_PIPE_CMD_EXECUTE_H
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/04 19:50:42 by khais #+# #+# */
|
||||
/* Updated: 2025/04/04 20:01:20 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:45:28 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,12 +15,14 @@
|
|||
#include "../common/do_waitpid.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void group_cmd_execute(t_group_cmd *cmd, t_minishell *app)
|
||||
void group_cmd_execute(t_group_cmd *cmd, t_minishell *app, bool should_exit)
|
||||
{
|
||||
int pid;
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
cmd_execute(cmd->cmd, app);
|
||||
cmd_execute(cmd->cmd, app, true);
|
||||
do_waitpid(app, pid);
|
||||
if (should_exit)
|
||||
exit(app->last_return_value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/04 19:42:36 by khais #+# #+# */
|
||||
/* Updated: 2025/04/04 19:43:09 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:45:40 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,6 +15,6 @@
|
|||
|
||||
# include "../../minishell.h"
|
||||
|
||||
void group_cmd_execute(t_group_cmd *cmd, t_minishell *app);
|
||||
void group_cmd_execute(t_group_cmd *cmd, t_minishell *app, bool should_exit);
|
||||
|
||||
#endif // GROUP_CMD_EXECUTE_H
|
||||
|
|
|
|||
|
|
@ -6,12 +6,13 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
|
||||
/* Updated: 2025/04/03 14:35:30 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 17:29:06 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "builtins.h"
|
||||
#include "libft.h"
|
||||
#include "simple_cmd_execute.h"
|
||||
|
||||
t_builtin_type get_builtin(t_simple_cmd *cmd)
|
||||
{
|
||||
|
|
@ -35,7 +36,8 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
|
|||
return (BUILTIN_INVALID);
|
||||
}
|
||||
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
|
||||
bool should_exit)
|
||||
{
|
||||
t_builtin_type type;
|
||||
static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = {
|
||||
|
|
@ -50,5 +52,8 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
|||
};
|
||||
|
||||
type = get_builtin(cmd);
|
||||
return (builtins[type](cmd, app));
|
||||
builtins[type](cmd, app);
|
||||
if (should_exit && type != BUILTIN_INVALID)
|
||||
exit(app->last_return_value);
|
||||
return (type);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
|
||||
/* Updated: 2025/04/03 14:35:44 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:57:16 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -25,6 +25,7 @@ t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app);
|
|||
t_builtin_type builtin_unset(t_simple_cmd *cmd, t_minishell *app);
|
||||
|
||||
t_builtin_type get_builtin(t_simple_cmd *cmd);
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
|
||||
bool should_exit);
|
||||
|
||||
#endif // BUILTINS_H
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
||||
/* Updated: 2025/04/16 16:40:39 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:54:19 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -74,6 +74,7 @@ static t_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app)
|
|||
*/
|
||||
static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
simple_cmd_post_process_debug(cmd, app);
|
||||
if (simple_cmd_expand_vars(cmd, app) == NULL)
|
||||
return (simple_cmd_destroy(cmd), NULL);
|
||||
if (simple_cmd_fieldsplit(cmd) == NULL)
|
||||
|
|
@ -85,20 +86,20 @@ static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
|
|||
return (cmd);
|
||||
}
|
||||
|
||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
|
||||
bool should_exit)
|
||||
{
|
||||
char *exe;
|
||||
int pid;
|
||||
|
||||
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
|
||||
return ;
|
||||
simple_cmd_post_process_debug(cmd, app);
|
||||
if (post_process_command(cmd, app) == NULL)
|
||||
return ;
|
||||
simple_cmd_execute_debug(cmd, app);
|
||||
if (handle_redirections(cmd, app) == NULL)
|
||||
return ;
|
||||
if (execute_builtin(cmd, app) != BUILTIN_INVALID)
|
||||
if (execute_builtin(cmd, app, should_exit) != BUILTIN_INVALID)
|
||||
return ;
|
||||
exe = get_cmdpath(cmd->words->word->word, app);
|
||||
if (exe == NULL)
|
||||
|
|
@ -111,5 +112,6 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
|||
execute_subprocess(exe, cmd, app);
|
||||
free(exe);
|
||||
do_waitpid(app, pid);
|
||||
return ;
|
||||
if (should_exit)
|
||||
exit(app->last_return_value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/03 14:35/14 by khais #+# #+# */
|
||||
/* Updated: 2025/04/16 16:40:11 by khais ### ########.fr */
|
||||
/* Created: 2025/04/03 14:35:14 by khais #+# #+# */
|
||||
/* Updated: 2025/04/16 16:43:41 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,7 +15,8 @@
|
|||
|
||||
# include "../../minishell.h"
|
||||
|
||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app);
|
||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
|
||||
bool should_exit);
|
||||
|
||||
typedef enum e_builtin_type
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
|
||||
/* Updated: 2025/04/15 15:26:24 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/16 16:46:41 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ static void execute_command(t_cmd *cmd, t_minishell *app)
|
|||
if (app->exec == false)
|
||||
return ;
|
||||
set_exec_mode_sig_handling();
|
||||
cmd_execute(cmd, app);
|
||||
cmd_execute(cmd, app, false);
|
||||
set_interactive_mode_sig_handling();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue