Rewrite shm buffer file allocation v2
We had duplicated code in many places, using hardcoded paths for temporary files into more than one path. Some cases did not bother with O_CLOEXEC, and all hardcoded paths that might not exist. Add an OS helper function for creating a unique anonymous file with close-on-exec semantics. The helper uses $XDG_RUNTIME_DIR as the directory for a file. This patch unifies the buffer file creation in both Weston and the clients. As simple clients are better not linking to libshared, as it would require e.g. Cairo, they pull the OS compatibility code directly. Android does not have mkostemp(), so a configure test is added for it, and a fallback used if it is not available. Changes in v2: remove all the alternate possible directory definitions and use XDG_RUNTIME_DIR only, and fail is it is not set. Signed-off-by: Pekka Paalanen <ppaalanen@gmail.com>
This commit is contained in:
committed by
Kristian Høgsberg
parent
b715ceca33
commit
1da1b8f3f1
+6
-2
@@ -27,11 +27,15 @@ simple_egl_SOURCES = simple-egl.c
|
||||
simple_egl_CPPFLAGS = $(SIMPLE_CLIENT_CFLAGS)
|
||||
simple_egl_LDADD = $(SIMPLE_CLIENT_LIBS) -lm
|
||||
|
||||
simple_shm_SOURCES = simple-shm.c
|
||||
simple_shm_SOURCES = simple-shm.c \
|
||||
../shared/os-compatibility.c \
|
||||
../shared/os-compatibility.h
|
||||
simple_shm_CPPFLAGS = $(SIMPLE_CLIENT_CFLAGS)
|
||||
simple_shm_LDADD = $(SIMPLE_CLIENT_LIBS)
|
||||
|
||||
simple_touch_SOURCES = simple-touch.c
|
||||
simple_touch_SOURCES = simple-touch.c \
|
||||
../shared/os-compatibility.c \
|
||||
../shared/os-compatibility.h
|
||||
simple_touch_CPPFLAGS = $(SIMPLE_CLIENT_CFLAGS)
|
||||
simple_touch_LDADD = $(SIMPLE_CLIENT_LIBS)
|
||||
endif
|
||||
|
||||
+6
-11
@@ -33,6 +33,7 @@
|
||||
|
||||
#include <wayland-client.h>
|
||||
#include "screenshooter-client-protocol.h"
|
||||
#include "../shared/os-compatibility.h"
|
||||
|
||||
/* The screenshooter is a good example of a custom object exposed by
|
||||
* the compositor and serves as a test bed for implementing client
|
||||
@@ -127,28 +128,22 @@ handle_global(struct wl_display *display, uint32_t id,
|
||||
static struct wl_buffer *
|
||||
create_shm_buffer(int width, int height, void **data_out)
|
||||
{
|
||||
char filename[] = "/tmp/wayland-shm-XXXXXX";
|
||||
struct wl_shm_pool *pool;
|
||||
struct wl_buffer *buffer;
|
||||
int fd, size, stride;
|
||||
void *data;
|
||||
|
||||
fd = mkstemp(filename);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "open %s failed: %m\n", filename);
|
||||
return NULL;
|
||||
}
|
||||
stride = width * 4;
|
||||
size = stride * height;
|
||||
if (ftruncate(fd, size) < 0) {
|
||||
fprintf(stderr, "ftruncate failed: %m\n");
|
||||
close(fd);
|
||||
|
||||
fd = os_create_anonymous_file(size);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
|
||||
size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
unlink(filename);
|
||||
|
||||
if (data == MAP_FAILED) {
|
||||
fprintf(stderr, "mmap failed: %m\n");
|
||||
close(fd);
|
||||
|
||||
+6
-11
@@ -31,6 +31,7 @@
|
||||
#include <signal.h>
|
||||
|
||||
#include <wayland-client.h>
|
||||
#include "../shared/os-compatibility.h"
|
||||
|
||||
struct display {
|
||||
struct wl_display *display;
|
||||
@@ -55,28 +56,22 @@ static struct wl_buffer *
|
||||
create_shm_buffer(struct display *display,
|
||||
int width, int height, uint32_t format, void **data_out)
|
||||
{
|
||||
char filename[] = "/tmp/wayland-shm-XXXXXX";
|
||||
struct wl_shm_pool *pool;
|
||||
struct wl_buffer *buffer;
|
||||
int fd, size, stride;
|
||||
void *data;
|
||||
|
||||
fd = mkstemp(filename);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "open %s failed: %m\n", filename);
|
||||
return NULL;
|
||||
}
|
||||
stride = width * 4;
|
||||
size = stride * height;
|
||||
if (ftruncate(fd, size) < 0) {
|
||||
fprintf(stderr, "ftruncate failed: %m\n");
|
||||
close(fd);
|
||||
|
||||
fd = os_create_anonymous_file(size);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
|
||||
size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
unlink(filename);
|
||||
|
||||
if (data == MAP_FAILED) {
|
||||
fprintf(stderr, "mmap failed: %m\n");
|
||||
close(fd);
|
||||
|
||||
+6
-11
@@ -32,6 +32,7 @@
|
||||
#include <GLES2/gl2.h>
|
||||
#include <wayland-client.h>
|
||||
#include <wayland-egl.h>
|
||||
#include "../shared/os-compatibility.h"
|
||||
|
||||
struct touch {
|
||||
struct wl_display *display;
|
||||
@@ -55,26 +56,20 @@ static void
|
||||
create_shm_buffer(struct touch *touch)
|
||||
{
|
||||
struct wl_shm_pool *pool;
|
||||
char filename[] = "/tmp/wayland-shm-XXXXXX";
|
||||
int fd, size, stride;
|
||||
|
||||
fd = mkstemp(filename);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "open %s failed: %m\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
stride = touch->width * 4;
|
||||
size = stride * touch->height;
|
||||
if (ftruncate(fd, size) < 0) {
|
||||
fprintf(stderr, "ftruncate failed: %m\n");
|
||||
close(fd);
|
||||
|
||||
fd = os_create_anonymous_file(size);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
|
||||
size);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
touch->data =
|
||||
mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
unlink(filename);
|
||||
|
||||
if (touch->data == MAP_FAILED) {
|
||||
fprintf(stderr, "mmap failed: %m\n");
|
||||
close(fd);
|
||||
|
||||
+3
-10
@@ -424,24 +424,17 @@ shm_surface_data_destroy(void *p)
|
||||
static struct wl_shm_pool *
|
||||
make_shm_pool(struct display *display, int size, void **data)
|
||||
{
|
||||
char filename[] = "/tmp/wayland-shm-XXXXXX";
|
||||
struct wl_shm_pool *pool;
|
||||
int fd;
|
||||
|
||||
fd = mkstemp(filename);
|
||||
fd = os_create_anonymous_file(size);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "open %s failed: %m\n", filename);
|
||||
return NULL;
|
||||
}
|
||||
if (ftruncate(fd, size) < 0) {
|
||||
fprintf(stderr, "ftruncate failed: %m\n");
|
||||
close(fd);
|
||||
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
|
||||
size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
unlink(filename);
|
||||
|
||||
if (*data == MAP_FAILED) {
|
||||
fprintf(stderr, "mmap failed: %m\n");
|
||||
close(fd);
|
||||
|
||||
Reference in New Issue
Block a user