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 | |
| 39 | static int |
| 40 | set_cloexec_or_close(int fd) |
| 41 | { |
| 42 | long flags; |
| 43 | |
| 44 | if (fd == -1) |
| 45 | return -1; |
| 46 | |
| 47 | flags = fcntl(fd, F_GETFD); |
| 48 | if (flags == -1) |
| 49 | goto err; |
| 50 | |
| 51 | if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) |
| 52 | goto err; |
| 53 | |
| 54 | return fd; |
| 55 | |
| 56 | err: |
| 57 | close(fd); |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | os_socketpair_cloexec(int domain, int type, int protocol, int *sv) |
| 63 | { |
| 64 | int ret; |
| 65 | |
| 66 | #ifdef SOCK_CLOEXEC |
| 67 | ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv); |
| 68 | if (ret == 0 || errno != EINVAL) |
| 69 | return ret; |
| 70 | #endif |
| 71 | |
| 72 | ret = socketpair(domain, type, protocol, sv); |
| 73 | if (ret < 0) |
| 74 | return ret; |
| 75 | |
| 76 | sv[0] = set_cloexec_or_close(sv[0]); |
| 77 | sv[1] = set_cloexec_or_close(sv[1]); |
| 78 | |
| 79 | if (sv[0] != -1 && sv[1] != -1) |
| 80 | return 0; |
| 81 | |
| 82 | close(sv[0]); |
| 83 | close(sv[1]); |
| 84 | return -1; |
| 85 | } |
| 86 | |
Pekka Paalanen | 647f2bf | 2012-05-30 15:53:43 +0300 | [diff] [blame] | 87 | int |
| 88 | os_epoll_create_cloexec(void) |
| 89 | { |
| 90 | int fd; |
| 91 | |
| 92 | #ifdef EPOLL_CLOEXEC |
| 93 | fd = epoll_create1(EPOLL_CLOEXEC); |
| 94 | if (fd >= 0) |
| 95 | return fd; |
| 96 | if (errno != EINVAL) |
| 97 | return -1; |
| 98 | #endif |
| 99 | |
| 100 | fd = epoll_create(1); |
| 101 | return set_cloexec_or_close(fd); |
| 102 | } |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 103 | |
| 104 | static int |
| 105 | create_tmpfile_cloexec(char *tmpname) |
| 106 | { |
| 107 | int fd; |
| 108 | |
| 109 | #ifdef HAVE_MKOSTEMP |
| 110 | fd = mkostemp(tmpname, O_CLOEXEC); |
| 111 | if (fd >= 0) |
| 112 | unlink(tmpname); |
| 113 | #else |
| 114 | fd = mkstemp(tmpname); |
| 115 | if (fd >= 0) { |
| 116 | fd = set_cloexec_or_close(fd); |
| 117 | unlink(tmpname); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | return fd; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Create a new, unique, anonymous file of the given size, and |
| 126 | * return the file descriptor for it. The file descriptor is set |
| 127 | * CLOEXEC. The file is immediately suitable for mmap()'ing |
| 128 | * the given size at offset zero. |
| 129 | * |
| 130 | * The file should not have a permanent backing store like a disk, |
| 131 | * but may have if XDG_RUNTIME_DIR is not properly implemented in OS. |
| 132 | * |
| 133 | * The file name is deleted from the file system. |
| 134 | * |
| 135 | * The file is suitable for buffer sharing between processes by |
| 136 | * transmitting the file descriptor over Unix sockets using the |
| 137 | * SCM_RIGHTS methods. |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 138 | * |
| 139 | * If the C library implements posix_fallocate(), it is used to |
| 140 | * guarantee that disk space is available for the file at the |
| 141 | * given size. If disk space is insufficent, errno is set to ENOSPC. |
| 142 | * If posix_fallocate() is not supported, program may receive |
| 143 | * SIGBUS on accessing mmap()'ed file contents instead. |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 144 | */ |
| 145 | int |
| 146 | os_create_anonymous_file(off_t size) |
| 147 | { |
| 148 | static const char template[] = "/weston-shared-XXXXXX"; |
| 149 | const char *path; |
| 150 | char *name; |
| 151 | int fd; |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 152 | int ret; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 153 | |
| 154 | path = getenv("XDG_RUNTIME_DIR"); |
| 155 | if (!path) { |
| 156 | errno = ENOENT; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | name = malloc(strlen(path) + sizeof(template)); |
| 161 | if (!name) |
| 162 | return -1; |
| 163 | |
| 164 | strcpy(name, path); |
| 165 | strcat(name, template); |
| 166 | |
| 167 | fd = create_tmpfile_cloexec(name); |
| 168 | |
| 169 | free(name); |
| 170 | |
| 171 | if (fd < 0) |
| 172 | return -1; |
| 173 | |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 174 | #ifdef HAVE_POSIX_FALLOCATE |
| 175 | ret = posix_fallocate(fd, 0, size); |
| 176 | if (ret != 0) { |
| 177 | close(fd); |
| 178 | errno = ret; |
| 179 | return -1; |
| 180 | } |
| 181 | #else |
| 182 | ret = ftruncate(fd, size); |
| 183 | if (ret < 0) { |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 184 | close(fd); |
| 185 | return -1; |
| 186 | } |
Pekka Paalanen | 5b4ddbc | 2013-11-29 17:48:51 +0200 | [diff] [blame] | 187 | #endif |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 188 | |
| 189 | return fd; |
| 190 | } |
Pekka Paalanen | b7a9498 | 2012-06-12 17:42:25 +0300 | [diff] [blame] | 191 | |
| 192 | #ifndef HAVE_STRCHRNUL |
| 193 | char * |
| 194 | strchrnul(const char *s, int c) |
| 195 | { |
| 196 | while (*s && *s != c) |
| 197 | s++; |
| 198 | return (char *)s; |
| 199 | } |
| 200 | #endif |