diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2013-07-16 06:54:42 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2013-07-16 06:54:42 +0000 |
commit | be47d6eceffd2c5dbbc1566d5eea490527fb2bd4 (patch) | |
tree | 0e8fda573576bb4181dba29d0e88380a8c38fafd /libgo/go/os | |
parent | efb30cdeb003fd7c585ee0d7657340086abcbd9e (diff) | |
download | gcc-be47d6eceffd2c5dbbc1566d5eea490527fb2bd4.zip gcc-be47d6eceffd2c5dbbc1566d5eea490527fb2bd4.tar.gz gcc-be47d6eceffd2c5dbbc1566d5eea490527fb2bd4.tar.bz2 |
libgo: Update to Go 1.1.1.
From-SVN: r200974
Diffstat (limited to 'libgo/go/os')
26 files changed, 720 insertions, 84 deletions
diff --git a/libgo/go/os/doc.go b/libgo/go/os/doc.go index c469e58..2cc1753 100644 --- a/libgo/go/os/doc.go +++ b/libgo/go/os/doc.go @@ -79,6 +79,8 @@ func (p *ProcessState) Sys() interface{} { // SysUsage returns system-dependent resource usage information about // the exited process. Convert it to the appropriate underlying // type, such as *syscall.Rusage on Unix, to access its contents. +// (On Unix, *syscall.Rusage matches struct rusage as defined in the +// getrusage(2) manual page.) func (p *ProcessState) SysUsage() interface{} { return p.sysUsage() } diff --git a/libgo/go/os/env_unix_test.go b/libgo/go/os/env_unix_test.go new file mode 100644 index 0000000..7eb4dc0 --- /dev/null +++ b/libgo/go/os/env_unix_test.go @@ -0,0 +1,30 @@ +// Copyright 2013 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. + +// +build darwin freebsd linux netbsd openbsd + +package os_test + +import ( + . "os" + "testing" +) + +var setenvEinvalTests = []struct { + k, v string +}{ + {"", ""}, // empty key + {"k=v", ""}, // '=' in key + {"\x00", ""}, // '\x00' in key + {"k", "\x00"}, // '\x00' in value +} + +func TestSetenvUnixEinval(t *testing.T) { + for _, tt := range setenvEinvalTests { + err := Setenv(tt.k, tt.v) + if err == nil { + t.Errorf(`Setenv(%q, %q) == nil, want error`, tt.k, tt.v) + } + } +} diff --git a/libgo/go/os/exec/exec.go b/libgo/go/os/exec/exec.go index 8368491..a3bbcf3 100644 --- a/libgo/go/os/exec/exec.go +++ b/libgo/go/os/exec/exec.go @@ -235,6 +235,8 @@ func (c *Cmd) Run() error { // Start starts the specified command but does not wait for it to complete. func (c *Cmd) Start() error { if c.err != nil { + c.closeDescriptors(c.closeAfterStart) + c.closeDescriptors(c.closeAfterWait) return c.err } if c.Process != nil { diff --git a/libgo/go/os/exec/exec_test.go b/libgo/go/os/exec/exec_test.go index ff8954f..fa7e88c 100644 --- a/libgo/go/os/exec/exec_test.go +++ b/libgo/go/os/exec/exec_test.go @@ -14,17 +14,23 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "runtime" "strconv" "strings" "testing" + "time" ) func helperCommand(s ...string) *Cmd { cs := []string{"-test.run=TestHelperProcess", "--"} cs = append(cs, s...) cmd := Command(os.Args[0], cs...) - cmd.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + path := os.Getenv("LD_LIBRARY_PATH") + if path != "" { + cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+path) + } return cmd } @@ -83,10 +89,16 @@ func TestNoExistBinary(t *testing.T) { func TestExitStatus(t *testing.T) { // Test that exit values are returned correctly - err := helperCommand("exit", "42").Run() + cmd := helperCommand("exit", "42") + err := cmd.Run() + want := "exit status 42" + switch runtime.GOOS { + case "plan9": + want = fmt.Sprintf("exit status: '%s %d: 42'", filepath.Base(cmd.Path), cmd.ProcessState.Pid()) + } if werr, ok := err.(*ExitError); ok { - if s, e := werr.Error(), "exit status 42"; s != e { - t.Errorf("from exit 42 got exit %q, want %q", s, e) + if s := werr.Error(); s != want { + t.Errorf("from exit 42 got exit %q, want %q", s, want) } } else { t.Fatalf("expected *ExitError from exit 42; got %T: %v", err, err) @@ -144,8 +156,141 @@ func TestPipes(t *testing.T) { check("Wait", err) } +// Issue 5071 +func TestPipeLookPathLeak(t *testing.T) { + fd0 := numOpenFDS(t) + for i := 0; i < 4; i++ { + cmd := Command("something-that-does-not-exist-binary") + cmd.StdoutPipe() + cmd.StderrPipe() + cmd.StdinPipe() + if err := cmd.Run(); err == nil { + t.Fatal("unexpected success") + } + } + fdGrowth := numOpenFDS(t) - fd0 + if fdGrowth > 2 { + t.Errorf("leaked %d fds; want ~0", fdGrowth) + } +} + +func numOpenFDS(t *testing.T) int { + lsof, err := Command("lsof", "-n", "-p", strconv.Itoa(os.Getpid())).Output() + if err != nil { + t.Skip("skipping test; error finding or running lsof") + return 0 + } + return bytes.Count(lsof, []byte("\n")) +} + var testedAlreadyLeaked = false +// basefds returns the number of expected file descriptors +// to be present in a process at start. +func basefds() uintptr { + n := os.Stderr.Fd() + 1 + + // Go runtime for 32-bit Plan 9 requires that /dev/bintime + // be kept open. + // See ../../runtime/time_plan9_386.c:/^runtime·nanotime + if runtime.GOOS == "plan9" && runtime.GOARCH == "386" { + n++ + } + return n +} + +func TestExtraFilesFDShuffle(t *testing.T) { + t.Skip("TODO: TestExtraFilesFDShuffle is too non-portable; skipping") + + // syscall.StartProcess maps all the FDs passed to it in + // ProcAttr.Files (the concatenation of stdin,stdout,stderr and + // ExtraFiles) into consecutive FDs in the child, that is: + // Files{11, 12, 6, 7, 9, 3} should result in the file + // represented by FD 11 in the parent being made available as 0 + // in the child, 12 as 1, etc. + // + // We want to test that FDs in the child do not get overwritten + // by one another as this shuffle occurs. The original implementation + // was buggy in that in some data dependent cases it would ovewrite + // stderr in the child with one of the ExtraFile members. + // Testing for this case is difficult because it relies on using + // the same FD values as that case. In particular, an FD of 3 + // must be at an index of 4 or higher in ProcAttr.Files and + // the FD of the write end of the Stderr pipe (as obtained by + // StderrPipe()) must be the same as the size of ProcAttr.Files; + // therefore we test that the read end of this pipe (which is what + // is returned to the parent by StderrPipe() being one less than + // the size of ProcAttr.Files, i.e. 3+len(cmd.ExtraFiles). + // + // Moving this test case around within the overall tests may + // affect the FDs obtained and hence the checks to catch these cases. + npipes := 2 + c := helperCommand("extraFilesAndPipes", strconv.Itoa(npipes+1)) + rd, wr, _ := os.Pipe() + defer rd.Close() + if rd.Fd() != 3 { + t.Errorf("bad test value for test pipe: fd %d", rd.Fd()) + } + stderr, _ := c.StderrPipe() + wr.WriteString("_LAST") + wr.Close() + + pipes := make([]struct { + r, w *os.File + }, npipes) + data := []string{"a", "b"} + + for i := 0; i < npipes; i++ { + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("unexpected error creating pipe: %s", err) + } + pipes[i].r = r + pipes[i].w = w + w.WriteString(data[i]) + c.ExtraFiles = append(c.ExtraFiles, pipes[i].r) + defer func() { + r.Close() + w.Close() + }() + } + // Put fd 3 at the end. + c.ExtraFiles = append(c.ExtraFiles, rd) + + stderrFd := int(stderr.(*os.File).Fd()) + if stderrFd != ((len(c.ExtraFiles) + 3) - 1) { + t.Errorf("bad test value for stderr pipe") + } + + expected := "child: " + strings.Join(data, "") + "_LAST" + + err := c.Start() + if err != nil { + t.Fatalf("Run: %v", err) + } + ch := make(chan string, 1) + go func(ch chan string) { + buf := make([]byte, 512) + n, err := stderr.Read(buf) + if err != nil { + t.Fatalf("Read: %s", err) + ch <- err.Error() + } else { + ch <- string(buf[:n]) + } + close(ch) + }(ch) + select { + case m := <-ch: + if m != expected { + t.Errorf("Read: '%s' not '%s'", m, expected) + } + case <-time.After(5 * time.Second): + t.Errorf("Read timedout") + } + c.Wait() +} + func TestExtraFiles(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("no operating system support; skipping") @@ -155,7 +300,7 @@ func TestExtraFiles(t *testing.T) { // our environment. if !testedAlreadyLeaked { testedAlreadyLeaked = true - for fd := os.Stderr.Fd() + 1; fd <= 101; fd++ { + for fd := basefds(); fd <= 101; fd++ { err := os.NewFile(fd, "").Close() if err == nil { t.Logf("Something already leaked - closed fd %d", fd) @@ -209,13 +354,16 @@ func TestExtraFiles(t *testing.T) { } c := helperCommand("read3") + var stdout, stderr bytes.Buffer + c.Stdout = &stdout + c.Stderr = &stderr c.ExtraFiles = []*os.File{tf} - bs, err := c.CombinedOutput() + err = c.Run() if err != nil { - t.Fatalf("CombinedOutput: %v; output %q", err, bs) + t.Fatalf("Run: %v; stdout %q, stderr %q", err, stdout.Bytes(), stderr.Bytes()) } - if string(bs) != text { - t.Errorf("got %q; want %q", string(bs), text) + if stdout.String() != text { + t.Errorf("got stdout %q, stderr %q; want %q on stdout", stdout.String(), stderr.String(), text) } } @@ -265,6 +413,13 @@ func TestExtraFilesRace(t *testing.T) { } la.Close() lb.Close() + for _, f := range ca.ExtraFiles { + f.Close() + } + for _, f := range cb.ExtraFiles { + f.Close() + } + } } @@ -360,7 +515,7 @@ func TestHelperProcess(*testing.T) { default: // Now verify that there are no other open fds. var files []*os.File - for wantfd := os.Stderr.Fd() + 2; wantfd <= 100; wantfd++ { + for wantfd := basefds() + 1; wantfd <= 100; wantfd++ { f, err := os.Open(os.Args[0]) if err != nil { fmt.Printf("error opening file with expected fd %d: %v", wantfd, err) @@ -384,7 +539,7 @@ func TestHelperProcess(*testing.T) { // what we do with fd3 as long as we refer to it; // closing it is the easy choice. fd3.Close() - os.Stderr.Write(bs) + os.Stdout.Write(bs) case "exit": n, _ := strconv.Atoi(args[0]) os.Exit(n) @@ -398,6 +553,35 @@ func TestHelperProcess(*testing.T) { } } os.Exit(0) + case "extraFilesAndPipes": + n, _ := strconv.Atoi(args[0]) + pipes := make([]*os.File, n) + for i := 0; i < n; i++ { + pipes[i] = os.NewFile(uintptr(3+i), strconv.Itoa(i)) + } + response := "" + for i, r := range pipes { + ch := make(chan string, 1) + go func(c chan string) { + buf := make([]byte, 10) + n, err := r.Read(buf) + if err != nil { + fmt.Fprintf(os.Stderr, "Child: read error: %v on pipe %d\n", err, i) + os.Exit(1) + } + c <- string(buf[:n]) + close(c) + }(ch) + select { + case m := <-ch: + response = response + m + case <-time.After(5 * time.Second): + fmt.Fprintf(os.Stderr, "Child: Timeout reading from pipe: %d\n", i) + os.Exit(1) + } + } + fmt.Fprintf(os.Stderr, "child: %s", response) + os.Exit(0) default: fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd) os.Exit(2) diff --git a/libgo/go/os/exec/lp_plan9.go b/libgo/go/os/exec/lp_plan9.go index 0e229e0..6846a35 100644 --- a/libgo/go/os/exec/lp_plan9.go +++ b/libgo/go/os/exec/lp_plan9.go @@ -8,7 +8,6 @@ import ( "errors" "os" "strings" - "syscall" ) // ErrNotFound is the error resulting if a path search failed to find an executable file. @@ -22,7 +21,7 @@ func findExecutable(file string) error { if m := d.Mode(); !m.IsDir() && m&0111 != 0 { return nil } - return syscall.EPERM + return os.ErrPermission } // LookPath searches for an executable binary named file diff --git a/libgo/go/os/exec/lp_unix.go b/libgo/go/os/exec/lp_unix.go index 2163221..1d1ec07 100644 --- a/libgo/go/os/exec/lp_unix.go +++ b/libgo/go/os/exec/lp_unix.go @@ -42,6 +42,9 @@ func LookPath(file string) (string, error) { return "", &Error{file, err} } pathenv := os.Getenv("PATH") + if pathenv == "" { + return "", &Error{file, ErrNotFound} + } for _, dir := range strings.Split(pathenv, ":") { if dir == "" { // Unix shell semantics: path element "" means "." diff --git a/libgo/go/os/exec/lp_unix_test.go b/libgo/go/os/exec/lp_unix_test.go new file mode 100644 index 0000000..625d784 --- /dev/null +++ b/libgo/go/os/exec/lp_unix_test.go @@ -0,0 +1,55 @@ +// Copyright 2013 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. + +// +build darwin freebsd linux netbsd openbsd + +package exec + +import ( + "io/ioutil" + "os" + "testing" +) + +func TestLookPathUnixEmptyPath(t *testing.T) { + tmp, err := ioutil.TempDir("", "TestLookPathUnixEmptyPath") + if err != nil { + t.Fatal("TempDir failed: ", err) + } + defer os.RemoveAll(tmp) + wd, err := os.Getwd() + if err != nil { + t.Fatal("Getwd failed: ", err) + } + err = os.Chdir(tmp) + if err != nil { + t.Fatal("Chdir failed: ", err) + } + defer os.Chdir(wd) + + f, err := os.OpenFile("exec_me", os.O_CREATE|os.O_EXCL, 0700) + if err != nil { + t.Fatal("OpenFile failed: ", err) + } + err = f.Close() + if err != nil { + t.Fatal("Close failed: ", err) + } + + pathenv := os.Getenv("PATH") + defer os.Setenv("PATH", pathenv) + + err = os.Setenv("PATH", "") + if err != nil { + t.Fatal("Setenv failed: ", err) + } + + path, err := LookPath("exec_me") + if err == nil { + t.Fatal("LookPath found exec_me in empty $PATH") + } + if path != "" { + t.Fatalf("LookPath path == %q when err != nil", path) + } +} diff --git a/libgo/go/os/exec/lp_windows.go b/libgo/go/os/exec/lp_windows.go index d8351d7..7c7289b 100644 --- a/libgo/go/os/exec/lp_windows.go +++ b/libgo/go/os/exec/lp_windows.go @@ -72,7 +72,7 @@ func LookPath(file string) (f string, err error) { return } if pathenv := os.Getenv(`PATH`); pathenv != `` { - for _, dir := range strings.Split(pathenv, `;`) { + for _, dir := range splitList(pathenv) { if f, err = findExecutable(dir+`\`+file, exts); err == nil { return } @@ -80,3 +80,36 @@ func LookPath(file string) (f string, err error) { } return ``, &Error{file, ErrNotFound} } + +func splitList(path string) []string { + // The same implementation is used in SplitList in path/filepath; + // consider changing path/filepath when changing this. + + if path == "" { + return []string{} + } + + // Split path, respecting but preserving quotes. + list := []string{} + start := 0 + quo := false + for i := 0; i < len(path); i++ { + switch c := path[i]; { + case c == '"': + quo = !quo + case c == os.PathListSeparator && !quo: + list = append(list, path[start:i]) + start = i + 1 + } + } + list = append(list, path[start:]) + + // Remove quotes. + for i, s := range list { + if strings.Contains(s, `"`) { + list[i] = strings.Replace(s, `"`, ``, -1) + } + } + + return list +} diff --git a/libgo/go/os/exec_posix.go b/libgo/go/os/exec_posix.go index 2ced4d6..f7b10f3 100644 --- a/libgo/go/os/exec_posix.go +++ b/libgo/go/os/exec_posix.go @@ -118,9 +118,9 @@ func (p *ProcessState) String() string { case status.Exited(): res = "exit status " + itod(status.ExitStatus()) case status.Signaled(): - res = "signal " + itod(int(status.Signal())) + res = "signal: " + status.Signal().String() case status.Stopped(): - res = "stop signal " + itod(int(status.StopSignal())) + res = "stop signal: " + status.StopSignal().String() if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 { res += " (trap " + itod(status.TrapCause()) + ")" } diff --git a/libgo/go/os/file_plan9.go b/libgo/go/os/file_plan9.go index fb2f234..d6d39a8 100644 --- a/libgo/go/os/file_plan9.go +++ b/libgo/go/os/file_plan9.go @@ -104,7 +104,6 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { append = true } - syscall.ForkLock.RLock() if (create && trunc) || excl { fd, e = syscall.Create(name, flag, syscallMode(perm)) } else { @@ -117,7 +116,6 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { } } } - syscall.ForkLock.RUnlock() if e != nil { return nil, &PathError{"open", name, e} @@ -246,13 +244,23 @@ func (f *File) pread(b []byte, off int64) (n int, err error) { // write writes len(b) bytes to the File. // It returns the number of bytes written and an error, if any. +// Since Plan 9 preserves message boundaries, never allow +// a zero-byte write. func (f *File) write(b []byte) (n int, err error) { + if len(b) == 0 { + return 0, nil + } return syscall.Write(f.fd, b) } // pwrite writes len(b) bytes to the File starting at byte offset off. // It returns the number of bytes written and an error, if any. +// Since Plan 9 preserves message boundaries, never allow +// a zero-byte write. func (f *File) pwrite(b []byte, off int64) (n int, err error) { + if len(b) == 0 { + return 0, nil + } return syscall.Pwrite(f.fd, b, off) } @@ -300,7 +308,7 @@ func Rename(oldname, newname string) error { d.Null() d.Name = newname - var buf [syscall.STATFIXLEN]byte + buf := make([]byte, syscall.STATFIXLEN+len(d.Name)) n, err := d.Marshal(buf[:]) if err != nil { return &PathError{"rename", oldname, err} diff --git a/libgo/go/os/file_posix.go b/libgo/go/os/file_posix.go index b979fed..3df43fe 100644 --- a/libgo/go/os/file_posix.go +++ b/libgo/go/os/file_posix.go @@ -46,8 +46,6 @@ func Readlink(name string) (string, error) { return string(b[0:n]), nil } } - // Silence 6g. - return "", nil } // Rename renames a file. diff --git a/libgo/go/os/file_unix.go b/libgo/go/os/file_unix.go index 38adcf9..993a438 100644 --- a/libgo/go/os/file_unix.go +++ b/libgo/go/os/file_unix.go @@ -209,7 +209,6 @@ func (f *File) write(b []byte) (n int, err error) { return n, err } - panic("not reached") } // pwrite writes len(b) bytes to the File starting at byte offset off. diff --git a/libgo/go/os/getwd.go b/libgo/go/os/getwd.go index 81d8fed..0235c5d 100644 --- a/libgo/go/os/getwd.go +++ b/libgo/go/os/getwd.go @@ -5,9 +5,15 @@ package os import ( + "sync" "syscall" ) +var getwdCache struct { + sync.Mutex + dir string +} + // Getwd returns a rooted path name corresponding to the // current directory. If the current directory can be // reached via multiple paths (due to symbolic links), @@ -35,6 +41,17 @@ func Getwd() (pwd string, err error) { } } + // Apply same kludge but to cached dir instead of $PWD. + getwdCache.Lock() + pwd = getwdCache.dir + getwdCache.Unlock() + if len(pwd) > 0 { + d, err := Stat(pwd) + if err == nil && SameFile(dot, d) { + return pwd, nil + } + } + // Root is a special case because it has no parent // and ends in a slash. root, err := Stat("/") @@ -73,8 +90,6 @@ func Getwd() (pwd string, err error) { } } } - fd.Close() - return "", ErrNotExist Found: pd, err := fd.Stat() @@ -88,5 +103,11 @@ func Getwd() (pwd string, err error) { // Set up for next round. dot = pd } + + // Save answer as hint to avoid the expensive path next time. + getwdCache.Lock() + getwdCache.dir = pwd + getwdCache.Unlock() + return pwd, nil } diff --git a/libgo/go/os/os_test.go b/libgo/go/os/os_test.go index 560f7a0..40ca44d 100644 --- a/libgo/go/os/os_test.go +++ b/libgo/go/os/os_test.go @@ -39,7 +39,6 @@ var sysdir = func() (sd *sysDir) { sd = &sysDir{ Getenv("SystemRoot") + "\\system32\\drivers\\etc", []string{ - "hosts", "networks", "protocol", "services", diff --git a/libgo/go/os/signal/signal.go b/libgo/go/os/signal/signal.go index dfdcf40..3004275 100644 --- a/libgo/go/os/signal/signal.go +++ b/libgo/go/os/signal/signal.go @@ -5,7 +5,7 @@ // Package signal implements access to incoming signals. package signal -// BUG(rsc): This package is not yet implemented on Plan 9 and Windows. +// BUG(rsc): This package is not yet implemented on Plan 9. import ( "os" @@ -14,13 +14,20 @@ import ( var handlers struct { sync.Mutex - list []handler + m map[chan<- os.Signal]*handler + ref [numSig]int64 } type handler struct { - c chan<- os.Signal - sig os.Signal - all bool + mask [(numSig + 31) / 32]uint32 +} + +func (h *handler) want(sig int) bool { + return (h.mask[sig/32]>>uint(sig&31))&1 != 0 +} + +func (h *handler) set(sig int) { + h.mask[sig/32] |= 1 << uint(sig&31) } // Notify causes package signal to relay incoming signals to c. @@ -32,6 +39,13 @@ type handler struct { // signal rate. For a channel used for notification of just one signal value, // a buffer of size 1 is sufficient. // +// It is allowed to call Notify multiple times with the same channel: +// each call expands the set of signals sent to that channel. +// The only way to remove signals from the set is to call Stop. +// +// It is allowed to call Notify multiple times with different channels +// and the same signals: each channel receives copies of incoming +// signals independently. func Notify(c chan<- os.Signal, sig ...os.Signal) { if c == nil { panic("os/signal: Notify using nil channel") @@ -39,32 +53,77 @@ func Notify(c chan<- os.Signal, sig ...os.Signal) { handlers.Lock() defer handlers.Unlock() + + h := handlers.m[c] + if h == nil { + if handlers.m == nil { + handlers.m = make(map[chan<- os.Signal]*handler) + } + h = new(handler) + handlers.m[c] = h + } + + add := func(n int) { + if n < 0 { + return + } + if !h.want(n) { + h.set(n) + if handlers.ref[n] == 0 { + enableSignal(n) + } + handlers.ref[n]++ + } + } + if len(sig) == 0 { - enableSignal(nil) - handlers.list = append(handlers.list, handler{c: c, all: true}) + for n := 0; n < numSig; n++ { + add(n) + } } else { for _, s := range sig { - // We use nil as a special wildcard value for enableSignal, - // so filter it out of the list of arguments. This is safe because - // we will never get an incoming nil signal, so discarding the - // registration cannot affect the observed behavior. - if s != nil { - enableSignal(s) - handlers.list = append(handlers.list, handler{c: c, sig: s}) + add(signum(s)) + } + } +} + +// Stop causes package signal to stop relaying incoming signals to c. +// It undoes the effect of all prior calls to Notify using c. +// When Stop returns, it is guaranteed that c will receive no more signals. +func Stop(c chan<- os.Signal) { + handlers.Lock() + defer handlers.Unlock() + + h := handlers.m[c] + if h == nil { + return + } + delete(handlers.m, c) + + for n := 0; n < numSig; n++ { + if h.want(n) { + handlers.ref[n]-- + if handlers.ref[n] == 0 { + disableSignal(n) } } } } func process(sig os.Signal) { + n := signum(sig) + if n < 0 { + return + } + handlers.Lock() defer handlers.Unlock() - for _, h := range handlers.list { - if h.all || h.sig == sig { + for c, h := range handlers.m { + if h.want(n) { // send but do not block for it select { - case h.c <- sig: + case c <- sig: default: } } diff --git a/libgo/go/os/signal/signal_stub.go b/libgo/go/os/signal/signal_stub.go index fc227cf4c..d0a6935 100644 --- a/libgo/go/os/signal/signal_stub.go +++ b/libgo/go/os/signal/signal_stub.go @@ -8,4 +8,10 @@ package signal import "os" -func enableSignal(sig os.Signal) {} +const numSig = 0 + +func signum(sig os.Signal) int { return -1 } + +func disableSignal(int) {} + +func enableSignal(int) {} diff --git a/libgo/go/os/signal/signal_test.go b/libgo/go/os/signal/signal_test.go index 509b273..d138333 100644 --- a/libgo/go/os/signal/signal_test.go +++ b/libgo/go/os/signal/signal_test.go @@ -7,15 +7,17 @@ package signal import ( + "flag" + "io/ioutil" "os" + "os/exec" "runtime" + "strconv" "syscall" "testing" "time" ) -const sighup = syscall.SIGHUP - func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) { select { case s := <-c: @@ -27,15 +29,17 @@ func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) { } } +// Test that basic signal handling works. func TestSignal(t *testing.T) { // Ask for SIGHUP c := make(chan os.Signal, 1) - Notify(c, sighup) + Notify(c, syscall.SIGHUP) + defer Stop(c) t.Logf("sighup...") // Send this process a SIGHUP - syscall.Kill(syscall.Getpid(), sighup) - waitSig(t, c, sighup) + syscall.Kill(syscall.Getpid(), syscall.SIGHUP) + waitSig(t, c, syscall.SIGHUP) // Ask for everything we can get. c1 := make(chan os.Signal, 1) @@ -71,6 +75,7 @@ func TestStress(t *testing.T) { go func() { sig := make(chan os.Signal, 1) Notify(sig, syscall.SIGUSR1) + defer Stop(sig) Loop: for { select { @@ -98,4 +103,106 @@ func TestStress(t *testing.T) { close(done) <-finished <-finished + // When run with 'go test -cpu=1,2,4' SIGUSR1 from this test can slip + // into subsequent TestSignal() causing failure. + // Sleep for a while to reduce the possibility of the failure. + time.Sleep(10 * time.Millisecond) +} + +var sendUncaughtSighup = flag.Int("send_uncaught_sighup", 0, "send uncaught SIGHUP during TestStop") + +// Test that Stop cancels the channel's registrations. +func TestStop(t *testing.T) { + sigs := []syscall.Signal{ + syscall.SIGWINCH, + syscall.SIGHUP, + } + + for _, sig := range sigs { + // Send the signal. + // If it's SIGWINCH, we should not see it. + // If it's SIGHUP, maybe we'll die. Let the flag tell us what to do. + if sig != syscall.SIGHUP || *sendUncaughtSighup == 1 { + syscall.Kill(syscall.Getpid(), sig) + } + time.Sleep(10 * time.Millisecond) + + // Ask for signal + c := make(chan os.Signal, 1) + Notify(c, sig) + defer Stop(c) + + // Send this process that signal + syscall.Kill(syscall.Getpid(), sig) + waitSig(t, c, sig) + + Stop(c) + select { + case s := <-c: + t.Fatalf("unexpected signal %v", s) + case <-time.After(10 * time.Millisecond): + // nothing to read - good + } + + // Send the signal. + // If it's SIGWINCH, we should not see it. + // If it's SIGHUP, maybe we'll die. Let the flag tell us what to do. + if sig != syscall.SIGHUP || *sendUncaughtSighup == 2 { + syscall.Kill(syscall.Getpid(), sig) + } + + select { + case s := <-c: + t.Fatalf("unexpected signal %v", s) + case <-time.After(10 * time.Millisecond): + // nothing to read - good + } + } +} + +// Test that when run under nohup, an uncaught SIGHUP does not kill the program, +// but a +func TestNohup(t *testing.T) { + // Ugly: ask for SIGHUP so that child will not have no-hup set + // even if test is running under nohup environment. + // We have no intention of reading from c. + c := make(chan os.Signal, 1) + Notify(c, syscall.SIGHUP) + + // When run without nohup, the test should crash on an uncaught SIGHUP. + // When run under nohup, the test should ignore uncaught SIGHUPs, + // because the runtime is not supposed to be listening for them. + // Either way, TestStop should still be able to catch them when it wants them + // and then when it stops wanting them, the original behavior should resume. + // + // send_uncaught_sighup=1 sends the SIGHUP before starting to listen for SIGHUPs. + // send_uncaught_sighup=2 sends the SIGHUP after no longer listening for SIGHUPs. + // + // Both should fail without nohup and succeed with nohup. + + for i := 1; i <= 2; i++ { + out, err := exec.Command(os.Args[0], "-test.run=TestStop", "-send_uncaught_sighup="+strconv.Itoa(i)).CombinedOutput() + if err == nil { + t.Fatalf("ran test with -send_uncaught_sighup=%d and it succeeded: expected failure.\nOutput:\n%s", i, out) + } + } + + Stop(c) + + // Again, this time with nohup, assuming we can find it. + _, err := os.Stat("/usr/bin/nohup") + if err != nil { + t.Skip("cannot find nohup; skipping second half of test") + } + + for i := 1; i <= 2; i++ { + os.Remove("nohup.out") + out, err := exec.Command("/usr/bin/nohup", os.Args[0], "-test.run=TestStop", "-send_uncaught_sighup="+strconv.Itoa(i)).CombinedOutput() + + data, _ := ioutil.ReadFile("nohup.out") + os.Remove("nohup.out") + if err != nil { + t.Fatalf("ran test with -send_uncaught_sighup=%d under nohup and it failed: expected success.\nError: %v\nOutput:\n%s%s", i, err, out, data) + } + } } diff --git a/libgo/go/os/signal/signal_unix.go b/libgo/go/os/signal/signal_unix.go index 20ee5f2..6b4c8ab 100644 --- a/libgo/go/os/signal/signal_unix.go +++ b/libgo/go/os/signal/signal_unix.go @@ -12,6 +12,7 @@ import ( ) // In assembly. +func signal_disable(uint32) func signal_enable(uint32) func signal_recv() uint32 @@ -26,13 +27,27 @@ func init() { go loop() } -func enableSignal(sig os.Signal) { +const ( + numSig = 65 // max across all systems +) + +func signum(sig os.Signal) int { switch sig := sig.(type) { - case nil: - signal_enable(^uint32(0)) case syscall.Signal: - signal_enable(uint32(sig)) + i := int(sig) + if i < 0 || i >= numSig { + return -1 + } + return i default: - // Can ignore: this signal (whatever it is) will never come in. + return -1 } } + +func enableSignal(sig int) { + signal_enable(uint32(sig)) +} + +func disableSignal(sig int) { + signal_disable(uint32(sig)) +} diff --git a/libgo/go/os/stat.go b/libgo/go/os/stat.go index 01afa39..9758d33 100644 --- a/libgo/go/os/stat.go +++ b/libgo/go/os/stat.go @@ -9,9 +9,9 @@ import ( "time" ) -func sameFile(sys1, sys2 interface{}) bool { - stat1 := sys1.(*syscall.Stat_t) - stat2 := sys2.(*syscall.Stat_t) +func sameFile(fs1, fs2 *fileStat) bool { + stat1 := fs1.sys.(*syscall.Stat_t) + stat2 := fs2.sys.(*syscall.Stat_t) return stat1.Dev == stat2.Dev && stat1.Ino == stat2.Ino } diff --git a/libgo/go/os/stat_atim.go b/libgo/go/os/stat_atim.go index 00506b2..605c1d9 100644 --- a/libgo/go/os/stat_atim.go +++ b/libgo/go/os/stat_atim.go @@ -9,9 +9,9 @@ import ( "time" ) -func sameFile(sys1, sys2 interface{}) bool { - stat1 := sys1.(*syscall.Stat_t) - stat2 := sys2.(*syscall.Stat_t) +func sameFile(fs1, fs2 *fileStat) bool { + stat1 := fs1.sys.(*syscall.Stat_t) + stat2 := fs2.sys.(*syscall.Stat_t) return stat1.Dev == stat2.Dev && stat1.Ino == stat2.Ino } diff --git a/libgo/go/os/stat_atimespec.go b/libgo/go/os/stat_atimespec.go index 6ba84f4..2ffb60f 100644 --- a/libgo/go/os/stat_atimespec.go +++ b/libgo/go/os/stat_atimespec.go @@ -9,9 +9,9 @@ import ( "time" ) -func sameFile(sys1, sys2 interface{}) bool { - stat1 := sys1.(*syscall.Stat_t) - stat2 := sys2.(*syscall.Stat_t) +func sameFile(fs1, fs2 *fileStat) bool { + stat1 := fs1.sys.(*syscall.Stat_t) + stat2 := fs2.sys.(*syscall.Stat_t) return stat1.Dev == stat2.Dev && stat1.Ino == stat2.Ino } diff --git a/libgo/go/os/stat_plan9.go b/libgo/go/os/stat_plan9.go index 6822cc0..25c9a8c 100644 --- a/libgo/go/os/stat_plan9.go +++ b/libgo/go/os/stat_plan9.go @@ -9,9 +9,9 @@ import ( "time" ) -func sameFile(sys1, sys2 interface{}) bool { - a := sys1.(*syscall.Dir) - b := sys2.(*syscall.Dir) +func sameFile(fs1, fs2 *fileStat) bool { + a := fs1.sys.(*syscall.Dir) + b := fs2.sys.(*syscall.Dir) return a.Qid.Path == b.Qid.Path && a.Type == b.Type && a.Dev == b.Dev } diff --git a/libgo/go/os/stat_solaris.go b/libgo/go/os/stat_solaris.go index 51a2f71e..e4622b9 100644 --- a/libgo/go/os/stat_solaris.go +++ b/libgo/go/os/stat_solaris.go @@ -9,9 +9,9 @@ import ( "time" ) -func sameFile(sys1, sys2 interface{}) bool { - stat1 := sys1.(*syscall.Stat_t) - stat2 := sys2.(*syscall.Stat_t) +func sameFile(fs1, fs2 *fileStat) bool { + stat1 := fs1.sys.(*syscall.Stat_t) + stat2 := fs2.sys.(*syscall.Stat_t) return stat1.Dev == stat2.Dev && stat1.Ino == stat2.Ino } diff --git a/libgo/go/os/types.go b/libgo/go/os/types.go index c561ea0..473d431 100644 --- a/libgo/go/os/types.go +++ b/libgo/go/os/types.go @@ -99,21 +99,8 @@ func (m FileMode) Perm() FileMode { return m & ModePerm } -// A fileStat is the implementation of FileInfo returned by Stat and Lstat. -type fileStat struct { - name string - size int64 - mode FileMode - modTime time.Time - sys interface{} -} - -func (fs *fileStat) Name() string { return fs.name } -func (fs *fileStat) Size() int64 { return fs.size } -func (fs *fileStat) Mode() FileMode { return fs.mode } -func (fs *fileStat) ModTime() time.Time { return fs.modTime } -func (fs *fileStat) IsDir() bool { return fs.mode.IsDir() } -func (fs *fileStat) Sys() interface{} { return fs.sys } +func (fs *fileStat) Name() string { return fs.name } +func (fs *fileStat) IsDir() bool { return fs.Mode().IsDir() } // SameFile reports whether fi1 and fi2 describe the same file. // For example, on Unix this means that the device and inode fields @@ -127,5 +114,5 @@ func SameFile(fi1, fi2 FileInfo) bool { if !ok1 || !ok2 { return false } - return sameFile(fs1.sys, fs2.sys) + return sameFile(fs1, fs2) } diff --git a/libgo/go/os/types_notwin.go b/libgo/go/os/types_notwin.go new file mode 100644 index 0000000..ea1a073 --- /dev/null +++ b/libgo/go/os/types_notwin.go @@ -0,0 +1,25 @@ +// 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. + +// +build !windows + +package os + +import ( + "time" +) + +// A fileStat is the implementation of FileInfo returned by Stat and Lstat. +type fileStat struct { + name string + size int64 + mode FileMode + modTime time.Time + sys interface{} +} + +func (fs *fileStat) Size() int64 { return fs.size } +func (fs *fileStat) Mode() FileMode { return fs.mode } +func (fs *fileStat) ModTime() time.Time { return fs.modTime } +func (fs *fileStat) Sys() interface{} { return fs.sys } diff --git a/libgo/go/os/types_windows.go b/libgo/go/os/types_windows.go new file mode 100644 index 0000000..3890168 --- /dev/null +++ b/libgo/go/os/types_windows.go @@ -0,0 +1,104 @@ +// 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. + +package os + +import ( + "sync" + "syscall" + "time" +) + +// A fileStat is the implementation of FileInfo returned by Stat and Lstat. +type fileStat struct { + name string + sys syscall.Win32FileAttributeData + + // used to implement SameFile + sync.Mutex + path string + vol uint32 + idxhi uint32 + idxlo uint32 +} + +func (fs *fileStat) Size() int64 { + return int64(fs.sys.FileSizeHigh)<<32 + int64(fs.sys.FileSizeLow) +} + +func (fs *fileStat) Mode() (m FileMode) { + if fs == &devNullStat { + return ModeDevice | ModeCharDevice | 0666 + } + if fs.sys.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 { + m |= ModeDir | 0111 + } + if fs.sys.FileAttributes&syscall.FILE_ATTRIBUTE_READONLY != 0 { + m |= 0444 + } else { + m |= 0666 + } + return m +} + +func (fs *fileStat) ModTime() time.Time { + return time.Unix(0, fs.sys.LastWriteTime.Nanoseconds()) +} + +// Sys returns syscall.Win32FileAttributeData for file fs. +func (fs *fileStat) Sys() interface{} { return &fs.sys } + +func (fs *fileStat) loadFileId() error { + fs.Lock() + defer fs.Unlock() + if fs.path == "" { + // already done + return nil + } + pathp, err := syscall.UTF16PtrFromString(fs.path) + if err != nil { + return err + } + h, err := syscall.CreateFile(pathp, 0, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0) + if err != nil { + return err + } + defer syscall.CloseHandle(h) + var i syscall.ByHandleFileInformation + err = syscall.GetFileInformationByHandle(syscall.Handle(h), &i) + if err != nil { + return err + } + fs.path = "" + fs.vol = i.VolumeSerialNumber + fs.idxhi = i.FileIndexHigh + fs.idxlo = i.FileIndexLow + return nil +} + +// devNullStat is fileStat structure describing DevNull file ("NUL"). +var devNullStat = fileStat{ + name: DevNull, + // hopefully this will work for SameFile + vol: 0, + idxhi: 0, + idxlo: 0, +} + +func sameFile(fs1, fs2 *fileStat) bool { + e := fs1.loadFileId() + if e != nil { + return false + } + e = fs2.loadFileId() + if e != nil { + return false + } + return fs1.vol == fs2.vol && fs1.idxhi == fs2.idxhi && fs1.idxlo == fs2.idxlo +} + +// For testing. +func atime(fi FileInfo) time.Time { + return time.Unix(0, fi.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) +} |