exit-status: list BSD exit codes too

Let's optionally translate BSD exit codes to error strings too.

My first approach on adding this was to turn ExitStatusLevel into a
bitmask rather than a linear level, with one bit for the various feature
bits. However, the exit code ranges are generally not defined
independently from each other, i.e. our own ones are defined with the
LSB ones in mind, and most sets are defined with the ISO C ones.

Hence, instead I changed the existing hierarchy of MINIMAL, SYSTEMD, LSB
with an alias of FULL == LSB, only slightly by seperating FULL and LSB
into two separate levels, so that there's now:

1. MINIMAL (only EXIT_SUCCESS/EXIT_FAILURE)
2. SYSTEMD (incorporating our own exit codes)
3. LSB (like SYSTEMD but adding in LSB service exit codes)
4. FULL (like FULL but adding BSD exit codes)

Note that across the codebase only FULL, SYSTEMD, and MINIMAL are used,
depending on context, how much we know about the process and whether we
are logging for debugging purposes or not. This means the LSB level
wouldn't really have to be separate, but it appeared careless to me to
fold it into FULL along with the BSD exit codes.

Note that this commit doesn't change much for regular codepaths: the
FULL exit status level is only used during debug logging, as a helper to
the user reading the debug logs.
This commit is contained in:
Lennart Poettering 2018-04-23 19:26:25 +02:00
parent 3e0bff7d0b
commit 0a233ba179
2 changed files with 73 additions and 11 deletions

View File

@ -7,6 +7,7 @@
#include <signal.h>
#include <stdlib.h>
#include <sysexits.h>
#include "exit-status.h"
#include "macro.h"
@ -14,10 +15,21 @@
const char* exit_status_to_string(int status, ExitStatusLevel level) {
/* We cast to int here, so that -Wenum doesn't complain that
* EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
/* Exit status ranges:
*
* 01 ISO C, EXIT_SUCCESS + EXIT_FAILURE
* 27 LSB exit codes for init scripts
* 863 (Currently unmapped)
* 6478 BSD defined exit codes
* 79199 (Currently unmapped)
* 200241 systemd's private error codes (might be extended to 254 in future development)
* 242254 (Currently unmapped, but see above)
* 255 (We should probably stay away from that one, it's frequently used by applications to indicate an
* exit reason that cannot really be expressed in a single exit status value such as a propagated
* signal or such)
*/
switch (status) {
switch (status) { /* We always cover the ISO C ones */
case EXIT_SUCCESS:
return "SUCCESS";
@ -26,8 +38,8 @@ const char* exit_status_to_string(int status, ExitStatusLevel level) {
return "FAILURE";
}
if (IN_SET(level, EXIT_STATUS_SYSTEMD, EXIT_STATUS_LSB)) {
switch (status) {
if (IN_SET(level, EXIT_STATUS_SYSTEMD, EXIT_STATUS_LSB, EXIT_STATUS_FULL)) {
switch (status) { /* Optionally we cover our own ones */
case EXIT_CHDIR:
return "CHDIR";
@ -151,8 +163,8 @@ const char* exit_status_to_string(int status, ExitStatusLevel level) {
}
}
if (level == EXIT_STATUS_LSB) {
switch (status) {
if (IN_SET(level, EXIT_STATUS_LSB, EXIT_STATUS_FULL)) {
switch (status) { /* Optionally we support LSB ones */
case EXIT_INVALIDARGUMENT:
return "INVALIDARGUMENT";
@ -174,6 +186,56 @@ const char* exit_status_to_string(int status, ExitStatusLevel level) {
}
}
if (level == EXIT_STATUS_FULL) {
switch (status) { /* Optionally, we support BSD exit statusses */
case EX_USAGE:
return "USAGE";
case EX_DATAERR:
return "DATAERR";
case EX_NOINPUT:
return "NOINPUT";
case EX_NOUSER:
return "NOUSER";
case EX_NOHOST:
return "NOHOST";
case EX_UNAVAILABLE:
return "UNAVAILABLE";
case EX_SOFTWARE:
return "SOFTWARE";
case EX_OSERR:
return "OSERR";
case EX_OSFILE:
return "OSFILE";
case EX_CANTCREAT:
return "CANTCREAT";
case EX_IOERR:
return "IOERR";
case EX_TEMPFAIL:
return "TEMPFAIL";
case EX_PROTOCOL:
return "PROTOCOL";
case EX_NOPERM:
return "NOPERM";
case EX_CONFIG:
return "CONFIG";
}
}
return NULL;
}

View File

@ -29,10 +29,10 @@ enum {
EXIT_NOTCONFIGURED = 6,
EXIT_NOTRUNNING = 7,
/* The LSB suggests that error codes >= 200 are "reserved". We
* use them here under the assumption that they hence are
* unused by init scripts. */
/* BSD's sysexits.h defines a couple EX_xyz exit codes in the range 64 … 78 */
/* The LSB suggests that error codes >= 200 are "reserved". We use them here under the assumption that they
* hence are unused by init scripts. */
EXIT_CHDIR = 200,
EXIT_NICE,
EXIT_FDS,
@ -81,7 +81,7 @@ typedef enum ExitStatusLevel {
EXIT_STATUS_MINIMAL, /* only cover libc EXIT_STATUS/EXIT_FAILURE */
EXIT_STATUS_SYSTEMD, /* cover libc and systemd's own exit codes */
EXIT_STATUS_LSB, /* cover libc, systemd's own and LSB exit codes */
EXIT_STATUS_FULL = EXIT_STATUS_LSB
EXIT_STATUS_FULL, /* cover libc, systemd's own, LSB and BSD (EX_xyz) exit codes */
} ExitStatusLevel;
typedef struct ExitStatusSet {