aboutsummaryrefslogtreecommitdiff
path: root/util/shm.c
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-10-28 23:05:21 +0100
committerGitHub <noreply@github.com>2018-10-28 23:05:21 +0100
commit659d39baaaa51e27c3e078390e3f1b588f390aa3 (patch)
treeffd5bc8bef3055653400f48837b2017bbaecb762 /util/shm.c
parentf668e49faeeb06bf71e8b0d21f745e0f68aab4ba (diff)
parent54e78e9e26d267915517f4e49e9868b3b3009c42 (diff)
Merge pull request #1332 from emersion/shm-open
util: use shm_open for in-memory files
Diffstat (limited to 'util/shm.c')
-rw-r--r--util/shm.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/util/shm.c b/util/shm.c
new file mode 100644
index 00000000..3783e473
--- /dev/null
+++ b/util/shm.c
@@ -0,0 +1,67 @@
+#define _POSIX_C_SOURCE 200112L
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <time.h>
+#include <unistd.h>
+#include <wlr/config.h>
+#include "util/shm.h"
+
+static void randname(char *buf) {
+ struct timespec ts;
+ clock_gettime(CLOCK_REALTIME, &ts);
+ long r = ts.tv_nsec;
+ for (int i = 0; i < 6; ++i) {
+ buf[i] = 'A'+(r&15)+(r&16)*2;
+ r >>= 5;
+ }
+}
+
+int create_shm_file(void) {
+ int retries = 100;
+ do {
+ char name[] = "/wlroots-XXXXXX";
+ randname(name + strlen(name) - 6);
+
+ --retries;
+ // CLOEXEC is guaranteed to be set by shm_open
+ int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
+ if (fd >= 0) {
+ shm_unlink(name);
+ return fd;
+ }
+ } while (retries > 0 && errno == EEXIST);
+
+ return -1;
+}
+
+int allocate_shm_file(size_t size) {
+ int fd = create_shm_file();
+ if (fd < 0) {
+ return -1;
+ }
+
+#ifdef WLR_HAS_POSIX_FALLOCATE
+ int ret;
+ do {
+ ret = posix_fallocate(fd, 0, size);
+ } while (ret == EINTR);
+ if (ret != 0) {
+ close(fd);
+ errno = ret;
+ return -1;
+ }
+#else
+ int ret;
+ do {
+ ret = ftruncate(fd, size);
+ } while (ret < 0 && errno == EINTR);
+ if (ret < 0) {
+ close(fd);
+ return -1;
+ }
+#endif
+
+ return fd;
+}