You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pullpipe.py 1.2KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839
  1. # -*- test-case-name: twisted.python.test.test_sendmsg -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. import sys
  5. import os
  6. import socket
  7. from struct import unpack
  8. from twisted.python.sendmsg import recvmsg
  9. def recvfd(socketfd):
  10. """
  11. Receive a file descriptor from a L{sendmsg} message on the given C{AF_UNIX}
  12. socket.
  13. @param socketfd: An C{AF_UNIX} socket, attached to another process waiting
  14. to send sockets via the ancillary data mechanism in L{send1msg}.
  15. @param fd: C{int}
  16. @return: a 2-tuple of (new file descriptor, description).
  17. @rtype: 2-tuple of (C{int}, C{bytes})
  18. """
  19. ourSocket = socket.fromfd(socketfd, socket.AF_UNIX, socket.SOCK_STREAM)
  20. data, ancillary, flags = recvmsg(ourSocket)
  21. [(cmsgLevel, cmsgType, packedFD)] = ancillary
  22. # cmsgLevel and cmsgType really need to be SOL_SOCKET / SCM_RIGHTS, but
  23. # since those are the *only* standard values, there's not much point in
  24. # checking.
  25. [unpackedFD] = unpack("i", packedFD)
  26. return (unpackedFD, data)
  27. if __name__ == '__main__':
  28. fd, description = recvfd(int(sys.argv[1]))
  29. os.write(fd, b"Test fixture data: " + description + b".\n")
  30. os.close(fd)