Skip to content

Instantly share code, notes, and snippets.

@devilholk
Created August 16, 2020 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilholk/83a0bd402a5b6569d086f348d984d3d0 to your computer and use it in GitHub Desktop.
Save devilholk/83a0bd402a5b6569d086f348d984d3d0 to your computer and use it in GitHub Desktop.
Test of sending file descriptors to another process using AF_UNIX
from socket import *
import struct
#Open a file (opens itself now)
this_script = open(__file__, 'r')
#List of file descriptors to send
fd_list = [this_script.fileno()]
#Prepare ancillary data
cmsg_level, cmsg_type, cmsg_data = SOL_SOCKET, SCM_RIGHTS, struct.pack('I'*len(fd_list), *fd_list)
anc_data = [(cmsg_level, cmsg_type, cmsg_data)]
#Socket to connect to
socket_path = '/tmp/fd_test.sock'
#Seek to a known location
this_script.seek(420)
print('File descriptors', fd_list)
with socket(AF_UNIX, SOCK_SEQPACKET) as target:
target.connect(socket_path)
target.sendmsg([b'Yo! Have some file descriptors =)'], anc_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment