From 1e49f1b08a3035c1f238fcd6615e332216ab582e Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 12 Aug 2012 15:10:13 +0200 Subject: [PATCH] Implement i3 --moreversion MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit From the code: 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). Here is an example output: $ ./i3 --moreversion Binary i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") © 2009-2012 Michael Stapelberg and contributors Running i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") (pid 14804) The i3 binary you just called: /home/michael/i3/i3 RUNNING BINARY DIFFERENT FROM BINARY ON DISK! The i3 binary you are running: /home/michael/i3/i3 $ i3 restart 2012-08-12 15:05:28 - Additional arguments passed. Sending them as a command to i3. IPC: received EOF instead of reply $ ./i3 --moreversion Binary i3 version: 4.2-202-gb8e782c (2012-08-12, branch "next") © 2009-2012 Michael Stapelberg and contributors 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 --- include/all.h | 1 + include/display_version.h | 27 +++++++ src/display_version.c | 165 ++++++++++++++++++++++++++++++++++++++ src/main.c | 11 ++- 4 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 include/display_version.h create mode 100644 src/display_version.c diff --git a/include/all.h b/include/all.h index c6b0351f..b83b9f4e 100644 --- a/include/all.h +++ b/include/all.h @@ -80,5 +80,6 @@ #include "commands.h" #include "commands_parser.h" #include "fake_outputs.h" +#include "display_version.h" #endif diff --git a/include/display_version.h b/include/display_version.h new file mode 100644 index 00000000..97b3902c --- /dev/null +++ b/include/display_version.h @@ -0,0 +1,27 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE) + * + * display_version.c: displays the running i3 version, runs as part of + * i3 --moreversion. + */ +#ifndef _DISPLAY_VERSION_H +#define _DISPLAY_VERSION_H + +/** + * 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); + +#endif diff --git a/src/display_version.c b/src/display_version.c new file mode 100644 index 00000000..669e7c7d --- /dev/null +++ b/src/display_version.c @@ -0,0 +1,165 @@ +#undef I3__FILE__ +#define I3__FILE__ "key_press.c" +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE) + * + * display_version.c: displays the running i3 version, runs as part of + * i3 --moreversion. + * + */ +#include +#include +#include +#include +#include +#include +#include "all.h" + +static bool human_readable_key; +static char *human_readable_version; + +#if YAJL_MAJOR >= 2 +static int version_string(void *ctx, const unsigned char *val, size_t len) { +#else +static int version_string(void *ctx, const unsigned char *val, unsigned int len) { +#endif + if (human_readable_key) + sasprintf(&human_readable_version, "%.*s", (int)len, val); + return 1; +} + +#if YAJL_MAJOR >= 2 +static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) { +#else +static int version_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) { +#endif + human_readable_key = (stringlen == strlen("human_readable") && + strncmp((const char*)stringval, "human_readable", strlen("human_readable")) == 0); + return 1; +} + +static yajl_callbacks version_callbacks = { + NULL, /* null */ + NULL, /* boolean */ + NULL, /* integer */ + NULL, /* double */ + NULL, /* number */ + &version_string, + NULL, /* start_map */ + &version_map_key, + NULL, /* end_map */ + NULL, /* start_array */ + NULL /* end_array */ +}; + +/* + * 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) { + char *socket_path = root_atom_contents("I3_SOCKET_PATH"); + if (socket_path == NULL) + exit(EXIT_SUCCESS); + + char *pid_from_atom = root_atom_contents("I3_PID"); + 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); + if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) + err(EXIT_FAILURE, "Could not connect to i3"); + + if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION, + (uint8_t*)"") == -1) + err(EXIT_FAILURE, "IPC: write()"); + + uint32_t reply_length; + uint8_t *reply; + int ret; + if ((ret = ipc_recv_message(sockfd, I3_IPC_MESSAGE_TYPE_GET_VERSION, + &reply_length, &reply)) != 0) { + if (ret == -1) + err(EXIT_FAILURE, "IPC: read()"); + exit(EXIT_FAILURE); + } + +#if YAJL_MAJOR >= 2 + yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL); +#else + yajl_parser_config parse_conf = { 0, 0 }; + + yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, NULL); +#endif + + yajl_status state = yajl_parse(handle, (const unsigned char*)reply, (int)reply_length); + if (state != yajl_status_ok) + err(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply); + + printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom); + printf("\n"); + char resolved_path[PATH_MAX]; + if (realpath(start_argv[0], resolved_path) == NULL) + err(EXIT_FAILURE, "realpath(%s)", start_argv[0]); + printf("The i3 binary you just called: %s\n", resolved_path); + +#ifdef __linux__ + char exepath[PATH_MAX], + destpath[PATH_MAX]; + snprintf(exepath, sizeof(exepath), "/proc/%s/exe", pid_from_atom); + + ssize_t linksize; + if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1) + 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. */ + snprintf(exepath, sizeof(exepath), "/proc/%s/cmdline", pid_from_atom); + + 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); +#endif + + yajl_free(handle); +} diff --git a/src/main.c b/src/main.c index b60bb6d7..47d66b87 100644 --- a/src/main.c +++ b/src/main.c @@ -257,6 +257,9 @@ int main(int argc, char *argv[]) { {"no-autostart", no_argument, 0, 'a'}, {"config", required_argument, 0, 'c'}, {"version", no_argument, 0, 'v'}, + {"moreversion", no_argument, 0, 'm'}, + {"more-version", no_argument, 0, 'm'}, + {"more_version", no_argument, 0, 'm'}, {"help", no_argument, 0, 'h'}, {"layout", required_argument, 0, 'L'}, {"restart", required_argument, 0, 0}, @@ -294,7 +297,7 @@ int main(int argc, char *argv[]) { start_argv = argv; - while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) { + while ((opt = getopt_long(argc, argv, "c:CvmaL:hld:V", long_options, &option_index)) != -1) { switch (opt) { case 'a': LOG("Autostart disabled using -a\n"); @@ -316,6 +319,12 @@ int main(int argc, char *argv[]) { case 'v': printf("i3 version " I3_VERSION " © 2009-2012 Michael Stapelberg and contributors\n"); exit(EXIT_SUCCESS); + break; + case 'm': + printf("Binary i3 version: " I3_VERSION " © 2009-2012 Michael Stapelberg and contributors\n"); + display_running_version(); + exit(EXIT_SUCCESS); + break; case 'V': set_verbosity(true); break; -- 2.39.5