Skip to content

Commit 271f016

Browse files
committed
fix: Change SIGABRT to SIGUSR1
While `SIGABRT` defaults to termination, it is also expected to produce a core dump. If we don't do that, we would change the expected behavior of `SIGABRT`. An example usage that produces a `SIGABRT` signal is the `abort()` function. Note: The `abort()` function would usually still work as expected: > If the SIGABRT signal is ignored, or caught by a handler that returns, the abort() function will still terminate the process. It does this by restoring the default disposition for SIGABRT and then raising the signal for a second time. However, in our case our signal handler for `SIGABRT` does not return but exit, hence the abort function never gets the chance to send the signal a second time with default behavior.
1 parent 40cf36c commit 271f016

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
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

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)