Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions inc/signal/minish_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minish_signal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/05 13:35:17 by ttsubo #+# #+# */
/* Updated: 2025/04/05 14:13:59 by ttsubo ### ########.fr */
/* Updated: 2025/05/05 17:25:02 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,7 +16,8 @@
# include <readline/readline.h>
# include <signal.h>
# include <stdio.h>
# include <unistd.h>

void minish_signal(void);

#endif
#endif
16 changes: 14 additions & 2 deletions src/invoke_cmd/exec_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 13:14:15 by dayano #+# #+# */
/* Updated: 2025/05/04 18:14:21 by dayano ### ########.fr */
/* Updated: 2025/05/05 17:33:50 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,6 +39,17 @@ static void _setup_stdout(t_cmd *cmd, int curr_fds[2])
}
}

static void _setup_process_signals(void (*handler)(int))
{
struct sigaction sa;

sa.sa_handler = handler;
sigemptyset(&(sa.sa_mask));
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
}

static pid_t _fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds,
t_minish *minish)
{
Expand All @@ -51,9 +62,10 @@ static pid_t _fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds,
exit(EX_OSERR);
}
if (pid > 0)
return (pid);
return (_setup_process_signals(SIG_IGN), pid);
_setup_stdin(cmd, cmd_head, pipefds->prev_fds);
_setup_stdout(cmd, pipefds->curr_fds);
_setup_process_signals(SIG_DFL);
if ((cmd->next != NULL) && is_redirect(cmd->next))
redirect(cmd->next);
if (is_builtin(cmd))
Expand Down
16 changes: 7 additions & 9 deletions src/invoke_cmd/invoke_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 21:19:33 by dayano #+# #+# */
/* Updated: 2025/05/04 18:11:39 by dayano ### ########.fr */
/* Updated: 2025/05/05 17:39:02 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,14 +37,11 @@ static bool is_unit_builtin(t_cmd *cmd_head)

int execute_builtin(t_cmd *cmd, t_minish *minish)
{
static const t_builtin builtin_funcs[] = {
builtin_echo, builtin_pwd, builtin_exit, builtin_cd,
builtin_env, builtin_export, builtin_unset, NULL
};
static char *builtin_list[] = {
"echo", "pwd", "exit", "cd",
"env", "export", "unset", NULL
};
static const t_builtin builtin_funcs[] = {builtin_echo, builtin_pwd,
builtin_exit, builtin_cd, builtin_env, builtin_export,
builtin_unset, NULL};
static char *builtin_list[] = {"echo", "pwd", "exit", "cd",
"env", "export", "unset", NULL};
int i;

i = 0;
Expand Down Expand Up @@ -108,6 +105,7 @@ int invoke_commands(t_cmd *cmd_head, t_minish *minish)
{
exec_pipeline(cmd_head, minish);
status = wait_pipeline(cmd_head);
minish_signal();
}
close(STDIN_FILENO);
dup2(original_stdin, STDIN_FILENO);
Expand Down
7 changes: 6 additions & 1 deletion src/invoke_cmd/wait_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/29 16:42:14 by dayano #+# #+# */
/* Updated: 2025/04/29 21:51:02 by dayano ### ########.fr */
/* Updated: 2025/05/05 17:06:51 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,7 +37,12 @@ static int _get_status(t_cmd *cmd)
if (WIFEXITED(status))
cmd->status = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
{
if (WTERMSIG(status) == 3)
write(STDOUT_FILENO, "Quit (core dumped)", 18);
write(STDOUT_FILENO, "\n", 1);
cmd->status = 128 + WTERMSIG(status);
}
else
cmd->status = EXIT_FAILURE;
return (cmd->status);
Expand Down
16 changes: 8 additions & 8 deletions src/minish_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
/* ::: :::::::: */
/* minish_signal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/05 13:35:06 by ttsubo #+# #+# */
/* Updated: 2025/04/27 14:54:18 by ttsubo ### ########.fr */
/* Updated: 2025/05/05 17:07:53 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#include "minish_signal.h"

/**
* @brief Handler function when ctrl+c is pressed
*
* @param signum
* @brief Handler function when ctrl+c is pressed
*
* @param signum
*/
static void ctrl_c(int signum)
{
(void)signum;
printf("\n");
write(STDOUT_FILENO, "\n", 1);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writeのほうがシステムコールを呼び出しているだけなので、安全だそうです。

POSIX 標準で「シグナルハンドラ内から呼んで安全」とされる関数群
https://man7.org/linux/man-pages/man7/signal-safety.7.html?utm_source=chatgpt.com

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正ありがとうございます。
writeで対応する形でOKです。

rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}

/**
* @brief Sets the value of the sigaction structure
*
*
* @param sa : sigaction structure
* @param handler : handler funciton
*/
Expand All @@ -44,7 +44,7 @@ static void _set_sigaction(struct sigaction *sa, void (*handler)(int))

/**
* @brief Sets SIGINT(ctrl+c) and SIGQUIT(ctrl+\).
*
*
* @note ctrl+d is EOF, so it is managed separately.
*/
void minish_signal(void)
Expand Down