<s> """<STR_LIT>""" <EOL> from ctypes import * <EOL> from struct import pack_into , unpack_from <EOL> from os import fdopen , O_RDONLY , O_WRONLY , O_RDWR <EOL> from fcntl import fcntl , F_GETFL <EOL> from socket import fromfd , _socketobject , AF_INET , SOCK_STREAM <EOL> SOL_SOCKET = <NUM_LIT:1> <EOL> SCM_RIGHTS = <NUM_LIT:1> <EOL> c_socklen_t = c_uint32 <EOL> class CMSGHdr ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , c_size_t ) , <EOL> ( "<STR_LIT>" , c_int ) , <EOL> ( "<STR_LIT>" , c_int ) ] <EOL> FDBuffer = ( c_byte * sizeof ( c_int ) ) <EOL> class CMSG ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , CMSGHdr ) , <EOL> ( "<STR_LIT>" , FDBuffer ) ] <EOL> class IOVec ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , c_char_p ) , <EOL> ( "<STR_LIT>" , c_size_t ) ] <EOL> class MSGHdr ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , c_char_p ) , <EOL> ( "<STR_LIT>" , c_socklen_t ) , <EOL> ( "<STR_LIT>" , POINTER ( IOVec ) ) , <EOL> ( "<STR_LIT>" , c_size_t ) , <EOL> ( "<STR_LIT>" , POINTER ( CMSG ) ) , <EOL> ( "<STR_LIT>" , c_size_t ) , <EOL> ( "<STR_LIT>" , c_int ) ] <EOL> def CMSG_ALIGN ( x ) : <EOL> return ( ( x + sizeof ( c_size_t ) - <NUM_LIT:1> ) & ~ ( sizeof ( c_size_t ) - <NUM_LIT:1> ) ) <EOL> def CMSG_SPACE ( x ) : <EOL> return CMSG_ALIGN ( x ) + CMSG_ALIGN ( sizeof ( CMSGHdr ) ) <EOL> def CMSG_LEN ( x ) : <EOL> return CMSG_ALIGN ( sizeof ( CMSGHdr ) ) + x <EOL> libc = CDLL ( "<STR_LIT>" , use_errno = True ) <EOL> if libc is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> sendmsg = libc . sendmsg <EOL> sendmsg . argtypes = ( c_int , POINTER ( MSGHdr ) , c_int ) <EOL> recvmsg = libc . recvmsg <EOL> recvmsg . argtypes = ( c_int , POINTER ( MSGHdr ) , c_int ) <EOL> strerror = libc . strerror <EOL> strerror . restype = c_char_p <EOL> strerror . argtypes = ( c_int , ) <EOL> errno = libc . errno <EOL> def send_socket ( socket , sobject ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( sobject , _socketobject ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> iov = IOVec ( ) <EOL> iov . iov_base = "<STR_LIT:A>" <EOL> iov . iov_len = <NUM_LIT:1> <EOL> cmsg = CMSG ( ) <EOL> cmsg . cmsg_hdr . cmsg_len = CMSG_LEN ( sizeof ( c_int ) ) <EOL> cmsg . cmsg_hdr . cmsg_level = SOL_SOCKET <EOL> cmsg . cmsg_hdr . cmsg_type = SCM_RIGHTS <EOL> pack_into ( "<STR_LIT:i>" , cmsg . cmsg_data , <NUM_LIT:0> , sobject . fileno ( ) ) <EOL> msgh = MSGHdr ( ) <EOL> msgh . msg_name = None <EOL> msgh . msg_namelen = <NUM_LIT:0> <EOL> msgh . msg_iov = ( iov , ) <EOL> msgh . msg_iovlen = <NUM_LIT:1> <EOL> msgh . msg_control = pointer ( cmsg ) <EOL> msgh . msg_controllen = CMSG_SPACE ( sizeof ( c_int ) ) <EOL> msgh . msg_flags = <NUM_LIT:0> <EOL> rc = sendmsg ( socket . fileno ( ) , pointer ( msgh ) , <NUM_LIT:0> ) <EOL> if rc == - <NUM_LIT:1> : <EOL> errno = get_errno ( ) <EOL> raise OSError ( errno , strerror ( errno ) ) <EOL> return True <EOL> def receive_socket ( socket ) : <EOL> """<STR_LIT>""" <EOL> iov = IOVec ( ) <EOL> iov . iov_base = "<STR_LIT:A>" <EOL> iov . iov_len = <NUM_LIT:1> <EOL> cmsg = CMSG ( ) <EOL> cmsg . cmsg_hdr . cmsg_len = CMSG_LEN ( sizeof ( c_int ) ) <EOL> msgh = MSGHdr ( ) <EOL> msgh . msg_name = None <EOL> msgh . msg_namelen = <NUM_LIT:0> <EOL> msgh . msg_iov = ( iov , ) <EOL> msgh . msg_iovlen = <NUM_LIT:1> <EOL> msgh . msg_control = pointer ( cmsg ) <EOL> msgh . msg_controllen = CMSG_SPACE ( sizeof ( c_int ) ) <EOL> msgh . msg_flags = <NUM_LIT:0> <EOL> rc = recvmsg ( socket . fileno ( ) , pointer ( msgh ) , <NUM_LIT:0> ) <EOL> if rc == - <NUM_LIT:1> : <EOL> errno = get_errno ( ) <EOL> raise OSError ( errno , strerror ( errno ) ) <EOL> ( value , ) = unpack_from ( "<STR_LIT:i>" , cmsg . cmsg_data ) <EOL> ( fd , ) = unpack_from ( "<STR_LIT:i>" , cmsg . cmsg_data ) <EOL> newfile = fromfd ( fd , AF_INET , SOCK_STREAM ) <EOL> return newfile </s>
