1/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25/* -*- Mode: C++; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
26
27#include "easylogging++.h"
28#include "WaitStatus.h"
29
30#include <sys/types.h>
31#include <sys/wait.h>
32
33using namespace std;
34
35WaitStatus::Type WaitStatus::type() const {
36 if (exit_code() >= 0) {
37 return EXIT;
38 }
39 if (fatal_sig() > 0) {
40 return FATAL_SIGNAL;
41 }
42 if (stop_sig() > 0) {
43 return SIGNAL_STOP;
44 }
45 if (group_stop() > 0) {
46 return GROUP_STOP;
47 }
48 if (is_syscall()) {
49 return SYSCALL_STOP;
50 }
51 if (ptrace_event() > 0) {
52 return PTRACE_EVENT;
53 }
54 LOG(FATAL) << "Status " << HEX(status) << " not understood";
55 return EXIT;
56}
57
58int WaitStatus::exit_code() const {
59 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
60}
61
62int WaitStatus::fatal_sig() const {
63 return WIFSIGNALED(status) ? WTERMSIG(status) : 0;
64}
65
66int WaitStatus::stop_sig() const {
67 if (!WIFSTOPPED(status) || ((status >> 16) & 0xff)) {
68 return 0;
69 }
70 int sig = WSTOPSIG(status);
71 if (sig == (SIGTRAP | 0x80)) {
72 return 0;
73 }
74 sig &= ~0x80;
75 return sig ? sig : SIGSTOP;
76}
77
78int WaitStatus::group_stop() const {
79 if (!WIFSTOPPED(status) || ((status >> 16) & 0xff) != PTRACE_EVENT_STOP) {
80 return 0;
81 }
82 int sig = WSTOPSIG(status);
83 sig &= ~0x80;
84 return sig ? sig : SIGSTOP;
85}
86
87bool WaitStatus::is_syscall() const {
88 if (!WIFSTOPPED(status) || ptrace_event()) {
89 return 0;
90 }
91 return WSTOPSIG(status) == (SIGTRAP | 0x80);
92}
93
94int WaitStatus::ptrace_event() const {
95 int event = (status >> 16) & 0xff;
96 return event == PTRACE_EVENT_STOP ? 0 : event;
97}
98
99int WaitStatus::ptrace_signal() const {
100 return WIFSTOPPED(status) ? (WSTOPSIG(status) & 0x7f) : 0;
101}
102