i3
display_version.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "key_press.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * display_version.c: displays the running i3 version, runs as part of
10  * i3 --moreversion.
11  *
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 #include "all.h"
21 
24 
25 static int version_string(void *ctx, const unsigned char *val, size_t len) {
27  sasprintf(&human_readable_version, "%.*s", (int)len, val);
29  sasprintf(&loaded_config_file_name, "%.*s", (int)len, val);
30  return 1;
31 }
32 
33 static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
34  human_readable_key = (stringlen == strlen("human_readable") &&
35  strncmp((const char *)stringval, "human_readable", strlen("human_readable")) == 0);
36  loaded_config_file_name_key = (stringlen == strlen("loaded_config_file_name") &&
37  strncmp((const char *)stringval, "loaded_config_file_name", strlen("loaded_config_file_name")) == 0);
38  return 1;
39 }
40 
41 static yajl_callbacks version_callbacks = {
42  .yajl_string = version_string,
43  .yajl_map_key = version_map_key,
44 };
45 
46 /*
47  * Connects to i3 to find out the currently running version. Useful since it
48  * might be different from the version compiled into this binary (maybe the
49  * user didn’t correctly install i3 or forgot te restart it).
50  *
51  * The output looks like this:
52  * Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
53  *
54  * The i3 binary you just called: /home/michael/i3/i3
55  * The i3 binary you are running: /home/michael/i3/i3
56  *
57  */
59  char *socket_path = root_atom_contents("I3_SOCKET_PATH", conn, conn_screen);
60  if (socket_path == NULL)
61  exit(EXIT_SUCCESS);
62 
63  char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen);
64  if (pid_from_atom == NULL) {
65  /* If I3_PID is not set, the running version is older than 4.2-200. */
66  printf("\nRunning version: < 4.2-200\n");
67  exit(EXIT_SUCCESS);
68  }
69 
70  /* Inform the user of what we are doing. While a single IPC request is
71  * really fast normally, in case i3 hangs, this will not terminate. */
72  printf("(Getting version from running i3, press ctrl-c to abort…)");
73  fflush(stdout);
74 
75  /* TODO: refactor this with the code for sending commands */
76  int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
77  if (sockfd == -1)
78  err(EXIT_FAILURE, "Could not create socket");
79 
80  struct sockaddr_un addr;
81  memset(&addr, 0, sizeof(struct sockaddr_un));
82  addr.sun_family = AF_LOCAL;
83  strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
84  if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
85  err(EXIT_FAILURE, "Could not connect to i3");
86 
87  if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
88  (uint8_t *)"") == -1)
89  err(EXIT_FAILURE, "IPC: write()");
90 
91  uint32_t reply_length;
92  uint32_t reply_type;
93  uint8_t *reply;
94  int ret;
95  if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
96  if (ret == -1)
97  err(EXIT_FAILURE, "IPC: read()");
98  exit(EXIT_FAILURE);
99  }
100 
101  if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
102  errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
103 
104  yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
105 
106  yajl_status state = yajl_parse(handle, (const unsigned char *)reply, (int)reply_length);
107  if (state != yajl_status_ok)
108  errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply);
109 
110  printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
111 
113  struct stat sb;
114  time_t now;
115  char mtime[64];
116  printf("Loaded i3 config: %s", loaded_config_file_name);
117  if (stat(loaded_config_file_name, &sb) == -1) {
118  printf("\n");
119  ELOG("Cannot stat config file \"%s\"\n", loaded_config_file_name);
120  } else {
121  strftime(mtime, sizeof(mtime), "%c", localtime(&(sb.st_mtime)));
122  time(&now);
123  printf(" (Last modified: %s, %.f seconds ago)\n", mtime, difftime(now, sb.st_mtime));
124  }
125  }
126 
127 #ifdef __linux__
128  size_t destpath_size = 1024;
129  ssize_t linksize;
130  char *exepath;
131  char *destpath = smalloc(destpath_size);
132 
133  sasprintf(&exepath, "/proc/%d/exe", getpid());
134 
135  while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
136  destpath_size = destpath_size * 2;
137  destpath = srealloc(destpath, destpath_size);
138  }
139  if (linksize == -1)
140  err(EXIT_FAILURE, "readlink(%s)", exepath);
141 
142  /* readlink() does not NULL-terminate strings, so we have to. */
143  destpath[linksize] = '\0';
144 
145  printf("\n");
146  printf("The i3 binary you just called: %s\n", destpath);
147 
148  free(exepath);
149  sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
150 
151  while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
152  destpath_size = destpath_size * 2;
153  destpath = srealloc(destpath, destpath_size);
154  }
155  if (linksize == -1)
156  err(EXIT_FAILURE, "readlink(%s)", exepath);
157 
158  /* readlink() does not NULL-terminate strings, so we have to. */
159  destpath[linksize] = '\0';
160 
161  /* Check if "(deleted)" is the readlink result. If so, the running version
162  * does not match the file on disk. */
163  if (strstr(destpath, "(deleted)") != NULL)
164  printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
165 
166  /* Since readlink() might put a "(deleted)" somewhere in the buffer and
167  * stripping that out seems hackish and ugly, we read the process’s argv[0]
168  * instead. */
169  free(exepath);
170  sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
171 
172  int fd;
173  if ((fd = open(exepath, O_RDONLY)) == -1)
174  err(EXIT_FAILURE, "open(%s)", exepath);
175  if (read(fd, destpath, sizeof(destpath)) == -1)
176  err(EXIT_FAILURE, "read(%s)", exepath);
177  close(fd);
178 
179  printf("The i3 binary you are running: %s\n", destpath);
180 
181  free(exepath);
182  free(destpath);
183 #endif
184 
185  yajl_free(handle);
186 }
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 display_running_version(void)
Connects to i3 to find out the currently running version.
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...
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
int conn_screen
Definition: main.c:45
static cmdp_state state
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:43
static char * loaded_config_file_name
static bool loaded_config_file_name_key
#define ELOG(fmt,...)
Definition: libi3.h:93
static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen)
static xcb_cursor_context_t * ctx
Definition: xcursor.c:19
static yajl_callbacks version_callbacks
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
static int version_string(void *ctx, const unsigned char *val, size_t len)
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...
static bool human_readable_key
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...
static char * human_readable_version