Created
August 16, 2020 09:24
-
-
Save devilholk/83a0bd402a5b6569d086f348d984d3d0 to your computer and use it in GitHub Desktop.
Test of sending file descriptors to another process using AF_UNIX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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