blob: 12cfc0433cc17e16d227a0ac601088232a70489b [file] [log] [blame]
# Copyright 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Process related utilities."""
from __future__ import print_function
import os
import signal
import sys
import time
def GetExitStatus(status):
"""Get the exit status of a child from an os.waitpid call.
status: The return value of os.waitpid(pid, 0)[1]
The exit status of the process. If the process exited with a signal,
the return value will be 128 plus the signal number.
if os.WIFSIGNALED(status):
return 128 + os.WTERMSIG(status)
assert os.WIFEXITED(status), 'Unexpected exit status %r' % status
return os.WEXITSTATUS(status)
def ExitAsStatus(status):
"""Exit the same way as |status|.
If the status field says it was killed by a signal, then we'll do that to
ourselves. Otherwise we'll exit with the exit code.
See for more details.
status: A status as returned by os.wait type funcs.
sig_status = status & 0xff
exit_status = (status >> 8) & 0xff
if sig_status:
# Kill ourselves with the same signal.
pid = os.getpid()
os.kill(pid, sig_status)
# Still here? Maybe the signal was masked.
signal.signal(sig_status, signal.SIG_DFL)
os.kill(pid, sig_status)
# Still here? Just exit.
exit_status = 127
# Exit with the code we want.