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-2012 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 "all.h"
20 
21 static bool human_readable_key;
23 
24 static int version_string(void *ctx, const unsigned char *val, size_t len) {
26  sasprintf(&human_readable_version, "%.*s", (int)len, val);
27  return 1;
28 }
29 
30 static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
31  human_readable_key = (stringlen == strlen("human_readable") &&
32  strncmp((const char *)stringval, "human_readable", strlen("human_readable")) == 0);
33  return 1;
34 }
35 
36 static yajl_callbacks version_callbacks = {
37  .yajl_string = version_string,
38  .yajl_map_key = version_map_key,
39 };
40 
41 /*
42  * Connects to i3 to find out the currently running version. Useful since it
43  * might be different from the version compiled into this binary (maybe the
44  * user didn’t correctly install i3 or forgot te restart it).
45  *
46  * The output looks like this:
47  * Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
48  *
49  * The i3 binary you just called: /home/michael/i3/i3
50  * The i3 binary you are running: /home/michael/i3/i3
51  *
52  */
54  char *socket_path = root_atom_contents("I3_SOCKET_PATH", conn, conn_screen);
55  if (socket_path == NULL)
56  exit(EXIT_SUCCESS);
57 
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");
62  exit(EXIT_SUCCESS);
63  }
64 
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…)");
68  fflush(stdout);
69 
70  /* TODO: refactor this with the code for sending commands */
71  int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
72  if (sockfd == -1)
73  err(EXIT_FAILURE, "Could not create socket");
74 
75  struct sockaddr_un addr;
76  memset(&addr, 0, sizeof(struct sockaddr_un));
77  addr.sun_family = AF_LOCAL;
78  strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
79  if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
80  err(EXIT_FAILURE, "Could not connect to i3");
81 
82  if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
83  (uint8_t *)"") == -1)
84  err(EXIT_FAILURE, "IPC: write()");
85 
86  uint32_t reply_length;
87  uint32_t reply_type;
88  uint8_t *reply;
89  int ret;
90  if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
91  if (ret == -1)
92  err(EXIT_FAILURE, "IPC: read()");
93  exit(EXIT_FAILURE);
94  }
95 
96  if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
97  errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
98 
99  yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
100 
101  yajl_status state = yajl_parse(handle, (const unsigned char *)reply, (int)reply_length);
102  if (state != yajl_status_ok)
103  errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply);
104 
105  printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
106 
107 #ifdef __linux__
108  size_t destpath_size = 1024;
109  ssize_t linksize;
110  char *exepath;
111  char *destpath = smalloc(destpath_size);
112 
113  sasprintf(&exepath, "/proc/%d/exe", getpid());
114 
115  while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
116  destpath_size = destpath_size * 2;
117  destpath = srealloc(destpath, destpath_size);
118  }
119  if (linksize == -1)
120  err(EXIT_FAILURE, "readlink(%s)", exepath);
121 
122  /* readlink() does not NULL-terminate strings, so we have to. */
123  destpath[linksize] = '\0';
124 
125  printf("\n");
126  printf("The i3 binary you just called: %s\n", destpath);
127 
128  free(exepath);
129  sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
130 
131  while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
132  destpath_size = destpath_size * 2;
133  destpath = srealloc(destpath, destpath_size);
134  }
135  if (linksize == -1)
136  err(EXIT_FAILURE, "readlink(%s)", exepath);
137 
138  /* readlink() does not NULL-terminate strings, so we have to. */
139  destpath[linksize] = '\0';
140 
141  /* Check if "(deleted)" is the readlink result. If so, the running version
142  * does not match the file on disk. */
143  if (strstr(destpath, "(deleted)") != NULL)
144  printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
145 
146  /* Since readlink() might put a "(deleted)" somewhere in the buffer and
147  * stripping that out seems hackish and ugly, we read the process’s argv[0]
148  * instead. */
149  free(exepath);
150  sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
151 
152  int fd;
153  if ((fd = open(exepath, O_RDONLY)) == -1)
154  err(EXIT_FAILURE, "open(%s)", exepath);
155  if (read(fd, destpath, sizeof(destpath)) == -1)
156  err(EXIT_FAILURE, "read(%s)", exepath);
157  close(fd);
158 
159  printf("The i3 binary you are running: %s\n", destpath);
160 
161  free(exepath);
162  free(destpath);
163 #endif
164 
165  yajl_free(handle);
166 }
static bool human_readable_key
xcb_connection_t * conn
Definition: main.c:47
static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen)
static yajl_callbacks version_callbacks
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...
static cmdp_state state
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...
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 xcb_cursor_context_t * ctx
Definition: xcursor.c:19
static char * human_readable_version
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
int conn_screen
Definition: main.c:49