blob: 6b2f3770ce8e2f7889aa30fe8a53995f26269822 [file] [log] [blame]
Pekka Paalanen51aaf642012-05-30 15:53:41 +03001/*
2 * Copyright © 2012 Collabora, Ltd.
3 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07004 * 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 Paalanen51aaf642012-05-30 15:53:41 +030011 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070012 * 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 Paalanen51aaf642012-05-30 15:53:41 +030024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030027
Pekka Paalanen51aaf642012-05-30 15:53:41 +030028#include <sys/types.h>
29#include <sys/socket.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <errno.h>
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030033#include <sys/epoll.h>
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030034#include <string.h>
35#include <stdlib.h>
Pekka Paalanen51aaf642012-05-30 15:53:41 +030036
37#include "os-compatibility.h"
38
Derek Foreman6bc33d62015-06-08 11:37:31 -050039int
40os_fd_set_cloexec(int fd)
Pekka Paalanen51aaf642012-05-30 15:53:41 +030041{
42 long flags;
43
44 if (fd == -1)
45 return -1;
46
47 flags = fcntl(fd, F_GETFD);
48 if (flags == -1)
Derek Foreman6bc33d62015-06-08 11:37:31 -050049 return -1;
Pekka Paalanen51aaf642012-05-30 15:53:41 +030050
51 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
Derek Foreman6bc33d62015-06-08 11:37:31 -050052 return -1;
Pekka Paalanen51aaf642012-05-30 15:53:41 +030053
Derek Foreman6bc33d62015-06-08 11:37:31 -050054 return 0;
55}
56
57static int
58set_cloexec_or_close(int fd)
59{
60 if (os_fd_set_cloexec(fd) != 0) {
61 close(fd);
62 return -1;
63 }
Pekka Paalanen51aaf642012-05-30 15:53:41 +030064 return fd;
Pekka Paalanen51aaf642012-05-30 15:53:41 +030065}
66
67int
68os_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 Paalanen647f2bf2012-05-30 15:53:43 +030093int
94os_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 Paalanen1da1b8f2012-06-06 16:59:43 +0300109
110static int
111create_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 Paalanen5b4ddbc2013-11-29 17:48:51 +0200144 *
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 Rehmanb5f838d2017-01-01 19:46:42 +0500147 * given size. If disk space is insufficient, errno is set to ENOSPC.
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200148 * If posix_fallocate() is not supported, program may receive
149 * SIGBUS on accessing mmap()'ed file contents instead.
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300150 */
151int
152os_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 Paalanen5b4ddbc2013-11-29 17:48:51 +0200158 int ret;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300159
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 Paalanen5b4ddbc2013-11-29 17:48:51 +0200180#ifdef HAVE_POSIX_FALLOCATE
Derek Foreman91d4bce2017-03-23 11:59:22 -0500181 do {
182 ret = posix_fallocate(fd, 0, size);
183 } while (ret == EINTR);
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200184 if (ret != 0) {
185 close(fd);
186 errno = ret;
187 return -1;
188 }
189#else
190 ret = ftruncate(fd, size);
191 if (ret < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300192 close(fd);
193 return -1;
194 }
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200195#endif
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300196
197 return fd;
198}
Pekka Paalanenb7a94982012-06-12 17:42:25 +0300199
200#ifndef HAVE_STRCHRNUL
201char *
202strchrnul(const char *s, int c)
203{
204 while (*s && *s != c)
205 s++;
206 return (char *)s;
207}
208#endif