本文整理汇总了Golang中syscall.WaitStatus.ExitStatus方法的典型用法代码示例。如果您正苦于以下问题:Golang WaitStatus.ExitStatus方法的具体用法?Golang WaitStatus.ExitStatus怎么用?Golang WaitStatus.ExitStatus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类syscall.WaitStatus
的用法示例。
在下文中一共展示了WaitStatus.ExitStatus方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: babySit
func babySit(process *os.Process) int {
// Forward all signals to the app
sigchan := make(chan os.Signal, 1)
sigutil.CatchAll(sigchan)
go func() {
for sig := range sigchan {
if sig == syscall.SIGCHLD {
continue
}
process.Signal(sig)
}
}()
// Wait for the app to exit. Also, as pid 1 it's our job to reap all
// orphaned zombies.
var wstatus syscall.WaitStatus
for {
pid, err := syscall.Wait4(-1, &wstatus, 0, nil)
if err == nil && pid == process.Pid {
break
}
}
return wstatus.ExitStatus()
}
示例2: StartProcess
// StartProcess kicks off the event loop and forever waits for signals from
// the traced process. This is currently done in a super-silly fashion and will
// hopefully benefit from Go channels/goroutines in the future.
func (p *Process) StartProcess() (ret int) {
var status syscall.WaitStatus
L:
for {
_, err := syscall.Wait4( /*p.Pid*/ -1, &status, 0, nil)
p.isRunning = false
switch {
// status == 0 means terminated??
case status.Exited() || status == 0 || err != nil:
ret = status.ExitStatus()
break L
case status.Stopped():
if bp, hit := p.InBreakpoint(); hit {
p.handleBreakpoint(bp)
}
//case status.Continued():
//case status.CoreDump():
//case status.Signaled():
//case status.ExitStatus():
//case status.StopSignal():
//case status.TrapCause():
default:
// fmt.Printf("Got status: %v\n", status)
}
p.Continue()
}
return
}
示例3: handleSigchld
func handleSigchld(mpid int) *resultPack {
for {
var status syscall.WaitStatus
var spid int
var err error
spid, err = syscall.Wait4(-mpid, &status, syscall.WNOHANG|syscall.WALL, nil)
if err != nil {
poePanic(err, "wait4 failed")
} else if spid == 0 {
return nil
}
if spid == mpid && status.Exited() {
return &resultPack{POE_SUCCESS, status.ExitStatus(), ""}
} else if spid == mpid && status.Signaled() {
return &resultPack{POE_SIGNALED, -1, fmt.Sprintf("Program terminated with signal %d (%s)", int(status.Signal()), status.Signal().String())}
} else if status.Stopped() {
e := status >> 16 & 0xff
switch e {
case PTRACE_EVENT_SECCOMP:
if res := handleSyscall(spid); res != nil {
return res
}
case syscall.PTRACE_EVENT_CLONE, syscall.PTRACE_EVENT_FORK, syscall.PTRACE_EVENT_VFORK:
syscall.PtraceCont(spid, 0)
default:
syscall.PtraceCont(spid, int(status.StopSignal()))
}
}
}
}
示例4: printStatus
func printStatus(ws syscall.WaitStatus) string {
switch {
case ws.Exited():
es := ws.ExitStatus()
if es == 0 {
return ""
}
return fmt.Sprintf("exited %v", es)
case ws.Signaled():
msg := fmt.Sprintf("signaled %v", ws.Signal())
if ws.CoreDump() {
msg += " (core dumped)"
}
return msg
case ws.Stopped():
msg := fmt.Sprintf("stopped %v", ws.StopSignal())
trap := ws.TrapCause()
if trap != -1 {
msg += fmt.Sprintf(" (trapped %v)", trap)
}
return msg
case ws.Continued():
return "continued"
default:
return fmt.Sprintf("unknown status %v", ws)
}
}
示例5: checkForDeath
// checkForDeath tries to clean up zombies and then checks if the
// process group is empty.
//
// It returns "true" if the answer is yes and there are no grace pings left
//
func (n *nelly) checkForDeath() bool {
// Check if there are any zombies to eat. Process.Wait() doesn't
// support the POSIX WNOHANG for portability reasons, so let's use
// the syscall.Wait4() which is POSIX-only.
var w syscall.WaitStatus
rusage := syscall.Rusage{}
zpid, err := syscall.Wait4(-1, &w, syscall.WNOHANG, &rusage)
if err != nil {
n.Error("Error in Wait4: %s", err.Error())
}
if zpid > 0 {
n.Error("Ate a tasty zombie (pid was %d, status was %d)", zpid, w.ExitStatus())
}
if n.processGroupIsEmpty() {
n.startGracePings--
if n.startGracePings <= 0 {
n.Error("Process group [%d] empty - exiting and hoping init sorts it all out", n.pgid)
return true
} else {
n.Error("Process group [%d] empty - grace pings left [%d]", n.pgid, n.startGracePings)
}
} else {
// We've had a good ping, no more Mr Nice Guy
n.startGracePings = 0
}
return false
}
示例6: waitStatusToError
// waitStatusToError converts syscall.WaitStatus to an Error.
func waitStatusToError(ws syscall.WaitStatus) error {
switch {
case ws.Exited():
es := ws.ExitStatus()
if es == 0 {
return nil
}
return errors.New(fmt.Sprint(es))
case ws.Signaled():
msg := fmt.Sprintf("signaled %v", ws.Signal())
if ws.CoreDump() {
msg += " (core dumped)"
}
return errors.New(msg)
case ws.Stopped():
msg := fmt.Sprintf("stopped %v", ws.StopSignal())
trap := ws.TrapCause()
if trap != -1 {
msg += fmt.Sprintf(" (trapped %v)", trap)
}
return errors.New(msg)
/*
case ws.Continued():
return newUnexitedStateUpdate("continued")
*/
default:
return fmt.Errorf("unknown WaitStatus", ws)
}
}
示例7: Shell
func Shell(f string, v ...interface{}) (retCode int, stdout, stderr string) {
var so, se bytes.Buffer
command := exec.Command("bash", "-c", fmt.Sprintf(f, v...))
command.Stdout = &so
command.Stderr = &se
var waitStatus syscall.WaitStatus
if err := command.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
retCode = waitStatus.ExitStatus()
} else {
// looks like a system error, for example, IO error
panic(err)
}
} else {
waitStatus = command.ProcessState.Sys().(syscall.WaitStatus)
retCode = waitStatus.ExitStatus()
}
stdout = string(so.Bytes())
stderr = string(se.Bytes())
return
}
示例8: ChildWaitingFunc
func ChildWaitingFunc(pid int, sig chan *ChildWaitData) {
var status syscall.WaitStatus
var rusage syscall.Rusage
result := &ChildWaitData{}
for {
wpid, err := syscall.Wait4(pid, &status, syscall.WUNTRACED|syscall.WCONTINUED, &rusage)
if wpid != pid {
continue
}
if status.Exited() {
result.ExitCode = uint32(status.ExitStatus())
break
}
if status.Stopped() {
result.SuccessCode |= EF_STOPPED
result.StopSignal = uint32(status.StopSignal())
syscall.Kill(pid, syscall.SIGKILL)
}
if status.Signaled() {
result.SuccessCode |= EF_KILLED_BY_OTHER
result.KillSignal = uint32(status.Signal())
break
}
if err != nil {
break
}
}
result.RusageCpuUser = time.Nanosecond * time.Duration(rusage.Utime.Nano())
result.RusageCpuKernel = time.Nanosecond * time.Duration(rusage.Stime.Nano())
sig <- result
close(sig)
}
示例9: waitStatusToStateUpdate
// waitStatusToStateUpdate converts syscall.WaitStatus to a StateUpdate.
func waitStatusToStateUpdate(ws syscall.WaitStatus) *stateUpdate {
switch {
case ws.Exited():
es := ws.ExitStatus()
if es == 0 {
return newExitedStateUpdate(ok)
}
return newExitedStateUpdate(newFailure(fmt.Sprint(es)))
case ws.Signaled():
msg := fmt.Sprintf("signaled %v", ws.Signal())
if ws.CoreDump() {
msg += " (core dumped)"
}
return newUnexitedStateUpdate(msg)
case ws.Stopped():
msg := fmt.Sprintf("stopped %v", ws.StopSignal())
trap := ws.TrapCause()
if trap != -1 {
msg += fmt.Sprintf(" (trapped %v)", trap)
}
return newUnexitedStateUpdate(msg)
case ws.Continued():
return newUnexitedStateUpdate("continued")
default:
return newUnexitedStateUpdate(fmt.Sprint("unknown status", ws))
}
}
示例10: babySit
func babySit(process *os.Process) int {
log := logger.New("fn", "babySit")
// Forward all signals to the app
sigchan := make(chan os.Signal, 1)
sigutil.CatchAll(sigchan)
go func() {
for sig := range sigchan {
log.Info("received signal", "type", sig)
if sig == syscall.SIGCHLD {
continue
}
log.Info("forwarding signal to command", "type", sig)
process.Signal(sig)
}
}()
// Wait for the app to exit. Also, as pid 1 it's our job to reap all
// orphaned zombies.
var wstatus syscall.WaitStatus
for {
pid, err := syscall.Wait4(-1, &wstatus, 0, nil)
if err == nil && pid == process.Pid {
break
}
}
if wstatus.Signaled() {
log.Info("command exited due to signal")
return 0
}
return wstatus.ExitStatus()
}
示例11: ConvertOfficeDocToPdf2
func ConvertOfficeDocToPdf2(file string) {
args := []string{"-f", "pdf",
"-eSelectPdfVersion=1",
"-eReduceImageResolution=true",
"-eMaxImageResolution=300",
"-p",
"8200",
"-o",
"~/foo1.pdf",
"~/foo.pptx",
}
cmd := exec.Command("unoconv", args...)
var waitStatus syscall.WaitStatus
if err := cmd.Run(); err != nil {
//fmt.Printf("Error:", err)
// Did the command fail because of an unsuccessful exit code
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
fmt.Printf("Failed: %d", waitStatus.ExitStatus())
}
} else {
// Command was successful
waitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus)
fmt.Printf("Success: %d", waitStatus.ExitStatus())
}
}
示例12: ExecBash
func ExecBash(command string, args []string) (int, string, error) {
cmd := exec.Command(command, args...)
var waitStatus syscall.WaitStatus
//Stdout buffer
cmdOutput := &bytes.Buffer{}
//Attach buffer to command stdout
cmd.Stdout = cmdOutput
//Execute command
err := cmd.Run()
defer func() {
if r := recover(); r != nil {
fmt.Printf("Command:%s execute error\n", command)
}
}()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
return waitStatus.ExitStatus(), "", err
} else {
fmt.Println("something wrong")
}
}
waitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus)
return waitStatus.ExitStatus(), cmdOutput.String(), nil
}
示例13: waitForContainerToExit
func waitForContainerToExit(dir string, containerPid int, signals chan os.Signal) (exitCode int) {
for range signals {
for {
var status syscall.WaitStatus
var rusage syscall.Rusage
wpid, err := syscall.Wait4(-1, &status, syscall.WNOHANG, &rusage)
if err != nil || wpid <= 0 {
break // wait for next SIGCHLD
}
if wpid == containerPid {
exitCode = status.ExitStatus()
if status.Signaled() {
exitCode = 128 + int(status.Signal())
}
ioWg.Wait() // wait for full output to be collected
check(ioutil.WriteFile(filepath.Join(dir, "exitcode"), []byte(strconv.Itoa(exitCode)), 0700))
return exitCode
}
}
}
panic("ran out of signals") // cant happen
}
示例14: reap
func (p *ProcessReaper) reap() {
for {
p.log.Debug("reap")
var status syscall.WaitStatus
var rusage syscall.Rusage
wpid, err := p.wait4(-1, &status, syscall.WNOHANG, &rusage)
if wpid == 0 || (wpid == -1 && err.Error() == "no child processes") {
break
}
if err != nil {
p.log.Error("reaper-wait-error", err, lager.Data{"wpid": wpid})
break
}
p.log.Info("reaped", lager.Data{"pid": wpid, "status": status, "rusage": rusage})
if ch, ok := p.waitChan(wpid); p.monitoredPids[wpid] && ok {
ch <- status.ExitStatus()
p.unmonitorPid(wpid)
p.log.Info("wait-once-sent-exit-status", lager.Data{"pid": wpid, "status": status, "rusage": rusage})
} else {
p.log.Info("wait-once-not-found", lager.Data{"pid": wpid, "status": status, "rusage": rusage})
}
}
}
示例15: Reap
// Called on running checks, to determine if they have finished
// running.
//
// If the Check has not finished executing, returns false.
//
// If the Check has been running for longer than its Timeout,
// a SIGTERM (and failing that a SIGKILL) is issued to forcibly
// terminate the rogue Check process. In either case, this returns
// as if the check has not yet finished, and Reap() will need to be
// called again to fully reap the Check
//
// If the Check has finished execution (on its own, or via forced
// termination), it will return true.
//
// Once complete, some additional meta-stats for the check execution
// are appended to the check output, to be submit up to bolo
func (self *Check) Reap() bool {
pid := self.process.Process.Pid
var ws syscall.WaitStatus
status, err := syscall.Wait4(pid, &ws, syscall.WNOHANG, nil)
if err != nil {
log.Error("Error waiting on check %s[%d]: %s", self.Name, pid, err.Error())
return false
}
if status == 0 {
// self to see if we need to sigkill due to failed sigterm
if time.Now().After(self.started_at.Add(time.Duration(self.Timeout+2) * time.Second)) {
log.Warn("Check %s[%d] has been running too long, sending SIGKILL", self.Name, pid)
if err := syscall.Kill(pid, syscall.SIGKILL); err != nil {
log.Error("Error sending SIGKILL to check %s[%d]: %s", self.Name, pid, err.Error())
}
self.sig_kill = true
}
// self to see if we need to sigterm due to self timeout expiry
if !self.sig_kill && time.Now().After(self.started_at.Add(time.Duration(self.Timeout)*time.Second)) {
log.Warn("Check %s[%d] has been running too long, sending SIGTERM", self.Name, pid)
if err := syscall.Kill(pid, syscall.SIGTERM); err != nil {
log.Error("Error sending SIGTERM to check %s[%d]: %s", self.Name, pid, err.Error())
}
self.sig_term = true
}
return false
}
self.ended_at = time.Now()
self.running = false
self.duration = time.Since(self.started_at)
self.latency = self.started_at.Sub(self.next_run)
self.output = string(self.stdout.Bytes())
self.err_msg = string(self.stderr.Bytes())
if ws.Exited() {
self.rc = ws.ExitStatus()
} else {
log.Debug("Check %s[%d] exited abnormally (signaled/stopped). Setting rc to UNKNOWN", self.Name, pid)
self.rc = UNKNOWN
}
if self.rc > UNKNOWN {
log.Debug("Check %s[%d] returned with an invalid exit code. Setting rc to UNKOWN", self.Name, pid)
self.rc = UNKNOWN
}
self.reschedule()
if self.ended_at.After(self.next_run) {
timeout_triggered := "not reached"
if self.sig_term || self.sig_kill {
timeout_triggered = "reached"
}
log.Warn("Check %s[%d] took %0.3f seconds to run, at interval %d (timeout of %d was %s)",
self.Name, pid, self.duration.Seconds(), self.Every, self.Timeout, timeout_triggered)
}
return true
}