diff options
author | Greg V <[email protected]> | 2018-01-18 23:29:14 +0300 |
---|---|---|
committer | Eric Engestrom <[email protected]> | 2019-08-07 22:57:55 +0000 |
commit | c0376a123418df0050dc45d3e1e84f6b29a6a1f3 (patch) | |
tree | cf94494957e38609c5fbd464e53f10652dbf9909 /src/util | |
parent | 519bebdb40d9df5926e8b16dedd36b8e0f356f60 (diff) |
util: add anon_file.h for all memfd/temp file usage
Move the Weston os_create_anonymous_file code from egl/wayland into util,
add support for Linux memfd and FreeBSD SHM_ANON,
use that code in anv/aubinator instead of explicit memfd calls for portability.
Acked-by: Lionel Landwerlin <[email protected]>
Reviewed-by: Eric Engestrom <[email protected]>
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Makefile.sources | 2 | ||||
-rw-r--r-- | src/util/anon_file.c | 155 | ||||
-rw-r--r-- | src/util/anon_file.h | 34 | ||||
-rw-r--r-- | src/util/meson.build | 2 |
4 files changed, 193 insertions, 0 deletions
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources index cbbf5126875..fdb9b6dd135 100644 --- a/src/util/Makefile.sources +++ b/src/util/Makefile.sources @@ -1,4 +1,6 @@ MESA_UTIL_FILES := \ + anon_file.h \ + anon_file.c \ bigmath.h \ bitscan.c \ bitscan.h \ diff --git a/src/util/anon_file.c b/src/util/anon_file.c new file mode 100644 index 00000000000..184b8445bad --- /dev/null +++ b/src/util/anon_file.c @@ -0,0 +1,155 @@ +/* + * Copyright © 2012 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * Based on weston shared/os-compatibility.c + */ + +#ifndef WIN32 +#include "anon_file.h" + +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> + +#ifdef __FreeBSD__ +#include <sys/mman.h> +#elif defined(HAVE_MEMFD_CREATE) +#include <sys/syscall.h> +#include <linux/memfd.h> +#endif + +#if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(HAVE_MKOSTEMP)) +static int +set_cloexec_or_close(int fd) +{ + long flags; + + if (fd == -1) + return -1; + + flags = fcntl(fd, F_GETFD); + if (flags == -1) + goto err; + + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) + goto err; + + return fd; + +err: + close(fd); + return -1; +} +#endif + +#if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE)) +static int +create_tmpfile_cloexec(char *tmpname) +{ + int fd; + +#ifdef HAVE_MKOSTEMP + fd = mkostemp(tmpname, O_CLOEXEC); +#else + fd = mkstemp(tmpname); +#endif + + if (fd < 0) { + return fd; + } + +#ifndef HAVE_MKOSTEMP + fd = set_cloexec_or_close(fd); +#endif + + unlink(tmpname); + return fd; +} +#endif + +/* + * Create a new, unique, anonymous file of the given size, and + * return the file descriptor for it. The file descriptor is set + * CLOEXEC. The file is immediately suitable for mmap()'ing + * the given size at offset zero. + * + * An optional name for debugging can be provided as the second argument. + * + * The file should not have a permanent backing store like a disk, + * but may have if XDG_RUNTIME_DIR is not properly implemented in OS. + * + * If memfd or SHM_ANON is supported, the filesystem is not touched at all. + * Otherwise, the file name is deleted from the file system. + * + * The file is suitable for buffer sharing between processes by + * transmitting the file descriptor over Unix sockets using the + * SCM_RIGHTS methods. + */ +int +os_create_anonymous_file(off_t size, char *debug_name) +{ + int fd, ret; +#ifdef __FreeBSD__ + (void*)debug_name; + fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600); +#elif defined(HAVE_MEMFD_CREATE) + if (!debug_name) + debug_name = "mesa-shared"; + fd = syscall(SYS_memfd_create, debug_name, MFD_CLOEXEC); +#else + const char *path; + char *name; + + path = getenv("XDG_RUNTIME_DIR"); + if (!path) { + errno = ENOENT; + return -1; + } + + if (debug_name) + asprintf(&name, "%s/mesa-shared-%s-XXXXXX", path, debug_name); + else + asprintf(&name, "%s/mesa-shared-XXXXXX", path); + if (!name) + return -1; + + fd = create_tmpfile_cloexec(name); + + free(name); +#endif + + if (fd < 0) + return -1; + + ret = ftruncate(fd, size); + if (ret < 0) { + close(fd); + return -1; + } + + return fd; +} +#endif diff --git a/src/util/anon_file.h b/src/util/anon_file.h new file mode 100644 index 00000000000..8bec8d5458b --- /dev/null +++ b/src/util/anon_file.h @@ -0,0 +1,34 @@ +/* + * Copyright © 2012 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _ANON_FILE_H_ +#define _ANON_FILE_H_ + +#include <sys/types.h> +#include "util/macros.h" + +int os_create_anonymous_file(off_t size, char *debug_name); + +#endif diff --git a/src/util/meson.build b/src/util/meson.build index 95aff3b442f..cf1616e7bc6 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -23,6 +23,8 @@ inc_util = include_directories('.') subdir('xmlpool') files_mesa_util = files( + 'anon_file.h', + 'anon_file.c', 'bigmath.h', 'bitscan.c', 'bitscan.h', |