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