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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StartProcess starts a new process with the program, arguments and attributes
|
|
|
|
// specified by name, argv and attr.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-03 03:17:34 +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
|
|
|
}
|
|
|
|
|
|
|
|
// Create array of integer (system) fds.
|
|
|
|
intfd := make([]int, len(attr.Files))
|
|
|
|
for i, f := range attr.Files {
|
|
|
|
if f == nil {
|
|
|
|
intfd[i] = -1
|
|
|
|
} else {
|
|
|
|
intfd[i] = f.Fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sysattr.Files = intfd
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Plan9Note implements the Signal interface on Plan 9.
|
|
|
|
type Plan9Note string
|
|
|
|
|
|
|
|
func (note Plan9Note) String() string {
|
|
|
|
return string(note)
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *Process) Signal(sig Signal) error {
|
2011-09-16 17:47:21 +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
|
|
|
}
|
|
|
|
|
|
|
|
f, e := OpenFile("/proc/"+itoa(p.Pid)+"/note", O_WRONLY, 0)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return NewSyscallError("signal", e)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, e = f.Write([]byte(sig.String()))
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill causes the Process to exit immediately.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *Process) Kill() error {
|
2011-09-16 17:47:21 +02:00
|
|
|
f, e := OpenFile("/proc/"+itoa(p.Pid)+"/ctl", O_WRONLY, 0)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return NewSyscallError("kill", e)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, e = f.Write([]byte("kill"))
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
// Waitmsg stores the information about an exited process as reported by Wait.
|
2011-09-16 17:47:21 +02:00
|
|
|
type Waitmsg struct {
|
|
|
|
syscall.Waitmsg
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
|
|
|
|
// Wait waits for the Process to exit or stop, and then returns a
|
2011-12-03 03:17:34 +01:00
|
|
|
// Waitmsg describing its status and an error, if any. The options
|
2011-04-07 19:09:10 +02:00
|
|
|
// (WNOHANG etc.) affect the behavior of the Wait call.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *Process) Wait(options int) (w *Waitmsg, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
var waitmsg syscall.Waitmsg
|
|
|
|
|
|
|
|
if p.Pid == -1 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2011-09-16 17:47:21 +02:00
|
|
|
p.done = true
|
2011-04-07 19:09:10 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
return &Waitmsg{waitmsg}, nil
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for process pid to exit or stop, and then returns a
|
2011-12-03 03:17:34 +01:00
|
|
|
// Waitmsg describing its status and an error, if any. The options
|
2011-04-07 19:09:10 +02:00
|
|
|
// (WNOHANG etc.) affect the behavior of the Wait call.
|
|
|
|
// Wait is equivalent to calling FindProcess and then Wait
|
|
|
|
// and Release on the result.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Wait(pid int, options int) (w *Waitmsg, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
p, e := FindProcess(pid)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
defer p.Release()
|
|
|
|
return p.Wait(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release releases any resources associated with the Process.
|
2011-12-03 03:17:34 +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
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func (w *Waitmsg) String() string {
|
|
|
|
if w == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
return "exit status: " + w.Msg
|
|
|
|
}
|