diff options
author | Konstantin Pereiaslov <perk11@perk11.info> | 2023-09-21 01:44:07 -0500 |
---|---|---|
committer | Konstantin Pereiaslov <perk11@perk11.info> | 2023-09-21 01:44:07 -0500 |
commit | de8b698545a43ee5d74dde1d6412260cf7e8e13f (patch) | |
tree | 584ee6447771fa0cd56e60a853fb0fd234a158c2 | |
parent | 3228448b644503651f7927bf1274a5db59d05d4e (diff) |
Pass errno to handle_kill_error rather than reading it there and use strerror() rather than parsing error manually.0.4.1
This fixes the compilation warning where reason was possibly undefined.
-rw-r--r-- | process_handling.c | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/process_handling.c b/process_handling.c index c6548ca..c7ef5f8 100644 --- a/process_handling.c +++ b/process_handling.c @@ -1,7 +1,9 @@ #include <stdio.h> #include <stdlib.h> #include <errno.h> +#include <limits.h> #include <signal.h> +#include <string.h> #include <unistd.h> #include <sys/wait.h> @@ -35,18 +37,10 @@ pid_t run_shell_command(const char *shell_command_to_run) { * * @param signal_name The name of the signal being sent. * @param pid The process ID of the target process. + * @param kill_errno errno of kill function */ -void handle_kill_error(char *signal_name, pid_t pid) { - const char *reason; - if (errno == EPERM) { - reason = "Operation not permitted"; - } else if (errno == EINVAL) { - reason = "Invalid signal number"; - } else if (errno == ESRCH) { - reason = "No such process"; - } - - fprintf(stderr, "Failed to send %s signal to PID %i: %s\n", signal_name, pid, reason); +void handle_kill_error(char *signal_name, pid_t pid, int kill_errno) { + fprintf(stderr, "Failed to send %s signal to PID %i: %s\n", signal_name, pid, strerror(kill_errno)); } void send_signal_to_pid(pid_t pid, int signal, char *signal_name) { @@ -55,7 +49,7 @@ void send_signal_to_pid(pid_t pid, int signal, char *signal_name) { } int kill_result = kill(pid, signal); if (kill_result == -1) { - handle_kill_error(signal_name, pid); + handle_kill_error(signal_name, pid, errno); exit(1); } else { if (debug) fprintf(stderr, "kill function sending %s returned %i\n",signal_name, kill_result); |