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 struct injected_reply {
49 /* BEGIN RandR 1.5 specific */
50 static struct injected_reply getmonitors_reply = {NULL, 0};
51 static struct injected_reply getoutputinfo_reply = {NULL, 0};
52 /* END RandR 1.5 specific */
54 #define XCB_PAD(i) (-(i)&3)
57 /* clientw is a libev watcher for the connection which we accept()ed. */
60 /* serverw is a libev watcher for the connection to X11 which we initiated
61 * on behalf of the client. */
64 /* sequence is the client-side sequence number counter. In X11’s wire
65 * encoding, sequence counters are not included in requests, only in
69 /* BEGIN RandR 1.5 specific */
70 /* sequence number of the most recent GetExtension request for RANDR */
72 /* sequence number of the most recent RRGetMonitors request */
74 /* sequence number of the most recent RRGetOutputInfo request */
77 int randr_major_opcode;
78 /* END RandR 1.5 specific */
83 * Returns -1 on error (with errno from read() untouched)
86 static size_t readall_into(void *buffer, const size_t len, int fd) {
87 size_t read_bytes = 0;
88 while (read_bytes < len) {
89 ssize_t n = read(fd, buffer + read_bytes, len - read_bytes);
93 read_bytes += (size_t)n;
99 * Exits the program with an error if the read failed.
102 static void must_read(int n) {
104 err(EXIT_FAILURE, "read()");
107 errx(EXIT_FAILURE, "EOF");
112 * Exits the program with an error if the write failed.
115 static void must_write(int n) {
117 err(EXIT_FAILURE, "write()");
121 static void uds_connection_cb(EV_P_ ev_io *w, int revents) {
122 struct sockaddr_un addr;
123 socklen_t addrlen = sizeof(addr);
124 const int clientfd = accept(w->fd, (struct sockaddr *)&addr, &addrlen);
125 if (clientfd == -1) {
126 if (errno == EINTR) {
129 err(EXIT_FAILURE, "accept()");
132 struct connstate *connstate = scalloc(1, sizeof(struct connstate));
134 ev_io *clientw = scalloc(1, sizeof(ev_io));
135 connstate->clientw = clientw;
136 clientw->data = connstate;
137 ev_io_init(clientw, read_client_setup_request_cb, clientfd, EV_READ);
138 ev_io_start(EV_A_ clientw);
141 // https://www.x.org/releases/current/doc/xproto/x11protocol.html#Encoding::Connection_Setup
142 static void read_client_setup_request_cb(EV_P_ ev_io *w, int revents) {
144 struct connstate *connstate = (struct connstate *)w->data;
146 /* Read X11 setup request in its entirety. */
147 xcb_setup_request_t setup_request;
148 must_read(readall_into(&setup_request, sizeof(setup_request), w->fd));
150 /* Establish a connection to X11. */
151 int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
153 err(EXIT_FAILURE, "socket()");
158 if (xcb_parse_display(getenv("DISPLAY"), &host, &displayp, NULL) == 0) {
159 errx(EXIT_FAILURE, "Could not parse DISPLAY=%s", getenv("DISPLAY"));
163 struct sockaddr_un addr;
164 memset(&addr, 0, sizeof(addr));
165 addr.sun_family = AF_LOCAL;
166 snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/.X11-unix/X%d", displayp);
167 if (connect(fd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
168 err(EXIT_FAILURE, "connect(%s)", addr.sun_path);
171 /* Relay setup request. */
172 must_write(writeall(fd, &setup_request, sizeof(setup_request)));
174 if (setup_request.authorization_protocol_name_len > 0 ||
175 setup_request.authorization_protocol_data_len > 0) {
176 const size_t authlen = setup_request.authorization_protocol_name_len +
177 XCB_PAD(setup_request.authorization_protocol_name_len) +
178 setup_request.authorization_protocol_data_len +
179 XCB_PAD(setup_request.authorization_protocol_data_len);
180 void *buf = smalloc(authlen);
181 must_read(readall_into(buf, authlen, w->fd));
182 must_write(writeall(fd, buf, authlen));
186 /* Wait for a response from the X11 server. */
187 ev_io *serverw = scalloc(1, sizeof(ev_io));
188 connstate->serverw = serverw;
189 serverw->data = connstate;
190 ev_io_init(serverw, read_server_setup_reply_cb, fd, EV_READ);
191 ev_io_start(EV_A_ serverw);
194 static void read_server_setup_reply_cb(EV_P_ ev_io *w, int revents) {
195 struct connstate *connstate = (struct connstate *)w->data;
196 xcb_setup_failed_t setup_failed;
197 must_read(readall_into(&setup_failed, sizeof(setup_failed), w->fd));
199 switch (setup_failed.status) {
201 errx(EXIT_FAILURE, "error authenticating at the X11 server");
204 errx(EXIT_FAILURE, "two-factor auth not implemented");
207 must_write(writeall(connstate->clientw->fd, &setup_failed, sizeof(xcb_setup_failed_t)));
208 const size_t len = (setup_failed.length * 4);
209 void *buf = smalloc(len);
210 must_read(readall_into(buf, len, w->fd));
211 must_write(writeall(connstate->clientw->fd, buf, len));
214 ev_set_cb(connstate->clientw, read_client_x11_packet_cb);
215 ev_set_cb(connstate->serverw, read_server_x11_packet_cb);
216 ev_io_start(EV_A_ connstate->clientw);
220 errx(EXIT_FAILURE, "X11 protocol error: expected setup_failed.status in [0..2], got %d", setup_failed.status);
224 // https://www.x.org/releases/current/doc/xproto/x11protocol.html#request_format
229 } generic_x11_request_t;
231 // https://www.x.org/releases/current/doc/xproto/x11protocol.html#reply_format
233 uint8_t code; /* if 1, this is a reply. if 0, this is an error. else, an event */
237 } generic_x11_reply_t;
239 static void read_client_x11_packet_cb(EV_P_ ev_io *w, int revents) {
240 struct connstate *connstate = (struct connstate *)w->data;
242 void *request = smalloc(sizeof(generic_x11_request_t));
243 must_read(readall_into(request, sizeof(generic_x11_request_t), connstate->clientw->fd));
244 const size_t len = (((generic_x11_request_t *)request)->length * 4);
245 if (len > sizeof(generic_x11_request_t)) {
246 request = srealloc(request, len);
247 must_read(readall_into(request + sizeof(generic_x11_request_t),
248 len - sizeof(generic_x11_request_t),
249 connstate->clientw->fd));
252 // XXX: sequence counter wrapping is not implemented, but should not be
253 // necessary given that this tool is scoped for test cases.
254 connstate->sequence++;
256 /* BEGIN RandR 1.5 specific */
257 const uint8_t opcode = ((generic_x11_request_t *)request)->opcode;
258 if (opcode == XCB_QUERY_EXTENSION) {
259 xcb_query_extension_request_t *req = request;
260 const char *name = request + sizeof(xcb_query_extension_request_t);
261 if (req->name_len == strlen("RANDR") &&
262 strncmp(name, "RANDR", strlen("RANDR")) == 0) {
263 connstate->getext_randr = connstate->sequence;
265 } else if (opcode == connstate->randr_major_opcode) {
266 const uint8_t randr_opcode = ((generic_x11_request_t *)request)->pad0;
267 if (randr_opcode == XCB_RANDR_GET_MONITORS) {
268 connstate->getmonitors = connstate->sequence;
269 } else if (randr_opcode == XCB_RANDR_GET_OUTPUT_INFO) {
270 connstate->getoutputinfo = connstate->sequence;
273 /* END RandR 1.5 specific */
275 must_write(writeall(connstate->serverw->fd, request, len));
279 static bool handle_sequence(struct connstate *connstate, uint16_t sequence) {
280 /* BEGIN RandR 1.5 specific */
281 if (sequence == connstate->getmonitors) {
282 printf("RRGetMonitors reply!\n");
283 if (getmonitors_reply.buf != NULL) {
284 printf("injecting reply\n");
285 ((generic_x11_reply_t *)getmonitors_reply.buf)->sequence = sequence;
286 must_write(writeall(connstate->clientw->fd, getmonitors_reply.buf, getmonitors_reply.len));
291 if (sequence == connstate->getoutputinfo) {
292 printf("RRGetOutputInfo reply!\n");
293 if (getoutputinfo_reply.buf != NULL) {
294 printf("injecting reply\n");
295 ((generic_x11_reply_t *)getoutputinfo_reply.buf)->sequence = sequence;
296 must_write(writeall(connstate->clientw->fd, getoutputinfo_reply.buf, getoutputinfo_reply.len));
300 /* END RandR 1.5 specific */
305 static void read_server_x11_packet_cb(EV_P_ ev_io *w, int revents) {
306 struct connstate *connstate = (struct connstate *)w->data;
307 // all packets from the server are at least 32 bytes in length
309 void *packet = smalloc(len);
310 must_read(readall_into(packet, len, connstate->serverw->fd));
311 switch (((generic_x11_reply_t *)packet)->code) {
313 const uint16_t sequence = ((xcb_request_error_t *)packet)->sequence;
314 if (handle_sequence(connstate, sequence)) {
321 len += ((generic_x11_reply_t *)packet)->length * 4;
323 packet = srealloc(packet, len);
324 must_read(readall_into(packet + 32, len - 32, connstate->serverw->fd));
327 /* BEGIN RandR 1.5 specific */
328 const uint16_t sequence = ((generic_x11_reply_t *)packet)->sequence;
330 if (sequence == connstate->getext_randr) {
331 xcb_query_extension_reply_t *reply = packet;
332 connstate->randr_major_opcode = reply->major_opcode;
334 /* END RandR 1.5 specific */
336 if (handle_sequence(connstate, sequence)) {
346 must_write(writeall(connstate->clientw->fd, packet, len));
350 static void child_cb(EV_P_ ev_child *w, int revents) {
351 ev_child_stop(EV_A_ w);
352 if (WIFEXITED(w->rstatus)) {
353 exit(WEXITSTATUS(w->rstatus));
355 exit(WTERMSIG(w->rstatus) + 128);
359 static void must_read_reply(const char *filename, struct injected_reply *reply) {
361 if ((f = fopen(filename, "r")) == NULL) {
362 err(EXIT_FAILURE, "fopen(%s)", filename);
365 if (fstat(fileno(f), &stbuf) != 0) {
366 err(EXIT_FAILURE, "fstat(%s)", filename);
368 reply->len = stbuf.st_size;
369 reply->buf = smalloc(stbuf.st_size);
370 int n = fread(reply->buf, 1, stbuf.st_size, f);
371 if (n != stbuf.st_size) {
372 err(EXIT_FAILURE, "fread(%s)", filename);
377 int main(int argc, char *argv[]) {
378 static struct option long_options[] = {
379 {"getmonitors_reply", required_argument, 0, 0},
380 {"getoutputinfo_reply", required_argument, 0, 0},
383 char *options_string = "";
385 int option_index = 0;
387 while ((opt = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
390 const char *option_name = long_options[option_index].name;
391 if (strcmp(option_name, "getmonitors_reply") == 0) {
392 must_read_reply(optarg, &getmonitors_reply);
393 } else if (strcmp(option_name, "getoutputinfo_reply") == 0) {
394 must_read_reply(optarg, &getoutputinfo_reply);
403 if (optind >= argc) {
404 errx(EXIT_FAILURE, "syntax: %s [options] <command>\n", argv[0]);
407 int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
409 err(EXIT_FAILURE, "socket(AF_UNIX)");
412 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
413 warn("Could not set FD_CLOEXEC");
416 struct sockaddr_un addr;
417 memset(&addr, 0, sizeof(struct sockaddr_un));
418 addr.sun_family = AF_UNIX;
421 for (i = 0; i < 100; i++) {
422 /* XXX: The path to X11 sockets differs on some platforms (e.g. Trusted
423 * Solaris, HPUX), but since libxcb doesn’t provide a function to
424 * generate the path, we’ll just have to hard-code it for now. */
425 snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/.X11-unix/X%d", i);
427 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
428 warn("bind(%s)", addr.sun_path);
431 /* Let the user know bind() was successful, so that they know the
432 * error messages can be disregarded. */
433 fprintf(stderr, "Successfuly bound to %s\n", addr.sun_path);
434 sun_path = sstrdup(addr.sun_path);
440 err(EXIT_FAILURE, "bind()");
443 atexit(cleanup_socket);
445 /* This program will be started for each testcase which requires it, so we
446 * expect precisely one connection. */
447 if (listen(fd, 1) == -1) {
448 err(EXIT_FAILURE, "listen()");
451 pid_t child = fork();
453 err(EXIT_FAILURE, "fork()");
457 sasprintf(&display, ":%d", i);
458 setenv("DISPLAY", display, 1);
461 char **child_args = argv + optind;
462 execvp(child_args[0], child_args);
463 err(EXIT_FAILURE, "exec()");
466 struct ev_loop *loop = ev_default_loop(0);
469 ev_child_init(&cw, child_cb, child, 0);
470 ev_child_start(loop, &cw);
473 ev_io_init(&watcher, uds_connection_cb, fd, EV_READ);
474 ev_io_start(loop, &watcher);