4

When I execute a command using system(COMMAND) function in C++ code running on Linux, the return values of calls of system(COMMAND) are not same from each other. Do we have any ways to detect segmentation fault or abnormal termination by using return value of system(COMMAND) function?

For example, when I got segmentation fault after executing a command by using system function, I confirmed the return value of that function was 35584. However, I could not know what that return value means, and I am not sure all possible segmentation faults return 35584. Is there a way to detect all possible values of return values when I encounter segmentation fault or abnormal termination?

1
  • You could use the lower level fork, pipe, dup2, close, execve, waitpid syscalls. Then waitpid gives the right status. You might also use some library specific functions (for example both Gtk and Qt give functions for asynchronous processes). Jan 9, 2013 at 9:34

4 Answers 4

6

man 3 system refers to man 2 wait for the meaning of return value. The latter page describes several macros usable with this value, notably WIFSIGNALED(status) and WTERMSIG(status). That's what you can use to check for segmentation fault or termination on other signals.

2
#include <cstdlib>
#include <iostream>

int main()
{
  const int result = system("./core_dump"); // core_dump only calls abort()
  std::cout << "result=" << result << '\n';
  std::cout << "Terminated by signal: " << (WIFSIGNALED(result) ? "yes" : "no") << '\n';
  std::cout << "Exited normally: " << (WIFEXITED(result) ? "yes" : "no") << '\n';
  return 0;
}

Output (on Ubuntu 12.10):

Aborted (core dumped)
result=34304
Terminated by signal: no
Exited normally: yes

It may be surprising that ./core_dump prints exited normally: yes. Although ./core_dump failed, system returns the exit code of the /bin/sh -c process running the command, which exited normally.

In contrast, calling system("/bin/kill -9 $$") instead of system("./core_dump") results in following output:

result=9
Terminated by signal: yes
Exited normally: no

One further note on the exit code. The core_dump example returned the exit code 34304. First, we have to shift it by 8 bits. The result is 35304 >> 8 == 134, which is the exit code for jobs that were killed with an abort signal. If it failed because of an segfault, we would have gotten 139 instead.

Here is a link to a overview of the exit codes.

Finally, here is core_dump's source code that is used for testing:

#include <cstdlib>

int main() {
  abort();
  return 0;
}
1
  • 1
    Because system returns the exit code of the /bin/sh -c process running the command, and that sh don't core dump, even if the program started by the command did. Jan 8, 2013 at 20:06
1

Linux exit codes are discussed in this question: Are there any standard exit status codes in Linux?

IIRC the return value of system() should correspond to the spawned process' exit code, but that appears to run counter to your observation. Strange. I will test.

EDIT: Observations:

  1. I can confirm your return value for system().
  2. Note that 35585 == 139 << 8. 139 is the linux exit code for a segfaulted process

Next to check glibc source to get an authoritative answer...

0

For any arbitrary command? Not really. It's up to each program to give meaning to the exit code. The only thing you can presume is 0 mean success but even that is just convention and not guaranteed either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.