Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Collabora, Ltd. |
| 3 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 11 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 27 | |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <unistd.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <errno.h> |
Pekka Paalanen | 647f2bf | 2012-05-30 15:53:43 +0300 | [diff] [blame] | 33 | #include <sys/epoll.h> |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 36 | |
| 37 | #include "os-compatibility.h" |
| 38 | |
Derek Foreman | 6bc33d6 | 2015-06-08 11:37:31 -0500 | [diff] [blame] | 39 | int |
| 40 | os_fd_set_cloexec(int fd) |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 41 | { |
| 42 | long flags; |
| 43 | |
| 44 | if (fd == -1) |
| 45 | return -1; |
| 46 | |
| 47 | flags = fcntl(fd, F_GETFD); |
| 48 | if (flags == -1) |
Derek Foreman | 6bc33d6 | 2015-06-08 11:37:31 -0500 | [diff] [blame] | 49 | return -1; |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 50 | |
| 51 | if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) |
Derek Foreman | 6bc33d6 | 2015-06-08 11:37:31 -0500 | [diff] [blame] | 52 | return -1; |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 53 | |
Derek Foreman | 6bc33d6 | 2015-06-08 11:37:31 -0500 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int |
| 58 | set_cloexec_or_close(int fd) |
| 59 | { |
| 60 | if (os_fd_set_cloexec(fd) != 0) { |
| 61 | close(fd); |
| 62 | return -1; |
| 63 | } |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 64 | return fd; |
Pekka Paalanen | 51aaf64 | 2012-05-30 15:53:41 +0300 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int |
| 68 | os_socketpair_cloexec(int domain, int type, int protocol, int *sv) |
| 69 | { |
| 70 | int ret; |
| 71 | |
| 72 | #ifdef SOCK_CLOEXEC |
| 73 | ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv); |
| 74 | if (ret == 0 || errno != EINVAL) |
| 75 | return ret; |
| 76 | #endif |
| 77 | |
| 78 | ret = socketpair(domain, type, protocol, sv); |
| 79 | if (ret < 0) |
| 80 | return ret; |
| 81 | |
| 82 | sv[0] = set_cloexec_or_close(sv[0]); |
| 83 | sv[1] = set_cloexec_or_close(sv[1]); |
| 84 | |
| 85 | if (sv[0] != -1 && sv[1] != -1) |
| 86 | return 0; |
| 87 | |
| 88 | close(sv[0]); |
| 89 | close(sv[1]); |
| 90 | return -1; |
| 91 | } |
| 92 | |
Pekka Paalanen | 647f2bf | 2012-05-30 15:53:43 +0300 | [diff] [blame] | 93 | int |
| 94 | os_epoll_create_cloexec(void) |
| 95 | { |
| 96 | int fd; |
| 97 | |
| 98 | #ifdef EPOLL_CLOEXEC |
| 99 | fd = epoll_create1(EPOLL_CLOEXEC); |
| 100 | if (fd >= 0) |
| 101 | return fd; |
| 102 | if (errno != EINVAL) |
| 103 | return -1; |
| 104 | #endif |
| 105 | |
| 106 | fd = epoll_create(1); |
| 107 | return set_cloexec_or_close(fd); |
| 108 | } |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 109 | |
| 110 | static int |
| 111 | create_tmpfile_cloexec(char *tmpname) |
| 112 | { |
| 113 | int fd; |
| 114 | |
| 115 | #ifdef HAVE_MKOSTEMP |
| 116 | fd = mkostemp(tmpname, O_CLOEXEC); |
| 117 | if (fd >= 0) |
| 118 | unlink(tmpname); |
| 119 | #else |
| 120 | fd = mkstemp(tmpname); |
| 121 | if (fd >= 0) { |
| 122 | fd = set_cloexec_or_close(fd); |
| 123 | unlink(tmpname); |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | return fd; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Create a new, unique, anonymous file of the given size, and |
| 132 | * return the file descriptor for it. The file descriptor is set |
| 133 | * CLOEXEC. The file is immediately suitable for mmap()'ing |
| 134 | * the given size at offset zero. |
| 135 | * |
| 136 | * The file should not have a permanent backing store like a disk, |
| 137 | * but may have if XDG_RUNTIME_DIR is not properly implemented in OS. |
| 138 | * |
| 139 | * The file name is deleted from the file system. |
| 140 | * |
| 141 | * The file is suitable for buffer sharing between processes by |
| 142 | * transmitting the file descriptor over Unix sockets using the |
| 143 | * SCM_RIGHTS methods. |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 144 | * |
| 145 | * If the C library implements posix_fallocate(), it is used to |
| 146 | * guarantee that disk space is available for the file at the |
Abdur Rehman | b5f838d | 2017-01-01 19:46:42 +0500 | [diff] [blame] | 147 | * given size. If disk space is insufficient, errno is set to ENOSPC. |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 148 | * If posix_fallocate() is not supported, program may receive |
| 149 | * SIGBUS on accessing mmap()'ed file contents instead. |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 150 | */ |
| 151 | int |
| 152 | os_create_anonymous_file(off_t size) |
| 153 | { |
| 154 | static const char template[] = "/weston-shared-XXXXXX"; |
| 155 | const char *path; |
| 156 | char *name; |
| 157 | int fd; |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 158 | int ret; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 159 | |
| 160 | path = getenv("XDG_RUNTIME_DIR"); |
| 161 | if (!path) { |
| 162 | errno = ENOENT; |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | name = malloc(strlen(path) + sizeof(template)); |
| 167 | if (!name) |
| 168 | return -1; |
| 169 | |
| 170 | strcpy(name, path); |
| 171 | strcat(name, template); |
| 172 | |
| 173 | fd = create_tmpfile_cloexec(name); |
| 174 | |
| 175 | free(name); |
| 176 | |
| 177 | if (fd < 0) |
| 178 | return -1; |
| 179 | |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 180 | #ifdef HAVE_POSIX_FALLOCATE |
Derek Foreman | 91d4bce | 2017-03-23 11:59:22 -0500 | [diff] [blame] | 181 | do { |
| 182 | ret = posix_fallocate(fd, 0, size); |
| 183 | } while (ret == EINTR); |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 184 | if (ret != 0) { |
| 185 | close(fd); |
| 186 | errno = ret; |
| 187 | return -1; |
| 188 | } |
| 189 | #else |
Derek Foreman | 5ef6bd7 | 2017-03-23 11:59:23 -0500 | [diff] [blame] | 190 | do { |
| 191 | ret = ftruncate(fd, size); |
| 192 | } while (ret < 0 && errno == EINTR); |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 193 | if (ret < 0) { |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 194 | close(fd); |
| 195 | return -1; |
| 196 | } |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 197 | #endif |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 198 | |
| 199 | return fd; |
| 200 | } |
Pekka Paalanen | b7a9498 | 2012-06-12 17:42:25 +0300 | [diff] [blame] | 201 | |
| 202 | #ifndef HAVE_STRCHRNUL |
| 203 | char * |
| 204 | strchrnul(const char *s, int c) |
| 205 | { |
| 206 | while (*s && *s != c) |
| 207 | s++; |
| 208 | return (char *)s; |
| 209 | } |
| 210 | #endif |