From 5d6f0124e9deb69e674469d008c4d43796eaeea2 Mon Sep 17 00:00:00 2001 From: Harikrishnan Balagopal Date: Thu, 26 Feb 2026 21:06:14 +0530 Subject: [PATCH] fix: exit the bsubmit with the same exit code as the bsub child process Signed-off-by: Harikrishnan Balagopal --- bsubmit/bsubmit.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/bsubmit/bsubmit.cpp b/bsubmit/bsubmit.cpp index f8d080a..db04a3a 100644 --- a/bsubmit/bsubmit.cpp +++ b/bsubmit/bsubmit.cpp @@ -279,18 +279,23 @@ bool verifyUserMapping(std::string fpath, std::string execName) } -void runcmd(std::string cmd) +int runcmd(std::string cmd) { std::string readline; FILE * fp = popen(cmd.c_str(), "r"); - if (fp != NULL) { - char buffer[4096]; - while(fgets(buffer, sizeof(buffer) - 1, fp) != NULL) { - std::cout << buffer; - } + if (!fp) { + throw std::runtime_error("popen() failed!"); } - - pclose(fp); + char buffer[4096]; + while(fgets(buffer, sizeof(buffer) - 1, fp) != NULL) { + std::cout << buffer; + } + // pclose returns the termination status of the command + int status = pclose(fp); + // Extract the exit code from the status + // WEXITSTATUS is a macro that gets the actual return value (0-255) + int exit_code = WEXITSTATUS(status); + return exit_code; } int changeUser(char * execUser) @@ -376,7 +381,7 @@ int main(int argc, char **argv) bsubcmd.append(" "); bsubcmd.append(argv[i]); } - runcmd(bsubcmd); + int exit_code = runcmd(bsubcmd); - return 0; + return exit_code; }