OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [os/] [exec_plan9.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
package os
6
 
7
import (
8
        "errors"
9
        "runtime"
10
        "syscall"
11
)
12
 
13
// StartProcess starts a new process with the program, arguments and attributes
14
// specified by name, argv and attr.
15
func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
16
        sysattr := &syscall.ProcAttr{
17
                Dir: attr.Dir,
18
                Env: attr.Env,
19
                Sys: attr.Sys,
20
        }
21
 
22
        // Create array of integer (system) fds.
23
        intfd := make([]int, len(attr.Files))
24
        for i, f := range attr.Files {
25
                if f == nil {
26
                        intfd[i] = -1
27
                } else {
28
                        intfd[i] = f.Fd()
29
                }
30
        }
31
 
32
        sysattr.Files = intfd
33
 
34
        pid, h, e := syscall.StartProcess(name, argv, sysattr)
35
        if e != nil {
36
                return nil, &PathError{"fork/exec", name, e}
37
        }
38
 
39
        return newProcess(pid, h), nil
40
}
41
 
42
// Plan9Note implements the Signal interface on Plan 9.
43
type Plan9Note string
44
 
45
func (note Plan9Note) String() string {
46
        return string(note)
47
}
48
 
49
func (p *Process) Signal(sig Signal) error {
50
        if p.done {
51
                return errors.New("os: process already finished")
52
        }
53
 
54
        f, e := OpenFile("/proc/"+itoa(p.Pid)+"/note", O_WRONLY, 0)
55
        if e != nil {
56
                return NewSyscallError("signal", e)
57
        }
58
        defer f.Close()
59
        _, e = f.Write([]byte(sig.String()))
60
        return e
61
}
62
 
63
// Kill causes the Process to exit immediately.
64
func (p *Process) Kill() error {
65
        f, e := OpenFile("/proc/"+itoa(p.Pid)+"/ctl", O_WRONLY, 0)
66
        if e != nil {
67
                return NewSyscallError("kill", e)
68
        }
69
        defer f.Close()
70
        _, e = f.Write([]byte("kill"))
71
        return e
72
}
73
 
74
// Exec replaces the current process with an execution of the
75
// named binary, with arguments argv and environment envv.
76
// If successful, Exec never returns.  If it fails, it returns an error.
77
// ForkExec is almost always a better way to execute a program.
78
func Exec(name string, argv []string, envv []string) error {
79
        e := syscall.Exec(name, argv, envv)
80
        if e != nil {
81
                return &PathError{"exec", name, e}
82
        }
83
 
84
        return nil
85
}
86
 
87
// Waitmsg stores the information about an exited process as reported by Wait.
88
type Waitmsg struct {
89
        syscall.Waitmsg
90
}
91
 
92
// Wait waits for the Process to exit or stop, and then returns a
93
// Waitmsg describing its status and an error, if any. The options
94
// (WNOHANG etc.) affect the behavior of the Wait call.
95
func (p *Process) Wait(options int) (w *Waitmsg, err error) {
96
        var waitmsg syscall.Waitmsg
97
 
98
        if p.Pid == -1 {
99
                return nil, EINVAL
100
        }
101
 
102
        for true {
103
                err = syscall.Await(&waitmsg)
104
 
105
                if err != nil {
106
                        return nil, NewSyscallError("wait", err)
107
                }
108
 
109
                if waitmsg.Pid == p.Pid {
110
                        p.done = true
111
                        break
112
                }
113
        }
114
 
115
        return &Waitmsg{waitmsg}, nil
116
}
117
 
118
// Wait waits for process pid to exit or stop, and then returns a
119
// Waitmsg describing its status and an error, if any. The options
120
// (WNOHANG etc.) affect the behavior of the Wait call.
121
// Wait is equivalent to calling FindProcess and then Wait
122
// and Release on the result.
123
func Wait(pid int, options int) (w *Waitmsg, err error) {
124
        p, e := FindProcess(pid)
125
        if e != nil {
126
                return nil, e
127
        }
128
        defer p.Release()
129
        return p.Wait(options)
130
}
131
 
132
// Release releases any resources associated with the Process.
133
func (p *Process) Release() error {
134
        // NOOP for Plan 9.
135
        p.Pid = -1
136
        // no need for a finalizer anymore
137
        runtime.SetFinalizer(p, nil)
138
        return nil
139
}
140
 
141
func findProcess(pid int) (p *Process, err error) {
142
        // NOOP for Plan 9.
143
        return newProcess(pid, 0), nil
144
}
145
 
146
func (w *Waitmsg) String() string {
147
        if w == nil {
148
                return ""
149
        }
150
        return "exit status: " + w.Msg
151
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.