<s> import errno <EOL> import os <EOL> import socket <EOL> import stat <EOL> import sys <EOL> import time <EOL> from gunicorn import util <EOL> from gunicorn . six import string_types <EOL> SD_LISTEN_FDS_START = <NUM_LIT:3> <EOL> class BaseSocket ( object ) : <EOL> def __init__ ( self , address , conf , log , fd = None ) : <EOL> self . log = log <EOL> self . conf = conf <EOL> self . cfg_addr = address <EOL> if fd is None : <EOL> sock = socket . socket ( self . FAMILY , socket . SOCK_STREAM ) <EOL> else : <EOL> sock = socket . fromfd ( fd , self . FAMILY , socket . SOCK_STREAM ) <EOL> self . sock = self . set_options ( sock , bound = ( fd is not None ) ) <EOL> def __str__ ( self , name ) : <EOL> return "<STR_LIT>" % self . sock . fileno ( ) <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . sock , name ) <EOL> def set_options ( self , sock , bound = False ) : <EOL> sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> if not bound : <EOL> self . bind ( sock ) <EOL> sock . setblocking ( <NUM_LIT:0> ) <EOL> if hasattr ( sock , "<STR_LIT>" ) : <EOL> sock . set_inheritable ( True ) <EOL> sock . listen ( self . conf . backlog ) <EOL> return sock <EOL> def bind ( self , sock ) : <EOL> sock . bind ( self . cfg_addr ) <EOL> def close ( self ) : <EOL> try : <EOL> self . sock . close ( ) <EOL> except socket . error as e : <EOL> self . log . info ( "<STR_LIT>" , str ( e ) ) <EOL> del self . sock <EOL> class TCPSocket ( BaseSocket ) : <EOL> FAMILY = socket . AF_INET <EOL> def __str__ ( self ) : <EOL> if self . conf . is_ssl : <EOL> scheme = "<STR_LIT>" <EOL> else : <EOL> scheme = "<STR_LIT:http>" <EOL> addr = self . sock . getsockname ( ) <EOL> return "<STR_LIT>" % ( scheme , addr [ <NUM_LIT:0> ] , addr [ <NUM_LIT:1> ] ) <EOL> def set_options ( self , sock , bound = False ) : <EOL> sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , <NUM_LIT:1> ) <EOL> return super ( TCPSocket , self ) . set_options ( sock , bound = bound ) <EOL> class TCP6Socket ( TCPSocket ) : <EOL> FAMILY = socket . AF_INET6 <EOL> def __str__ ( self ) : <EOL> ( host , port , fl , sc ) = self . sock . getsockname ( ) <EOL> return "<STR_LIT>" % ( host , port ) <EOL> class UnixSocket ( BaseSocket ) : <EOL> FAMILY = socket . AF_UNIX <EOL> def __init__ ( self , addr , conf , log , fd = None ) : <EOL> if fd is None : <EOL> try : <EOL> st = os . stat ( addr ) <EOL> except OSError as e : <EOL> if e . args [ <NUM_LIT:0> ] != errno . ENOENT : <EOL> raise <EOL> else : <EOL> if stat . S_ISSOCK ( st . st_mode ) : <EOL> os . remove ( addr ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % addr ) <EOL> self . parent = os . getpid ( ) <EOL> super ( UnixSocket , self ) . __init__ ( addr , conf , log , fd = fd ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . cfg_addr <EOL> def bind ( self , sock ) : <EOL> old_umask = os . umask ( self . conf . umask ) <EOL> sock . bind ( self . cfg_addr ) <EOL> util . chown ( self . cfg_addr , self . conf . uid , self . conf . gid ) <EOL> os . umask ( old_umask ) <EOL> def close ( self ) : <EOL> super ( UnixSocket , self ) . close ( ) <EOL> if self . parent == os . getpid ( ) : <EOL> os . unlink ( self . cfg_addr ) <EOL> def _sock_type ( addr ) : <EOL> if isinstance ( addr , tuple ) : <EOL> if util . is_ipv6 ( addr [ <NUM_LIT:0> ] ) : <EOL> sock_type = TCP6Socket <EOL> else : <EOL> sock_type = TCPSocket <EOL> elif isinstance ( addr , string_types ) : <EOL> sock_type = UnixSocket <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % addr ) <EOL> return sock_type <EOL> def create_sockets ( conf , log ) : <EOL> """<STR_LIT>""" <EOL> listeners = [ ] <EOL> if ( '<STR_LIT>' in os . environ <EOL> and int ( os . environ . get ( '<STR_LIT>' ) ) == os . getpid ( ) ) : <EOL> for i in range ( int ( os . environ . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) ) : <EOL> fd = i + SD_LISTEN_FDS_START <EOL> try : <EOL> sock = socket . fromfd ( fd , socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> sockname = sock . getsockname ( ) <EOL> if isinstance ( sockname , str ) and sockname . startswith ( '<STR_LIT:/>' ) : <EOL> listeners . append ( UnixSocket ( sockname , conf , log , fd = fd ) ) <EOL> elif len ( sockname ) == <NUM_LIT:2> and '<STR_LIT:.>' in sockname [ <NUM_LIT:0> ] : <EOL> listeners . append ( TCPSocket ( "<STR_LIT>" % sockname , conf , log , <EOL> fd = fd ) ) <EOL> elif len ( sockname ) == <NUM_LIT:4> and '<STR_LIT::>' in sockname [ <NUM_LIT:0> ] : <EOL> listeners . append ( TCP6Socket ( "<STR_LIT>" % sockname [ : <NUM_LIT:2> ] , conf , <EOL> log , fd = fd ) ) <EOL> except socket . error : <EOL> pass <EOL> del os . environ [ '<STR_LIT>' ] , os . environ [ '<STR_LIT>' ] <EOL> if listeners : <EOL> log . debug ( '<STR_LIT>' , <EOL> "<STR_LIT:U+002C>" . join ( [ str ( l ) for l in listeners ] ) ) <EOL> return listeners <EOL> laddr = conf . address <EOL> if conf . certfile and not os . path . exists ( conf . certfile ) : <EOL> raise ValueError ( '<STR_LIT>' % conf . certfile ) <EOL> if conf . keyfile and not os . path . exists ( conf . keyfile ) : <EOL> raise ValueError ( '<STR_LIT>' % conf . keyfile ) <EOL> if '<STR_LIT>' in os . environ : <EOL> fds = os . environ . pop ( '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> for i , fd in enumerate ( fds ) : <EOL> fd = int ( fd ) <EOL> addr = laddr [ i ] <EOL> sock_type = _sock_type ( addr ) <EOL> try : <EOL> listeners . append ( sock_type ( addr , conf , log , fd = fd ) ) <EOL> except socket . error as e : <EOL> if e . args [ <NUM_LIT:0> ] == errno . ENOTCONN : <EOL> log . error ( "<STR_LIT>" ) <EOL> else : <EOL> raise <EOL> return listeners <EOL> for addr in laddr : <EOL> sock_type = _sock_type ( addr ) <EOL> sock = None <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> try : <EOL> sock = sock_type ( addr , conf , log ) <EOL> except socket . error as e : <EOL> if e . args [ <NUM_LIT:0> ] == errno . EADDRINUSE : <EOL> log . error ( "<STR_LIT>" , str ( addr ) ) <EOL> if e . args [ <NUM_LIT:0> ] == errno . EADDRNOTAVAIL : <EOL> log . error ( "<STR_LIT>" , str ( addr ) ) <EOL> if i < <NUM_LIT:5> : <EOL> log . error ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> else : <EOL> break <EOL> if sock is None : <EOL> log . error ( "<STR_LIT>" , str ( addr ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> listeners . append ( sock ) <EOL> return listeners </s>
