unix: add Send on Linux
Added Send function which in turn calls existing Sendto.
Fixes golang/go#47288
Change-Id: I5c928a19cc4f10961a9e6d2802e197cc3b799438
Reviewed-on: https://go-review.googlesource.com/c/sys/+/336569
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Matt Layher <mdlayher@gmail.com>
diff --git a/unix/syscall_unix.go b/unix/syscall_unix.go
index a7618ce..cf296a2 100644
--- a/unix/syscall_unix.go
+++ b/unix/syscall_unix.go
@@ -313,6 +313,10 @@
return
}
+func Send(s int, buf []byte, flags int) (err error) {
+ return sendto(s, buf, flags, nil, 0)
+}
+
func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
ptr, n, err := to.sockaddr()
if err != nil {
diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go
index 91b02b3..2f67283 100644
--- a/unix/syscall_unix_test.go
+++ b/unix/syscall_unix_test.go
@@ -8,6 +8,7 @@
package unix_test
import (
+ "bytes"
"flag"
"fmt"
"io/ioutil"
@@ -890,6 +891,44 @@
}
}
+func TestSend(t *testing.T) {
+ ec := make(chan error, 2)
+ ts := []byte("HELLO GOPHER")
+
+ fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
+ if err != nil {
+ t.Fatalf("Socketpair: %v", err)
+ }
+ defer unix.Close(fds[0])
+ defer unix.Close(fds[1])
+
+ go func() {
+ data := make([]byte, len(ts))
+
+ _, _, err := unix.Recvfrom(fds[1], data, 0)
+ if err != nil {
+ ec <- err
+ }
+ if !bytes.Equal(ts, data) {
+ ec <- fmt.Errorf("data sent != data received. Received %q", data)
+ }
+ ec <- nil
+ }()
+ err = unix.Send(fds[0], ts, 0)
+ if err != nil {
+ ec <- err
+ }
+
+ select {
+ case err = <-ec:
+ if err != nil {
+ t.Fatalf("Send: %v", err)
+ }
+ case <-time.After(2 * time.Second):
+ t.Fatal("Send: nothing received after 2 seconds")
+ }
+}
+
// mktmpfifo creates a temporary FIFO and provides a cleanup function.
func mktmpfifo(t *testing.T) (*os.File, func()) {
err := unix.Mkfifo("fifo", 0666)