Skip to content

Commit bd00528

Browse files
ianlancetaylorgopherbot
authored andcommitted
Revert "net: remove fallback path in sysSocket"
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 <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent ce8eadf commit bd00528

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/net/sock_cloexec.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package net
1111

1212
import (
13+
"internal/poll"
1314
"os"
1415
"syscall"
1516
)
@@ -18,8 +19,32 @@ import (
1819
// descriptor as nonblocking and close-on-exec.
1920
func sysSocket(family, sotype, proto int) (int, error) {
2021
s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
22+
// On Linux the SOCK_NONBLOCK and SOCK_CLOEXEC flags were
23+
// introduced in 2.6.27 kernel and on FreeBSD both flags were
24+
// introduced in 10 kernel. If we get an EINVAL error on Linux
25+
// or EPROTONOSUPPORT error on FreeBSD, fall back to using
26+
// socket without them.
27+
switch err {
28+
case nil:
29+
return s, nil
30+
default:
31+
return -1, os.NewSyscallError("socket", err)
32+
case syscall.EPROTONOSUPPORT, syscall.EINVAL:
33+
}
34+
35+
// See ../syscall/exec_unix.go for description of ForkLock.
36+
syscall.ForkLock.RLock()
37+
s, err = socketFunc(family, sotype, proto)
38+
if err == nil {
39+
syscall.CloseOnExec(s)
40+
}
41+
syscall.ForkLock.RUnlock()
2142
if err != nil {
2243
return -1, os.NewSyscallError("socket", err)
2344
}
45+
if err = syscall.SetNonblock(s, true); err != nil {
46+
poll.CloseFunc(s)
47+
return -1, os.NewSyscallError("setnonblock", err)
48+
}
2449
return s, nil
2550
}

0 commit comments

Comments
 (0)