From: Sebastien GODARD Date: Sat, 24 Sep 2022 15:08:12 +0000 (+0200) Subject: pidstat: pidstat should return the exit code of the child process (#339) X-Git-Tag: v12.7.1~13 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=43ac86d4aa6371114bf42d3570f80c8f7d73438c;p=sysstat 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 --- 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; }