]> git.sur5r.net Git - i3/i3/blob - i3-dump-log/main.c
Merge pull request #2350 from madroach/OpenBSD
[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 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 "shmlog.h"
29 #include <i3/ipc.h>
30
31 #if !defined(__OpenBSD__)
32 static uint32_t offset_next_write;
33 #endif
34 static uint32_t wrap_count;
35
36 static i3_shmlog_header *header;
37 static char *logbuffer,
38     *walk;
39
40 static int check_for_wrap(void) {
41     if (wrap_count == header->wrap_count)
42         return 0;
43
44     /* The log wrapped. Print the remaining content and reset walk to the top
45      * of the log. */
46     wrap_count = header->wrap_count;
47     const int len = (logbuffer + header->offset_last_wrap) - walk;
48     swrite(STDOUT_FILENO, walk, len);
49     walk = logbuffer + sizeof(i3_shmlog_header);
50     return 1;
51 }
52
53 static void print_till_end(void) {
54     check_for_wrap();
55     const int len = (logbuffer + header->offset_next_write) - walk;
56     swrite(STDOUT_FILENO, walk, len);
57     walk += len;
58 }
59
60 int main(int argc, char *argv[]) {
61     int o, option_index = 0;
62     bool verbose = false;
63 #if !defined(__OpenBSD__)
64     bool follow = false;
65 #endif
66
67     static struct option long_options[] = {
68         {"version", no_argument, 0, 'v'},
69         {"verbose", no_argument, 0, 'V'},
70 #if !defined(__OpenBSD__)
71         {"follow", no_argument, 0, 'f'},
72 #endif
73         {"help", no_argument, 0, 'h'},
74         {0, 0, 0, 0}
75     };
76
77 #if !defined(__OpenBSD__)
78     char *options_string = "s:vfVh";
79 #else
80     char *options_string = "vVh";
81 #endif
82
83     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
84         if (o == 'v') {
85             printf("i3-dump-log " I3_VERSION "\n");
86             return 0;
87         } else if (o == 'V') {
88             verbose = true;
89 #if !defined(__OpenBSD__)
90         } else if (o == 'f') {
91             follow = true;
92 #endif
93         } else if (o == 'h') {
94             printf("i3-dump-log " I3_VERSION "\n");
95 #if !defined(__OpenBSD__)
96             printf("i3-dump-log [-fhVv]\n");
97 #else
98             printf("i3-dump-log [-hVv]\n");
99 #endif
100             return 0;
101         }
102     }
103
104     char *shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0);
105     if (shmname == NULL) {
106         /* Something failed. Let’s invest a little effort to find out what it
107          * is. This is hugely helpful for users who want to debug i3 but are
108          * not used to the procedure yet. */
109         xcb_connection_t *conn;
110         int screen;
111         if ((conn = xcb_connect(NULL, &screen)) == NULL ||
112             xcb_connection_has_error(conn)) {
113             fprintf(stderr, "i3-dump-log: ERROR: Cannot connect to X11.\n\n");
114             if (getenv("DISPLAY") == NULL) {
115                 fprintf(stderr, "Your DISPLAY environment variable is not set.\n");
116                 fprintf(stderr, "Are you running i3-dump-log via SSH or on a virtual console?\n");
117                 fprintf(stderr, "Try DISPLAY=:0 i3-dump-log\n");
118                 exit(1);
119             }
120             fprintf(stderr, "FYI: The DISPLAY environment variable is set to \"%s\".\n", getenv("DISPLAY"));
121             exit(1);
122         }
123         if (root_atom_contents("I3_CONFIG_PATH", conn, screen) != NULL) {
124             fprintf(stderr, "i3-dump-log: ERROR: i3 is running, but SHM logging is not enabled.\n\n");
125             if (!is_debug_build()) {
126                 fprintf(stderr, "You seem to be using a release version of i3:\n  %s\n\n", I3_VERSION);
127                 fprintf(stderr, "Release versions do not use SHM logging by default,\ntherefore i3-dump-log does not work.\n\n");
128                 fprintf(stderr, "Please follow this guide instead:\nhttp://i3wm.org/docs/debugging-release-version.html\n");
129                 exit(1);
130             }
131         }
132         errx(EXIT_FAILURE, "Cannot get I3_SHMLOG_PATH atom contents. Is i3 running on this display?");
133     }
134
135     if (*shmname == '\0')
136         errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
137
138     struct stat statbuf;
139
140     /* NB: While we must never write, we need O_RDWR for the pthread condvar. */
141     int logbuffer_shm = shm_open(shmname, O_RDWR, 0);
142     if (logbuffer_shm == -1)
143         err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
144
145     if (fstat(logbuffer_shm, &statbuf) != 0)
146         err(EXIT_FAILURE, "stat(%s)", shmname);
147
148     /* NB: While we must never write, we need PROT_WRITE for the pthread condvar. */
149     logbuffer = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
150     if (logbuffer == MAP_FAILED)
151         err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
152
153     header = (i3_shmlog_header *)logbuffer;
154
155     if (verbose)
156         printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
157                header->offset_next_write, header->offset_last_wrap, header->size, shmname);
158     free(shmname);
159     walk = logbuffer + header->offset_next_write;
160
161     /* We first need to print old content in case there was at least one
162      * wrapping already. */
163
164     if (*walk != '\0') {
165         /* In case there was a write to the buffer already, skip the first
166          * old line, it very likely is mangled. Not a problem, though, the log
167          * is chatty enough to have plenty lines left. */
168         while (*walk != '\n')
169             walk++;
170         walk++;
171     }
172
173     /* In case there was no wrapping, this is a no-op, otherwise it prints the
174      * old lines. */
175     wrap_count = 0;
176     check_for_wrap();
177
178     /* Then start from the beginning and print the newer lines */
179     walk = logbuffer + sizeof(i3_shmlog_header);
180     print_till_end();
181
182 #if !defined(__OpenBSD__)
183     if (follow) {
184         /* Since pthread_cond_wait() expects a mutex, we need to provide one.
185          * To not lock i3 (that’s bad, mhkay?) we just define one outside of
186          * the shared memory. */
187         pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
188         pthread_mutex_lock(&dummy_mutex);
189         while (1) {
190             pthread_cond_wait(&(header->condvar), &dummy_mutex);
191             /* If this was not a spurious wakeup, print the new lines. */
192             if (header->offset_next_write != offset_next_write) {
193                 offset_next_write = header->offset_next_write;
194                 print_till_end();
195             }
196         }
197     }
198 #endif
199
200     return 0;
201 }