]> Cypherpunks repositories - gostls13.git/commitdiff
Revert "net: remove fallback path in sysSocket"
authorIan Lance Taylor <iant@golang.org>
Tue, 6 Jun 2023 17:41:06 +0000 (17:41 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 9 Jun 2023 00:17:39 +0000 (00:17 +0000)
This reverts CL 40364.

Reason for revert: Fallback path is still required on Solaris.

For #45964
For #59359

Change-Id: I4b8c8af77ee987cad6617221793b90c9a8829c3e
Reviewed-on: https://go-review.googlesource.com/c/go/+/501276
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/net/sock_cloexec.go

index 3f1cc9827aa3636de3a1bb6e42e5e599a6c322a6..f4c116502884b978aa59d5db59aa9d3a72c53e8e 100644 (file)
@@ -10,6 +10,7 @@
 package net
 
 import (
+       "internal/poll"
        "os"
        "syscall"
 )
@@ -18,8 +19,32 @@ import (
 // descriptor as nonblocking and close-on-exec.
 func sysSocket(family, sotype, proto int) (int, error) {
        s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
+       // On Linux the SOCK_NONBLOCK and SOCK_CLOEXEC flags were
+       // introduced in 2.6.27 kernel and on FreeBSD both flags were
+       // introduced in 10 kernel. If we get an EINVAL error on Linux
+       // or EPROTONOSUPPORT error on FreeBSD, fall back to using
+       // socket without them.
+       switch err {
+       case nil:
+               return s, nil
+       default:
+               return -1, os.NewSyscallError("socket", err)
+       case syscall.EPROTONOSUPPORT, syscall.EINVAL:
+       }
+
+       // See ../syscall/exec_unix.go for description of ForkLock.
+       syscall.ForkLock.RLock()
+       s, err = socketFunc(family, sotype, proto)
+       if err == nil {
+               syscall.CloseOnExec(s)
+       }
+       syscall.ForkLock.RUnlock()
        if err != nil {
                return -1, os.NewSyscallError("socket", err)
        }
+       if err = syscall.SetNonblock(s, true); err != nil {
+               poll.CloseFunc(s)
+               return -1, os.NewSyscallError("setnonblock", err)
+       }
        return s, nil
 }