| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package linux |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
const ( |
| 24 |
WNOHANG = 0x00000001 |
| 25 |
WUNTRACED = 0x00000002 |
| 26 |
WSTOPPED = WUNTRACED |
| 27 |
WEXITED = 0x00000004 |
| 28 |
WCONTINUED = 0x00000008 |
| 29 |
WNOWAIT = 0x01000000 |
| 30 |
WNOTHREAD = 0x20000000 |
| 31 |
WALL = 0x40000000 |
| 32 |
WCLONE = 0x80000000 |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
const ( |
| 37 |
P_ALL = 0x0 |
| 38 |
P_PID = 0x1 |
| 39 |
P_PGID = 0x2 |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
type WaitStatus uint32 |
| 45 |
|
| 46 |
|
| 47 |
func WaitStatusExit(status int32) WaitStatus { |
| 48 |
return WaitStatus(uint32(status) << 8) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func WaitStatusTerminationSignal(sig Signal) WaitStatus { |
| 54 |
return WaitStatus(uint32(sig)) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
func WaitStatusStopped(code uint32) WaitStatus { |
| 60 |
return WaitStatus(code<<8 | 0x7f) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
func WaitStatusContinued() WaitStatus { |
| 66 |
return WaitStatus(0xffff) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func (ws WaitStatus) WithCoreDump() WaitStatus { |
| 74 |
return ws | 0x80 |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
func (ws WaitStatus) Exited() bool { |
| 80 |
return ws&0x7f == 0 |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
func (ws WaitStatus) Signaled() bool { |
| 86 |
|
| 87 |
return ((ws&0x7f)+1)>>1 != 0 |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
func (ws WaitStatus) CoreDumped() bool { |
| 95 |
return ws&0x80 != 0 |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func (ws WaitStatus) Stopped() bool { |
| 101 |
return ws&0xff == 0x7f |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
func (ws WaitStatus) Continued() bool { |
| 107 |
return ws == 0xffff |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
func (ws WaitStatus) ExitStatus() uint32 { |
| 115 |
return uint32((ws & 0xff00) >> 8) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
func (ws WaitStatus) TerminationSignal() Signal { |
| 123 |
return Signal(ws & 0x7f) |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
func (ws WaitStatus) StopSignal() Signal { |
| 131 |
return Signal((ws & 0xff00) >> 8) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
func (ws WaitStatus) PtraceEvent() uint32 { |
| 138 |
return uint32(ws >> 16) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (ws WaitStatus) String() string { |
| 143 |
switch { |
| 144 |
case ws.Exited(): |
| 145 |
return fmt.Sprintf("exit status %d", ws.ExitStatus()) |
| 146 |
case ws.Signaled(): |
| 147 |
if ws.CoreDumped() { |
| 148 |
return fmt.Sprintf("killed by signal %d (core dumped)", ws.TerminationSignal()) |
| 149 |
} |
| 150 |
return fmt.Sprintf("killed by signal %d", ws.TerminationSignal()) |
| 151 |
case ws.Stopped(): |
| 152 |
if ev := ws.PtraceEvent(); ev != 0 { |
| 153 |
return fmt.Sprintf("stopped by signal %d (PTRACE_EVENT %d)", ws.StopSignal(), ev) |
| 154 |
} |
| 155 |
return fmt.Sprintf("stopped by signal %d", ws.StopSignal()) |
| 156 |
case ws.Continued(): |
| 157 |
return "continued" |
| 158 |
default: |
| 159 |
return fmt.Sprintf("unknown status %#x", uint32(ws)) |
| 160 |
} |
| 161 |
} |