From 43ac86d4aa6371114bf42d3570f80c8f7d73438c Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sat, 24 Sep 2022 17:08:12 +0200 Subject: [PATCH] pidstat: pidstat should return the exit code of the child process (#339) When a process is monitored with option -e, pidstat should return the exit code of this process and a default value of 0. Signed-off-by: Sebastien GODARD --- pidstat.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pidstat.c b/pidstat.c index 7e302d5..57469bd 100644 --- a/pidstat.c +++ b/pidstat.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #ifdef HAVE_LINUX_SCHED_H @@ -73,6 +74,7 @@ unsigned int actflag = 0; /* Activity flag */ struct sigaction alrm_act, int_act, chld_act; int signal_caught = 0; +int status = 0; int dplaces_nr = -1; /* Number of decimal places */ @@ -120,7 +122,18 @@ void alarm_handler(int sig) */ void int_handler(int sig) { - signal_caught = 1; + int status_code; + + signal_caught = TRUE; + + if (sig == SIGCHLD) { + /* + * SIGCHLD tells the parent process that it can now + * get the exit status of the child via wait(). + */ + wait(&status_code); + status = WEXITSTATUS(status_code); + } } /* @@ -2830,5 +2843,9 @@ int main(int argc, char **argv) /* Free structures */ sfree_pid(&pid_list, TRUE); - return 0; + /* + * @status contains the exit code of the child process monitored with option -e, + * or 0 otherwise. + */ + return status; } -- 2.40.0