Skip to content

Commit 603b116

Browse files
authored
Merge pull request #338 from LeaYeh/fix-sigabrt-misuse
[FIX] Fix `SIGABRT` misuse by switching to `SIGUSR1`
2 parents 40cf36c + 362b8f4 commit 603b116

File tree

6 files changed

+19
-20
lines changed

6 files changed

+19
-20
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ https://starchart.cc/LeaYeh/minishell
9595
| | | | Handled `ctrl-D` as `EOF` as bash behavior. ||
9696
| | | | Handled `ctrl-\\` as `SIGQUIT` as bash behavior. ||
9797
| | | Exception Handling | Ignored `SIGPIPE` in internal process and set it back as default in external commands sub-process. ||
98-
| | | | Used `SIGABRT` and `SIGTERM` to raise internal critical error to all related process and handle it depends on scenario. ||
98+
| | | | Used `SIGUSR1` and `SIGTERM` to raise internal critical error to all related process and handle it depends on scenario. ||
9999

100100

101101
# DevOPS Spirit
@@ -564,15 +564,15 @@ Using signals for exception handling in shell programming, similar to Bash's des
564564
- By utilizing signals, we can promptly broadcast error messages and take appropriate actions, such as terminating subprocesses or halting execution, depending on the severity of the error.
565565
2. Ensuring Subprocess Termination:
566566
- In the event of a critical error, it's crucial to ensure that all subprocesses spawned by the shell are terminated to prevent any lingering processes that could potentially cause issues or consume resources.
567-
- Signals like `SIGABRT` and `SIGTERM` are used to terminate subprocesses gracefully, ensuring that they clean up resources and exit properly.
567+
- Signals like `SIGUSR1` and `SIGTERM` are used to terminate subprocesses gracefully, ensuring that they clean up resources and exit properly.
568568
3. Providing Feedback to Users:
569569
- Exception handling using signals allows us to provide feedback to users about the occurrence of critical errors. We can print error messages, indicating the nature of the error and any relevant details, enhancing user understanding and troubleshooting.
570570
4. Facilitating Controlled Shutdown:
571571
- In situations where critical errors occur, it's essential to have mechanisms in place to facilitate a controlled shutdown of the shell environment. Signals help initiate a controlled exit, allowing the shell to clean up resources, release memory, and exit gracefully.
572572

573573
Let's take a closer look at how signals are utilized in our source code:
574574

575-
- `raise_error_and_escape`: This function raises an error message and initiates an abort signal (`SIGABRT`) to terminate all subprocesses. It's used for critical errors where immediate termination is necessary.
575+
- `raise_error_and_escape`: This function raises an error message and initiates an abort signal (`SIGUSR1`) to terminate all subprocesses. It's used for critical errors where immediate termination is necessary.
576576
- `raise_error_to_all_subprocess`: Similar to the previous function, this one raises an error message and sends a termination signal (`SIGTERM`) to all subprocesses. It's used when the error is critical but allows for a graceful shutdown.
577577
- `raise_error_to_own_subprocess` and `signal_to_all_subprocess`: These functions are used for handling errors within subprocesses. They ensure that subprocesses receive termination signals (`SIGTERM`) and facilitate controlled shutdowns.
578578

