/*1* Copyright © 2012 Collabora, Ltd.2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice (including the12* next paragraph) shall be included in all copies or substantial13* portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,16* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF17* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS19* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN20* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN21* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.23*/2425/*26* Based on weston shared/os-compatibility.c27*/2829#ifndef _WIN3230#include "anon_file.h"3132#include <unistd.h>33#include <fcntl.h>34#include <errno.h>35#include <stdlib.h>3637#if defined(__FreeBSD__) || defined(__OpenBSD__)38#include <sys/mman.h>39#elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID)40#include <sys/syscall.h>41#include <linux/memfd.h>42#else43#include <stdio.h>44#endif4546#if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(HAVE_MKOSTEMP) || defined(ANDROID))47static int48set_cloexec_or_close(int fd)49{50long flags;5152if (fd == -1)53return -1;5455flags = fcntl(fd, F_GETFD);56if (flags == -1)57goto err;5859if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)60goto err;6162return fd;6364err:65close(fd);66return -1;67}68#endif6970#if !(defined(__FreeBSD__) || defined(HAVE_MEMFD_CREATE) || defined(ANDROID))71static int72create_tmpfile_cloexec(char *tmpname)73{74int fd;7576#ifdef HAVE_MKOSTEMP77fd = mkostemp(tmpname, O_CLOEXEC);78#else79fd = mkstemp(tmpname);80#endif8182if (fd < 0) {83return fd;84}8586#ifndef HAVE_MKOSTEMP87fd = set_cloexec_or_close(fd);88#endif8990unlink(tmpname);91return fd;92}93#endif9495/*96* Create a new, unique, anonymous file of the given size, and97* return the file descriptor for it. The file descriptor is set98* CLOEXEC. The file is immediately suitable for mmap()'ing99* the given size at offset zero.100*101* An optional name for debugging can be provided as the second argument.102*103* The file should not have a permanent backing store like a disk,104* but may have if XDG_RUNTIME_DIR is not properly implemented in OS.105*106* If memfd or SHM_ANON is supported, the filesystem is not touched at all.107* Otherwise, the file name is deleted from the file system.108*109* The file is suitable for buffer sharing between processes by110* transmitting the file descriptor over Unix sockets using the111* SCM_RIGHTS methods.112*/113int114os_create_anonymous_file(off_t size, const char *debug_name)115{116int fd, ret;117#ifdef __FreeBSD__118fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600);119#elif defined(__OpenBSD__)120char template[] = "/tmp/mesa-XXXXXXXXXX";121fd = shm_mkstemp(template);122if (fd != -1)123shm_unlink(template);124#elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID)125if (!debug_name)126debug_name = "mesa-shared";127fd = syscall(SYS_memfd_create, debug_name, MFD_CLOEXEC);128#else129const char *path;130char *name;131132path = getenv("XDG_RUNTIME_DIR");133if (!path) {134errno = ENOENT;135return -1;136}137138if (debug_name)139asprintf(&name, "%s/mesa-shared-%s-XXXXXX", path, debug_name);140else141asprintf(&name, "%s/mesa-shared-XXXXXX", path);142if (!name)143return -1;144145fd = create_tmpfile_cloexec(name);146147free(name);148#endif149150if (fd < 0)151return -1;152153ret = ftruncate(fd, size);154if (ret < 0) {155close(fd);156return -1;157}158159return fd;160}161#endif162163164