exec_unix.go 1.66 KB
Newer Older
1 2 3 4
// 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.

5
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
6

7 8 9
package os

import (
10
	"errors"
11 12
	"runtime"
	"syscall"
13
	"time"
14 15
)

16
func (p *Process) wait() (ps *ProcessState, err error) {
17
	if p.Pid == -1 {
18
		return nil, syscall.EINVAL
19 20
	}
	var status syscall.WaitStatus
21 22
	var rusage syscall.Rusage
	pid1, e := syscall.Wait4(p.Pid, &status, 0, &rusage)
23
	if e != nil {
24 25
		return nil, NewSyscallError("wait", e)
	}
26
	if pid1 != 0 {
27
		p.setDone()
28
	}
29 30 31 32 33 34
	ps = &ProcessState{
		pid:    pid1,
		status: status,
		rusage: &rusage,
	}
	return ps, nil
35 36
}

37 38
var errFinished = errors.New("os: process already finished")

39
func (p *Process) signal(sig Signal) error {
40 41 42
	if p.Pid == -1 {
		return errors.New("os: process already released")
	}
43 44 45 46 47 48
	if p.Pid == 0 {
		return errors.New("os: process not initialized")
	}
	if p.done() {
		return errFinished
	}
49 50 51 52 53
	s, ok := sig.(syscall.Signal)
	if !ok {
		return errors.New("os: unsupported signal type")
	}
	if e := syscall.Kill(p.Pid, s); e != nil {
54 55 56
		if e == syscall.ESRCH {
			return errFinished
		}
57
		return e
58 59 60 61
	}
	return nil
}

62
func (p *Process) release() error {
63 64 65 66 67 68 69
	// NOOP for unix.
	p.Pid = -1
	// no need for a finalizer anymore
	runtime.SetFinalizer(p, nil)
	return nil
}

70
func findProcess(pid int) (p *Process, err error) {
71 72 73
	// NOOP for unix.
	return newProcess(pid, 0), nil
}
74

75
func (p *ProcessState) userTime() time.Duration {
76 77 78
	return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
}

79
func (p *ProcessState) systemTime() time.Duration {
80 81
	return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
}