File: xsocket.go

package info (click to toggle)
golang-github-insomniacslk-dhcp 0.0~git20251020.175e84f-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,104 kB
  • sloc: makefile: 3
file content (34 lines) | stat: -rw-r--r-- 723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package xsocket

import (
	"syscall"

	"golang.org/x/sys/unix"
)

// CloexecSocket creates a new socket with the close-on-exec flag set.
//
// If the OS doesn't support the close-on-exec flag, this function will try a workaround.
func CloexecSocket(domain, typ, proto int) (int, error) {
	fd, err := socketCloexec(domain, typ, proto)
	if err == nil {
		return fd, nil
	}

	if err == unix.EINVAL || err == unix.EPROTONOSUPPORT {
		// SOCK_CLOEXEC is not supported, try without it, but avoid racing with fork/exec
		syscall.ForkLock.RLock()
		defer syscall.ForkLock.RUnlock()

		fd, err = unix.Socket(domain, typ, proto)
		if err != nil {
			return -1, err
		}

		unix.CloseOnExec(fd)

		return fd, nil
	}

	return fd, err
}