1 /*
2  * Copyright © 2012 Collabora, Ltd.
3  *
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:
11  *
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.
24  */
25 
26 /*
27  * Based on weston shared/os-compatibility.c
28  */
29 
30 #include "anon_file.h"
31 #include "detect_os.h"
32 
33 #ifndef _WIN32
34 
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 
40 #if defined(HAVE_MEMFD_CREATE) || defined(__FreeBSD__) || defined(__OpenBSD__)
41 #include <sys/mman.h>
42 #elif DETECT_OS_ANDROID
43 #include <sys/syscall.h>
44 #include <linux/memfd.h>
45 #else
46 #include <stdio.h>
47 #endif
48 
49 #if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(HAVE_MKOSTEMP) || defined(ANDROID))
50 static int
set_cloexec_or_close(int fd)51 set_cloexec_or_close(int fd)
52 {
53    long flags;
54 
55    if (fd == -1)
56       return -1;
57 
58    flags = fcntl(fd, F_GETFD);
59    if (flags == -1)
60       goto err;
61 
62    if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
63       goto err;
64 
65    return fd;
66 
67 err:
68    close(fd);
69    return -1;
70 }
71 #endif
72 
73 #if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(ANDROID))
74 static int
create_tmpfile_cloexec(char * tmpname)75 create_tmpfile_cloexec(char *tmpname)
76 {
77    int fd;
78 
79 #ifdef HAVE_MKOSTEMP
80    fd = mkostemp(tmpname, O_CLOEXEC);
81 #else
82    fd = mkstemp(tmpname);
83 #endif
84 
85    if (fd < 0) {
86       return fd;
87    }
88 
89 #ifndef HAVE_MKOSTEMP
90    fd = set_cloexec_or_close(fd);
91 #endif
92 
93    unlink(tmpname);
94    return fd;
95 }
96 #endif
97 
98 /*
99  * Create a new, unique, anonymous file of the given size, and
100  * return the file descriptor for it. The file descriptor is set
101  * CLOEXEC. The file is immediately suitable for mmap()'ing
102  * the given size at offset zero.
103  *
104  * An optional name for debugging can be provided as the second argument.
105  *
106  * The file should not have a permanent backing store like a disk,
107  * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
108  *
109  * If memfd or SHM_ANON is supported, the filesystem is not touched at all.
110  * Otherwise, the file name is deleted from the file system.
111  *
112  * The file is suitable for buffer sharing between processes by
113  * transmitting the file descriptor over Unix sockets using the
114  * SCM_RIGHTS methods.
115  */
116 int
os_create_anonymous_file(int64_t size,const char * debug_name)117 os_create_anonymous_file(int64_t size, const char *debug_name)
118 {
119    int fd, ret;
120 #if defined(HAVE_MEMFD_CREATE)
121    if (!debug_name)
122       debug_name = "mesa-shared";
123    fd = memfd_create(debug_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
124 #elif DETECT_OS_ANDROID
125    if (!debug_name)
126       debug_name = "mesa-shared";
127    fd = syscall(SYS_memfd_create, debug_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
128 #elif defined(__FreeBSD__)
129    fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600);
130 #elif defined(__OpenBSD__)
131    char template[] = "/tmp/mesa-XXXXXXXXXX";
132    fd = shm_mkstemp(template);
133    if (fd != -1)
134       shm_unlink(template);
135 #else
136    const char *path;
137    char *name;
138 
139    path = getenv("XDG_RUNTIME_DIR");
140    if (!path) {
141       errno = ENOENT;
142       return -1;
143    }
144 
145    if (debug_name)
146       asprintf(&name, "%s/mesa-shared-%s-XXXXXX", path, debug_name);
147    else
148       asprintf(&name, "%s/mesa-shared-XXXXXX", path);
149    if (!name)
150       return -1;
151 
152    fd = create_tmpfile_cloexec(name);
153 
154    free(name);
155 #endif
156 
157    if (fd < 0)
158       return -1;
159 
160    ret = ftruncate(fd, (off_t)size);
161    if (ret < 0) {
162       close(fd);
163       return -1;
164    }
165 
166    return fd;
167 }
168 #else
169 
170 #include <windows.h>
171 #include <io.h>
172 
173 int
os_create_anonymous_file(int64_t size,const char * debug_name)174 os_create_anonymous_file(int64_t size, const char *debug_name)
175 {
176    (void)debug_name;
177    HANDLE h = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
178       PAGE_READWRITE, (size >> 32), size & 0xFFFFFFFF, NULL);
179    return _open_osfhandle((intptr_t)h, 0);
180 }
181 #endif
182