blob: b903347faedb9a8bbbf836790523f03f007fc33a [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
39static int
40set_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
56err:
57 close(fd);
58 return -1;
59}
60
61int
62os_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 Paalanen647f2bf2012-05-30 15:53:43 +030087int
88os_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 Paalanen1da1b8f2012-06-06 16:59:43 +0300103
104static int
105create_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 Paalanen5b4ddbc2013-11-29 17:48:51 +0200138 *
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 Paalanen1da1b8f2012-06-06 16:59:43 +0300144 */
145int
146os_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 Paalanen5b4ddbc2013-11-29 17:48:51 +0200152 int ret;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300153
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 Paalanen5b4ddbc2013-11-29 17:48:51 +0200174#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 Paalanen1da1b8f2012-06-06 16:59:43 +0300184 close(fd);
185 return -1;
186 }
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200187#endif
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300188
189 return fd;
190}
Pekka Paalanenb7a94982012-06-12 17:42:25 +0300191
192#ifndef HAVE_STRCHRNUL
193char *
194strchrnul(const char *s, int c)
195{
196 while (*s && *s != c)
197 s++;
198 return (char *)s;
199}
200#endif