Created
July 13, 2011 13:53
-
-
Save sbz/1080330 to your computer and use it in GitHub Desktop.
create a unix socket on X11 display in C
This file contains hidden or 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
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <sys/un.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdio.h> | |
#define _PATH_UNIX_X "/tmp/.X11-unix/X%d" | |
#define DISPLAY_PORT 0 | |
int | |
main(void) { | |
int rc,sock; | |
struct sockaddr_un addr; | |
sock = socket(AF_UNIX, SOCK_STREAM, 0); | |
if (sock<0) return -1; | |
memset(&addr, 0, sizeof(addr)); | |
addr.sun_family = AF_UNIX; | |
snprintf(addr.sun_path, sizeof(addr.sun_path), _PATH_UNIX_X, DISPLAY_PORT); | |
rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); | |
sleep(10); | |
close(sock); | |
return 0; | |
} |
I'm doing the same im Assembly, you did a great job!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Connection can be tested using ss(8) on Linux:
% ss -x -p -f unix -r | grep xsock
u_str ESTAB 0 0 * 1701647 * 0 users:(("xsock",2066,3))
Transmitted data can be sniffed using strace(1):
% export XORG_FD=$(sudo lsof 2>&1 |grep X0 | awk '{print $4}')
% export XORG_PID=$(sudo lsof 2>&1 |grep X0|awk '{print $2}')
% sudo strace -e trace=read,write -e read=${XORG_FD} -e write=${XORG_FD} -p ${XORG_PID}