Skip to content

Commit

Permalink
Add descriptions to well-known process exit codes
Browse files Browse the repository at this point in the history
And log them:

```
Command returned '1'.
Command returned '127'.
Command returned '143'.

Command returned '1' (failure).
Command returned '127' (not found).
Command returned '143' (SIGTERM).
```

This should provide more hints when investigating what commands did, why
they finished and how.
  • Loading branch information
happz committed May 22, 2024
1 parent b782a60 commit 45da8b0
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,52 @@ def configure_constant(default: int, envvar: str) -> int:


class ProcessExitCodes(enum.IntEnum):
#: Successful run.
SUCCESS = 0
#: Unsuccessful run.
FAILURE = 1

#: tmt pidfile lock operation failed.
TEST_PIDFILE_LOCK_FAILED = 122
#: tmt pidfile unlock operation failed.
TEST_PIDFILE_UNLOCK_FAILED = 123

#: Command was terminated because of a timeout.
TIMEOUT = 124

#: Permission denied (or) unable to execute.
PERMISSION_DENIED = 126
#: Command not found, or PATH error.
NOT_FOUND = 127

# (128 + N) where N is a signal send to the process
#: Terminated by either ``Ctrl+C`` combo or ``SIGINT`` signal.
SIGINT = 130
#: Terminated by a ``SIGTERM`` signal.
SIGTERM = 143

@classmethod
def is_pidfile(cls, exit_code: Optional[int]) -> bool:
return exit_code in (ProcessExitCodes.TEST_PIDFILE_LOCK_FAILED,
ProcessExitCodes.TEST_PIDFILE_UNLOCK_FAILED)

@classmethod
def format(cls, exit_code: int) -> Optional[str]:
""" Format a given exit code for nicer logging """

member = cls._value2member_map_.get(exit_code)

if member is None:
return 'unrecognized'

if member in (cls.SUCCESS, cls.FAILURE, cls.TIMEOUT):
return member.name.lower()

if member.name.startswith('SIG'):
return member.name

return member.name.lower().replace('_', ' ')


# Default select.select(timeout) in seconds
DEFAULT_SELECT_TIMEOUT = 5
Expand Down Expand Up @@ -1365,10 +1401,12 @@ def log_event(msg: str) -> None:

stdout, stderr = stdout_logger.get_output(), stderr_logger.get_output()

# Handle the exit code, return output
if process.returncode != 0:
logger.debug(f"Command returned '{process.returncode}'.", level=3)
logger.debug(
f"Command returned '{process.returncode}' "
f"({ProcessExitCodes.format(process.returncode)}).", level=3)

# Handle the exit code, return output
if process.returncode != ProcessExitCodes.SUCCESS:
if not stream_output:
if stdout is not None:
for line in stdout.splitlines():
Expand Down

0 comments on commit 45da8b0

Please sign in to comment.