/
proc_enums.py
167 lines (141 loc) · 5.99 KB
/
proc_enums.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright 2018 Alexey Stepanov aka penguinolog.
# Copyright 2016 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Process enumerators.
Linux signals, Linux & bash return codes.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import enum
import typing # noqa # pylint: disable=unused-import
import six
__all__ = ("SigNum", "ExitCodes", "exit_code_to_enum", "exit_codes_to_enums")
@enum.unique
class SigNum(int, enum.Enum):
"""Signal enumerators."""
SIGHUP = 1 # Hangup (POSIX).
SIGINT = 2 # Interrupt (ANSI).
SIGQUIT = 3 # Quit (POSIX).
SIGILL = 4 # Illegal instruction (ANSI).
SIGTRAP = 5 # Trace trap (POSIX).
SIGABRT = 6 # Abort (ANSI).
SIGBUS = 7 # BUS error (4.2 BSD).
SIGFPE = 8 # Floating-point exception (ANSI).
SIGKILL = 9 # Kill, unblockable (POSIX).
SIGUSR1 = 10 # User-defined signal 1 (POSIX).
SIGSEGV = 11 # Segmentation violation (ANSI).
SIGUSR2 = 12 # User-defined signal 2 (POSIX).
SIGPIPE = 13 # Broken pipe (POSIX).
SIGALRM = 14 # Alarm clock (POSIX).
SIGTERM = 15 # Termination (ANSI).
SIGSTKFLT = 16 # Stack fault.
SIGCHLD = 17 # Child status has changed (POSIX).
SIGCONT = 18 # Continue (POSIX).
SIGSTOP = 19 # Stop, unblockable (POSIX).
SIGTSTP = 20 # Keyboard stop (POSIX).
SIGTTIN = 21 # Background read from tty (POSIX).
SIGTTOU = 22 # Background write to tty (POSIX).
SIGURG = 23 # Urgent condition on socket (4.2 BSD).
SIGXCPU = 24 # CPU limit exceeded (4.2 BSD).
SIGXFSZ = 25 # File size limit exceeded (4.2 BSD).
SIGVTALRM = 26 # Virtual alarm clock (4.2 BSD).
SIGPROF = 27 # Profiling alarm clock (4.2 BSD).
SIGWINCH = 28 # Window size change (4.3 BSD, Sun).
SIGPOLL = 29 # Pollable event occurred (System V)
SIGPWR = 30 # Power failure restart (System V).
SIGSYS = 31 # Bad system call.
def __str__(self): # type: () -> str # pragma: no cover
"""Representation for logs."""
return "{self.name}<{self.value:d}(0x{self.value:02X})>".format( # pylint: disable=missing-format-attribute
self=self
)
if six.PY3: # pragma: no cover
digit_type = int
else: # pragma: no cover
# noinspection PyUnresolvedReferences
digit_type = long # noqa # pylint: disable=undefined-variable, long-builtin
@enum.unique
class ExitCodes(digit_type, enum.Enum):
"""Linux & bash exit codes."""
EX_OK = 0 # successful termination
EX_INVALID = 0xDEADBEEF # uint32 debug value. Impossible for POSIX
EX_ERROR = 1 # general failure
EX_BUILTIN = 2 # Misuse of shell builtins (according to Bash)
EX_USAGE = 64 # command line usage error
EX_DATAERR = 65 # data format error
EX_NOINPUT = 66 # cannot open input
EX_NOUSER = 67 # addressee unknown
EX_NOHOST = 68 # host name unknown
EX_UNAVAILABLE = 69 # service unavailable
EX_SOFTWARE = 70 # internal software error
EX_OSERR = 71 # system error (e.g., can't fork)
EX_OSFILE = 72 # critical OS file missing
EX_CANTCREAT = 73 # can't create (user) output file
EX_IOERR = 74 # input/output error
EX_TEMPFAIL = 75 # temp failure; user is invited to retry
EX_PROTOCOL = 76 # remote error in protocol
EX_NOPERM = 77 # permission denied
EX_CONFIG = 78 # configuration error
EX_NOEXEC = 126 # If a command is found but is not executable
EX_NOCMD = 127 # If a command is not found
# Signal exits:
EX_SIGHUP = 128 + SigNum.SIGHUP
EX_SIGINT = 128 + SigNum.SIGINT
EX_SIGQUIT = 128 + SigNum.SIGQUIT
EX_SIGILL = 128 + SigNum.SIGILL
EX_SIGTRAP = 128 + SigNum.SIGTRAP
EX_SIGABRT = 128 + SigNum.SIGABRT
EX_SIGBUS = 128 + SigNum.SIGBUS
EX_SIGFPE = 128 + SigNum.SIGFPE
EX_SIGKILL = 128 + SigNum.SIGKILL
EX_SIGUSR1 = 128 + SigNum.SIGUSR1
EX_SIGSEGV = 128 + SigNum.SIGSEGV
EX_SIGUSR2 = 128 + SigNum.SIGUSR2
EX_SIGPIPE = 128 + SigNum.SIGPIPE
EX_SIGALRM = 128 + SigNum.SIGALRM
EX_SIGTERM = 128 + SigNum.SIGTERM
EX_SIGSTKFLT = 128 + SigNum.SIGSTKFLT
EX_SIGCHLD = 128 + SigNum.SIGCHLD
EX_SIGCONT = 128 + SigNum.SIGCONT
EX_SIGSTOP = 128 + SigNum.SIGSTOP
EX_SIGTSTP = 128 + SigNum.SIGTSTP
EX_SIGTTIN = 128 + SigNum.SIGTTIN
EX_SIGTTOU = 128 + SigNum.SIGTTOU
EX_SIGURG = 128 + SigNum.SIGURG
EX_SIGXCPU = 128 + SigNum.SIGXCPU
EX_SIGXFSZ = 128 + SigNum.SIGXFSZ
EX_SIGVTALRM = 128 + SigNum.SIGVTALRM
EX_SIGPROF = 128 + SigNum.SIGPROF
EX_SIGWINCH = 128 + SigNum.SIGWINCH
EX_SIGPOLL = 128 + SigNum.SIGPOLL
EX_SIGPWR = 128 + SigNum.SIGPWR
EX_SIGSYS = 128 + SigNum.SIGSYS
def __str__(self): # type: () -> str
"""Representation for logs."""
return "{self.name}<{self.value:d}(0x{self.value:02X})>".format( # pylint: disable=missing-format-attribute
self=self
)
def exit_code_to_enum(code): # type: (typing.Union[int, ExitCodes]) -> typing.Union[int, ExitCodes]
"""Convert exit code to enum if possible."""
if isinstance(code, int) and code in ExitCodes.__members__.values():
return ExitCodes(code)
return code # pragma: no cover
def exit_codes_to_enums(
codes=None # type: typing.Optional[typing.Iterable[typing.Union[int, ExitCodes]]]
): # type: (...) -> typing.List[typing.Union[int, ExitCodes]]
"""Convert integer exit codes to enums."""
if codes is None:
return [ExitCodes.EX_OK]
return [exit_code_to_enum(code) for code in codes]