2 #define I3__FILE__ "key_press.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * display_version.c: displays the running i3 version, runs as part of
13 #include <sys/types.h>
16 #include <sys/socket.h>
21 static bool human_readable_key;
22 static char *human_readable_version;
24 static int version_string(void *ctx, const unsigned char *val, size_t len) {
25 if (human_readable_key)
26 sasprintf(&human_readable_version, "%.*s", (int)len, val);
30 static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
31 human_readable_key = (stringlen == strlen("human_readable") &&
32 strncmp((const char *)stringval, "human_readable", strlen("human_readable")) == 0);
36 static yajl_callbacks version_callbacks = {
37 .yajl_string = version_string,
38 .yajl_map_key = version_map_key,
42 * Connects to i3 to find out the currently running version. Useful since it
43 * might be different from the version compiled into this binary (maybe the
44 * user didn’t correctly install i3 or forgot te restart it).
46 * The output looks like this:
47 * Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
49 * The i3 binary you just called: /home/michael/i3/i3
50 * The i3 binary you are running: /home/michael/i3/i3
53 void display_running_version(void) {
54 char *socket_path = root_atom_contents("I3_SOCKET_PATH", conn, conn_screen);
55 if (socket_path == NULL)
58 char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen);
59 if (pid_from_atom == NULL) {
60 /* If I3_PID is not set, the running version is older than 4.2-200. */
61 printf("\nRunning version: < 4.2-200\n");
65 /* Inform the user of what we are doing. While a single IPC request is
66 * really fast normally, in case i3 hangs, this will not terminate. */
67 printf("(Getting version from running i3, press ctrl-c to abort…)");
70 /* TODO: refactor this with the code for sending commands */
71 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
73 err(EXIT_FAILURE, "Could not create socket");
75 struct sockaddr_un addr;
76 memset(&addr, 0, sizeof(struct sockaddr_un));
77 addr.sun_family = AF_LOCAL;
78 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
79 if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
80 err(EXIT_FAILURE, "Could not connect to i3");
82 if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
84 err(EXIT_FAILURE, "IPC: write()");
86 uint32_t reply_length;
90 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
92 err(EXIT_FAILURE, "IPC: read()");
96 if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
97 errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
99 yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
101 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, (int)reply_length);
102 if (state != yajl_status_ok)
103 errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply);
105 printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
108 size_t destpath_size = 1024;
111 char *destpath = smalloc(destpath_size);
113 sasprintf(&exepath, "/proc/%d/exe", getpid());
115 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
116 destpath_size = destpath_size * 2;
117 destpath = srealloc(destpath, destpath_size);
120 err(EXIT_FAILURE, "readlink(%s)", exepath);
122 /* readlink() does not NULL-terminate strings, so we have to. */
123 destpath[linksize] = '\0';
126 printf("The i3 binary you just called: %s\n", destpath);
129 sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
131 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
132 destpath_size = destpath_size * 2;
133 destpath = srealloc(destpath, destpath_size);
136 err(EXIT_FAILURE, "readlink(%s)", exepath);
138 /* readlink() does not NULL-terminate strings, so we have to. */
139 destpath[linksize] = '\0';
141 /* Check if "(deleted)" is the readlink result. If so, the running version
142 * does not match the file on disk. */
143 if (strstr(destpath, "(deleted)") != NULL)
144 printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
146 /* Since readlink() might put a "(deleted)" somewhere in the buffer and
147 * stripping that out seems hackish and ugly, we read the process’s argv[0]
150 sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
153 if ((fd = open(exepath, O_RDONLY)) == -1)
154 err(EXIT_FAILURE, "open(%s)", exepath);
155 if (read(fd, destpath, sizeof(destpath)) == -1)
156 err(EXIT_FAILURE, "read(%s)", exepath);
159 printf("The i3 binary you are running: %s\n", destpath);