2 * Copyright © 2012 Collabora, Ltd.
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:
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.
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
27 * Based on weston shared/os-compatibility.c
31 #include "anon_file.h"
38 #if defined(__FreeBSD__) || defined(__OpenBSD__)
40 #elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID)
41 #include <sys/syscall.h>
42 #include <linux/memfd.h>
47 #if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(HAVE_MKOSTEMP) || defined(ANDROID))
49 set_cloexec_or_close(int fd
)
56 flags
= fcntl(fd
, F_GETFD
);
60 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
71 #if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(ANDROID))
73 create_tmpfile_cloexec(char *tmpname
)
78 fd
= mkostemp(tmpname
, O_CLOEXEC
);
80 fd
= mkstemp(tmpname
);
88 fd
= set_cloexec_or_close(fd
);
97 * Create a new, unique, anonymous file of the given size, and
98 * return the file descriptor for it. The file descriptor is set
99 * CLOEXEC. The file is immediately suitable for mmap()'ing
100 * the given size at offset zero.
102 * An optional name for debugging can be provided as the second argument.
104 * The file should not have a permanent backing store like a disk,
105 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
107 * If memfd or SHM_ANON is supported, the filesystem is not touched at all.
108 * Otherwise, the file name is deleted from the file system.
110 * The file is suitable for buffer sharing between processes by
111 * transmitting the file descriptor over Unix sockets using the
112 * SCM_RIGHTS methods.
115 os_create_anonymous_file(off_t size
, const char *debug_name
)
120 fd
= shm_open(SHM_ANON
, O_CREAT
| O_RDWR
| O_CLOEXEC
, 0600);
121 #elif defined(__OpenBSD__)
122 char template[] = "/tmp/mesa-XXXXXXXXXX";
123 fd
= shm_mkstemp(template);
125 shm_unlink(template);
126 #elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID)
128 debug_name
= "mesa-shared";
129 fd
= syscall(SYS_memfd_create
, debug_name
, MFD_CLOEXEC
);
134 path
= getenv("XDG_RUNTIME_DIR");
141 asprintf(&name
, "%s/mesa-shared-%s-XXXXXX", path
, debug_name
);
143 asprintf(&name
, "%s/mesa-shared-XXXXXX", path
);
147 fd
= create_tmpfile_cloexec(name
);
155 ret
= ftruncate(fd
, size
);