yocto-sumo源碼解析(十一): recvfds
阿新 • • 發佈:2018-11-18
socket sum arr bsp cti 不為 runt data eve
def recvfds(sock, size): ‘‘‘Receive an array of fds over an AF_UNIX socket.‘‘‘ a = array.array(‘i‘) #聲明一個整型數組,itemsize bytes_size = a.itemsize * size #數組容量設置 msg, ancdata, flags, addr= sock.recvmsg(1, socket.CMSG_LEN(bytes_size)) #從套接字中讀取數據到msg,ancdata,recvmsg函數是標準posix函數,可以參閱:http://pubs.opengroup.org/onlinepubs/009695399/functions/recvmsg.html if not msg and not ancdata: raise EOFError try: if len(ancdata) != 1: #對於接收到的附加數據,長度不為1個元組的情況下退出raise RuntimeError(‘received %d items of ancdata‘ % len(ancdata)) cmsg_level, cmsg_type, cmsg_data = ancdata[0] #從附加數據中提取(cmsg_level, cmsg_type, cmsg_data)元組 if (cmsg_level == socket.SOL_SOCKET and cmsg_type== socket.SCM_RIGHTS): if len(cmsg_data) % a.itemsize != 0: raise ValueError a.frombytes(cmsg_data) #從cmsg_data中提取fd到數組a assert len(a) % 256 == msg[0] return list(a) except (ValueError, IndexError): pass raise RuntimeError(‘Invalid data received‘)
yocto-sumo源碼解析(十一): recvfds