OSDN Git Service

libgo: Update to weekly.2012-01-20.
[pf3gnuchains/gcc-fork.git] / libgo / go / os / exec_windows.go
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 package os
6
7 import (
8         "errors"
9         "runtime"
10         "syscall"
11         "unsafe"
12 )
13
14 // Wait waits for the Process to exit or stop, and then returns a
15 // Waitmsg describing its status and an error, if any.
16 func (p *Process) Wait(options int) (w *Waitmsg, err error) {
17         s, e := syscall.WaitForSingleObject(syscall.Handle(p.handle), syscall.INFINITE)
18         switch s {
19         case syscall.WAIT_OBJECT_0:
20                 break
21         case syscall.WAIT_FAILED:
22                 return nil, NewSyscallError("WaitForSingleObject", e)
23         default:
24                 return nil, errors.New("os: unexpected result from WaitForSingleObject")
25         }
26         var ec uint32
27         e = syscall.GetExitCodeProcess(syscall.Handle(p.handle), &ec)
28         if e != nil {
29                 return nil, NewSyscallError("GetExitCodeProcess", e)
30         }
31         p.done = true
32         return &Waitmsg{p.Pid, syscall.WaitStatus{s, ec}, new(syscall.Rusage)}, nil
33 }
34
35 // Signal sends a signal to the Process.
36 func (p *Process) Signal(sig Signal) error {
37         if p.done {
38                 return errors.New("os: process already finished")
39         }
40         switch sig.(UnixSignal) {
41         case SIGKILL:
42                 e := syscall.TerminateProcess(syscall.Handle(p.handle), 1)
43                 return NewSyscallError("TerminateProcess", e)
44         }
45         return syscall.Errno(syscall.EWINDOWS)
46 }
47
48 // Release releases any resources associated with the Process.
49 func (p *Process) Release() error {
50         if p.handle == -1 {
51                 return EINVAL
52         }
53         e := syscall.CloseHandle(syscall.Handle(p.handle))
54         if e != nil {
55                 return NewSyscallError("CloseHandle", e)
56         }
57         p.handle = -1
58         // no need for a finalizer anymore
59         runtime.SetFinalizer(p, nil)
60         return nil
61 }
62
63 func findProcess(pid int) (p *Process, err error) {
64         const da = syscall.STANDARD_RIGHTS_READ |
65                 syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE
66         h, e := syscall.OpenProcess(da, false, uint32(pid))
67         if e != nil {
68                 return nil, NewSyscallError("OpenProcess", e)
69         }
70         return newProcess(pid, int(h)), nil
71 }
72
73 func init() {
74         var argc int32
75         cmd := syscall.GetCommandLine()
76         argv, e := syscall.CommandLineToArgv(cmd, &argc)
77         if e != nil {
78                 return
79         }
80         defer syscall.LocalFree(syscall.Handle(uintptr(unsafe.Pointer(argv))))
81         Args = make([]string, argc)
82         for i, v := range (*argv)[:argc] {
83                 Args[i] = string(syscall.UTF16ToString((*v)[:]))
84         }
85 }