1# Copyright (c) 2010, 2011 Nicira, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at:
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import signal
17
18
19def _signal_status_msg(type_, signr):
20    s = "%s by signal %d" % (type_, signr)
21    for name in signal.__dict__:
22        if name.startswith("SIG") and getattr(signal, name) == signr:
23            return "%s (%s)" % (s, name)
24    return s
25
26
27def status_msg(status):
28    """Given 'status', which is a process status in the form reported by
29    waitpid(2) and returned by process_status(), returns a string describing
30    how the process terminated."""
31    if os.WIFEXITED(status):
32        s = "exit status %d" % os.WEXITSTATUS(status)
33    elif os.WIFSIGNALED(status):
34        s = _signal_status_msg("killed", os.WTERMSIG(status))
35    elif os.WIFSTOPPED(status):
36        s = _signal_status_msg("stopped", os.WSTOPSIG(status))
37    else:
38        s = "terminated abnormally (%x)" % status
39    if os.WCOREDUMP(status):
40        s += ", core dumped"
41    return s
42