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>
diff --git a/src/compositor.c b/src/compositor.c
index 7cc176c..93eeb24 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -2279,7 +2279,6 @@
weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
{
char *keymap_str;
- char *path;
xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
XKB_MOD_NAME_CTRL);
@@ -2302,46 +2301,30 @@
}
xkb_info->keymap_size = strlen(keymap_str) + 1;
- /* Create a temporary file in /dev/shm to use for mapping the keymap,
- * and then unlink it as soon as we can. */
- path = strdup("/dev/shm/weston-keymap-XXXXXX");
- if (path == NULL) {
- fprintf(stderr, "failed to allocate keymap path\n");
- goto err_keymap_str;
- }
-
- xkb_info->keymap_fd = mkostemp(path, O_CLOEXEC);
+ xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
if (xkb_info->keymap_fd < 0) {
- fprintf(stderr, "failed to create temporary keymap file\n");
- goto err_path;
- }
- unlink(path);
-
- if (ftruncate(xkb_info->keymap_fd, xkb_info->keymap_size) != 0) {
- fprintf(stderr, "failed to enlarage temporary keymap file\n");
- goto err_path;
+ fprintf(stderr,
+ "creating a keymap file for %lu bytes failed: %m\n",
+ (unsigned long) xkb_info->keymap_size);
+ goto err_keymap_str;
}
xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
PROT_READ | PROT_WRITE,
MAP_SHARED, xkb_info->keymap_fd, 0);
if (xkb_info->keymap_area == MAP_FAILED) {
- fprintf(stderr, "failed to mmap() %lu bytes on %s\n",
- (unsigned long) xkb_info->keymap_size,
- path);
+ fprintf(stderr, "failed to mmap() %lu bytes\n",
+ (unsigned long) xkb_info->keymap_size);
goto err_dev_zero;
}
strcpy(xkb_info->keymap_area, keymap_str);
free(keymap_str);
- free(path);
return;
err_dev_zero:
close(xkb_info->keymap_fd);
xkb_info->keymap_fd = -1;
-err_path:
- free(path);
err_keymap_str:
free(keymap_str);
exit(EXIT_FAILURE);