2 # -*- test-case-name: twisted.python.test.test_sendmsg -*-
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
7 from struct import unpack
9 # This makes me sad. Why aren't things nice?
10 sys.path.insert(0, __file__.rsplit('/', 4)[0])
12 from twisted.python.sendmsg import recv1msg
16 Receive a file descriptor from a L{send1msg} message on the given C{AF_UNIX}
19 @param socketfd: An C{AF_UNIX} socket, attached to another process waiting
20 to send sockets via the ancillary data mechanism in L{send1msg}.
24 @return: a 2-tuple of (new file descriptor, description).
26 @rtype: 2-tuple of (C{int}, C{str})
28 data, flags, ancillary = recv1msg(socketfd)
29 [(cmsg_level, cmsg_type, packedFD)] = ancillary
30 # cmsg_level and cmsg_type really need to be SOL_SOCKET / SCM_RIGHTS, but
31 # since those are the *only* standard values, there's not much point in
33 [unpackedFD] = unpack("i", packedFD)
34 return (unpackedFD, data)
37 if __name__ == '__main__':
38 fd, description = recvfd(int(sys.argv[1]))
39 os.write(fd, "Test fixture data: %s.\n" % (description,))