1

In linux (specifically I have Ubuntu 14) if some program terminates with an error, I can get numerical error code via $? variable

$ ./failing_app
$ echo $?

However the number itself does not tell me much, how do I get error name and description?

There is a list of errors in $ man errno but it gives only names, not numerical values.

I've search the google and results are quite strange: e.g. I have a toy Rust program in which I'm trying to access array element out of bounds. The program panics and $? is 101, however it is said to correspond to Network unreachable error, which doesn't make any sense.

xaxa
  • 1,057
  • 1
  • 24
  • 53
  • 1
    The kernel error codes are an enum which can and will change from one OS version to another, and possibly between compilers etc. – tripleee Apr 12 '16 at 07:26
  • I wanted to nominate http://stackoverflow.com/questions/20965762/meaning-of-exit-status-1-retured-by-linux-command as a duplicate but there are no upvoted answers here yet. Leaving this note for myself or somebody else to maybe revisit later. – tripleee Apr 12 '16 at 07:36

3 Answers3

1

The exit status of a program ($? in the shell) is unrelated to the C errno.

In a C program, normally the exit status comes from the argument to exit or the return value of main. The convention is that 0 means a successful exit (or true for the shell) and other values are a failure (ir false).

However if a program died from receiving a signal, the shell sets $? to 128 plus the signal number. For example, on a segmentation fault (SIGSEGV, which is 11), $? would be 139.

To list signal numbers, I run kill -l.

Chris Emerson
  • 13,041
  • 3
  • 44
  • 66
1

There is no single central defining authority. Each program assigns its own semantics to error codes. The good ones have documentation in the manual page; for examples, see e.g. the grep and xargs manual pages from GNU.

The exit(3) Linux manual page also notes that "BSD has attempted to standardize exit codes". The BSD sysexits(3) manual page is actually good recommended reading, but as indicated, hardly more than a nudge for a limited number of error scenarios.

The kernel has a much more detailed and well-documented set of possible errors and their causes, but this is obviously limited to system calls, and does not address application-level errors at all. For Linux, see the errno(3) manual page.

The Advanced Bash Scripting Guide has a section about common conventions -- but like the ABS in general, it is an unholy mixture of standards, conventions, the author's personal opinion, guesswork, and lies. (None of the exit codes in the table are "reserved", even though it says so, and the text which refers to the table emphasizes this incorrect fact.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

This link provides a list for errors with the numbers. Hope this is what you are looking for. http://www.thegeekstuff.com/2010/10/linux-error-codes/

Also, on my system I see the file /usr/include/asm-generic/errno-base.h has the definitions of the errors as well as error numbers. This would be a better reference than the link.

Kushal
  • 447
  • 1
  • 5
  • 13
  • Seems like `$ cat /usr/include/asm-generic/err*` is a more full list. However I can get segmentation fault with errno 139, which is not in this list – xaxa Apr 10 '16 at 15:21
  • Then most likely, it is a user defined error. It seems to be for a segmentation fault. Too hard to say without seeing your code. Please upload your code. – Kushal Apr 10 '16 at 16:28
  • It's something as simple as `int main() { int x[3] = { 1,2,3 }; printf("%i\n", x[50000]); return 0; }` – xaxa Apr 10 '16 at 16:54
  • Yeah that is a segmentation fault. Meaning you're trying to access memory you're not supposed to access. In your case, x is an array of 3 but you are trying to access subsequent memory addresses beyond the 3 elements. Hence the segmentation fault. – Kushal Apr 10 '16 at 17:08
  • I know why this is happening, my question is about error codes – xaxa Apr 11 '16 at 09:45
  • What description do you need? the error itself is usually defined. I think the error codes sometimes vary depending on application. So, all general codes are defined. You will find the general error codes along with description in the header files in /usr/include – Kushal Apr 11 '16 at 14:58
  • From a more practical point of view I want to write a wrapper script that would execute a given task/application and in case of error do something like send me email etc. In this email, I want to see a meaningful name of the error, not just a number... I do understand that applications can return any number to the OS, but let's leave such cases behind... I'm quite surprised that there are no `man` pages with descriptions of standard errors, and I'm even more surprised that such a simple application as above produces an error with a code that I can't find anywhere. It just feels wrong. – xaxa Apr 11 '16 at 19:05
  • When a process is killed by a signal, the exit code is 128 plus the signal number. So 139 is basically a synonym for segfault (128 plus SIGSEGV which is 11). – tripleee Dec 11 '22 at 15:23