Skip to content

Commit

Permalink
Use shm_open instead of XDG_RUNTIME_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Nov 14, 2018
1 parent ecc483a commit ce19788
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ add_project_arguments('-Wno-missing-braces', language: 'c')

mako_inc = include_directories('include')

cc = meson.get_compiler('c')

cairo = dependency('cairo')
pango = dependency('pango')
pangocairo = dependency('pangocairo')
realtime = cc.find_library('rt')
wayland_client = dependency('wayland-client')
wayland_protos = dependency('wayland-protocols', version: '>=1.14')

Expand Down Expand Up @@ -54,6 +57,7 @@ executable(
logind,
pango,
pangocairo,
realtime,
wayland_client,
],
include_directories: [mako_inc],
Expand Down
72 changes: 31 additions & 41 deletions pool-buffer.c
Original file line number Diff line number Diff line change
@@ -1,53 +1,47 @@
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 200112L
#include <cairo/cairo.h>
#include <errno.h>
#include <fcntl.h>
#include <pango/pangocairo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <wayland-client.h>

#include "pool-buffer.h"

static bool set_cloexec(int fd) {
long flags = fcntl(fd, F_GETFD);
if (flags == -1) {
return false;
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;
}
}

if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
return false;
}
static int anonymous_shm_open(void) {
char name[] = "/grim-XXXXXX";
int retries = 100;

return true;
}
do {
randname(name + strlen(name) - 6);

static int create_pool_file(size_t size, char **name) {
static const char template[] = "mako-XXXXXX";
const char *path = getenv("XDG_RUNTIME_DIR");
if (path == NULL) {
fprintf(stderr, "XDG_RUNTIME_DIR is not set\n");
return -1;
}
--retries;
// shm_open guarantees that O_CLOEXEC is set
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);

size_t name_size = strlen(template) + 1 + strlen(path) + 1;
*name = malloc(name_size);
if (*name == NULL) {
fprintf(stderr, "allocation failed\n");
return -1;
}
snprintf(*name, name_size, "%s/%s", path, template);
return -1;
}

int fd = mkstemp(*name);
static int create_shm_file(off_t size) {
int fd = anonymous_shm_open();
if (fd < 0) {
return -1;
}

if (!set_cloexec(fd)) {
close(fd);
return -1;
return fd;
}

if (ftruncate(fd, size) < 0) {
Expand Down Expand Up @@ -77,15 +71,14 @@ static struct pool_buffer *create_buffer(struct wl_shm *shm,

void *data = NULL;
if (size > 0) {
char *name;
int fd = create_pool_file(size, &name);
int fd = create_shm_file(size);
if (fd == -1) {
return NULL;
}

data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
free(name);
close(fd);
return NULL;
}

Expand All @@ -96,9 +89,6 @@ static struct pool_buffer *create_buffer(struct wl_shm *shm,
wl_shm_pool_destroy(pool);

close(fd);
fd = -1;
unlink(name);
free(name);
}

buf->data = data;
Expand Down

0 comments on commit ce19788

Please # to comment.