i3
display_version.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
7 * display_version.c: displays the running i3 version, runs as part of
8 * i3 --moreversion.
9 *
10 */
11#include "all.h"
12
13#include <fcntl.h>
14#include <time.h>
15#include <unistd.h>
16
20
23
24static int version_string(void *ctx, const unsigned char *val, size_t len) {
26 sasprintf(&human_readable_version, "%.*s", (int)len, val);
27 }
29 sasprintf(&loaded_config_file_name, "%.*s", (int)len, val);
30 }
32 IncludedFile *file = scalloc(1, sizeof(IncludedFile));
33 sasprintf(&(file->path), "%.*s", (int)len, val);
34 TAILQ_INSERT_TAIL(&included_files, file, files);
35 }
36 return 1;
37}
38
39static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
40#define KEY_MATCHES(x) (stringlen == strlen(x) && strncmp((const char *)stringval, x, strlen(x)) == 0)
41 human_readable_key = KEY_MATCHES("human_readable");
42 loaded_config_file_name_key = KEY_MATCHES("loaded_config_file_name");
43 included_config_file_names = KEY_MATCHES("included_config_file_names");
44#undef KEY_MATCHES
45 return 1;
46}
47
48static yajl_callbacks version_callbacks = {
49 .yajl_string = version_string,
50 .yajl_map_key = version_map_key,
51};
52
53static void print_config_path(const char *path, const char *role) {
54 struct stat sb;
55 time_t now;
56 char mtime[64];
57
58 printf(" %s (%s)", path, role);
59 if (stat(path, &sb) == -1) {
60 printf("\n");
61 ELOG("Cannot stat config file \"%s\"\n", path);
62 } else {
63 strftime(mtime, sizeof(mtime), "%c", localtime(&(sb.st_mtime)));
64 time(&now);
65 printf(" (last modified: %s, %.f seconds ago)\n", mtime, difftime(now, sb.st_mtime));
66 }
67}
68
69/*
70 * Connects to i3 to find out the currently running version. Useful since it
71 * might be different from the version compiled into this binary (maybe the
72 * user didn’t correctly install i3 or forgot to restart it).
73 *
74 * The output looks like this:
75 * Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
76 *
77 * The i3 binary you just called: /home/michael/i3/i3
78 * The i3 binary you are running: /home/michael/i3/i3
79 *
80 */
82 if (getenv("DISPLAY") == NULL) {
83 fprintf(stderr, "\nYour DISPLAY environment variable is not set.\n");
84 fprintf(stderr, "Are you running i3 via SSH or on a virtual console?\n");
85 fprintf(stderr, "Try DISPLAY=:0 i3 --moreversion\n");
86 exit(EXIT_FAILURE);
87 }
88
89 char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen);
90 if (pid_from_atom == NULL) {
91 /* If I3_PID is not set, the running version is older than 4.2-200. */
92 printf("\nRunning version: < 4.2-200\n");
93 exit(EXIT_SUCCESS);
94 }
95
96 /* Inform the user of what we are doing. While a single IPC request is
97 * really fast normally, in case i3 hangs, this will not terminate. */
98 printf("(Getting version from running i3, press ctrl-c to abort…)");
99 fflush(stdout);
100
101 int sockfd = ipc_connect(NULL);
102 if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
103 (uint8_t *)"") == -1) {
104 err(EXIT_FAILURE, "IPC: write()");
105 }
106
107 uint32_t reply_length;
108 uint32_t reply_type;
109 uint8_t *reply;
110 int ret;
111 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
112 if (ret == -1) {
113 err(EXIT_FAILURE, "IPC: read()");
114 }
115 exit(EXIT_FAILURE);
116 }
117
118 if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION) {
119 errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
120 }
121
122 yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
123
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);
127 }
128
129 printf("\r\x1b[K");
130 printf("Running i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
131
133 printf("Loaded i3 config:\n");
135 IncludedFile *file;
136 TAILQ_FOREACH (file, &included_files, files) {
137 print_config_path(file->path, "included");
138 }
139 }
140
141#ifdef __linux__
142 size_t destpath_size = 1024;
143 ssize_t linksize;
144 char *exepath;
145 char *destpath = smalloc(destpath_size);
146
147 sasprintf(&exepath, "/proc/%d/exe", getpid());
148
149 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
150 destpath_size = destpath_size * 2;
151 destpath = srealloc(destpath, destpath_size);
152 }
153 if (linksize == -1) {
154 err(EXIT_FAILURE, "readlink(%s)", exepath);
155 }
156
157 /* readlink() does not NULL-terminate strings, so we have to. */
158 destpath[linksize] = '\0';
159
160 printf("\n");
161 printf("The i3 binary you just called: %s\n", destpath);
162
163 free(exepath);
164 sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
165
166 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
167 destpath_size = destpath_size * 2;
168 destpath = srealloc(destpath, destpath_size);
169 }
170 if (linksize == -1) {
171 err(EXIT_FAILURE, "readlink(%s)", exepath);
172 }
173
174 /* readlink() does not NULL-terminate strings, so we have to. */
175 destpath[linksize] = '\0';
176
177 /* Check if "(deleted)" is the readlink result. If so, the running version
178 * does not match the file on disk. */
179 if (strstr(destpath, "(deleted)") != NULL) {
180 printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
181 }
182
183 /* Since readlink() might put a "(deleted)" somewhere in the buffer and
184 * stripping that out seems hackish and ugly, we read the process’s argv[0]
185 * instead. */
186 free(exepath);
187 sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
188
189 int fd;
190 if ((fd = open(exepath, O_RDONLY)) == -1) {
191 err(EXIT_FAILURE, "open(%s)", exepath);
192 }
193 if (read(fd, destpath, sizeof(destpath)) == -1) {
194 err(EXIT_FAILURE, "read(%s)", exepath);
195 }
196 close(fd);
197
198 printf("The i3 binary you are running: %s\n", destpath);
199
200 free(exepath);
201 free(destpath);
202#endif
203
204 yajl_free(handle);
205 free(reply);
206 free(pid_from_atom);
207}
static cmdp_state state
struct includedfiles_head included_files
Definition config.c:22
static yajl_callbacks version_callbacks
static bool included_config_file_names
static void print_config_path(const char *path, const char *role)
#define KEY_MATCHES(x)
static char * loaded_config_file_name
static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen)
static int version_string(void *ctx, const unsigned char *val, size_t len)
static bool human_readable_key
static char * human_readable_version
static bool loaded_config_file_name_key
void display_running_version(void)
Connects to i3 to find out the currently running version.
static xcb_cursor_context_t * ctx
Definition xcursor.c:19
int conn_screen
Definition main.c:56
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
#define ELOG(fmt,...)
Definition libi3.h:100
int ipc_recv_message(int sockfd, uint32_t *message_type, uint32_t *reply_length, uint8_t **reply)
Reads a message from the given socket file descriptor and stores its length (reply_length) as well as...
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
char * root_atom_contents(const char *atomname, xcb_connection_t *provided_conn, int screen)
Try to get the contents of the given atom (for example I3_SOCKET_PATH) from the X11 root window and r...
int ipc_connect(const char *socket_path)
Connects to the i3 IPC socket and returns the file descriptor for the socket.
int ipc_send_message(int sockfd, const uint32_t message_size, const uint32_t message_type, const uint8_t *payload)
Formats a message (payload) of the given size and type and sends it to i3 via the given socket file d...
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:376
List entry struct for an included file.