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