Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / python / test / pullpipe.py
1 #!/usr/bin/python
2 # -*- test-case-name: twisted.python.test.test_sendmsg -*-
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
5
6 import sys, os
7 from struct import unpack
8
9 # This makes me sad.  Why aren't things nice?
10 sys.path.insert(0, __file__.rsplit('/', 4)[0])
11
12 from twisted.python.sendmsg import recv1msg
13
14 def recvfd(socketfd):
15     """
16     Receive a file descriptor from a L{send1msg} message on the given C{AF_UNIX}
17     socket.
18
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}.
21
22     @param fd: C{int}
23
24     @return: a 2-tuple of (new file descriptor, description).
25
26     @rtype: 2-tuple of (C{int}, C{str})
27     """
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
32     # checking.
33     [unpackedFD] = unpack("i", packedFD)
34     return (unpackedFD, data)
35
36
37 if __name__ == '__main__':
38     fd, description = recvfd(int(sys.argv[1]))
39     os.write(fd, "Test fixture data: %s.\n" % (description,))
40     os.close(fd)