-
Notifications
You must be signed in to change notification settings - Fork 1
add signal function #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add signal function #219
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 */ | ||
| /* */ | ||
| /* ************************************************************************** */ | ||
|
|
||
|
|
@@ -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) | ||
| { | ||
|
|
@@ -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)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 */ | ||
| /* */ | ||
| /* ************************************************************************** */ | ||
|
|
||
|
|
@@ -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; | ||
|
|
@@ -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); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 */ | ||
| /* */ | ||
| /* ************************************************************************** */ | ||
|
|
||
|
|
@@ -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); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正ありがとうございます。
writeで対応する形でOKです。