2011-04-07 19:09:10 +02:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2011-12-07 02:11:29 +01:00
|
|
|
"errors"
|
2011-04-07 19:09:10 +02:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2012-03-02 21:01:37 +01:00
|
|
|
"time"
|
2011-04-07 19:09:10 +02:00
|
|
|
)
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// The only signal values guaranteed to be present on all systems
|
|
|
|
// are Interrupt (send the process an interrupt) and Kill (force
|
|
|
|
// the process to exit).
|
|
|
|
var (
|
|
|
|
Interrupt Signal = syscall.Note("interrupt")
|
|
|
|
Kill Signal = syscall.Note("kill")
|
|
|
|
)
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
sysattr := &syscall.ProcAttr{
|
|
|
|
Dir: attr.Dir,
|
|
|
|
Env: attr.Env,
|
2011-09-16 17:47:21 +02:00
|
|
|
Sys: attr.Sys,
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
for _, f := range attr.Files {
|
|
|
|
sysattr.Files = append(sysattr.Files, f.Fd())
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pid, h, e := syscall.StartProcess(name, argv, sysattr)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-04-07 19:09:10 +02:00
|
|
|
return nil, &PathError{"fork/exec", name, e}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newProcess(pid, h), nil
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func (p *Process) writeProcFile(file string, data string) error {
|
|
|
|
f, e := OpenFile("/proc/"+itoa(p.Pid)+"/"+file, O_WRONLY, 0)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, e = f.Write([]byte(data))
|
|
|
|
return e
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) signal(sig Signal) error {
|
2012-10-03 07:27:36 +02:00
|
|
|
if p.done() {
|
2011-12-07 02:11:29 +01:00
|
|
|
return errors.New("os: process already finished")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
if sig == Kill {
|
|
|
|
// Special-case the kill signal since it doesn't use /proc/$pid/note.
|
|
|
|
return p.Kill()
|
|
|
|
}
|
|
|
|
if e := p.writeProcFile("note", sig.String()); e != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return NewSyscallError("signal", e)
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
return nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) kill() error {
|
2012-10-23 06:31:11 +02:00
|
|
|
if e := p.writeProcFile("ctl", "kill"); e != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return NewSyscallError("kill", e)
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
return nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) wait() (ps *ProcessState, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
var waitmsg syscall.Waitmsg
|
|
|
|
|
|
|
|
if p.Pid == -1 {
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, ErrInvalid
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for true {
|
|
|
|
err = syscall.Await(&waitmsg)
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
if err != nil {
|
2011-04-07 19:09:10 +02:00
|
|
|
return nil, NewSyscallError("wait", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if waitmsg.Pid == p.Pid {
|
2012-10-03 07:27:36 +02:00
|
|
|
p.setDone()
|
2011-04-07 19:09:10 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
ps = &ProcessState{
|
|
|
|
pid: waitmsg.Pid,
|
|
|
|
status: &waitmsg,
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
return ps, nil
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) release() error {
|
2011-04-07 19:09:10 +02:00
|
|
|
// NOOP for Plan 9.
|
|
|
|
p.Pid = -1
|
|
|
|
// no need for a finalizer anymore
|
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func findProcess(pid int) (p *Process, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
// NOOP for Plan 9.
|
|
|
|
return newProcess(pid, 0), nil
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
// ProcessState stores information about a process, as reported by Wait.
|
2012-03-02 21:01:37 +01:00
|
|
|
type ProcessState struct {
|
|
|
|
pid int // The process's id.
|
|
|
|
status *syscall.Waitmsg // System-dependent status info.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pid returns the process id of the exited process.
|
|
|
|
func (p *ProcessState) Pid() int {
|
|
|
|
return p.pid
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *ProcessState) exited() bool {
|
2012-03-02 21:01:37 +01:00
|
|
|
return p.status.Exited()
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *ProcessState) success() bool {
|
2012-03-02 21:01:37 +01:00
|
|
|
return p.status.ExitStatus() == 0
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *ProcessState) sys() interface{} {
|
2012-03-02 21:01:37 +01:00
|
|
|
return p.status
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *ProcessState) sysUsage() interface{} {
|
2012-03-02 21:01:37 +01:00
|
|
|
return p.status
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *ProcessState) userTime() time.Duration {
|
2012-03-02 21:01:37 +01:00
|
|
|
return time.Duration(p.status.Time[0]) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *ProcessState) systemTime() time.Duration {
|
2012-03-02 21:01:37 +01:00
|
|
|
return time.Duration(p.status.Time[1]) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProcessState) String() string {
|
|
|
|
if p == nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return "<nil>"
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
return "exit status: " + p.status.Msg
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|