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 * 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, loaded_config_file_name_key;
22 static char *human_readable_version, *loaded_config_file_name;
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);
27 if (loaded_config_file_name_key)
28 sasprintf(&loaded_config_file_name, "%.*s", (int)len, val);
32 static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
33 human_readable_key = (stringlen == strlen("human_readable") &&
34 strncmp((const char *)stringval, "human_readable", strlen("human_readable")) == 0);
35 loaded_config_file_name_key = (stringlen == strlen("loaded_config_file_name") &&
36 strncmp((const char *)stringval, "loaded_config_file_name", strlen("loaded_config_file_name")) == 0);
40 static yajl_callbacks version_callbacks = {
41 .yajl_string = version_string,
42 .yajl_map_key = version_map_key,
46 * Connects to i3 to find out the currently running version. Useful since it
47 * might be different from the version compiled into this binary (maybe the
48 * user didn’t correctly install i3 or forgot te restart it).
50 * The output looks like this:
51 * Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
53 * The i3 binary you just called: /home/michael/i3/i3
54 * The i3 binary you are running: /home/michael/i3/i3
57 void display_running_version(void) {
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 int sockfd = ipc_connect(NULL);
71 if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
73 err(EXIT_FAILURE, "IPC: write()");
75 uint32_t reply_length;
79 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
81 err(EXIT_FAILURE, "IPC: read()");
85 if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
86 errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
88 yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
90 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, (int)reply_length);
91 if (state != yajl_status_ok)
92 errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply);
94 printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
96 if (loaded_config_file_name) {
100 printf("Loaded i3 config: %s", loaded_config_file_name);
101 if (stat(loaded_config_file_name, &sb) == -1) {
103 ELOG("Cannot stat config file \"%s\"\n", loaded_config_file_name);
105 strftime(mtime, sizeof(mtime), "%c", localtime(&(sb.st_mtime)));
107 printf(" (Last modified: %s, %.f seconds ago)\n", mtime, difftime(now, sb.st_mtime));
112 size_t destpath_size = 1024;
115 char *destpath = smalloc(destpath_size);
117 sasprintf(&exepath, "/proc/%d/exe", getpid());
119 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
120 destpath_size = destpath_size * 2;
121 destpath = srealloc(destpath, destpath_size);
124 err(EXIT_FAILURE, "readlink(%s)", exepath);
126 /* readlink() does not NULL-terminate strings, so we have to. */
127 destpath[linksize] = '\0';
130 printf("The i3 binary you just called: %s\n", destpath);
133 sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
135 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
136 destpath_size = destpath_size * 2;
137 destpath = srealloc(destpath, destpath_size);
140 err(EXIT_FAILURE, "readlink(%s)", exepath);
142 /* readlink() does not NULL-terminate strings, so we have to. */
143 destpath[linksize] = '\0';
145 /* Check if "(deleted)" is the readlink result. If so, the running version
146 * does not match the file on disk. */
147 if (strstr(destpath, "(deleted)") != NULL)
148 printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
150 /* Since readlink() might put a "(deleted)" somewhere in the buffer and
151 * stripping that out seems hackish and ugly, we read the process’s argv[0]
154 sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
157 if ((fd = open(exepath, O_RDONLY)) == -1)
158 err(EXIT_FAILURE, "open(%s)", exepath);
159 if (read(fd, destpath, sizeof(destpath)) == -1)
160 err(EXIT_FAILURE, "read(%s)", exepath);
163 printf("The i3 binary you are running: %s\n", destpath);