Retro68/gcc/libgo/go/net/sys_cloexec.go

58 lines
1.7 KiB
Go
Raw Normal View History

2014-09-21 17:33:12 +00:00
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file implements sysSocket and accept for platforms that do not
// provide a fast path for setting SetNonblock and CloseOnExec.
2015-08-28 15:33:40 +00:00
// +build darwin dragonfly nacl netbsd openbsd solaris
2014-09-21 17:33:12 +00:00
package net
2017-04-10 11:32:00 +00:00
import (
"os"
"syscall"
)
2014-09-21 17:33:12 +00:00
// Wrapper around the socket system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
2015-08-28 15:33:40 +00:00
func sysSocket(family, sotype, proto int) (int, error) {
2014-09-21 17:33:12 +00:00
// See ../syscall/exec_unix.go for description of ForkLock.
syscall.ForkLock.RLock()
2017-04-10 11:32:00 +00:00
s, err := socketFunc(family, sotype, proto)
2014-09-21 17:33:12 +00:00
if err == nil {
syscall.CloseOnExec(s)
}
syscall.ForkLock.RUnlock()
if err != nil {
2017-04-10 11:32:00 +00:00
return -1, os.NewSyscallError("socket", err)
2014-09-21 17:33:12 +00:00
}
if err = syscall.SetNonblock(s, true); err != nil {
2017-04-10 11:32:00 +00:00
closeFunc(s)
return -1, os.NewSyscallError("setnonblock", err)
2014-09-21 17:33:12 +00:00
}
return s, nil
}
// Wrapper around the accept system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
2015-08-28 15:33:40 +00:00
func accept(s int) (int, syscall.Sockaddr, error) {
2014-09-21 17:33:12 +00:00
// See ../syscall/exec_unix.go for description of ForkLock.
// It is probably okay to hold the lock across syscall.Accept
// because we have put fd.sysfd into non-blocking mode.
// However, a call to the File method will put it back into
// blocking mode. We can't take that risk, so no use of ForkLock here.
2017-04-10 11:32:00 +00:00
ns, sa, err := acceptFunc(s)
2014-09-21 17:33:12 +00:00
if err == nil {
2015-08-28 15:33:40 +00:00
syscall.CloseOnExec(ns)
2014-09-21 17:33:12 +00:00
}
if err != nil {
2017-04-10 11:32:00 +00:00
return -1, nil, os.NewSyscallError("accept", err)
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
if err = syscall.SetNonblock(ns, true); err != nil {
2017-04-10 11:32:00 +00:00
closeFunc(ns)
return -1, nil, os.NewSyscallError("setnonblock", err)
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
return ns, sa, nil
2014-09-21 17:33:12 +00:00
}