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;
25 static int version_string(void *ctx, const unsigned char *val, size_t len) {
27 static int version_string(void *ctx, const unsigned char *val, unsigned int len) {
29 if (human_readable_key)
30 sasprintf(&human_readable_version, "%.*s", (int)len, val);
35 static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
37 static int version_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) {
39 human_readable_key = (stringlen == strlen("human_readable") &&
40 strncmp((const char*)stringval, "human_readable", strlen("human_readable")) == 0);
44 static yajl_callbacks version_callbacks = {
54 NULL, /* start_array */
59 * Connects to i3 to find out the currently running version. Useful since it
60 * might be different from the version compiled into this binary (maybe the
61 * user didn’t correctly install i3 or forgot te restart it).
63 * The output looks like this:
64 * Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
66 * The i3 binary you just called: /home/michael/i3/i3
67 * The i3 binary you are running: /home/michael/i3/i3
70 void display_running_version(void) {
71 char *socket_path = root_atom_contents("I3_SOCKET_PATH");
72 if (socket_path == NULL)
75 char *pid_from_atom = root_atom_contents("I3_PID");
76 if (pid_from_atom == NULL) {
77 /* If I3_PID is not set, the running version is older than 4.2-200. */
78 printf("\nRunning version: < 4.2-200\n");
82 /* Inform the user of what we are doing. While a single IPC request is
83 * really fast normally, in case i3 hangs, this will not terminate. */
84 printf("(Getting version from running i3, press ctrl-c to abort…)");
87 /* TODO: refactor this with the code for sending commands */
88 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
90 err(EXIT_FAILURE, "Could not create socket");
92 struct sockaddr_un addr;
93 memset(&addr, 0, sizeof(struct sockaddr_un));
94 addr.sun_family = AF_LOCAL;
95 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
96 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
97 err(EXIT_FAILURE, "Could not connect to i3");
99 if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
101 err(EXIT_FAILURE, "IPC: write()");
103 uint32_t reply_length;
107 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
109 err(EXIT_FAILURE, "IPC: read()");
113 if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
114 errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
117 yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
119 yajl_parser_config parse_conf = { 0, 0 };
121 yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, NULL);
124 yajl_status state = yajl_parse(handle, (const unsigned char*)reply, (int)reply_length);
125 if (state != yajl_status_ok)
126 errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply);
128 printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
131 char exepath[PATH_MAX],
135 snprintf(exepath, sizeof(exepath), "/proc/%d/exe", getpid());
137 if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
138 err(EXIT_FAILURE, "readlink(%s)", exepath);
140 /* readlink() does not NULL-terminate strings, so we have to. */
141 destpath[linksize] = '\0';
144 printf("The i3 binary you just called: %s\n", destpath);
146 snprintf(exepath, sizeof(exepath), "/proc/%s/exe", pid_from_atom);
148 if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
149 err(EXIT_FAILURE, "readlink(%s)", exepath);
151 /* readlink() does not NULL-terminate strings, so we have to. */
152 destpath[linksize] = '\0';
154 /* Check if "(deleted)" is the readlink result. If so, the running version
155 * does not match the file on disk. */
156 if (strstr(destpath, "(deleted)") != NULL)
157 printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
159 /* Since readlink() might put a "(deleted)" somewhere in the buffer and
160 * stripping that out seems hackish and ugly, we read the process’s argv[0]
162 snprintf(exepath, sizeof(exepath), "/proc/%s/cmdline", pid_from_atom);
165 if ((fd = open(exepath, O_RDONLY)) == -1)
166 err(EXIT_FAILURE, "open(%s)", exepath);
167 if (read(fd, destpath, sizeof(destpath)) == -1)
168 err(EXIT_FAILURE, "read(%s)", exepath);
171 printf("The i3 binary you are running: %s\n", destpath);