include/signals.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* signals.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/18 18:35:55 by lyeh #+# #+# */
9-
/* Updated: 2024/03/18 18:36:10 by lyeh ### ########.fr */
9+
/* Updated: 2024/05/21 23:10:49 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -17,7 +17,6 @@
1717

1818
void handle_signal_std(int signo, siginfo_t *info, void *context);
1919
void handle_signal_record(int signo, siginfo_t *info, void *context);
20-
void handle_signal_heredoc(int signo, siginfo_t *info, void *context);
2120
void setup_signal(t_sh *shell, int signo, t_sig state);
2221
void raise_error_and_escape(t_sh *shell, char *msg);
2322
void raise_error_to_all_subprocess(t_sh *shell, int exit_code, char *msg);

source/backend/executor/external_cmd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* external_cmd.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/19 15:17:06 by lyeh #+# #+# */
9-
/* Updated: 2024/05/03 17:18:03 by lyeh ### ########.fr */
9+
/* Updated: 2024/06/01 11:03:14 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -63,7 +63,7 @@ static void reset_external_signal_handler(t_sh *shell)
6363
setup_signal(shell, SIGINT, SIG_DEFAULT);
6464
setup_signal(shell, SIGQUIT, SIG_DEFAULT);
6565
setup_signal(shell, SIGTERM, SIG_DEFAULT);
66-
setup_signal(shell, SIGABRT, SIG_DEFAULT);
66+
setup_signal(shell, SIGUSR1, SIG_DEFAULT);
6767
}
6868

6969
static void handle_exec_error(t_sh *shell, char *exec_path)

source/shell/init.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* init.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/12/08 20:06:39 by lyeh #+# #+# */
9-
/* Updated: 2024/03/21 17:36:42 by lyeh ### ########.fr */
9+
/* Updated: 2024/06/01 11:03:14 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -36,7 +36,7 @@ bool init_shell(t_sh *shell)
3636
handle_signal_std(0, NULL, shell);
3737
handle_signal_record(0, NULL, shell);
3838
setup_signal(shell, SIGINT, SIG_STANDARD);
39-
setup_signal(shell, SIGABRT, SIG_STANDARD);
39+
setup_signal(shell, SIGUSR1, SIG_STANDARD);
4040
setup_signal(shell, SIGTERM, SIG_STANDARD);
4141
setup_signal(shell, SIGQUIT, SIG_IGNORE);
4242
return (true);

source/signal/exception_broadcaster.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* exception_broadcaster.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/18 17:45:41 by lyeh #+# #+# */
9-
/* Updated: 2024/03/18 17:48:59 by lyeh ### ########.fr */
9+
/* Updated: 2024/06/01 11:03:14 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -19,7 +19,7 @@ void raise_error_and_escape(t_sh *shell, char *msg)
1919
{
2020
if (msg)
2121
print_error(STY_RED"%s: error: %s\n"STY_RES, PROGRAM_NAME, msg);
22-
kill(-shell->pid, SIGABRT);
22+
kill(-shell->pid, SIGUSR1);
2323
}
2424

2525
void raise_error_to_all_subprocess(t_sh *shell, int exit_code, char *msg)
@@ -28,7 +28,7 @@ void raise_error_to_all_subprocess(t_sh *shell, int exit_code, char *msg)
2828
if (msg)
2929
print_error(STY_RED"%s: error: %s\n"STY_RES, PROGRAM_NAME, msg);
3030
setup_signal(shell, SIGINT, SIG_STANDARD);
31-
setup_signal(shell, SIGABRT, SIG_STANDARD);
31+
setup_signal(shell, SIGUSR1, SIG_STANDARD);
3232
setup_signal(shell, SIGQUIT, SIG_IGNORE);
3333
kill(-shell->pid, SIGTERM);
3434
}
@@ -39,7 +39,7 @@ void raise_error_to_own_subprocess(t_sh *shell, int exit_code, char *msg)
3939
if (msg)
4040
print_error(STY_RED"%s: error: %s\n"STY_RES, PROGRAM_NAME, msg);
4141
setup_signal(shell, SIGINT, SIG_STANDARD);
42-
setup_signal(shell, SIGABRT, SIG_STANDARD);
42+
setup_signal(shell, SIGUSR1, SIG_STANDARD);
4343
setup_signal(shell, SIGTERM, SIG_STANDARD);
4444
setup_signal(shell, SIGQUIT, SIG_IGNORE);
4545
signal_to_all_subprocess(shell, SIGTERM);

source/signal/signal_handler.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* signal_handler.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/18 17:46:19 by lyeh #+# #+# */
9-
/* Updated: 2024/03/19 10:27:36 by lyeh ### ########.fr */
9+
/* Updated: 2024/06/01 11:03:14 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -49,7 +49,7 @@ void handle_signal_std(int signo, siginfo_t *info, void *context)
4949
rl_on_new_line();
5050
rl_replace_line("", 0);
5151
}
52-
else if (signo == SIGABRT)
52+
else if (signo == SIGUSR1)
5353
{
5454
if (shell->subshell_level == 0)
5555
clean_and_exit_shell(

0 commit comments

Comments
 (0)