i3 - improved tiling WM


Remove references to PATH_MAX macro

Patch status: needinfo

Patch by Lancelot SIX

Long description:

Since the macro PATH_MAX is not defined on every system (GNU/Hurd being
one of those who do not define it), we remove all references to this
macro. Instead, we use a buffer of arbitraty size and grow it when
needed to contain paths.

To apply this patch, use:
curl http://cr.i3wm.org/patch/317/raw.patch | git am

b/i3-nagbar/main.c

23
@@ -159,8 +159,9 @@ static void handle_button_release(xcb_connection_t *conn, xcb_button_release_eve
24
     fclose(script);
25
 
26
     char *link_path;
27
+    char *exe_path = get_exe_path(argv0);
28
     sasprintf(&link_path, "%s.nagbar_cmd", script_path);
29
-    symlink(get_exe_path(argv0), link_path);
30
+    symlink(exe_path, link_path);
31
 
32
     char *terminal_cmd;
33
     sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
34
@@ -172,6 +173,7 @@ static void handle_button_release(xcb_connection_t *conn, xcb_button_release_eve
35
     free(link_path);
36
     free(terminal_cmd);
37
     free(script_path);
38
+    free(exe_path);
39
 
40
     /* TODO: unset flag, re-render */
41
 }

b/i3bar/src/xcb.c

46
@@ -961,26 +961,48 @@ char *init_xcb_early() {
47
     get_atoms();
48
 
49
     xcb_get_property_cookie_t path_cookie;
50
+    xcb_get_property_reply_t *path_reply;
51
+    int len;
52
+    size_t path_size = 1024;
53
+    char *path = NULL;
54
+
55
     path_cookie = xcb_get_property_unchecked(xcb_connection,
56
-                                   0,
57
-                                   xcb_root,
58
-                                   atoms[I3_SOCKET_PATH],
59
-                                   XCB_GET_PROPERTY_TYPE_ANY,
60
-                                   0, PATH_MAX);
61
+                                             0,
62
+                                             xcb_root,
63
+                                             atoms[I3_SOCKET_PATH],
64
+                                             XCB_GET_PROPERTY_TYPE_ANY,
65
+                                             0,
66
+                                             (path_size / 4));
67
 
68
     /* We check, if i3 set its socket-path */
69
-    xcb_get_property_reply_t *path_reply = xcb_get_property_reply(xcb_connection,
70
-                                                                  path_cookie,
71
-                                                                  NULL);
72
-    char *path = NULL;
73
+    path_reply = xcb_get_property_reply(xcb_connection,
74
+                                        path_cookie,
75
+                                        NULL);
76
     if (path_reply) {
77
-        int len = xcb_get_property_value_length(path_reply);
78
+        len = xcb_get_property_value_length(path_reply);
79
+
80
+        if (len > 0 && len < path_size) {
81
+            path_size += (path_reply->bytes_after / 4) + 1;
82
+            path_cookie = xcb_get_property_unchecked(xcb_connection,
83
+                                                     0,
84
+                                                     xcb_root,
85
+                                                     atoms[I3_SOCKET_PATH],
86
+                                                     XCB_GET_PROPERTY_TYPE_ANY,
87
+                                                     0,
88
+                                                     (path_size / 4));
89
+
90
+            path_reply = xcb_get_property_reply(xcb_connection,
91
+                                                path_cookie,
92
+                                                NULL);
93
+        }
94
+    }
95
+    if (path_reply) {
96
+        len = xcb_get_property_value_length(path_reply);
97
         if (len != 0) {
98
             path = strndup(xcb_get_property_value(path_reply), len);
99
         }
100
     }
101
 
102
-
103
     if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
104
         xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
105
         xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {

b/include/libi3.h

110
@@ -369,7 +369,8 @@ char *get_process_filename(const char *prefix);
111
  *
112
  * The implementation follows http://stackoverflow.com/a/933996/712014
113
  *
114
+ * Returned value must be freed by the caller.
115
  */
116
-const char *get_exe_path(const char *argv0);
117
+char *get_exe_path(const char *argv0);
118
 
119
 #endif

b/libi3/get_exe_path.c

124
@@ -3,6 +3,7 @@
125
 #include <stdio.h>
126
 #include <limits.h>
127
 #include <stdlib.h>
128
+#include <errno.h>
129
 
130
 #include "libi3.h"
131
 
132
@@ -11,10 +12,14 @@
133
  *
134
  * The implementation follows http://stackoverflow.com/a/933996/712014
135
  *
136
+ * Returned value must be freed by the caller.
137
  */
138
-const char *get_exe_path(const char *argv0) {
139
-	static char destpath[PATH_MAX];
140
-	char tmp[PATH_MAX];
141
+char *get_exe_path(const char *argv0) {
142
+	size_t destpath_size = 1024;
143
+	size_t tmp_size = 1024;
144
+	char *destpath = smalloc(destpath_size);
145
+	char *tmp = smalloc(tmp_size);
146
+
147
 
148
 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
149
 	/* Linux and Debian/kFreeBSD provide /proc/self/exe */
150
@@ -25,30 +30,51 @@ const char *get_exe_path(const char *argv0) {
151
 #endif
152
 	ssize_t linksize;
153
 
154
-	if ((linksize = readlink(exepath, destpath, sizeof(destpath) - 1)) != -1) {
155
+	while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
156
+		destpath_size = destpath_size * 2;
157
+		destpath = srealloc(destpath, destpath_size);
158
+	}
159
+	if (linksize != -1) {
160
 		/* readlink() does not NULL-terminate strings, so we have to. */
161
 		destpath[linksize] = '\0';
162
-
163
+		free(tmp);
164
 		return destpath;
165
 	}
166
 #endif
167
 
168
 	/* argv[0] is most likely a full path if it starts with a slash. */
169
-	if (argv0[0] == '/')
170
-		return argv0;
171
+	if (argv0[0] == '/') {
172
+		free(tmp);
173
+		free(destpath);
174
+		return sstrdup(argv0);
175
+	}
176
 
177
 	/* if argv[0] contains a /, prepend the working directory */
178
-	if (strchr(argv0, '/') != NULL &&
179
-		getcwd(tmp, sizeof(tmp)) != NULL) {
180
-		snprintf(destpath, sizeof(destpath), "%s/%s", tmp, argv0);
181
-		return destpath;
182
+	if (strchr(argv0, '/') != NULL) {
183
+		char *retgcwd;
184
+		while ((retgcwd = getcwd(tmp, tmp_size)) == NULL && errno == ERANGE) {
185
+			tmp_size = tmp_size * 2;
186
+			tmp = srealloc(tmp, tmp_size);
187
+		}
188
+		if (retgcwd != NULL) {
189
+			while (snprintf(destpath, destpath_size, "%s/%s", tmp, argv0) >= destpath_size) {
190
+				destpath_size = destpath_size * 2;
191
+				destpath = srealloc(destpath, destpath_size);
192
+			}
193
+
194
+			free(tmp);
195
+			return destpath;
196
+		}
197
 	}
198
 
199
 	/* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
200
 	char *path = getenv("PATH");
201
 	if (path == NULL) {
202
 		/* _CS_PATH is typically something like "/bin:/usr/bin" */
203
-		confstr(_CS_PATH, tmp, sizeof(tmp));
204
+		while (confstr(_CS_PATH, tmp, tmp_size) > tmp_size) {
205
+			tmp_size = tmp_size * 2;
206
+			tmp = srealloc(tmp, tmp_size);
207
+		}
208
 		sasprintf(&path, ":%s", tmp);
209
 	} else {
210
 		path = strdup(path);
211
@@ -59,16 +85,22 @@ const char *get_exe_path(const char *argv0) {
212
 		if ((component = strtok(str, ":")) == NULL)
213
 			break;
214
 		str = NULL;
215
-		snprintf(destpath, sizeof(destpath), "%s/%s", component, argv0);
216
+		while(snprintf(destpath, destpath_size, "%s/%s", component, argv0) >= destpath_size) {
217
+			destpath_size = destpath_size * 2;
218
+			destpath = srealloc(destpath, destpath_size);
219
+		}
220
 		/* Of course this is not 100% equivalent to actually exec()ing the
221
 		 * binary, but meh. */
222
 		if (access(destpath, X_OK) == 0) {
223
 			free(path);
224
+			free(tmp);
225
 			return destpath;
226
 		}
227
 	}
228
-	free(path);
229
 
230
 	/* Last resort: maybe it’s in /usr/bin? */
231
-	return "/usr/bin/i3-nagbar";
232
+	free(destpath);
233
+	free(path);
234
+	free(tmp);
235
+	return sstrdup("/usr/bin/i3-nagbar");
236
 }

b/libi3/root_atom_contents.c

241
@@ -29,6 +29,9 @@ char *root_atom_contents(const char *atomname) {
242
     xcb_intern_atom_reply_t *atom_reply;
243
     int screen;
244
     char *content;
245
+    /* Should be a multiple of 4 */
246
+    int path_max_size = 1024;
247
+    int prop_value_size = 0;
248
 
249
     if ((conn = xcb_connect(NULL, &screen)) == NULL ||
250
         xcb_connection_has_error(conn))
251
@@ -46,10 +49,24 @@ char *root_atom_contents(const char *atomname) {
252
     xcb_get_property_cookie_t prop_cookie;
253
     xcb_get_property_reply_t *prop_reply;
254
     prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
255
-                                             XCB_GET_PROPERTY_TYPE_ANY, 0, PATH_MAX);
256
+                                             XCB_GET_PROPERTY_TYPE_ANY, 0, path_max_size / 4);
257
     prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
258
-    if (prop_reply == NULL || xcb_get_property_value_length(prop_reply) == 0)
259
+    if (prop_reply) {
260
+        prop_value_size = xcb_get_property_value_length(prop_reply);
261
+        if (prop_reply && prop_value_size > 0 && prop_value_size < path_max_size) {
262
+            path_max_size += (prop_reply->bytes_after / 4) + 1;
263
+            prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
264
+                            XCB_GET_PROPERTY_TYPE_ANY, 0, path_max_size / 4);
265
+            prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
266
+	    prop_value_size = 0;
267
+	    if (prop_reply) {
268
+                prop_value_size = xcb_get_property_value_length(prop_reply);
269
+	    }
270
+        }
271
+    }
272
+    if (prop_reply == NULL || prop_value_size == 0)
273
         return NULL;
274
+
275
     if (prop_reply->type == XCB_ATOM_CARDINAL) {
276
         /* We treat a CARDINAL as a >= 32-bit unsigned int. The only CARDINAL
277
          * we query is I3_PID, which is 32-bit. */

b/src/display_version.c

282
@@ -128,13 +128,23 @@ void display_running_version(void) {
283
     printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
284
 
285
 #ifdef __linux__
286
-    char exepath[PATH_MAX],
287
-         destpath[PATH_MAX];
288
+    size_t exepath_size = 1024,
289
+           destpath_size = 1024;
290
     ssize_t linksize;
291
 
292
-    snprintf(exepath, sizeof(exepath), "/proc/%d/exe", getpid());
293
+    char *exepath = smalloc(exepath_size);
294
+    char *destpath = smalloc(destpath_size);
295
 
296
-    if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
297
+    while (snprintf(exepath, exepath_size, "/proc/%d/exe", getpid()) >= exepath_size) {
298
+            exepath_size = exepath_size * 2;
299
+            exepath = srealloc(exepath, exepath_size);
300
+    }
301
+
302
+    while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
303
+            destpath_size = destpath_size * 2;
304
+            destpath = srealloc(destpath, destpath_size);
305
+    }
306
+    if (linksize == -1)
307
         err(EXIT_FAILURE, "readlink(%s)", exepath);
308
 
309
     /* readlink() does not NULL-terminate strings, so we have to. */
310
@@ -143,9 +153,16 @@ void display_running_version(void) {
311
     printf("\n");
312
     printf("The i3 binary you just called: %s\n", destpath);
313
 
314
-    snprintf(exepath, sizeof(exepath), "/proc/%s/exe", pid_from_atom);
315
+    while (snprintf(exepath, exepath_size, "/proc/%s/exe", pid_from_atom) >= exepath_size) {
316
+            exepath_size = exepath_size * 2;
317
+            exepath = srealloc(exepath, exepath_size);
318
+    }
319
 
320
-    if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
321
+    while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
322
+        destpath_size = destpath_size * 2;
323
+        destpath = srealloc(destpath, destpath_size);
324
+    }
325
+    if (linksize == -1)
326
         err(EXIT_FAILURE, "readlink(%s)", exepath);
327
 
328
     /* readlink() does not NULL-terminate strings, so we have to. */
329
@@ -159,7 +176,10 @@ void display_running_version(void) {
330
     /* Since readlink() might put a "(deleted)" somewhere in the buffer and
331
      * stripping that out seems hackish and ugly, we read the process’s argv[0]
332
      * instead. */
333
-    snprintf(exepath, sizeof(exepath), "/proc/%s/cmdline", pid_from_atom);
334
+    while (snprintf(exepath, exepath_size, "/proc/%s/cmdline", pid_from_atom) >= exepath_size) {
335
+        exepath_size = exepath_size * 2;
336
+        exepath = srealloc(exepath, exepath_size);
337
+    }
338
 
339
     int fd;
340
     if ((fd = open(exepath, O_RDONLY)) == -1)
341
@@ -169,6 +189,9 @@ void display_running_version(void) {
342
     close(fd);
343
 
344
     printf("The i3 binary you are running: %s\n", destpath);
345
+
346
+    free(exepath);
347
+    free(destpath);
348
 #endif
349
 
350
     yajl_free(handle);

b/src/main.c

355
@@ -488,18 +488,25 @@ int main(int argc, char *argv[]) {
356
 
357
         /* The following code is helpful, but not required. We thus don’t pay
358
          * much attention to error handling, non-linux or other edge cases. */
359
-        char cwd[PATH_MAX];
360
         LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
361
-        if (getcwd(cwd, sizeof(cwd)) != NULL)
362
+        size_t cwd_size = 1024;
363
+        char *cwd = smalloc(cwd_size);
364
+        while (getcwd(cwd, cwd_size) == NULL && errno == ERANGE) {
365
+                cwd_size = cwd_size * 2;
366
+                cwd = srealloc(cwd, cwd_size);
367
+        }
368
+        if (cwd != NULL)
369
             LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
370
+
371
         int patternfd;
372
         if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
373
-            memset(cwd, '\0', sizeof(cwd));
374
-            if (read(patternfd, cwd, sizeof(cwd)) > 0)
375
+            memset(cwd, '\0', cwd_size);
376
+            if (read(patternfd, cwd, cwd_size) > 0)
377
                 /* a trailing newline is included in cwd */
378
                 LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
379
             close(patternfd);
380
         }
381
+        free(cwd);
382
     }
383
 
384
     LOG("i3 " I3_VERSION " starting\n");