From 15123e1e1f71fb044f0ebc6b23b37d9744890bf9 Mon Sep 17 00:00:00 2001 From: Mark Jeffery Date: Thu, 22 Jul 2021 15:03:52 +0200 Subject: [PATCH] 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 Reviewed-by: Ian Lance Taylor Reviewed-by: Matt Layher Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Trust: Matt Layher --- unix/syscall_unix.go | 4 ++++ unix/syscall_unix_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/unix/syscall_unix.go b/unix/syscall_unix.go index a7618ceb..cf296a24 100644 --- a/unix/syscall_unix.go +++ b/unix/syscall_unix.go @@ -313,6 +313,10 @@ func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) { 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 91b02b31..2f672832 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 TestUtimesNanoAt(t *testing.T) { } } +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)