blob: 611e7c8068542356306485da13c891e45e9665e4 [file] [log] [blame]
Pekka Paalanen51aaf642012-05-30 15:53:41 +03001/*
2 * Copyright © 2012 Collabora, Ltd.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030024
Pekka Paalanen51aaf642012-05-30 15:53:41 +030025#include <sys/types.h>
26#include <sys/socket.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <errno.h>
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030030#include <sys/epoll.h>
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030031#include <string.h>
32#include <stdlib.h>
Pekka Paalanen51aaf642012-05-30 15:53:41 +030033
34#include "os-compatibility.h"
35
36static int
37set_cloexec_or_close(int fd)
38{
39 long flags;
40
41 if (fd == -1)
42 return -1;
43
44 flags = fcntl(fd, F_GETFD);
45 if (flags == -1)
46 goto err;
47
48 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
49 goto err;
50
51 return fd;
52
53err:
54 close(fd);
55 return -1;
56}
57
58int
59os_socketpair_cloexec(int domain, int type, int protocol, int *sv)
60{
61 int ret;
62
63#ifdef SOCK_CLOEXEC
64 ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv);
65 if (ret == 0 || errno != EINVAL)
66 return ret;
67#endif
68
69 ret = socketpair(domain, type, protocol, sv);
70 if (ret < 0)
71 return ret;
72
73 sv[0] = set_cloexec_or_close(sv[0]);
74 sv[1] = set_cloexec_or_close(sv[1]);
75
76 if (sv[0] != -1 && sv[1] != -1)
77 return 0;
78
79 close(sv[0]);
80 close(sv[1]);
81 return -1;
82}
83
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030084int
85os_epoll_create_cloexec(void)
86{
87 int fd;
88
89#ifdef EPOLL_CLOEXEC
90 fd = epoll_create1(EPOLL_CLOEXEC);
91 if (fd >= 0)
92 return fd;
93 if (errno != EINVAL)
94 return -1;
95#endif
96
97 fd = epoll_create(1);
98 return set_cloexec_or_close(fd);
99}
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300100
101static int
102create_tmpfile_cloexec(char *tmpname)
103{
104 int fd;
105
106#ifdef HAVE_MKOSTEMP
107 fd = mkostemp(tmpname, O_CLOEXEC);
108 if (fd >= 0)
109 unlink(tmpname);
110#else
111 fd = mkstemp(tmpname);
112 if (fd >= 0) {
113 fd = set_cloexec_or_close(fd);
114 unlink(tmpname);
115 }
116#endif
117
118 return fd;
119}
120
121/*
122 * Create a new, unique, anonymous file of the given size, and
123 * return the file descriptor for it. The file descriptor is set
124 * CLOEXEC. The file is immediately suitable for mmap()'ing
125 * the given size at offset zero.
126 *
127 * The file should not have a permanent backing store like a disk,
128 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
129 *
130 * The file name is deleted from the file system.
131 *
132 * The file is suitable for buffer sharing between processes by
133 * transmitting the file descriptor over Unix sockets using the
134 * SCM_RIGHTS methods.
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200135 *
136 * If the C library implements posix_fallocate(), it is used to
137 * guarantee that disk space is available for the file at the
138 * given size. If disk space is insufficent, errno is set to ENOSPC.
139 * If posix_fallocate() is not supported, program may receive
140 * SIGBUS on accessing mmap()'ed file contents instead.
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300141 */
142int
143os_create_anonymous_file(off_t size)
144{
145 static const char template[] = "/weston-shared-XXXXXX";
146 const char *path;
147 char *name;
148 int fd;
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200149 int ret;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300150
151 path = getenv("XDG_RUNTIME_DIR");
152 if (!path) {
153 errno = ENOENT;
154 return -1;
155 }
156
157 name = malloc(strlen(path) + sizeof(template));
158 if (!name)
159 return -1;
160
161 strcpy(name, path);
162 strcat(name, template);
163
164 fd = create_tmpfile_cloexec(name);
165
166 free(name);
167
168 if (fd < 0)
169 return -1;
170
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200171#ifdef HAVE_POSIX_FALLOCATE
172 ret = posix_fallocate(fd, 0, size);
173 if (ret != 0) {
174 close(fd);
175 errno = ret;
176 return -1;
177 }
178#else
179 ret = ftruncate(fd, size);
180 if (ret < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300181 close(fd);
182 return -1;
183 }
Pekka Paalanen5b4ddbc2013-11-29 17:48:51 +0200184#endif
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300185
186 return fd;
187}
Pekka Paalanenb7a94982012-06-12 17:42:25 +0300188
189#ifndef HAVE_STRCHRNUL
190char *
191strchrnul(const char *s, int c)
192{
193 while (*s && *s != c)
194 s++;
195 return (char *)s;
196}
197#endif