2012-08-12 15:10:13 +02:00
|
|
|
|
#undef I3__FILE__
|
|
|
|
|
#define I3__FILE__ "key_press.c"
|
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2012-08-12 15:10:13 +02:00
|
|
|
|
*
|
|
|
|
|
* display_version.c: displays the running i3 version, runs as part of
|
|
|
|
|
* i3 --moreversion.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#include <fcntl.h>
|
2015-07-09 16:25:50 +02:00
|
|
|
|
#include <time.h>
|
2012-08-12 15:10:13 +02:00
|
|
|
|
#include "all.h"
|
|
|
|
|
|
2015-07-09 16:25:50 +02:00
|
|
|
|
static bool human_readable_key, loaded_config_file_name_key;
|
|
|
|
|
static char *human_readable_version, *loaded_config_file_name;
|
2012-08-12 15:10:13 +02:00
|
|
|
|
|
|
|
|
|
static int version_string(void *ctx, const unsigned char *val, size_t len) {
|
|
|
|
|
if (human_readable_key)
|
|
|
|
|
sasprintf(&human_readable_version, "%.*s", (int)len, val);
|
2015-07-09 16:25:50 +02:00
|
|
|
|
if (loaded_config_file_name_key)
|
|
|
|
|
sasprintf(&loaded_config_file_name, "%.*s", (int)len, val);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
|
|
|
|
|
human_readable_key = (stringlen == strlen("human_readable") &&
|
2014-06-15 19:07:02 +02:00
|
|
|
|
strncmp((const char *)stringval, "human_readable", strlen("human_readable")) == 0);
|
2015-07-09 16:25:50 +02:00
|
|
|
|
loaded_config_file_name_key = (stringlen == strlen("loaded_config_file_name") &&
|
|
|
|
|
strncmp((const char *)stringval, "loaded_config_file_name", strlen("loaded_config_file_name")) == 0);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static yajl_callbacks version_callbacks = {
|
2014-01-01 17:19:55 +01:00
|
|
|
|
.yajl_string = version_string,
|
|
|
|
|
.yajl_map_key = version_map_key,
|
2012-08-12 15:10:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Connects to i3 to find out the currently running version. Useful since it
|
|
|
|
|
* might be different from the version compiled into this binary (maybe the
|
|
|
|
|
* user didn’t correctly install i3 or forgot te restart it).
|
|
|
|
|
*
|
|
|
|
|
* The output looks like this:
|
|
|
|
|
* Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804)
|
|
|
|
|
*
|
|
|
|
|
* The i3 binary you just called: /home/michael/i3/i3
|
|
|
|
|
* The i3 binary you are running: /home/michael/i3/i3
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void display_running_version(void) {
|
2013-11-22 15:48:45 +01:00
|
|
|
|
char *socket_path = root_atom_contents("I3_SOCKET_PATH", conn, conn_screen);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
if (socket_path == NULL)
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
2013-11-22 15:48:45 +01:00
|
|
|
|
char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
if (pid_from_atom == NULL) {
|
|
|
|
|
/* If I3_PID is not set, the running version is older than 4.2-200. */
|
|
|
|
|
printf("\nRunning version: < 4.2-200\n");
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Inform the user of what we are doing. While a single IPC request is
|
|
|
|
|
* really fast normally, in case i3 hangs, this will not terminate. */
|
|
|
|
|
printf("(Getting version from running i3, press ctrl-c to abort…)");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
/* TODO: refactor this with the code for sending commands */
|
|
|
|
|
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
|
if (sockfd == -1)
|
|
|
|
|
err(EXIT_FAILURE, "Could not create socket");
|
|
|
|
|
|
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
|
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
2014-06-15 19:07:02 +02:00
|
|
|
|
if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
|
2012-08-12 15:10:13 +02:00
|
|
|
|
err(EXIT_FAILURE, "Could not connect to i3");
|
|
|
|
|
|
|
|
|
|
if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
(uint8_t *)"") == -1)
|
2012-08-12 15:10:13 +02:00
|
|
|
|
err(EXIT_FAILURE, "IPC: write()");
|
|
|
|
|
|
|
|
|
|
uint32_t reply_length;
|
2013-01-23 18:50:21 +01:00
|
|
|
|
uint32_t reply_type;
|
2012-08-12 15:10:13 +02:00
|
|
|
|
uint8_t *reply;
|
|
|
|
|
int ret;
|
2013-01-23 18:50:21 +01:00
|
|
|
|
if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
|
2012-08-12 15:10:13 +02:00
|
|
|
|
if (ret == -1)
|
|
|
|
|
err(EXIT_FAILURE, "IPC: read()");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 18:50:21 +01:00
|
|
|
|
if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
|
|
|
|
|
errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
|
|
|
|
|
|
2012-08-12 15:10:13 +02:00
|
|
|
|
yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
|
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
|
yajl_status state = yajl_parse(handle, (const unsigned char *)reply, (int)reply_length);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
if (state != yajl_status_ok)
|
2012-08-12 18:34:03 +02:00
|
|
|
|
errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
|
|
|
|
|
printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
|
|
|
|
|
|
2015-07-09 16:25:50 +02:00
|
|
|
|
if (loaded_config_file_name) {
|
|
|
|
|
struct stat sb;
|
|
|
|
|
time_t now;
|
|
|
|
|
char mtime[64];
|
|
|
|
|
printf("Loaded i3 config: %s", loaded_config_file_name);
|
|
|
|
|
if (stat(loaded_config_file_name, &sb) == -1) {
|
|
|
|
|
printf("\n");
|
|
|
|
|
ELOG("Cannot stat config file \"%s\"\n", loaded_config_file_name);
|
|
|
|
|
} else {
|
|
|
|
|
strftime(mtime, sizeof(mtime), "%c", localtime(&(sb.st_mtime)));
|
|
|
|
|
time(&now);
|
|
|
|
|
printf(" (Last modified: %s, %.f seconds ago)\n", mtime, difftime(now, sb.st_mtime));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 15:10:13 +02:00
|
|
|
|
#ifdef __linux__
|
2013-11-21 22:03:49 +01:00
|
|
|
|
size_t destpath_size = 1024;
|
2012-08-13 13:38:04 +02:00
|
|
|
|
ssize_t linksize;
|
2013-11-21 22:03:49 +01:00
|
|
|
|
char *exepath;
|
|
|
|
|
char *destpath = smalloc(destpath_size);
|
2012-08-13 13:38:04 +02:00
|
|
|
|
|
2013-11-21 22:03:49 +01:00
|
|
|
|
sasprintf(&exepath, "/proc/%d/exe", getpid());
|
2012-08-13 13:38:04 +02:00
|
|
|
|
|
2013-12-25 20:01:37 +01:00
|
|
|
|
while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
|
2014-06-15 19:07:02 +02:00
|
|
|
|
destpath_size = destpath_size * 2;
|
|
|
|
|
destpath = srealloc(destpath, destpath_size);
|
2013-11-21 22:03:49 +01:00
|
|
|
|
}
|
|
|
|
|
if (linksize == -1)
|
2012-08-13 13:38:04 +02:00
|
|
|
|
err(EXIT_FAILURE, "readlink(%s)", exepath);
|
|
|
|
|
|
|
|
|
|
/* readlink() does not NULL-terminate strings, so we have to. */
|
|
|
|
|
destpath[linksize] = '\0';
|
|
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("The i3 binary you just called: %s\n", destpath);
|
|
|
|
|
|
2013-11-21 22:03:49 +01:00
|
|
|
|
free(exepath);
|
|
|
|
|
sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
|
2013-12-25 20:01:37 +01:00
|
|
|
|
while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
|
2013-11-21 22:03:49 +01:00
|
|
|
|
destpath_size = destpath_size * 2;
|
|
|
|
|
destpath = srealloc(destpath, destpath_size);
|
|
|
|
|
}
|
|
|
|
|
if (linksize == -1)
|
2012-08-12 15:10:13 +02:00
|
|
|
|
err(EXIT_FAILURE, "readlink(%s)", exepath);
|
|
|
|
|
|
|
|
|
|
/* readlink() does not NULL-terminate strings, so we have to. */
|
|
|
|
|
destpath[linksize] = '\0';
|
|
|
|
|
|
|
|
|
|
/* Check if "(deleted)" is the readlink result. If so, the running version
|
|
|
|
|
* does not match the file on disk. */
|
|
|
|
|
if (strstr(destpath, "(deleted)") != NULL)
|
|
|
|
|
printf("RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
|
|
|
|
|
|
|
|
|
|
/* Since readlink() might put a "(deleted)" somewhere in the buffer and
|
|
|
|
|
* stripping that out seems hackish and ugly, we read the process’s argv[0]
|
|
|
|
|
* instead. */
|
2013-11-21 22:03:49 +01:00
|
|
|
|
free(exepath);
|
|
|
|
|
sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
if ((fd = open(exepath, O_RDONLY)) == -1)
|
|
|
|
|
err(EXIT_FAILURE, "open(%s)", exepath);
|
|
|
|
|
if (read(fd, destpath, sizeof(destpath)) == -1)
|
|
|
|
|
err(EXIT_FAILURE, "read(%s)", exepath);
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
|
|
printf("The i3 binary you are running: %s\n", destpath);
|
2013-11-21 22:03:49 +01:00
|
|
|
|
|
|
|
|
|
free(exepath);
|
|
|
|
|
free(destpath);
|
2012-08-12 15:10:13 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
yajl_free(handle);
|
|
|
|
|
}
|