fix: signal

This commit is contained in:
Camille Chauvet 2023-04-05 15:32:04 +00:00
parent e0fb7f1fd0
commit 5d482fc65a
16 changed files with 68 additions and 97 deletions

View File

@ -6,16 +6,16 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:42:09 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:42:09 by cchauvet #+# #+# */
/* Updated: 2023/03/27 13:42:58 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 14:48:30 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "./data_private.h" #include "./data_private.h"
#include "data.h" #include "data.h"
t_data *ft_get_data(void) int *ft_get_exit_code(void)
{ {
static t_data data; static int exit_code;
return (&data); return (&exit_code);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:43:39 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:43:39 by cchauvet #+# #+# */
/* Updated: 2023/03/27 13:43:40 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 14:45:23 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,10 +18,9 @@ typedef struct s_data
{ {
t_list **env; t_list **env;
t_list **cmds; t_list **cmds;
int exit_code; int *exit_code;
int child_pid;
} t_data; } t_data;
t_data *ft_get_data(void); int *ft_get_exit_code(void);
#endif #endif

4
env/env_fill.c vendored
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */ /* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
/* Updated: 2023/03/29 19:06:44 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 15:09:46 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,7 +40,7 @@ static char *ft_getvalue(t_data *data, char *key)
char *value; char *value;
if (ft_strcmp(key, "?") == 0) if (ft_strcmp(key, "?") == 0)
value = ft_itoa(data->exit_code); value = ft_itoa(*data->exit_code);
else if (ft_strcmp(key, "$") == 0) else if (ft_strcmp(key, "$") == 0)
value = ft_strdup("PID"); value = ft_strdup("PID");
else if (key[0] == '\0') else if (key[0] == '\0')

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */ /* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/04/05 12:30:09 by alouis-j ### ########.fr */ /* Updated: 2023/04/05 15:13:33 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,14 +30,14 @@ static int ft_execute_own_cmd(t_data *data, t_cmd *cmd)
return_code = (echo(cmd->fd_out[0], cmd->args + 1)); return_code = (echo(cmd->fd_out[0], cmd->args + 1));
else else
{ {
return_code = ft_exit(cmd->args + 1, data->exit_code); return_code = ft_exit(cmd->args + 1, *data->exit_code);
if (return_code >= 0) if (return_code >= 0)
{ {
data->exit_code = return_code; *data->exit_code = return_code;
return (-2); return (-2);
} }
} }
data->exit_code = return_code; *data->exit_code = return_code;
return (return_code); return (return_code);
} }
@ -46,11 +46,11 @@ static bool ft_executor(t_data *data, t_cmd *cmd, char **env)
if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL) if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL)
return (0); return (0);
cmd->pid = fork(); cmd->pid = fork();
ft_get_data()->child_pid = cmd->pid;
if (cmd->pid == -1) if (cmd->pid == -1)
return (1); return (1);
if (cmd->pid == 0) if (cmd->pid == 0)
{ {
signal(SIGQUIT, ft_quit);
dup2(cmd->fd_in[0], 0); dup2(cmd->fd_in[0], 0);
dup2(cmd->fd_out[0], 1); dup2(cmd->fd_out[0], 1);
ft_lstiter(*data->cmds, ft_cmdcloser); ft_lstiter(*data->cmds, ft_cmdcloser);

View File

@ -6,12 +6,14 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:45:53 by cchauvet #+# #+# */ /* Created: 2023/03/28 15:45:53 by cchauvet #+# #+# */
/* Updated: 2023/03/29 18:55:22 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 15:15:56 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef EXECUTION_PRIVATE_H #ifndef EXECUTION_PRIVATE_H
# define EXECUTION_PRIVATE_H # define EXECUTION_PRIVATE_H
# include <signal.h>
# include "../signal/signal.h"
# include "../data/data.h" # include "../data/data.h"
# include "../libftx/libftx.h" # include "../libftx/libftx.h"
# include "../cmd/cmd.h" # include "../cmd/cmd.h"

Binary file not shown.

44
main.c
View File

@ -6,14 +6,17 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/03/30 13:12:41 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 15:31:19 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "data/data.h" #include "data/data.h"
#include "env/env.h" #include "env/env.h"
#include "libftx/libft/libft.h"
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include "signal/signal.h"
#include <stdlib.h>
static char *ft_get_user_input(void) static char *ft_get_user_input(void)
{ {
@ -56,17 +59,22 @@ static void ft_cmds_waiter(t_data *data)
waitpid(cmd->pid, &exit_status, 0); waitpid(cmd->pid, &exit_status, 0);
if (WIFSIGNALED(exit_status)) if (WIFSIGNALED(exit_status))
{ {
if (WTERMSIG(exit_status) == SIGKILL) if (exit_status == 131)
data->exit_code = 131; {
else ft_printf("Quit (core dumped)\n");
data->exit_code = 130; *data->exit_code = 131;
} }
else else
data->exit_code = WEXITSTATUS(exit_status); {
ft_putchar_fd('\n', 1);
*data->exit_code = 130;
}
}
else
*data->exit_code = WEXITSTATUS(exit_status);
} }
current = current->next; current = current->next;
} }
data->child_pid = 0;
} }
static int ft_minishell(t_data *data, char *line) static int ft_minishell(t_data *data, char *line)
@ -93,8 +101,7 @@ static int ft_minishell(t_data *data, char *line)
int ft_init_data(t_data *data, char **env) int ft_init_data(t_data *data, char **env)
{ {
data->exit_code = 0; data->exit_code = ft_get_exit_code();
data->child_pid = 0;
data->cmds = malloc(sizeof(t_cmd *)); data->cmds = malloc(sizeof(t_cmd *));
if (data->cmds == NULL) if (data->cmds == NULL)
{ {
@ -114,29 +121,28 @@ int ft_init_data(t_data *data, char **env)
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
{ {
t_data *data; t_data data;
char *line; char *line;
(void) ac; (void) ac;
(void) av; (void) av;
signal(SIGINT, ft_ctrlc); signal(SIGINT, ft_ctrlc);
signal(SIGQUIT, ft_quit); signal(SIGQUIT, SIG_IGN);
data = ft_get_data(); if (ft_init_data(&data, env))
if (ft_init_data(data, env))
return (1); return (1);
line = ft_get_user_input(); line = ft_get_user_input();
while (line != NULL) while (line != NULL)
{ {
if (ft_minishell(data, line) == 1) if (ft_minishell(&data, line) == 1)
break ; break ;
free(line); free(line);
line = ft_get_user_input(); line = ft_get_user_input();
if (line == NULL) if (line == NULL)
break ; break ;
} }
ft_lstclear(data->cmds, ft_cmddel); ft_lstclear(data.cmds, ft_cmddel);
free(data->cmds); free(data.cmds);
ft_lstclear(data->env, env_del); ft_lstclear(data.env, env_del);
free(data->env); free(data.env);
return (data->exit_code); return (*data.exit_code);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */ /* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:03:54 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 14:46:56 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,7 @@
# include "./utils/utils.h" # include "./utils/utils.h"
# include "./data/data.h" # include "./data/data.h"
# include "./signal/signal.h" # include "./signal/signal.h"
# include <stdio.h>
# include <readline/readline.h> # include <readline/readline.h>
# include <readline/history.h> # include <readline/history.h>
# include <signal.h> # include <signal.h>

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */ /* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
/* Updated: 2023/04/05 14:12:02 by alouis-j ### ########.fr */ /* Updated: 2023/04/05 14:53:18 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -98,7 +98,7 @@ static bool ft_check_outfile_append(t_data *data, t_cmd *cmd,
{ {
if (cmd->fd_out[0] == -2) if (cmd->fd_out[0] == -2)
return (0); return (0);
if (ft_file_is_writable(data, redirection)) if (ft_file_is_appendable(data, redirection))
{ {
fd = open(redirection, fd = open(redirection,
O_WRONLY | O_APPEND | O_CREAT, 0644); O_WRONLY | O_APPEND | O_CREAT, 0644);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:36:11 by cchauvet #+# #+# */ /* Created: 2023/02/15 17:36:11 by cchauvet #+# #+# */
/* Updated: 2023/03/30 13:26:31 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 15:12:41 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,14 +20,14 @@ int ft_file_is_readable(t_data *data, const char *path)
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd == -1) if (fd == -1)
{ {
data->exit_code = 1; *data->exit_code = 1;
ft_eprintf("minishell: %s: No such file or directory\n", path); ft_eprintf("minishell: %s: No such file or directory\n", path);
return (0); return (0);
} }
readable = read(fd, "", 0); readable = read(fd, "", 0);
if (readable == -1) if (readable == -1)
{ {
data->exit_code = 1; *data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (0);
} }
@ -43,14 +43,14 @@ int ft_file_is_writable(t_data *data, const char *path)
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644); fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (fd == -1) if (fd == -1)
{ {
data->exit_code = 1; *data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (0);
} }
writeable = write(fd, "", 0); writeable = write(fd, "", 0);
if (writeable == -1) if (writeable == -1)
{ {
data->exit_code = 1; *data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (0);
} }
@ -66,14 +66,14 @@ int ft_file_is_appendable(t_data *data, const char *path)
fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644); fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd == -1) if (fd == -1)
{ {
data->exit_code = 1; *data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (0);
} }
writeable = write(fd, "", 0); writeable = write(fd, "", 0);
if (writeable == -1) if (writeable == -1)
{ {
data->exit_code = 1; *data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */ /* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
/* Updated: 2023/04/05 14:07:10 by alouis-j ### ########.fr */ /* Updated: 2023/04/05 14:58:01 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -82,6 +82,7 @@ int ft_heredoc(t_data *data, char *stop)
break ; break ;
else if (return_code == 1) else if (return_code == 1)
{ {
if (*ft_get_heredoc() > 2)
close(*ft_get_heredoc()); close(*ft_get_heredoc());
*ft_get_heredoc() = -1; *ft_get_heredoc() = -1;
return (-2); return (-2);

View File

@ -6,61 +6,23 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */ /* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */
/* Updated: 2023/04/05 14:09:39 by alouis-j ### ########.fr */ /* Updated: 2023/04/05 15:05:11 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "signal_private.h" #include "signal_private.h"
static void ft_new_line(void) void ft_ctrlc(int num)
{ {
(void) num;
rl_replace_line("", 0); rl_replace_line("", 0);
rl_on_new_line(); rl_on_new_line();
ft_putchar_fd('\n', 1); ft_putchar_fd('\n', 1);
rl_redisplay(); rl_redisplay();
} }
void ft_ctrlc(int num)
{
t_data *data;
if (num == SIGQUIT)
return ;
data = ft_get_data();
data->exit_code = 130;
if (*ft_get_heredoc() != -1)
{
if (*ft_get_heredoc() > 2)
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
}
else
{
if (data->child_pid > 1)
{
data->child_pid = 0;
ft_putchar_fd('\n', 1);
}
else
ft_new_line();
}
}
void ft_quit(int num) void ft_quit(int num)
{ {
t_data *data;
(void) num; (void) num;
data = ft_get_data();
data->exit_code = 131;
if (data->child_pid > 1)
{
ft_printf("Quit (core dumped)\n"); ft_printf("Quit (core dumped)\n");
data->child_pid = 0;
}
else
{
rl_replace_line("", 0);
rl_redisplay();
}
} }

View File

@ -6,13 +6,14 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:42 by cchauvet #+# #+# */ /* Created: 2023/03/28 15:59:42 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:04:46 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 15:05:00 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef SIGNAL_PRIVATE_H #ifndef SIGNAL_PRIVATE_H
# define SIGNAL_PRIVATE_H # define SIGNAL_PRIVATE_H
# include <signal.h> # include <signal.h>
# include <stdio.h>
# include <readline/readline.h> # include <readline/readline.h>
# include <unistd.h> # include <unistd.h>
# include "../libftx/libftx.h" # include "../libftx/libftx.h"

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */ /* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
/* Updated: 2023/03/30 15:58:39 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 15:11:16 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -99,7 +99,7 @@ int ft_syntax_verif(t_data *data, const char *str)
|| ft_pipe_is_alone(str) || ft_pipe_is_alone(str)
|| ft_special_char_dub(str)) || ft_special_char_dub(str))
{ {
data->exit_code = 2; *data->exit_code = 2;
return (1); return (1);
} }
return (0); return (0);

1
tester

@ -1 +0,0 @@
Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:39:48 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:39:48 by cchauvet #+# #+# */
/* Updated: 2023/03/30 13:04:53 by cchauvet ### ########.fr */ /* Updated: 2023/04/05 15:09:29 by alouis-j ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,13 +19,13 @@ char *ft_get_executable_with_path(t_data *data, const char *name)
if (access(name, F_OK) != 0) if (access(name, F_OK) != 0)
{ {
data->exit_code = 127; *data->exit_code = 127;
ft_eprintf("minishell: %s: No such file or directery\n", name); ft_eprintf("minishell: %s: No such file or directery\n", name);
return (NULL); return (NULL);
} }
if (access(name, X_OK) != 0) if (access(name, X_OK) != 0)
{ {
data->exit_code = 126; *data->exit_code = 126;
ft_eprintf("minishell: %s: permission denied\n", name); ft_eprintf("minishell: %s: permission denied\n", name);
return (NULL); return (NULL);
} }
@ -46,7 +46,7 @@ static char **ft_get_paths(t_data *data, const char *name)
paths = get_value_by_key("PATH", data->env); paths = get_value_by_key("PATH", data->env);
if (paths == NULL) if (paths == NULL)
{ {
data->exit_code = 127; *data->exit_code = 127;
ft_eprintf("minishell: %s: command not found\n", name); ft_eprintf("minishell: %s: command not found\n", name);
return (NULL); return (NULL);
} }
@ -97,7 +97,7 @@ static char *ft_get_executable_without_path(t_data *data, const char *name)
ft_freer_tab_ultimate(1, paths); ft_freer_tab_ultimate(1, paths);
if (path == NULL) if (path == NULL)
{ {
data->exit_code = 127; *data->exit_code = 127;
ft_eprintf("minishell: %s: command not found\n", name); ft_eprintf("minishell: %s: command not found\n", name);
} }
return (path); return (path);