blob: 9e15a830d5e676b6a87af2433b095a2ee15fb49a [file] [log] [blame]
# -*- coding: utf-8 -*-
# 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 errno
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.
exit_status = os.WEXITSTATUS(status)
if os.WIFSIGNALED(status):
# Kill ourselves with the same signal.
sig_status = os.WTERMSIG(status)
pid = os.getpid()
os.kill(pid, sig_status)
# Still here? Maybe the signal was masked.
signal.signal(sig_status, signal.SIG_DFL)
except RuntimeError as e:
if e.args[0] != errno.EINVAL:
os.kill(pid, sig_status)
# Still here? Just exit.
exit_status = 127
# Exit with the code we want.