diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/pool-buffer.c | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/client/pool-buffer.c b/client/pool-buffer.c index 1f54a77c..7610d223 100644 --- a/client/pool-buffer.c +++ b/client/pool-buffer.c | |||
@@ -1,37 +1,56 @@ | |||
1 | #define _XOPEN_SOURCE 500 | 1 | #define _XOPEN_SOURCE 500 |
2 | #include <assert.h> | 2 | #include <assert.h> |
3 | #include <cairo/cairo.h> | 3 | #include <cairo/cairo.h> |
4 | #include <fcntl.h> | ||
5 | #include <pango/pangocairo.h> | ||
4 | #include <stdio.h> | 6 | #include <stdio.h> |
5 | #include <stdlib.h> | 7 | #include <stdlib.h> |
6 | #include <string.h> | 8 | #include <string.h> |
7 | #include <sys/mman.h> | 9 | #include <sys/mman.h> |
8 | #include <pango/pangocairo.h> | ||
9 | #include <unistd.h> | 10 | #include <unistd.h> |
10 | #include <wayland-client.h> | 11 | #include <wayland-client.h> |
11 | #include "config.h" | 12 | #include "config.h" |
12 | #include "pool-buffer.h" | 13 | #include "pool-buffer.h" |
13 | 14 | ||
15 | static bool set_cloexec(int fd) { | ||
16 | long flags = fcntl(fd, F_GETFD); | ||
17 | if (flags == -1) { | ||
18 | return false; | ||
19 | } | ||
20 | |||
21 | if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { | ||
22 | return false; | ||
23 | } | ||
24 | |||
25 | return true; | ||
26 | } | ||
27 | |||
14 | static int create_pool_file(size_t size, char **name) { | 28 | static int create_pool_file(size_t size, char **name) { |
15 | static const char template[] = "sway-client-XXXXXX"; | 29 | static const char template[] = "sway-client-XXXXXX"; |
16 | const char *path = getenv("XDG_RUNTIME_DIR"); | 30 | const char *path = getenv("XDG_RUNTIME_DIR"); |
17 | if (!path) { | 31 | if (path == NULL) { |
32 | fprintf(stderr, "XDG_RUNTIME_DIR is not set\n"); | ||
18 | return -1; | 33 | return -1; |
19 | } | 34 | } |
20 | 35 | ||
21 | int ts = (path[strlen(path) - 1] == '/'); | 36 | size_t name_size = strlen(template) + 1 + strlen(path) + 1; |
22 | 37 | *name = malloc(name_size); | |
23 | *name = malloc( | 38 | if (*name == NULL) { |
24 | strlen(template) + | 39 | fprintf(stderr, "allocation failed\n"); |
25 | strlen(path) + | 40 | return -1; |
26 | (ts ? 0 : 1) + 1); | 41 | } |
27 | sprintf(*name, "%s%s%s", path, ts ? "" : "/", template); | 42 | snprintf(*name, name_size, "%s/%s", path, template); |
28 | 43 | ||
29 | int fd = mkstemp(*name); | 44 | int fd = mkstemp(*name); |
30 | |||
31 | if (fd < 0) { | 45 | if (fd < 0) { |
32 | return -1; | 46 | return -1; |
33 | } | 47 | } |
34 | 48 | ||
49 | if (!set_cloexec(fd)) { | ||
50 | close(fd); | ||
51 | return -1; | ||
52 | } | ||
53 | |||
35 | if (ftruncate(fd, size) < 0) { | 54 | if (ftruncate(fd, size) < 0) { |
36 | close(fd); | 55 | close(fd); |
37 | return -1; | 56 | return -1; |