From 67cb62099c27de00cf80b30e37a7617c2c0ec2ed Mon Sep 17 00:00:00 2001 From: benoitc Date: Tue, 26 Nov 2019 10:14:23 +0100 Subject: [PATCH] remove socketfromfd module socket.fromfd using socket.AF_UNIX as type should be enough to be cross-platform since the address is larger than for others family. This should allow the code to work cross-platform. --- gunicorn/sock.py | 3 +- gunicorn/socketfromfd.py | 109 --------------------------------------- 2 files changed, 1 insertion(+), 111 deletions(-) delete mode 100644 gunicorn/socketfromfd.py diff --git a/gunicorn/sock.py b/gunicorn/sock.py index e53e578ef..d45867709 100644 --- a/gunicorn/sock.py +++ b/gunicorn/sock.py @@ -11,7 +11,6 @@ import time from gunicorn import util -from gunicorn.socketfromfd import fromfd class BaseSocket(object): @@ -168,7 +167,7 @@ def create_sockets(conf, log, fds=None): # sockets are already bound if fdaddr: for fd in fdaddr: - sock = fromfd(fd) + sock = socket.fromfd(fd, socket.AF_UNIX, socket.SOCK_STREAM) sock_name = sock.getsockname() sock_type = _sock_type(sock_name) listener = sock_type(sock_name, conf, log, fd=fd) diff --git a/gunicorn/socketfromfd.py b/gunicorn/socketfromfd.py deleted file mode 100644 index 71b40d3cf..000000000 --- a/gunicorn/socketfromfd.py +++ /dev/null @@ -1,109 +0,0 @@ -# Copyright (C) 2016 Christian Heimes -"""socketfromfd -- socket.fromd() with auto-discovery - -ATTENTION: Do not remove this backport till the minimum required version is - Python 3.7. See https://bugs.python.org/issue28134 for details. -""" -from __future__ import print_function - -import ctypes -import os -import socket -import sys -import platform - -from .util import find_library - -__all__ = ('fromfd',) - -SO_DOMAIN = getattr(socket, 'SO_DOMAIN', 39) -SO_TYPE = getattr(socket, 'SO_TYPE', 3) -SO_PROTOCOL = getattr(socket, 'SO_PROTOCOL', 38) - -_libc_name = find_library('c') -if _libc_name is not None: - if sys.platform.startswith("aix"): - member = ( - '(shr_64.o)' if ctypes.sizeof(ctypes.c_voidp) == 8 else '(shr.o)') - # 0x00040000 correspondes to RTLD_MEMBER, undefined in Python <= 3.6 - dlopen_mode = (ctypes.DEFAULT_MODE | 0x00040000 | os.RTLD_NOW) - libc = ctypes.CDLL(_libc_name+member, - use_errno=True, - mode=dlopen_mode) - else: - libc = ctypes.CDLL(_libc_name, use_errno=True) -else: - raise OSError('libc not found') - - -def _errcheck_errno(result, func, arguments): - """Raise OSError by errno for -1 - """ - if result == -1: - errno = ctypes.get_errno() - raise OSError(errno, os.strerror(errno)) - return arguments - - -if platform.system() == 'SunOS': - _libc_getsockopt = libc._so_getsockopt -else: - _libc_getsockopt = libc.getsockopt -_libc_getsockopt.argtypes = [ - ctypes.c_int, # int sockfd - ctypes.c_int, # int level - ctypes.c_int, # int optname - ctypes.c_void_p, # void *optval - ctypes.POINTER(ctypes.c_uint32) # socklen_t *optlen -] -_libc_getsockopt.restype = ctypes.c_int # 0: ok, -1: err -_libc_getsockopt.errcheck = _errcheck_errno - - -def _raw_getsockopt(fd, level, optname): - """Make raw getsockopt() call for int32 optval - - :param fd: socket fd - :param level: SOL_* - :param optname: SO_* - :return: value as int - """ - optval = ctypes.c_int(0) - optlen = ctypes.c_uint32(4) - _libc_getsockopt(fd, level, optname, - ctypes.byref(optval), ctypes.byref(optlen)) - return optval.value - - -def fromfd(fd, keep_fd=True): - """Create a socket from a file descriptor - - socket domain (family), type and protocol are auto-detected. By default - the socket uses a dup()ed fd. The original fd can be closed. - - The parameter `keep_fd` influences fd duplication. Under Python 2 the - fd is still duplicated but the input fd is closed. Under Python 3 and - with `keep_fd=True`, the new socket object uses the same fd. - - :param fd: socket fd - :type fd: int - :param keep_fd: keep input fd - :type keep_fd: bool - :return: socket.socket instance - :raises OSError: for invalid socket fd - """ - family = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_DOMAIN) - typ = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_TYPE) - proto = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_PROTOCOL) - if sys.version_info.major == 2: - # Python 2 has no fileno argument and always duplicates the fd - sockobj = socket.fromfd(fd, family, typ, proto) - sock = socket.socket(None, None, None, _sock=sockobj) - if not keep_fd: - os.close(fd) - return sock - else: - if keep_fd: - return socket.fromfd(fd, family, typ, proto) - else: - return socket.socket(family, typ, proto, fileno=fd)