From 7e2725aad19c19359656095e743a0405e5de7f5e Mon Sep 17 00:00:00 2001 From: Konstantin Pereiaslov Date: Mon, 4 Sep 2023 01:37:51 -0500 Subject: [PATCH] Move wait_for_pid_to_exit_synchronously and exit_if_pid_has_finished to process_handling.c --- main.c | 21 --------------------- process_handling.c | 26 ++++++++++++++++++++++++++ process_handling.h | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index 536985d..ecc29f7 100644 --- a/main.c +++ b/main.c @@ -54,18 +54,6 @@ void print_version() { printf("runwhenidle %s\n", VERSION); } -void exit_if_pid_has_finished(pid_t pid) { - int status; - if (debug) fprintf(stderr, "Checking if PID %i has finished\n", pid); - if (waitpid(pid, &status, WNOHANG + WUNTRACED) == pid && WIFEXITED(status)) { - int exit_code = WEXITSTATUS(status); - if (verbose) { - fprintf(stderr, "PID %i has finished with exit code %u\n", pid, exit_code); - } - exit(exit_code); - } -} - char *read_remaining_arguments_as_char(int argc, char *const *argv) { if (optind == argc) { //there is one argument remaining @@ -110,16 +98,7 @@ long unsigned query_user_idle_time() return IDLE_TIME_NOT_AVAILABLE_VALUE; } -int wait_for_pid_to_exit_synchronously(int pid) { - int status; - waitpid(pid, &status, 0); - int exit_code = WEXITSTATUS(status); - if (verbose) { - fprintf(stderr, "PID %i has finished with exit code %u\n", pid, exit_code); - } - return exit_code; -} int handle_interruption() { if (command_paused) { if (verbose) { diff --git a/process_handling.c b/process_handling.c index 69f3d19..93160f1 100644 --- a/process_handling.c +++ b/process_handling.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "process_handling.h" #include "output_settings.h" @@ -72,3 +73,28 @@ void resume_command(pid_t pid) { } send_signal_to_pid(pid, SIGCONT, "SIGCONT"); } + + +int wait_for_pid_to_exit_synchronously(int pid) { + int status; + waitpid(pid, &status, 0); + int exit_code = WEXITSTATUS(status); + if (verbose) { + fprintf(stderr, "PID %i has finished with exit code %u\n", pid, exit_code); + } + + return exit_code; +} + + +void exit_if_pid_has_finished(pid_t pid) { + int status; + if (debug) fprintf(stderr, "Checking if PID %i has finished\n", pid); + if (waitpid(pid, &status, WNOHANG + WUNTRACED) == pid && WIFEXITED(status)) { + int exit_code = WEXITSTATUS(status); + if (verbose) { + fprintf(stderr, "PID %i has finished with exit code %u\n", pid, exit_code); + } + exit(exit_code); + } +} diff --git a/process_handling.h b/process_handling.h index 55aaea7..2153797 100644 --- a/process_handling.h +++ b/process_handling.h @@ -30,4 +30,20 @@ void resume_command(pid_t pid); * @return The PID of the child process on success, */ pid_t run_shell_command(const char *shell_command_to_run); + +/** + * Waits for a specific process to exit synchronously and returns its exit code. + * + * @param pid The process ID (PID) of the target process to wait for. + * @return The exit code of the process + */ +int wait_for_pid_to_exit_synchronously(int pid); + +/** + * Checks if a specific process has finished and exits the current process with the same exit code if it has. + * This function does not block and returns immediately if the process has not finished. + * + * @param pid The process ID (PID) of the target process to check. + */ +void exit_if_pid_has_finished(pid_t pid); #endif //RUNWHENIDLE_PROCESS_HANDLING_H