| 1 |
/* |
| 2 |
* Copyright © 2013 Keith Packard |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 5 |
* documentation for any purpose is hereby granted without fee, provided that |
| 6 |
* the above copyright notice appear in all copies and that both that copyright |
| 7 |
* notice and this permission notice appear in supporting documentation, and |
| 8 |
* that the name of the copyright holders not be used in advertising or |
| 9 |
* publicity pertaining to distribution of the software without specific, |
| 10 |
* written prior permission. The copyright holders make no representations |
| 11 |
* about the suitability of this software for any purpose. It is provided "as |
| 12 |
* is" without express or implied warranty. |
| 13 |
* |
| 14 |
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 |
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 |
* OF THIS SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#if HAVE_CONFIG_H |
| 24 |
#include "config.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "xshmfenceint.h" |
| 28 |
|
| 29 |
#include <fcntl.h> |
| 30 |
|
| 31 |
#if !HAVE_MEMFD_CREATE |
| 32 |
#if HAVE_DECL___NR_MEMFD_CREATE |
| 33 |
#include <asm/unistd.h> |
| 34 |
static int memfd_create(const char *name, |
| 35 |
unsigned int flags) |
| 36 |
{ |
| 37 |
return syscall(__NR_memfd_create, name, flags); |
| 38 |
} |
| 39 |
#define HAVE_MEMFD_CREATE 1 |
| 40 |
#endif |
| 41 |
#endif |
| 42 |
|
| 43 |
#if HAVE_MEMFD_CREATE |
| 44 |
|
| 45 |
/* Get defines for the memfd_create syscall, using the |
| 46 |
* header if available, or just defining the constants otherwise |
| 47 |
*/ |
| 48 |
|
| 49 |
#if HAVE_MEMFD_H |
| 50 |
#include <sys/memfd.h> |
| 51 |
#else |
| 52 |
/* flags for memfd_create(2) (unsigned int) */ |
| 53 |
#define MFD_CLOEXEC 0x0001U |
| 54 |
#define MFD_ALLOW_SEALING 0x0002U |
| 55 |
#endif |
| 56 |
|
| 57 |
#endif |
| 58 |
|
| 59 |
/** |
| 60 |
* xshmfence_alloc_shm: |
| 61 |
* |
| 62 |
* Allocates a shared memory object large enough to hold a single |
| 63 |
* fence. |
| 64 |
* |
| 65 |
* Return value: the file descriptor of the object, or -1 on failure |
| 66 |
* (in which case, errno will be set as appropriate). |
| 67 |
**/ |
| 68 |
int |
| 69 |
xshmfence_alloc_shm(void) |
| 70 |
{ |
| 71 |
char template[] = SHMDIR "/shmfd-XXXXXX"; |
| 72 |
int fd = -1; |
| 73 |
#ifndef HAVE_MKOSTEMP |
| 74 |
int flags; |
| 75 |
#endif |
| 76 |
|
| 77 |
#if HAVE_MEMFD_CREATE |
| 78 |
static int disable_memfd = -1; |
| 79 |
|
| 80 |
if (disable_memfd == -1) { |
| 81 |
const char *val = getenv("XSHMFENCE_NO_MEMFD"); |
| 82 |
disable_memfd = val ? !!atoi(val) : 0; |
| 83 |
} |
| 84 |
|
| 85 |
if (disable_memfd <= 0) |
| 86 |
fd = memfd_create("xshmfence", MFD_CLOEXEC|MFD_ALLOW_SEALING); |
| 87 |
|
| 88 |
if (fd < 0) |
| 89 |
#endif |
| 90 |
#ifdef SHM_ANON |
| 91 |
fd = shm_open(SHM_ANON, O_RDWR|O_CLOEXEC, 0600); |
| 92 |
if (fd < 0) |
| 93 |
#endif |
| 94 |
{ |
| 95 |
#ifdef O_TMPFILE |
| 96 |
fd = open(SHMDIR, O_TMPFILE|O_RDWR|O_CLOEXEC|O_EXCL, 0666); |
| 97 |
if (fd < 0) |
| 98 |
#endif |
| 99 |
{ |
| 100 |
#ifdef HAVE_MKOSTEMP |
| 101 |
fd = mkostemp(template, O_CLOEXEC); |
| 102 |
#else |
| 103 |
fd = mkstemp(template); |
| 104 |
#endif |
| 105 |
if (fd < 0) |
| 106 |
return fd; |
| 107 |
unlink(template); |
| 108 |
#ifndef HAVE_MKOSTEMP |
| 109 |
flags = fcntl(fd, F_GETFD); |
| 110 |
if (flags != -1) { |
| 111 |
flags |= FD_CLOEXEC; |
| 112 |
fcntl(fd, F_SETFD, &flags); |
| 113 |
} |
| 114 |
#endif |
| 115 |
} |
| 116 |
} |
| 117 |
if (ftruncate(fd, sizeof (struct xshmfence)) < 0) { |
| 118 |
close(fd); |
| 119 |
return -1; |
| 120 |
} |
| 121 |
xshmfence_init(fd); |
| 122 |
return fd; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* xshmfence_map_shm: |
| 127 |
* |
| 128 |
* Map a shared memory fence referenced by @fd. |
| 129 |
* |
| 130 |
* Return value: the fence or NULL (in which case, errno will be set |
| 131 |
* as appropriate). |
| 132 |
**/ |
| 133 |
struct xshmfence * |
| 134 |
xshmfence_map_shm(int fd) |
| 135 |
{ |
| 136 |
struct xshmfence *addr; |
| 137 |
addr = mmap (NULL, sizeof (struct xshmfence) , PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 138 |
if (addr == MAP_FAILED) { |
| 139 |
close (fd); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
return addr; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* xshmfence_unmap_shm: |
| 147 |
* |
| 148 |
* Unap a shared memory fence @f. |
| 149 |
**/ |
| 150 |
void |
| 151 |
xshmfence_unmap_shm(struct xshmfence *f) |
| 152 |
{ |
| 153 |
munmap(f, sizeof (struct xshmfence)); |
| 154 |
} |