github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/net/sys_cloexec.go (about) 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 do not provide a fast path 6 // for setting SetNonblock and CloseOnExec. 7 8 //go:build aix || darwin || (solaris && !illumos) 9 // +build aix darwin solaris,!illumos 10 11 package net 12 13 import ( 14 "internal/poll" 15 "os" 16 "syscall" 17 ) 18 19 // Wrapper around the socket system call that marks the returned file 20 // descriptor as nonblocking and close-on-exec. 21 func sysSocket(family, sotype, proto int) (int, error) { 22 // See ../syscall/exec_unix.go for description of ForkLock. 23 syscall.ForkLock.RLock() 24 s, err := socketFunc(family, sotype, proto) 25 if err == nil { 26 syscall.CloseOnExec(s) 27 } 28 syscall.ForkLock.RUnlock() 29 if err != nil { 30 return -1, os.NewSyscallError("socket", err) 31 } 32 if err = syscall.SetNonblock(s, true); err != nil { 33 poll.CloseFunc(s) 34 return -1, os.NewSyscallError("setnonblock", err) 35 } 36 return s, nil 37 }