]> Cypherpunks.ru repositories - gostls13.git/blob - src/net/sock_cloexec.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / src / net / sock_cloexec.go
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // This file implements sysSocket for platforms that provide a fast path for
6 // setting SetNonblock and CloseOnExec.
7
8 //go:build dragonfly || freebsd || linux || netbsd || openbsd || solaris
9
10 package net
11
12 import (
13         "internal/poll"
14         "os"
15         "syscall"
16 )
17
18 // Wrapper around the socket system call that marks the returned file
19 // descriptor as nonblocking and close-on-exec.
20 func sysSocket(family, sotype, proto int) (int, error) {
21         s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
22         // TODO: We can remove the fallback on Linux and *BSD,
23         // as currently supported versions all support accept4
24         // with SOCK_CLOEXEC, but Solaris does not. See issue #59359.
25         switch err {
26         case nil:
27                 return s, nil
28         default:
29                 return -1, os.NewSyscallError("socket", err)
30         case syscall.EPROTONOSUPPORT, syscall.EINVAL:
31         }
32
33         // See ../syscall/exec_unix.go for description of ForkLock.
34         syscall.ForkLock.RLock()
35         s, err = socketFunc(family, sotype, proto)
36         if err == nil {
37                 syscall.CloseOnExec(s)
38         }
39         syscall.ForkLock.RUnlock()
40         if err != nil {
41                 return -1, os.NewSyscallError("socket", err)
42         }
43         if err = syscall.SetNonblock(s, true); err != nil {
44                 poll.CloseFunc(s)
45                 return -1, os.NewSyscallError("setnonblock", err)
46         }
47         return s, nil
48 }