2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * inject_randr1.5.c: An X11 proxy which interprets RandR 1.5 GetMonitors
8 * requests and overwrites their reply with a custom reply.
10 * This tool can be refactored as necessary in order to perform the same
11 * purpose for other request types. The RandR 1.5 specific portions of the code
12 * have been marked as such to make such a refactoring easier.
19 #include <sys/types.h>
20 #include <sys/socket.h>
23 #include <sys/resource.h>
28 static void uds_connection_cb(EV_P_ ev_io *w, int revents);
29 static void read_client_setup_request_cb(EV_P_ ev_io *w, int revents);
30 static void read_server_setup_reply_cb(EV_P_ ev_io *w, int revents);
31 static void read_client_x11_packet_cb(EV_P_ ev_io *w, int revents);
32 static void read_server_x11_packet_cb(EV_P_ ev_io *w, int revents);
34 static char *sun_path = NULL;
36 void cleanup_socket(void) {
37 if (sun_path != NULL) {
44 /* BEGIN RandR 1.5 specific */
45 static void *injected_reply = NULL;
46 static off_t injected_reply_len = 0;
47 /* END RandR 1.5 specific */
49 #define XCB_PAD(i) (-(i)&3)
52 /* clientw is a libev watcher for the connection which we accept()ed. */
55 /* serverw is a libev watcher for the connection to X11 which we initiated
56 * on behalf of the client. */
59 /* sequence is the client-side sequence number counter. In X11’s wire
60 * encoding, sequence counters are not included in requests, only in
64 /* BEGIN RandR 1.5 specific */
65 /* sequence number of the most recent GetExtension request for RANDR */
67 /* sequence number of the most recent RRGetMonitors request */
70 int randr_major_opcode;
71 /* END RandR 1.5 specific */
76 * Returns -1 on error (with errno from read() untouched)
79 static size_t readall_into(void *buffer, const size_t len, int fd) {
80 size_t read_bytes = 0;
81 while (read_bytes < len) {
82 ssize_t n = read(fd, buffer + read_bytes, len - read_bytes);
86 read_bytes += (size_t)n;
92 * Exits the program with an error if the read failed.
95 static void must_read(int n) {
97 err(EXIT_FAILURE, "read()");
100 errx(EXIT_FAILURE, "EOF");
105 * Exits the program with an error if the write failed.
108 static void must_write(int n) {
110 err(EXIT_FAILURE, "write()");
114 static void uds_connection_cb(EV_P_ ev_io *w, int revents) {
115 struct sockaddr_un addr;
116 socklen_t addrlen = sizeof(addr);
117 const int clientfd = accept(w->fd, (struct sockaddr *)&addr, &addrlen);
118 if (clientfd == -1) {
119 if (errno == EINTR) {
122 err(EXIT_FAILURE, "accept()");
125 struct connstate *connstate = scalloc(1, sizeof(struct connstate));
127 ev_io *clientw = scalloc(1, sizeof(ev_io));
128 connstate->clientw = clientw;
129 clientw->data = connstate;
130 ev_io_init(clientw, read_client_setup_request_cb, clientfd, EV_READ);
131 ev_io_start(EV_A_ clientw);
134 // https://www.x.org/releases/current/doc/xproto/x11protocol.html#Encoding::Connection_Setup
135 static void read_client_setup_request_cb(EV_P_ ev_io *w, int revents) {
137 struct connstate *connstate = (struct connstate *)w->data;
139 /* Read X11 setup request in its entirety. */
140 xcb_setup_request_t setup_request;
141 must_read(readall_into(&setup_request, sizeof(setup_request), w->fd));
143 /* Establish a connection to X11. */
144 int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
146 err(EXIT_FAILURE, "socket()");
151 if (xcb_parse_display(getenv("DISPLAY"), &host, &displayp, NULL) == 0) {
152 errx(EXIT_FAILURE, "Could not parse DISPLAY=%s", getenv("DISPLAY"));
156 struct sockaddr_un addr;
157 memset(&addr, 0, sizeof(addr));
158 addr.sun_family = AF_LOCAL;
159 snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/.X11-unix/X%d", displayp);
160 if (connect(fd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
161 err(EXIT_FAILURE, "connect(%s)", addr.sun_path);
164 /* Relay setup request. */
165 must_write(writeall(fd, &setup_request, sizeof(setup_request)));
167 if (setup_request.authorization_protocol_name_len > 0 ||
168 setup_request.authorization_protocol_data_len > 0) {
169 const size_t authlen = setup_request.authorization_protocol_name_len +
170 XCB_PAD(setup_request.authorization_protocol_name_len) +
171 setup_request.authorization_protocol_data_len +
172 XCB_PAD(setup_request.authorization_protocol_data_len);
173 void *buf = smalloc(authlen);
174 must_read(readall_into(buf, authlen, w->fd));
175 must_write(writeall(fd, buf, authlen));
179 /* Wait for a response from the X11 server. */
180 ev_io *serverw = scalloc(1, sizeof(ev_io));
181 connstate->serverw = serverw;
182 serverw->data = connstate;
183 ev_io_init(serverw, read_server_setup_reply_cb, fd, EV_READ);
184 ev_io_start(EV_A_ serverw);
187 static void read_server_setup_reply_cb(EV_P_ ev_io *w, int revents) {
188 struct connstate *connstate = (struct connstate *)w->data;
189 xcb_setup_failed_t setup_failed;
190 must_read(readall_into(&setup_failed, sizeof(setup_failed), w->fd));
192 switch (setup_failed.status) {
194 errx(EXIT_FAILURE, "error authenticating at the X11 server");
197 errx(EXIT_FAILURE, "two-factor auth not implemented");
200 must_write(writeall(connstate->clientw->fd, &setup_failed, sizeof(xcb_setup_failed_t)));
201 const size_t len = (setup_failed.length * 4);
202 void *buf = smalloc(len);
203 must_read(readall_into(buf, len, w->fd));
204 must_write(writeall(connstate->clientw->fd, buf, len));
207 ev_set_cb(connstate->clientw, read_client_x11_packet_cb);
208 ev_set_cb(connstate->serverw, read_server_x11_packet_cb);
209 ev_io_start(EV_A_ connstate->clientw);
213 errx(EXIT_FAILURE, "X11 protocol error: expected setup_failed.status in [0..2], got %d", setup_failed.status);
217 // https://www.x.org/releases/current/doc/xproto/x11protocol.html#request_format
222 } generic_x11_request_t;
224 // https://www.x.org/releases/current/doc/xproto/x11protocol.html#reply_format
226 uint8_t code; /* if 1, this is a reply. if 0, this is an error. else, an event */
230 } generic_x11_reply_t;
232 static void read_client_x11_packet_cb(EV_P_ ev_io *w, int revents) {
233 struct connstate *connstate = (struct connstate *)w->data;
235 void *request = smalloc(sizeof(generic_x11_request_t));
236 must_read(readall_into(request, sizeof(generic_x11_request_t), connstate->clientw->fd));
237 const size_t len = (((generic_x11_request_t *)request)->length * 4);
238 if (len > sizeof(generic_x11_request_t)) {
239 request = srealloc(request, len);
240 must_read(readall_into(request + sizeof(generic_x11_request_t),
241 len - sizeof(generic_x11_request_t),
242 connstate->clientw->fd));
245 // XXX: sequence counter wrapping is not implemented, but should not be
246 // necessary given that this tool is scoped for test cases.
247 connstate->sequence++;
249 /* BEGIN RandR 1.5 specific */
250 const uint8_t opcode = ((generic_x11_request_t *)request)->opcode;
251 if (opcode == XCB_QUERY_EXTENSION) {
252 xcb_query_extension_request_t *req = request;
253 const char *name = request + sizeof(xcb_query_extension_request_t);
254 if (req->name_len == strlen("RANDR") &&
255 strncmp(name, "RANDR", strlen("RANDR")) == 0) {
256 connstate->getext_randr = connstate->sequence;
258 } else if (opcode == connstate->randr_major_opcode) {
259 const uint8_t randr_opcode = ((generic_x11_request_t *)request)->pad0;
260 if (randr_opcode == XCB_RANDR_GET_MONITORS) {
261 connstate->getmonitors = connstate->sequence;
264 /* END RandR 1.5 specific */
266 must_write(writeall(connstate->serverw->fd, request, len));
270 static void read_server_x11_packet_cb(EV_P_ ev_io *w, int revents) {
271 struct connstate *connstate = (struct connstate *)w->data;
272 // all packets from the server are at least 32 bytes in length
274 void *packet = smalloc(len);
275 must_read(readall_into(packet, len, connstate->serverw->fd));
276 switch (((generic_x11_reply_t *)packet)->code) {
281 len += ((generic_x11_reply_t *)packet)->length * 4;
283 packet = srealloc(packet, len);
284 must_read(readall_into(packet + 32, len - 32, connstate->serverw->fd));
287 /* BEGIN RandR 1.5 specific */
288 const uint16_t sequence = ((generic_x11_reply_t *)packet)->sequence;
290 if (sequence == connstate->getext_randr) {
291 xcb_query_extension_reply_t *reply = packet;
292 connstate->randr_major_opcode = reply->major_opcode;
295 if (sequence == connstate->getmonitors) {
296 printf("RRGetMonitors reply!\n");
297 xcb_randr_get_monitors_reply_t *reply = packet;
298 if (injected_reply != NULL) {
299 printf("injecting reply\n");
300 ((generic_x11_reply_t *)injected_reply)->sequence = sequence;
301 must_write(writeall(connstate->clientw->fd, injected_reply, injected_reply_len));
306 /* END RandR 1.5 specific */
313 must_write(writeall(connstate->clientw->fd, packet, len));
317 static void child_cb(EV_P_ ev_child *w, int revents) {
318 ev_child_stop(EV_A_ w);
319 if (WIFEXITED(w->rstatus)) {
320 exit(WEXITSTATUS(w->rstatus));
322 exit(WTERMSIG(w->rstatus) + 128);
326 static void must_read_reply(const char *filename) {
328 if ((f = fopen(filename, "r")) == NULL) {
329 err(EXIT_FAILURE, "fopen(%s)", filename);
332 if (fstat(fileno(f), &stbuf) != 0) {
333 err(EXIT_FAILURE, "fstat(%s)", filename);
335 /* BEGIN RandR 1.5 specific */
336 injected_reply_len = stbuf.st_size;
337 injected_reply = smalloc(stbuf.st_size);
338 int n = fread(injected_reply, 1, stbuf.st_size, f);
339 /* END RandR 1.5 specific */
340 if (n != stbuf.st_size) {
341 err(EXIT_FAILURE, "fread(%s)", filename);
346 int main(int argc, char *argv[]) {
347 static struct option long_options[] = {
348 {"getmonitors_reply", required_argument, 0, 0},
351 char *options_string = "";
353 int option_index = 0;
355 while ((opt = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
358 if (strcmp(long_options[option_index].name, "getmonitors_reply") == 0) {
359 must_read_reply(optarg);
367 if (optind >= argc) {
368 errx(EXIT_FAILURE, "syntax: %s [options] <command>\n", argv[0]);
371 int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
373 err(EXIT_FAILURE, "socket(AF_UNIX)");
376 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
377 warn("Could not set FD_CLOEXEC");
380 struct sockaddr_un addr;
381 memset(&addr, 0, sizeof(struct sockaddr_un));
382 addr.sun_family = AF_UNIX;
385 for (i = 0; i < 100; i++) {
386 /* XXX: The path to X11 sockets differs on some platforms (e.g. Trusted
387 * Solaris, HPUX), but since libxcb doesn’t provide a function to
388 * generate the path, we’ll just have to hard-code it for now. */
389 snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/.X11-unix/X%d", i);
391 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
392 warn("bind(%s)", addr.sun_path);
395 /* Let the user know bind() was successful, so that they know the
396 * error messages can be disregarded. */
397 fprintf(stderr, "Successfuly bound to %s\n", addr.sun_path);
398 sun_path = sstrdup(addr.sun_path);
404 err(EXIT_FAILURE, "bind()");
407 atexit(cleanup_socket);
409 /* This program will be started for each testcase which requires it, so we
410 * expect precisely one connection. */
411 if (listen(fd, 1) == -1) {
412 err(EXIT_FAILURE, "listen()");
415 pid_t child = fork();
417 err(EXIT_FAILURE, "fork()");
421 sasprintf(&display, ":%d", i);
422 setenv("DISPLAY", display, 1);
425 char **child_args = argv + optind;
426 execvp(child_args[0], child_args);
427 err(EXIT_FAILURE, "exec()");
430 struct ev_loop *loop = ev_default_loop(0);
433 ev_child_init(&cw, child_cb, child, 0);
434 ev_child_start(loop, &cw);
437 ev_io_init(&watcher, uds_connection_cb, fd, EV_READ);
438 ev_io_start(loop, &watcher);