PR target/82855
[official-gcc.git] / libgo / go / os / exec_posix.go
blob9e792b4b34f3504903805e6137c2624d11173e79
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build aix darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
7 package os
9 import (
10 "syscall"
13 // The only signal values guaranteed to be present on all systems
14 // are Interrupt (send the process an interrupt) and Kill (force
15 // the process to exit).
16 var (
17 Interrupt Signal = syscall.SIGINT
18 Kill Signal = syscall.SIGKILL
21 func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
22 // If there is no SysProcAttr (ie. no Chroot or changed
23 // UID/GID), double-check existence of the directory we want
24 // to chdir into. We can make the error clearer this way.
25 if attr != nil && attr.Sys == nil && attr.Dir != "" {
26 if _, err := Stat(attr.Dir); err != nil {
27 pe := err.(*PathError)
28 pe.Op = "chdir"
29 return nil, pe
33 sysattr := &syscall.ProcAttr{
34 Dir: attr.Dir,
35 Env: attr.Env,
36 Sys: attr.Sys,
38 if sysattr.Env == nil {
39 sysattr.Env = Environ()
41 for _, f := range attr.Files {
42 sysattr.Files = append(sysattr.Files, f.Fd())
45 pid, h, e := syscall.StartProcess(name, argv, sysattr)
46 if e != nil {
47 return nil, &PathError{"fork/exec", name, e}
49 return newProcess(pid, h), nil
52 func (p *Process) kill() error {
53 return p.Signal(Kill)
56 // ProcessState stores information about a process, as reported by Wait.
57 type ProcessState struct {
58 pid int // The process's id.
59 status syscall.WaitStatus // System-dependent status info.
60 rusage *syscall.Rusage
63 // Pid returns the process id of the exited process.
64 func (p *ProcessState) Pid() int {
65 return p.pid
68 func (p *ProcessState) exited() bool {
69 return p.status.Exited()
72 func (p *ProcessState) success() bool {
73 return p.status.ExitStatus() == 0
76 func (p *ProcessState) sys() interface{} {
77 return p.status
80 func (p *ProcessState) sysUsage() interface{} {
81 return p.rusage
84 func (p *ProcessState) String() string {
85 if p == nil {
86 return "<nil>"
88 status := p.Sys().(syscall.WaitStatus)
89 res := ""
90 switch {
91 case status.Exited():
92 res = "exit status " + itoa(status.ExitStatus())
93 case status.Signaled():
94 res = "signal: " + status.Signal().String()
95 case status.Stopped():
96 res = "stop signal: " + status.StopSignal().String()
97 if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
98 res += " (trap " + itoa(status.TrapCause()) + ")"
100 case status.Continued():
101 res = "continued"
103 if status.CoreDump() {
104 res += " (core dumped)"
106 return res