Skip to content

Instantly share code, notes, and snippets.

@sbz
Created July 13, 2011 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbz/1080330 to your computer and use it in GitHub Desktop.
Save sbz/1080330 to your computer and use it in GitHub Desktop.
create a unix socket on X11 display in C
#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;
}
@sbz
Copy link
Author

sbz commented Jul 13, 2011

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}

@ONEMILIAN
Copy link

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