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 accept for platforms that provide a fast path for
6// setting SetNonblock and CloseOnExec, but don't necessarily have accept4.
7// This is the code we used for accept in Go 1.17 and earlier.
8// On Linux the accept4 system call was introduced in 2.6.28 kernel,
9// and our minimum requirement is 2.6.32, so we simplified the function.
10// Unfortunately, on ARM accept4 wasn't added until 2.6.36, so for ARM
11// only we continue using the older code.
12
13//go:build linux && arm
14
15package poll
16
17import "syscall"
18
19// Wrapper around the accept system call that marks the returned file
20// descriptor as nonblocking and close-on-exec.
21func accept(s int) (int, syscall.Sockaddr, string, error) {
22	ns, sa, err := Accept4Func(s, syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC)
23	switch err {
24	case nil:
25		return ns, sa, "", nil
26	default: // errors other than the ones listed
27		return -1, sa, "accept4", err
28	case syscall.ENOSYS: // syscall missing
29	case syscall.EINVAL: // some Linux use this instead of ENOSYS
30	case syscall.EACCES: // some Linux use this instead of ENOSYS
31	case syscall.EFAULT: // some Linux use this instead of ENOSYS
32	}
33
34	// See ../syscall/exec_unix.go for description of ForkLock.
35	// It is probably okay to hold the lock across syscall.Accept
36	// because we have put fd.sysfd into non-blocking mode.
37	// However, a call to the File method will put it back into
38	// blocking mode. We can't take that risk, so no use of ForkLock here.
39	ns, sa, err = AcceptFunc(s)
40	if err == nil {
41		syscall.CloseOnExec(ns)
42	}
43	if err != nil {
44		return -1, nil, "accept", err
45	}
46	if err = syscall.SetNonblock(ns, true); err != nil {
47		CloseFunc(ns)
48		return -1, nil, "setnonblock", err
49	}
50	return ns, sa, "", nil
51}
52