Skip to content

Commit 5d6f012

Browse files
fix: exit the bsubmit with the same exit code as the bsub child process
Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
1 parent 80e3207 commit 5d6f012

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

bsubmit/bsubmit.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,23 @@ bool verifyUserMapping(std::string fpath, std::string execName)
279279
}
280280

281281

282-
void runcmd(std::string cmd)
282+
int runcmd(std::string cmd)
283283
{
284284
std::string readline;
285285
FILE * fp = popen(cmd.c_str(), "r");
286-
if (fp != NULL) {
287-
char buffer[4096];
288-
while(fgets(buffer, sizeof(buffer) - 1, fp) != NULL) {
289-
std::cout << buffer;
290-
}
286+
if (!fp) {
287+
throw std::runtime_error("popen() failed!");
291288
}
292-
293-
pclose(fp);
289+
char buffer[4096];
290+
while(fgets(buffer, sizeof(buffer) - 1, fp) != NULL) {
291+
std::cout << buffer;
292+
}
293+
// pclose returns the termination status of the command
294+
int status = pclose(fp);
295+
// Extract the exit code from the status
296+
// WEXITSTATUS is a macro that gets the actual return value (0-255)
297+
int exit_code = WEXITSTATUS(status);
298+
return exit_code;
294299
}
295300

296301
int changeUser(char * execUser)
@@ -376,7 +381,7 @@ int main(int argc, char **argv)
376381
bsubcmd.append(" ");
377382
bsubcmd.append(argv[i]);
378383
}
379-
runcmd(bsubcmd);
384+
int exit_code = runcmd(bsubcmd);
380385

381-
return 0;
386+
return exit_code;
382387
}

0 commit comments

Comments
 (0)