Skip to content

Commit

Permalink
Pass errno to handle_kill_error rather than reading it there and use …
Browse files Browse the repository at this point in the history
…strerror() rather than parsing error manually.

This fixes the compilation warning where reason was possibly undefined.
  • Loading branch information
perk11 committed Sep 21, 2023
1 parent 3228448 commit de8b698
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions process_handling.c
Original file line number Diff line number Diff line change
@@ -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>

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit de8b698

Please sign in to comment.