i3
display_version.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved dynamic 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 <sys/types.h>
14#include <sys/stat.h>
15#include <sys/wait.h>
16#include <sys/socket.h>
17#include <sys/un.h>
18#include <fcntl.h>
19#include <time.h>
20
23
24static int version_string(void *ctx, const unsigned char *val, size_t len) {
26 sasprintf(&human_readable_version, "%.*s", (int)len, val);
28 sasprintf(&loaded_config_file_name, "%.*s", (int)len, val);
29 return 1;
30}
31
32static 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);
37 return 1;
38}
39
40static yajl_callbacks version_callbacks = {
41 .yajl_string = version_string,
42 .yajl_map_key = version_map_key,
43};
44
45/*
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).
49 *
50 * The output looks like this:
51 * Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
52 *
53 * The i3 binary you just called: /home/michael/i3/i3
54 * The i3 binary you are running: /home/michael/i3/i3
55 *
56 */
58 if (getenv("DISPLAY") == NULL) {
59 fprintf(stderr, "\nYour DISPLAY environment variable is not set.\n");
60 fprintf(stderr, "Are you running i3 via SSH or on a virtual console?\n");
61 fprintf(stderr, "Try DISPLAY=:0 i3 --moreversion\n");
62 exit(EXIT_FAILURE);
63 }
64
65 char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen);
66 if (pid_from_atom == NULL) {
67 /* If I3_PID is not set, the running version is older than 4.2-200. */
68 printf("\nRunning version: < 4.2-200\n");
69 exit(EXIT_SUCCESS);
70 }
71
72 /* Inform the user of what we are doing. While a single IPC request is
73 * really fast normally, in case i3 hangs, this will not terminate. */
74 printf("(Getting version from running i3, press ctrl-c to abort…)");
75 fflush(stdout);
76
77 int sockfd = ipc_connect(NULL);
78 if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
79 (uint8_t *)"") == -1)
80 err(EXIT_FAILURE, "IPC: write()");
81
82 uint32_t reply_length;
83 uint32_t reply_type;
84 uint8_t *reply;
85 int ret;
86 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
87 if (ret == -1)
88 err(EXIT_FAILURE, "IPC: read()");
89 exit(EXIT_FAILURE);
90 }
91
92 if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
93 errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
94
95 yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
96
97 yajl_status state = yajl_parse(handle, (const unsigned char *)reply, (int)reply_length);
98 if (state != yajl_status_ok)
99 errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply);
100
101 printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
102
104 struct stat sb;
105 time_t now;
106 char mtime[64];
107 printf("Loaded i3 config: %s", loaded_config_file_name);
108 if (stat(loaded_config_file_name, &sb) == -1) {
109 printf("\n");
110 ELOG("Cannot stat config file \"%s\"\n", loaded_config_file_name);
111 } else {
112 strftime(mtime, sizeof(mtime), "%c", localtime(&(sb.st_mtime)));
113 time(&now);
114 printf(" (Last modified: %s, %.f seconds ago)\n", mtime, difftime(now, sb.st_mtime));
115 }
116 }
117
118#ifdef __linux__
119 size_t destpath_size = 1024;
120 ssize_t linksize;
121 char *exepath;
122 char *destpath = smalloc(destpath_size);
123
124 sasprintf(&exepath, "/proc/%d/exe", getpid());
125
126 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
127 destpath_size = destpath_size * 2;
128 destpath = srealloc(destpath, destpath_size);
129 }
130 if (linksize == -1)
131 err(EXIT_FAILURE, "readlink(%s)", exepath);
132
133 /* readlink() does not NULL-terminate strings, so we have to. */
134 destpath[linksize] = '\0';
135
136 printf("\n");
137 printf("The i3 binary you just called: %s\n", destpath);
138
139 free(exepath);
140 sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
141
142 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
143 destpath_size = destpath_size * 2;
144 destpath = srealloc(destpath, destpath_size);
145 }
146 if (linksize == -1)
147 err(EXIT_FAILURE, "readlink(%s)", exepath);
148
149 /* readlink() does not NULL-terminate strings, so we have to. */
150 destpath[linksize] = '\0';
151
152 /* Check if "(deleted)" is the readlink result. If so, the running version
153 * does not match the file on disk. */
154 if (strstr(destpath, "(deleted)") != NULL)
155 printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
156
157 /* Since readlink() might put a "(deleted)" somewhere in the buffer and
158 * stripping that out seems hackish and ugly, we read the process’s argv[0]
159 * instead. */
160 free(exepath);
161 sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
162
163 int fd;
164 if ((fd = open(exepath, O_RDONLY)) == -1)
165 err(EXIT_FAILURE, "open(%s)", exepath);
166 if (read(fd, destpath, sizeof(destpath)) == -1)
167 err(EXIT_FAILURE, "read(%s)", exepath);
168 close(fd);
169
170 printf("The i3 binary you are running: %s\n", destpath);
171
172 free(exepath);
173 free(destpath);
174#endif
175
176 yajl_free(handle);
177 free(reply);
178 free(pid_from_atom);
179}
static cmdp_state state
static yajl_callbacks version_callbacks
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.
int conn_screen
Definition: main.c:46
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:44
static xcb_cursor_context_t * ctx
Definition: xcursor.c:19
#define ELOG(fmt,...)
Definition: libi3.h:99
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...
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...