blob: ed97f85e22ff3d8336549f13d9726cc2cb4f4121 [file] [log] [blame]
Alex Brainman4ecebfe2011-02-04 14:41:26 +11001// 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
Dave Cheney7c8280c2014-02-25 09:47:42 -05005// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
Russ Cox27159562011-09-15 16:48:57 -04006
Alex Brainman4ecebfe2011-02-04 14:41:26 +11007package os
8
9import (
Russ Cox08a073a2011-11-01 21:49:08 -040010 "errors"
Alex Brainman4ecebfe2011-02-04 14:41:26 +110011 "runtime"
12 "syscall"
Rob Pikeccacab62012-02-21 14:10:34 +110013 "time"
Alex Brainman4ecebfe2011-02-04 14:41:26 +110014)
15
Russ Cox30db6d42012-03-01 21:56:54 -050016func (p *Process) wait() (ps *ProcessState, err error) {
Alex Brainman4ecebfe2011-02-04 14:41:26 +110017 if p.Pid == -1 {
Rob Pike56069f02012-02-17 10:04:29 +110018 return nil, syscall.EINVAL
Alex Brainman4ecebfe2011-02-04 14:41:26 +110019 }
20 var status syscall.WaitStatus
Rob Pikeccacab62012-02-21 14:10:34 +110021 var rusage syscall.Rusage
22 pid1, e := syscall.Wait4(p.Pid, &status, 0, &rusage)
Russ Coxc017a822011-11-13 22:44:52 -050023 if e != nil {
Alex Brainman4ecebfe2011-02-04 14:41:26 +110024 return nil, NewSyscallError("wait", e)
25 }
Rob Pikeb5a3bd52012-02-20 15:36:08 +110026 if pid1 != 0 {
Dave Cheney122a5582012-08-21 10:41:31 +100027 p.setDone()
Brad Fitzpatrickd53385f2011-07-11 15:47:42 -070028 }
Rob Pikeccacab62012-02-21 14:10:34 +110029 ps = &ProcessState{
30 pid: pid1,
Rob Pike880cda52012-02-23 07:51:49 +110031 status: status,
Rob Pikeccacab62012-02-21 14:10:34 +110032 rusage: &rusage,
33 }
34 return ps, nil
Alex Brainman4ecebfe2011-02-04 14:41:26 +110035}
36
Russ Coxd21b37b2014-10-06 15:49:19 -040037var errFinished = errors.New("os: process already finished")
38
Russ Cox30db6d42012-03-01 21:56:54 -050039func (p *Process) signal(sig Signal) error {
Brad Fitzpatrick07e2b402014-02-28 08:31:12 -080040 if p.Pid == -1 {
41 return errors.New("os: process already released")
42 }
Russ Coxd21b37b2014-10-06 15:49:19 -040043 if p.Pid == 0 {
44 return errors.New("os: process not initialized")
45 }
46 if p.done() {
47 return errFinished
48 }
Russ Cox35586f72012-02-13 13:52:37 -050049 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 {
Russ Coxd21b37b2014-10-06 15:49:19 -040054 if e == syscall.ESRCH {
55 return errFinished
56 }
Russ Coxc017a822011-11-13 22:44:52 -050057 return e
Evan Shaw94b974a2011-06-06 19:53:30 +100058 }
59 return nil
60}
61
Alex Brainmaned238ca2012-03-01 17:36:35 +110062func (p *Process) release() error {
Alex Brainman4ecebfe2011-02-04 14:41:26 +110063 // NOOP for unix.
64 p.Pid = -1
65 // no need for a finalizer anymore
66 runtime.SetFinalizer(p, nil)
67 return nil
68}
69
Alex Brainman994e0642012-01-17 16:51:54 +110070func findProcess(pid int) (p *Process, err error) {
Alex Brainman4ecebfe2011-02-04 14:41:26 +110071 // NOOP for unix.
72 return newProcess(pid, 0), nil
73}
Rob Pikeccacab62012-02-21 14:10:34 +110074
Russ Cox30db6d42012-03-01 21:56:54 -050075func (p *ProcessState) userTime() time.Duration {
Rob Pikeccacab62012-02-21 14:10:34 +110076 return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
77}
78
Russ Cox30db6d42012-03-01 21:56:54 -050079func (p *ProcessState) systemTime() time.Duration {
Rob Pikeccacab62012-02-21 14:10:34 +110080 return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
81}