]> git.sur5r.net Git - i3/i3/blob - i3-dump-log/main.c
Merge branch 'master' into next
[i3/i3] / i3-dump-log / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * i3-dump-log/main.c: Dumps the i3 SHM log to stdout.
8  *
9  */
10 #include <stdio.h>
11 #include <stdbool.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <err.h>
20 #include <stdint.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26
27 #include "libi3.h"
28 #include <i3/ipc.h>
29
30 int main(int argc, char *argv[]) {
31     char *socket_path = getenv("I3SOCK");
32     int o, option_index = 0;
33     int message_type = I3_IPC_MESSAGE_TYPE_GET_LOG_MARKERS;
34     bool verbose = false;
35
36     static struct option long_options[] = {
37         {"socket", required_argument, 0, 's'},
38         {"version", no_argument, 0, 'v'},
39         {"verbose", no_argument, 0, 'V'},
40         {"help", no_argument, 0, 'h'},
41         {0, 0, 0, 0}
42     };
43
44     char *options_string = "s:vVh";
45
46     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
47         if (o == 's') {
48             if (socket_path != NULL)
49                 free(socket_path);
50             socket_path = sstrdup(optarg);
51         } else if (o == 'v') {
52             printf("i3-dump-log " I3_VERSION "\n");
53             return 0;
54         } else if (o == 'V') {
55             verbose = true;
56         } else if (o == 'h') {
57             printf("i3-dump-log " I3_VERSION "\n");
58             printf("i3-dump-log [-s <socket>]\n");
59             return 0;
60         }
61     }
62
63     if (socket_path == NULL)
64         socket_path = socket_path_from_x11();
65
66     /* Fall back to the default socket path */
67     if (socket_path == NULL)
68         socket_path = sstrdup("/tmp/i3-ipc.sock");
69
70     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
71     if (sockfd == -1)
72         err(EXIT_FAILURE, "Could not create socket");
73
74     struct sockaddr_un addr;
75     memset(&addr, 0, sizeof(struct sockaddr_un));
76     addr.sun_family = AF_LOCAL;
77     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
78     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
79         err(EXIT_FAILURE, "Could not connect to i3");
80
81     if (ipc_send_message(sockfd, 0, message_type, NULL) == -1)
82         err(EXIT_FAILURE, "IPC: write()");
83
84     uint32_t reply_length;
85     uint8_t *reply;
86     int ret;
87     if ((ret = ipc_recv_message(sockfd, message_type, &reply_length, &reply)) != 0) {
88         if (ret == -1)
89             err(EXIT_FAILURE, "IPC: read()");
90         exit(1);
91     }
92     char *buffer = NULL;
93     sasprintf(&buffer, "%.*s", reply_length, reply);
94     /* The reply will look like this:
95      * {"offset_next_write":1729,"offset_last_wrap":1996,"size":2048,"shmname":"/i3-log-399"}
96      * IMO, it’s not worth linking a JSON parser in just for this. If the
97      * structure changes in the future, this decision needs to be re-evaluated
98      * :). */
99     int offset_next_write, offset_last_wrap, logbuffer_size;
100     char *next_write_str = strstr(buffer, "offset_next_write"),
101          *last_wrap_str = strstr(buffer, "offset_last_wrap"),
102          *size_str = strstr(buffer, "size"),
103          *shmname = strstr(buffer, "shmname");
104     if (!next_write_str ||
105         !last_wrap_str ||
106         !size_str ||
107         !shmname ||
108         sscanf(next_write_str, "offset_next_write\":%d", &offset_next_write) != 1 ||
109         sscanf(last_wrap_str, "offset_last_wrap\":%d", &offset_last_wrap) != 1 ||
110         sscanf(size_str, "size\":%d", &logbuffer_size) != 1)
111         errx(EXIT_FAILURE, "invalid IPC reply: %s\n", buffer);
112
113     shmname += strlen("shmname\":\"");
114     char *quote = strchr(shmname, '"');
115     if (!quote)
116         errx(EXIT_FAILURE, "invalid IPC reply: %s\n", buffer);
117     *quote = '\0';
118
119     if (verbose)
120         printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
121                offset_next_write, offset_last_wrap, logbuffer_size, shmname);
122
123     if (*shmname == '\0')
124         errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
125
126     int logbuffer_shm = shm_open(shmname, O_RDONLY, 0);
127     if (logbuffer_shm == -1)
128         err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
129
130     char *logbuffer = mmap(NULL, logbuffer_size, PROT_READ, MAP_SHARED, logbuffer_shm, 0);
131     if (logbuffer == MAP_FAILED)
132         err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
133
134     int chars;
135     char *walk = logbuffer + offset_next_write;
136     /* Skip the first line, it very likely is mangled. Not a problem, though,
137      * the log is chatty enough to have plenty lines left. */
138     while (*walk != '\0')
139         walk++;
140
141     /* Print the oldest log lines. We use printf("%s") to stop on \0. */
142     while (walk < (logbuffer + offset_last_wrap)) {
143         chars = printf("%s", walk);
144         /* Shortcut: If there are two consecutive \0 bytes, this part of the
145          * buffer was never touched. To not call printf() for every byte of the
146          * buffer, we directly exit the loop. */
147         if (*walk == '\0' && *(walk+1) == '\0')
148             break;
149         walk += (chars > 0 ? chars : 1);
150     }
151
152     /* Then start from the beginning and print the newer lines */
153     walk = logbuffer;
154     while (walk < (logbuffer + offset_next_write)) {
155         chars = printf("%s", walk);
156         walk += (chars > 0 ? chars : 1);
157     }
158
159     return 0;
160 }