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.
4
5// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
6
7package os
8
9import (
10	"errors"
11	"runtime"
12	"syscall"
13	"time"
14)
15
16func (p *Process) wait() (ps *ProcessState, err error) {
17	if p.Pid == -1 {
18		return nil, syscall.EINVAL
19	}
20	var status syscall.WaitStatus
21	var rusage syscall.Rusage
22	pid1, e := syscall.Wait4(p.Pid, &status, 0, &rusage)
23	if e != nil {
24		return nil, NewSyscallError("wait", e)
25	}
26	if pid1 != 0 {
27		p.setDone()
28	}
29	ps = &ProcessState{
30		pid:    pid1,
31		status: status,
32		rusage: &rusage,
33	}
34	return ps, nil
35}
36
37var errFinished = errors.New("os: process already finished")
38
39func (p *Process) signal(sig Signal) error {
40	if p.Pid == -1 {
41		return errors.New("os: process already released")
42	}
43	if p.Pid == 0 {
44		return errors.New("os: process not initialized")
45	}
46	if p.done() {
47		return errFinished
48	}
49	s, ok := sig.(syscall.Signal)
50	if !ok {
51		return errors.New("os: unsupported signal type")
52	}
53	if e := syscall.Kill(p.Pid, s); e != nil {
54		if e == syscall.ESRCH {
55			return errFinished
56		}
57		return e
58	}
59	return nil
60}
61
62func (p *Process) release() error {
63	// NOOP for unix.
64	p.Pid = -1
65	// no need for a finalizer anymore
66	runtime.SetFinalizer(p, nil)
67	return nil
68}
69
70func findProcess(pid int) (p *Process, err error) {
71	// NOOP for unix.
72	return newProcess(pid, 0), nil
73}
74
75func (p *ProcessState) userTime() time.Duration {
76	return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
77}
78
79func (p *ProcessState) systemTime() time.Duration {
80	return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
81}
82