util: add anon_file.h for all memfd/temp file usage
[mesa.git] / src / util / anon_file.c
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 #ifndef WIN32
31 #include "anon_file.h"
32
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <errno.h>
36
37 #ifdef __FreeBSD__
38 #include <sys/mman.h>
39 #elif defined(HAVE_MEMFD_CREATE)
40 #include <sys/syscall.h>
41 #include <linux/memfd.h>
42 #endif
43
44 #if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(HAVE_MKOSTEMP))
45 static int
46 set_cloexec_or_close(int fd)
47 {
48 long flags;
49
50 if (fd == -1)
51 return -1;
52
53 flags = fcntl(fd, F_GETFD);
54 if (flags == -1)
55 goto err;
56
57 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
58 goto err;
59
60 return fd;
61
62 err:
63 close(fd);
64 return -1;
65 }
66 #endif
67
68 #if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE))
69 static int
70 create_tmpfile_cloexec(char *tmpname)
71 {
72 int fd;
73
74 #ifdef HAVE_MKOSTEMP
75 fd = mkostemp(tmpname, O_CLOEXEC);
76 #else
77 fd = mkstemp(tmpname);
78 #endif
79
80 if (fd < 0) {
81 return fd;
82 }
83
84 #ifndef HAVE_MKOSTEMP
85 fd = set_cloexec_or_close(fd);
86 #endif
87
88 unlink(tmpname);
89 return fd;
90 }
91 #endif
92
93 /*
94 * Create a new, unique, anonymous file of the given size, and
95 * return the file descriptor for it. The file descriptor is set
96 * CLOEXEC. The file is immediately suitable for mmap()'ing
97 * the given size at offset zero.
98 *
99 * An optional name for debugging can be provided as the second argument.
100 *
101 * The file should not have a permanent backing store like a disk,
102 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
103 *
104 * If memfd or SHM_ANON is supported, the filesystem is not touched at all.
105 * Otherwise, the file name is deleted from the file system.
106 *
107 * The file is suitable for buffer sharing between processes by
108 * transmitting the file descriptor over Unix sockets using the
109 * SCM_RIGHTS methods.
110 */
111 int
112 os_create_anonymous_file(off_t size, char *debug_name)
113 {
114 int fd, ret;
115 #ifdef __FreeBSD__
116 (void*)debug_name;
117 fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600);
118 #elif defined(HAVE_MEMFD_CREATE)
119 if (!debug_name)
120 debug_name = "mesa-shared";
121 fd = syscall(SYS_memfd_create, debug_name, MFD_CLOEXEC);
122 #else
123 const char *path;
124 char *name;
125
126 path = getenv("XDG_RUNTIME_DIR");
127 if (!path) {
128 errno = ENOENT;
129 return -1;
130 }
131
132 if (debug_name)
133 asprintf(&name, "%s/mesa-shared-%s-XXXXXX", path, debug_name);
134 else
135 asprintf(&name, "%s/mesa-shared-XXXXXX", path);
136 if (!name)
137 return -1;
138
139 fd = create_tmpfile_cloexec(name);
140
141 free(name);
142 #endif
143
144 if (fd < 0)
145 return -1;
146
147 ret = ftruncate(fd, size);
148 if (ret < 0) {
149 close(fd);
150 return -1;
151 }
152
153 return fd;
154 }
155 #endif