2011-03-17 00:05:44 +01: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.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
2011-03-17 00:05:44 +01:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2012-03-02 21:01:37 +01:00
|
|
|
"time"
|
2011-03-17 00:05:44 +01:00
|
|
|
)
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) wait() (ps *ProcessState, err error) {
|
2011-03-17 00:05:44 +01:00
|
|
|
if p.Pid == -1 {
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
var status syscall.WaitStatus
|
2012-03-02 21:01:37 +01:00
|
|
|
var rusage syscall.Rusage
|
|
|
|
pid1, e := syscall.Wait4(p.Pid, &status, 0, &rusage)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-03-17 00:05:44 +01:00
|
|
|
return nil, NewSyscallError("wait", e)
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
if pid1 != 0 {
|
2012-10-03 07:27:36 +02:00
|
|
|
p.setDone()
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
ps = &ProcessState{
|
|
|
|
pid: pid1,
|
|
|
|
status: status,
|
|
|
|
rusage: &rusage,
|
|
|
|
}
|
|
|
|
return ps, nil
|
2011-03-17 00:05:44 +01: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-03 03:17:34 +01:00
|
|
|
return errors.New("os: process already finished")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
if p.Pid == -1 {
|
|
|
|
return errors.New("os: process already released")
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
s, ok := sig.(syscall.Signal)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("os: unsupported signal type")
|
|
|
|
}
|
|
|
|
if e := syscall.Kill(p.Pid, s); e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return e
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) release() error {
|
2011-03-17 00:05:44 +01:00
|
|
|
// NOOP for unix.
|
|
|
|
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-03-17 00:05:44 +01:00
|
|
|
// NOOP for unix.
|
|
|
|
return newProcess(pid, 0), nil
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
|
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.rusage.Utime.Nano()) * time.Nanosecond
|
|
|
|
}
|
|
|
|
|
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.rusage.Stime.Nano()) * time.Nanosecond
|
|
|
|
}
|