Program exit status conventions
Asked Answered
B

2

13

What is conventional return values for applications in Windows and GNU/Linux respectivly. 0 means success. But what should be used on user-requested abort. When I abort on Windows, it returns 3, but this value is not in the list of system error codes if it is not an ERROR_PATH_NOT_FOUND. GNU binutils uses 1. From a user perspective, returning GetLastError or errno would be good since they are documented, but is these only seems to cover lower level status codes. I am looking for a value that represents "Application terminated unsuccessfully"

The reason I wounder is that I want to

exit(errcode)

from a signal handler that catches some Access Violation/SIGSEGV (i.e programming errors) after printing a message about where it occured. Then the error code should be destiguishable from user input errors.

Blankenship answered 15/8, 2013 at 8:0 Comment(2)
Exit code of non-zero implies unsuccessful termination.Stanley
Well since -1 is nonzero, and is not an already used error code it is fine.Blankenship
B
9

This might help, http://tldp.org/LDP/abs/html/exitcodes.html those are the standard exit code. The rest of them I think are program dependant. basically you need to verify the documentation of the specific software you are looking for. As @devnull said, any exit code that is not zero implies an unsuccessful termination

Bottrop answered 15/8, 2013 at 15:51 Comment(1)
For completeness, exit code 125 means "not the error we are looking for" to git-bisect. It should be perfectly fine to misuse this value for any other purpose — it only matters in one-off scripts running under git-bisect, for seeking out a specific error, and they must control their exit status tightly anyway.Chiou
T
4

This is only few conventions about exit codes. Let's see what some manuals say:

The GNU C Library Reference Manual

  1. There are conventions for what sorts of status values certain programs should return. The most common convention is simply 0 for success and 1 for failure ...

  2. A general convention reserves status values 128 and up for special purposes

  3. Some non-POSIX systems use different conventions for exit status values
  4. For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure.

ISO/IEC 9899:2011 (C11 standard)

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

That means if you want (and for most cases that's enough) just indicate success or failure you definitely should use EXIT_SUCCESS and EXIT_FAILURE. If you want indicate other errors, you should reinvent your own exit statuses. For example:

#define HEX_FILE_CREATE 2
#define HEX_FILE_CREATE 3
...

There is additional tips on what and how you should return:

  1. Warning: Don’t try to use the number of errors as the exit status. This is actually not very useful; a parent process would generally not care how many errors occurred. Worse than that, it does not work, because the status value is truncated to eight bits. Thus, if the program tried to report 256 errors, the parent would receive a report of 0 errors—that is, success
  2. For the same reason, it does not work to use the value of errno as the exit status—these can exceed 255

Conclusion:

  1. For success always use EXIT_SUCCESS
  2. Your failure exit status should be beetween 1 and 127
  3. Don't use errno error code as exit status
Triphammer answered 24/1, 2014 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.