yocto-sumo源码解析(十一): recvfds
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')