blob: 66934a8c982a648afc357bd4866d648b445f39cf [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
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030023#define _GNU_SOURCE
24
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.
135 */
136int
137os_create_anonymous_file(off_t size)
138{
139 static const char template[] = "/weston-shared-XXXXXX";
140 const char *path;
141 char *name;
142 int fd;
143
144 path = getenv("XDG_RUNTIME_DIR");
145 if (!path) {
146 errno = ENOENT;
147 return -1;
148 }
149
150 name = malloc(strlen(path) + sizeof(template));
151 if (!name)
152 return -1;
153
154 strcpy(name, path);
155 strcat(name, template);
156
157 fd = create_tmpfile_cloexec(name);
158
159 free(name);
160
161 if (fd < 0)
162 return -1;
163
164 if (ftruncate(fd, size) < 0) {
165 close(fd);
166 return -1;
167 }
168
169 return fd;
170}