func makeListener()

in api/make_listener_posix.go [31:72]


func makeListener(cfg Config) (net.Listener, error) {
	if len(cfg.User) > 0 {
		return nil, errors.New("specifying a user is not supported under this platform")
	}

	if len(cfg.SecurityDescriptor) > 0 {
		return nil, errors.New("security_descriptor option for the HTTP endpoint only work on Windows")
	}

	if npipe.IsNPipe(cfg.Host) {
		return nil, fmt.Errorf("cannot use %s as the host, named pipes are only supported on Windows", cfg.Host)
	}

	network, path, err := parse(cfg.Host, cfg.Port)
	if err != nil {
		return nil, err
	}

	if network == unixNetwork {
		if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
			return nil, fmt.Errorf("cannot remove existing unix socket file at location %s: %w", path, err)
		}
	}

	l, err := net.Listen(network, path)
	if err != nil {
		return nil, err
	}

	// Ensure file mode
	if network == unixNetwork {
		if err := os.Chmod(path, socketFileMode); err != nil {
			return nil, fmt.Errorf("could not set mode %d for unix socket file at location %s: %w",
				socketFileMode,
				path,
				err,
			)
		}
	}

	return l, nil
}