2011-03-08 11:32:24 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
|
*
|
2015-06-10 10:54:15 -07:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
2011-03-08 11:32:24 +01:00
|
|
|
*
|
2015-06-10 10:54:15 -07:00
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
|
* portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2011-03-08 11:32:24 +01:00
|
|
|
*
|
|
|
|
|
* Authors:
|
|
|
|
|
* Kristian Høgsberg <krh@bitplanet.net>
|
|
|
|
|
* Benjamin Franzke <benjaminfranzke@googlemail.com>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2012-05-23 16:09:55 +03:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
2014-10-03 14:39:59 -05:00
|
|
|
#include <stdbool.h>
|
2011-03-08 11:32:24 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <unistd.h>
|
2013-11-13 15:32:05 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <pthread.h>
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2012-10-19 23:06:53 -04:00
|
|
|
#include "wayland-private.h"
|
2011-03-08 11:32:24 +01:00
|
|
|
#include "wayland-server.h"
|
|
|
|
|
|
2013-11-13 15:32:05 +00:00
|
|
|
/* This once_t is used to synchronize installing the SIGBUS handler
|
|
|
|
|
* and creating the TLS key. This will be done in the first call
|
|
|
|
|
* wl_shm_buffer_begin_access which can happen from any thread */
|
|
|
|
|
static pthread_once_t wl_shm_sigbus_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
static pthread_key_t wl_shm_sigbus_data_key;
|
|
|
|
|
static struct sigaction wl_shm_old_sigbus_action;
|
|
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
struct wl_shm_pool {
|
2013-06-01 17:40:54 -05:00
|
|
|
struct wl_resource *resource;
|
2012-04-03 12:08:50 -04:00
|
|
|
int refcount;
|
|
|
|
|
char *data;
|
2014-04-07 14:42:20 -07:00
|
|
|
int32_t size;
|
2012-04-03 12:08:50 -04:00
|
|
|
};
|
|
|
|
|
|
2011-03-08 11:32:24 +01:00
|
|
|
struct wl_shm_buffer {
|
2013-06-20 20:36:49 -05:00
|
|
|
struct wl_resource *resource;
|
|
|
|
|
int32_t width, height;
|
2011-03-08 11:32:24 +01:00
|
|
|
int32_t stride;
|
2011-08-30 21:26:19 -04:00
|
|
|
uint32_t format;
|
2012-05-22 15:39:40 +03:00
|
|
|
int offset;
|
2012-04-03 12:08:50 -04:00
|
|
|
struct wl_shm_pool *pool;
|
2011-03-08 11:32:24 +01:00
|
|
|
};
|
|
|
|
|
|
2013-11-13 15:32:05 +00:00
|
|
|
struct wl_shm_sigbus_data {
|
|
|
|
|
struct wl_shm_pool *current_pool;
|
|
|
|
|
int access_count;
|
|
|
|
|
int fallback_mapping_used;
|
|
|
|
|
};
|
|
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
static void
|
|
|
|
|
shm_pool_unref(struct wl_shm_pool *pool)
|
|
|
|
|
{
|
|
|
|
|
pool->refcount--;
|
|
|
|
|
if (pool->refcount)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
munmap(pool->data, pool->size);
|
|
|
|
|
free(pool);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-08 11:32:24 +01:00
|
|
|
static void
|
2011-08-18 17:53:50 -04:00
|
|
|
destroy_buffer(struct wl_resource *resource)
|
2011-03-08 11:32:24 +01:00
|
|
|
{
|
2013-06-01 17:40:54 -05:00
|
|
|
struct wl_shm_buffer *buffer = wl_resource_get_user_data(resource);
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
if (buffer->pool)
|
|
|
|
|
shm_pool_unref(buffer->pool);
|
2011-03-08 11:32:24 +01:00
|
|
|
free(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2011-08-18 17:53:50 -04:00
|
|
|
shm_buffer_destroy(struct wl_client *client, struct wl_resource *resource)
|
2011-03-08 11:32:24 +01:00
|
|
|
{
|
Switch protocol to using serial numbers for ordering events and requests
The wayland protocol, as X, uses timestamps to match up certain
requests with input events. The problem is that sometimes we need to
send out an event that doesn't have a corresponding timestamped input
event. For example, the pointer focus surface goes away and new
surface needs to receive a pointer enter event. These events are
normally timestamped with the evdev event timestamp, but in this case,
we don't have a evdev timestamp. So we have to go to gettimeofday (or
clock_gettime()) and then we don't know if it's coming from the same
time source etc.
However for all these cases we don't need a real time timestamp, we
just need a serial number that encodes the order of events inside the
server. So we introduce a serial number mechanism that we can use to
order events. We still need real-time timestamps for actual input
device events (motion, buttons, keys, touch), to be able to reason
about double-click speed and movement speed so events that correspond to user input carry both a serial number and a timestamp.
The serial number also give us a mechanism to key together events that
are "logically the same" such as a unicode event and a keycode event,
or a motion event and a relative event from a raw device.
2012-04-11 22:25:51 -04:00
|
|
|
wl_resource_destroy(resource);
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
2012-03-22 11:33:35 +02:00
|
|
|
static const struct wl_buffer_interface shm_buffer_interface = {
|
2011-03-08 11:32:24 +01:00
|
|
|
shm_buffer_destroy
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-03 14:39:59 -05:00
|
|
|
static bool
|
2013-08-06 20:05:53 +02:00
|
|
|
format_is_supported(struct wl_client *client, uint32_t format)
|
|
|
|
|
{
|
|
|
|
|
struct wl_display *display = wl_client_get_display(client);
|
|
|
|
|
struct wl_array *formats;
|
|
|
|
|
uint32_t *p;
|
|
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
|
case WL_SHM_FORMAT_ARGB8888:
|
|
|
|
|
case WL_SHM_FORMAT_XRGB8888:
|
2014-10-03 14:39:59 -05:00
|
|
|
return true;
|
2013-08-06 20:05:53 +02:00
|
|
|
default:
|
|
|
|
|
formats = wl_display_get_additional_shm_formats(display);
|
|
|
|
|
wl_array_for_each(p, formats)
|
|
|
|
|
if(*p == format)
|
2014-10-03 14:39:59 -05:00
|
|
|
return true;
|
2013-08-06 20:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-03 14:39:59 -05:00
|
|
|
return false;
|
2013-08-06 20:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
static void
|
|
|
|
|
shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
|
|
|
|
|
uint32_t id, int32_t offset,
|
|
|
|
|
int32_t width, int32_t height,
|
|
|
|
|
int32_t stride, uint32_t format)
|
2011-03-08 11:32:24 +01:00
|
|
|
{
|
2013-06-01 17:40:54 -05:00
|
|
|
struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
|
2011-03-08 11:32:24 +01:00
|
|
|
struct wl_shm_buffer *buffer;
|
|
|
|
|
|
2013-08-06 20:05:53 +02:00
|
|
|
if (!format_is_supported(client, format)) {
|
2012-04-03 12:08:50 -04:00
|
|
|
wl_resource_post_error(resource,
|
|
|
|
|
WL_SHM_ERROR_INVALID_FORMAT,
|
2013-08-06 20:05:53 +02:00
|
|
|
"invalid format 0x%x", format);
|
2012-04-03 12:08:50 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (offset < 0 || width <= 0 || height <= 0 || stride < width ||
|
|
|
|
|
INT32_MAX / stride <= height ||
|
|
|
|
|
offset > pool->size - stride * height) {
|
|
|
|
|
wl_resource_post_error(resource,
|
|
|
|
|
WL_SHM_ERROR_INVALID_STRIDE,
|
|
|
|
|
"invalid width, height or stride (%dx%d, %u)",
|
|
|
|
|
width, height, stride);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer = malloc(sizeof *buffer);
|
|
|
|
|
if (buffer == NULL) {
|
2013-07-02 15:39:03 -04:00
|
|
|
wl_client_post_no_memory(client);
|
2012-04-03 12:08:50 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2013-06-20 20:36:49 -05:00
|
|
|
buffer->width = width;
|
|
|
|
|
buffer->height = height;
|
2011-08-30 21:26:19 -04:00
|
|
|
buffer->format = format;
|
2011-03-08 11:32:24 +01:00
|
|
|
buffer->stride = stride;
|
2012-05-22 15:39:40 +03:00
|
|
|
buffer->offset = offset;
|
2012-04-03 12:08:50 -04:00
|
|
|
buffer->pool = pool;
|
|
|
|
|
pool->refcount++;
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2013-06-27 20:09:20 -05:00
|
|
|
buffer->resource =
|
|
|
|
|
wl_resource_create(client, &wl_buffer_interface, 1, id);
|
2013-07-02 15:39:03 -04:00
|
|
|
if (buffer->resource == NULL) {
|
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
|
shm_pool_unref(pool);
|
|
|
|
|
free(buffer);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 20:09:20 -05:00
|
|
|
wl_resource_set_implementation(buffer->resource,
|
|
|
|
|
&shm_buffer_interface,
|
|
|
|
|
buffer, destroy_buffer);
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2012-04-03 12:08:50 -04:00
|
|
|
destroy_pool(struct wl_resource *resource)
|
2011-03-08 11:32:24 +01:00
|
|
|
{
|
2013-06-01 17:40:54 -05:00
|
|
|
struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
shm_pool_unref(pool);
|
|
|
|
|
}
|
2011-08-30 21:26:19 -04:00
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
static void
|
|
|
|
|
shm_pool_destroy(struct wl_client *client, struct wl_resource *resource)
|
|
|
|
|
{
|
Switch protocol to using serial numbers for ordering events and requests
The wayland protocol, as X, uses timestamps to match up certain
requests with input events. The problem is that sometimes we need to
send out an event that doesn't have a corresponding timestamped input
event. For example, the pointer focus surface goes away and new
surface needs to receive a pointer enter event. These events are
normally timestamped with the evdev event timestamp, but in this case,
we don't have a evdev timestamp. So we have to go to gettimeofday (or
clock_gettime()) and then we don't know if it's coming from the same
time source etc.
However for all these cases we don't need a real time timestamp, we
just need a serial number that encodes the order of events inside the
server. So we introduce a serial number mechanism that we can use to
order events. We still need real-time timestamps for actual input
device events (motion, buttons, keys, touch), to be able to reason
about double-click speed and movement speed so events that correspond to user input carry both a serial number and a timestamp.
The serial number also give us a mechanism to key together events that
are "logically the same" such as a unicode event and a keycode event,
or a motion event and a relative event from a raw device.
2012-04-11 22:25:51 -04:00
|
|
|
wl_resource_destroy(resource);
|
2012-04-03 12:08:50 -04:00
|
|
|
}
|
|
|
|
|
|
2012-05-22 15:39:40 +03:00
|
|
|
static void
|
|
|
|
|
shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
|
|
|
|
|
int32_t size)
|
|
|
|
|
{
|
2013-06-01 17:40:54 -05:00
|
|
|
struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
|
2012-05-22 15:39:40 +03:00
|
|
|
void *data;
|
|
|
|
|
|
2014-04-07 14:42:20 -07:00
|
|
|
if (size < pool->size) {
|
|
|
|
|
wl_resource_post_error(resource,
|
|
|
|
|
WL_SHM_ERROR_INVALID_FD,
|
|
|
|
|
"shrinking pool invalid");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-22 15:39:40 +03:00
|
|
|
|
2014-04-07 14:42:20 -07:00
|
|
|
data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
|
2012-05-22 15:39:40 +03:00
|
|
|
if (data == MAP_FAILED) {
|
|
|
|
|
wl_resource_post_error(resource,
|
|
|
|
|
WL_SHM_ERROR_INVALID_FD,
|
2012-05-23 16:09:55 +03:00
|
|
|
"failed mremap");
|
2012-05-22 15:39:40 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool->data = data;
|
|
|
|
|
pool->size = size;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
struct wl_shm_pool_interface shm_pool_interface = {
|
|
|
|
|
shm_pool_create_buffer,
|
2012-05-22 15:39:40 +03:00
|
|
|
shm_pool_destroy,
|
|
|
|
|
shm_pool_resize
|
2012-04-03 12:08:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
shm_create_pool(struct wl_client *client, struct wl_resource *resource,
|
|
|
|
|
uint32_t id, int fd, int32_t size)
|
|
|
|
|
{
|
|
|
|
|
struct wl_shm_pool *pool;
|
|
|
|
|
|
|
|
|
|
pool = malloc(sizeof *pool);
|
|
|
|
|
if (pool == NULL) {
|
2013-07-02 15:39:03 -04:00
|
|
|
wl_client_post_no_memory(client);
|
2012-07-09 11:35:57 +02:00
|
|
|
goto err_close;
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
if (size <= 0) {
|
2011-09-01 09:53:33 -04:00
|
|
|
wl_resource_post_error(resource,
|
|
|
|
|
WL_SHM_ERROR_INVALID_STRIDE,
|
2012-04-03 12:08:50 -04:00
|
|
|
"invalid size (%d)", size);
|
2012-07-09 11:35:57 +02:00
|
|
|
goto err_free;
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
2012-04-03 12:08:50 -04:00
|
|
|
pool->refcount = 1;
|
|
|
|
|
pool->size = size;
|
|
|
|
|
pool->data = mmap(NULL, size,
|
|
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
|
if (pool->data == MAP_FAILED) {
|
2011-09-01 09:53:33 -04:00
|
|
|
wl_resource_post_error(resource,
|
|
|
|
|
WL_SHM_ERROR_INVALID_FD,
|
|
|
|
|
"failed mmap fd %d", fd);
|
2013-11-15 14:17:56 +01:00
|
|
|
goto err_close;
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
2012-07-09 11:35:57 +02:00
|
|
|
close(fd);
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2013-06-27 20:09:20 -05:00
|
|
|
pool->resource =
|
|
|
|
|
wl_resource_create(client, &wl_shm_pool_interface, 1, id);
|
2013-07-02 15:39:03 -04:00
|
|
|
if (!pool->resource) {
|
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
|
munmap(pool->data, pool->size);
|
|
|
|
|
free(pool);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-04-03 12:08:50 -04:00
|
|
|
|
2013-06-27 20:09:20 -05:00
|
|
|
wl_resource_set_implementation(pool->resource,
|
|
|
|
|
&shm_pool_interface,
|
|
|
|
|
pool, destroy_pool);
|
2012-07-09 11:35:57 +02:00
|
|
|
|
2012-07-09 21:50:39 -04:00
|
|
|
return;
|
|
|
|
|
|
2012-07-09 11:35:57 +02:00
|
|
|
err_close:
|
|
|
|
|
close(fd);
|
|
|
|
|
err_free:
|
|
|
|
|
free(pool);
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
2012-03-22 11:33:35 +02:00
|
|
|
static const struct wl_shm_interface shm_interface = {
|
2012-04-03 12:08:50 -04:00
|
|
|
shm_create_pool
|
2011-03-08 11:32:24 +01:00
|
|
|
};
|
|
|
|
|
|
2011-08-19 16:57:48 -04:00
|
|
|
static void
|
|
|
|
|
bind_shm(struct wl_client *client,
|
|
|
|
|
void *data, uint32_t version, uint32_t id)
|
|
|
|
|
{
|
2011-08-30 21:26:19 -04:00
|
|
|
struct wl_resource *resource;
|
2013-08-06 20:05:53 +02:00
|
|
|
struct wl_display *display = wl_client_get_display(client);
|
|
|
|
|
struct wl_array *additional_formats;
|
|
|
|
|
uint32_t *p;
|
2011-08-30 21:26:19 -04:00
|
|
|
|
2013-06-27 20:09:20 -05:00
|
|
|
resource = wl_resource_create(client, &wl_shm_interface, 1, id);
|
2013-07-02 15:39:03 -04:00
|
|
|
if (!resource) {
|
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 20:09:20 -05:00
|
|
|
wl_resource_set_implementation(resource, &shm_interface, data, NULL);
|
2011-08-30 21:26:19 -04:00
|
|
|
|
2012-03-02 17:08:59 +02:00
|
|
|
wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
|
|
|
|
|
wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
|
2013-08-06 20:05:53 +02:00
|
|
|
|
|
|
|
|
additional_formats = wl_display_get_additional_shm_formats(display);
|
|
|
|
|
wl_array_for_each(p, additional_formats)
|
|
|
|
|
wl_shm_send_format(resource, *p);
|
2011-08-19 16:57:48 -04:00
|
|
|
}
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2012-03-26 16:33:24 -04:00
|
|
|
WL_EXPORT int
|
|
|
|
|
wl_display_init_shm(struct wl_display *display)
|
2011-03-08 11:32:24 +01:00
|
|
|
{
|
2013-07-09 19:18:10 -04:00
|
|
|
if (!wl_global_create(display, &wl_shm_interface, 1, NULL, bind_shm))
|
2012-03-26 16:33:24 -04:00
|
|
|
return -1;
|
2011-03-08 11:32:24 +01:00
|
|
|
|
2012-03-26 16:33:24 -04:00
|
|
|
return 0;
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 20:36:48 -05:00
|
|
|
WL_EXPORT struct wl_shm_buffer *
|
|
|
|
|
wl_shm_buffer_get(struct wl_resource *resource)
|
|
|
|
|
{
|
2013-06-27 20:09:18 -05:00
|
|
|
if (resource == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2013-06-20 20:36:48 -05:00
|
|
|
if (wl_resource_instance_of(resource, &wl_buffer_interface,
|
|
|
|
|
&shm_buffer_interface))
|
|
|
|
|
return wl_resource_get_user_data(resource);
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-08 11:32:24 +01:00
|
|
|
WL_EXPORT int32_t
|
2013-06-20 20:36:48 -05:00
|
|
|
wl_shm_buffer_get_stride(struct wl_shm_buffer *buffer)
|
2011-03-08 11:32:24 +01:00
|
|
|
{
|
|
|
|
|
return buffer->stride;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-14 12:28:51 +00:00
|
|
|
|
|
|
|
|
/** Get a pointer to the memory for the SHM buffer
|
|
|
|
|
*
|
|
|
|
|
* \param buffer The buffer object
|
|
|
|
|
*
|
|
|
|
|
* Returns a pointer which can be used to read the data contained in
|
|
|
|
|
* the given SHM buffer.
|
|
|
|
|
*
|
2014-09-10 13:46:09 -05:00
|
|
|
* As this buffer is memory-mapped, reading from it may generate
|
2013-11-14 12:28:51 +00:00
|
|
|
* SIGBUS signals. This can happen if the client claims that the
|
|
|
|
|
* buffer is larger than it is or if something truncates the
|
|
|
|
|
* underlying file. To prevent this signal from causing the compositor
|
|
|
|
|
* to crash you should call wl_shm_buffer_begin_access and
|
|
|
|
|
* wl_shm_buffer_end_access around code that reads from the memory.
|
|
|
|
|
*
|
|
|
|
|
* \memberof wl_shm_buffer
|
|
|
|
|
*/
|
2011-03-08 11:32:24 +01:00
|
|
|
WL_EXPORT void *
|
2013-06-20 20:36:48 -05:00
|
|
|
wl_shm_buffer_get_data(struct wl_shm_buffer *buffer)
|
2011-03-08 11:32:24 +01:00
|
|
|
{
|
2012-08-15 23:03:21 -04:00
|
|
|
if (buffer->pool)
|
|
|
|
|
return buffer->pool->data + buffer->offset;
|
|
|
|
|
else
|
|
|
|
|
return buffer + 1;
|
2011-03-08 11:32:24 +01:00
|
|
|
}
|
2011-08-30 21:26:19 -04:00
|
|
|
|
|
|
|
|
WL_EXPORT uint32_t
|
2013-06-20 20:36:48 -05:00
|
|
|
wl_shm_buffer_get_format(struct wl_shm_buffer *buffer)
|
2011-08-30 21:26:19 -04:00
|
|
|
{
|
|
|
|
|
return buffer->format;
|
|
|
|
|
}
|
2012-07-20 12:04:42 -04:00
|
|
|
|
2012-07-20 12:30:07 -04:00
|
|
|
WL_EXPORT int32_t
|
2013-06-20 20:36:48 -05:00
|
|
|
wl_shm_buffer_get_width(struct wl_shm_buffer *buffer)
|
2012-07-20 12:04:42 -04:00
|
|
|
{
|
2013-06-20 20:36:49 -05:00
|
|
|
return buffer->width;
|
2012-07-20 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
|
2012-07-20 12:30:07 -04:00
|
|
|
WL_EXPORT int32_t
|
2013-06-20 20:36:48 -05:00
|
|
|
wl_shm_buffer_get_height(struct wl_shm_buffer *buffer)
|
2012-07-20 12:04:42 -04:00
|
|
|
{
|
2013-06-20 20:36:49 -05:00
|
|
|
return buffer->height;
|
2012-07-20 12:04:42 -04:00
|
|
|
}
|
2013-11-13 15:32:05 +00:00
|
|
|
|
2015-10-19 20:54:49 -05:00
|
|
|
/** Get a reference to a shm_buffer's shm_pool
|
|
|
|
|
*
|
|
|
|
|
* \param buffer The buffer object
|
|
|
|
|
*
|
|
|
|
|
* Returns a pointer to a buffer's shm_pool and increases the
|
|
|
|
|
* shm_pool refcount.
|
|
|
|
|
*
|
|
|
|
|
* The compositor must remember to call wl_shm_pool_unref when
|
|
|
|
|
* it no longer needs the reference to ensure proper destruction
|
|
|
|
|
* of the pool.
|
|
|
|
|
*
|
|
|
|
|
* \memberof wl_shm_buffer
|
|
|
|
|
* \sa wl_shm_pool_unref
|
|
|
|
|
*/
|
|
|
|
|
WL_EXPORT struct wl_shm_pool *
|
|
|
|
|
wl_shm_buffer_ref_pool(struct wl_shm_buffer *buffer)
|
|
|
|
|
{
|
|
|
|
|
assert(buffer->pool->refcount);
|
|
|
|
|
|
|
|
|
|
buffer->pool->refcount++;
|
|
|
|
|
return buffer->pool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Unreference a shm_pool
|
|
|
|
|
*
|
|
|
|
|
* \param buffer The pool object
|
|
|
|
|
*
|
|
|
|
|
* Drops a reference to a wl_shm_pool object.
|
|
|
|
|
*
|
|
|
|
|
* This is only necessary if the compositor has explicitly
|
|
|
|
|
* taken a reference with wl_shm_buffer_ref_pool(), otherwise
|
|
|
|
|
* the pool will be automatically destroyed when appropriate.
|
|
|
|
|
*
|
|
|
|
|
* \memberof wl_shm_pool
|
|
|
|
|
* \sa wl_shm_buffer_ref_pool
|
|
|
|
|
*/
|
|
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_shm_pool_unref(struct wl_shm_pool *pool)
|
|
|
|
|
{
|
|
|
|
|
shm_pool_unref(pool);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-13 15:32:05 +00:00
|
|
|
static void
|
|
|
|
|
reraise_sigbus(void)
|
|
|
|
|
{
|
|
|
|
|
/* If SIGBUS is raised for some other reason than accessing
|
|
|
|
|
* the pool then we'll uninstall the signal handler so we can
|
|
|
|
|
* reraise it. This would presumably kill the process */
|
|
|
|
|
sigaction(SIGBUS, &wl_shm_old_sigbus_action, NULL);
|
|
|
|
|
raise(SIGBUS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
sigbus_handler(int signum, siginfo_t *info, void *context)
|
|
|
|
|
{
|
|
|
|
|
struct wl_shm_sigbus_data *sigbus_data =
|
|
|
|
|
pthread_getspecific(wl_shm_sigbus_data_key);
|
|
|
|
|
struct wl_shm_pool *pool;
|
|
|
|
|
|
|
|
|
|
if (sigbus_data == NULL) {
|
|
|
|
|
reraise_sigbus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool = sigbus_data->current_pool;
|
|
|
|
|
|
|
|
|
|
/* If the offending address is outside the mapped space for
|
|
|
|
|
* the pool then the error is a real problem so we'll reraise
|
|
|
|
|
* the signal */
|
|
|
|
|
if (pool == NULL ||
|
|
|
|
|
(char *) info->si_addr < pool->data ||
|
|
|
|
|
(char *) info->si_addr >= pool->data + pool->size) {
|
|
|
|
|
reraise_sigbus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sigbus_data->fallback_mapping_used = 1;
|
|
|
|
|
|
|
|
|
|
/* This should replace the previous mapping */
|
|
|
|
|
if (mmap(pool->data, pool->size,
|
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
|
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
|
|
|
|
|
0, 0) == (void *) -1) {
|
|
|
|
|
reraise_sigbus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
destroy_sigbus_data(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct wl_shm_sigbus_data *sigbus_data = data;
|
|
|
|
|
|
|
|
|
|
free(sigbus_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
init_sigbus_data_key(void)
|
|
|
|
|
{
|
|
|
|
|
struct sigaction new_action = {
|
|
|
|
|
.sa_sigaction = sigbus_handler,
|
|
|
|
|
.sa_flags = SA_SIGINFO | SA_NODEFER
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sigemptyset(&new_action.sa_mask);
|
|
|
|
|
|
|
|
|
|
sigaction(SIGBUS, &new_action, &wl_shm_old_sigbus_action);
|
|
|
|
|
|
|
|
|
|
pthread_key_create(&wl_shm_sigbus_data_key, destroy_sigbus_data);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-14 12:28:51 +00:00
|
|
|
/** Mark that the given SHM buffer is about to be accessed
|
|
|
|
|
*
|
|
|
|
|
* \param buffer The SHM buffer
|
|
|
|
|
*
|
|
|
|
|
* An SHM buffer is a memory-mapped file given by the client.
|
|
|
|
|
* According to POSIX, reading from a memory-mapped region that
|
|
|
|
|
* extends off the end of the file will cause a SIGBUS signal to be
|
|
|
|
|
* generated. Normally this would cause the compositor to terminate.
|
|
|
|
|
* In order to make the compositor robust against clients that change
|
|
|
|
|
* the size of the underlying file or lie about its size, you should
|
|
|
|
|
* protect access to the buffer by calling this function before
|
|
|
|
|
* reading from the memory and call wl_shm_buffer_end_access
|
|
|
|
|
* afterwards. This will install a signal handler for SIGBUS which
|
|
|
|
|
* will prevent the compositor from crashing.
|
|
|
|
|
*
|
|
|
|
|
* After calling this function the signal handler will remain
|
|
|
|
|
* installed for the lifetime of the compositor process. Note that
|
|
|
|
|
* this function will not work properly if the compositor is also
|
|
|
|
|
* installing its own handler for SIGBUS.
|
|
|
|
|
*
|
|
|
|
|
* If a SIGBUS signal is received for an address within the range of
|
|
|
|
|
* the SHM pool of the given buffer then the client will be sent an
|
|
|
|
|
* error event when wl_shm_buffer_end_access is called. If the signal
|
|
|
|
|
* is for an address outside that range then the signal handler will
|
|
|
|
|
* reraise the signal which would will likely cause the compositor to
|
|
|
|
|
* terminate.
|
|
|
|
|
*
|
|
|
|
|
* It is safe to nest calls to these functions as long as the nested
|
|
|
|
|
* calls are all accessing the same buffer. The number of calls to
|
|
|
|
|
* wl_shm_buffer_end_access must match the number of calls to
|
|
|
|
|
* wl_shm_buffer_begin_access. These functions are thread-safe and it
|
|
|
|
|
* is allowed to simultaneously access different buffers or the same
|
|
|
|
|
* buffer from multiple threads.
|
|
|
|
|
*
|
|
|
|
|
* \memberof wl_shm_buffer
|
|
|
|
|
*/
|
2013-11-13 15:32:05 +00:00
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_shm_buffer_begin_access(struct wl_shm_buffer *buffer)
|
|
|
|
|
{
|
|
|
|
|
struct wl_shm_pool *pool = buffer->pool;
|
|
|
|
|
struct wl_shm_sigbus_data *sigbus_data;
|
|
|
|
|
|
|
|
|
|
pthread_once(&wl_shm_sigbus_once, init_sigbus_data_key);
|
|
|
|
|
|
|
|
|
|
sigbus_data = pthread_getspecific(wl_shm_sigbus_data_key);
|
|
|
|
|
if (sigbus_data == NULL) {
|
|
|
|
|
sigbus_data = malloc(sizeof *sigbus_data);
|
|
|
|
|
if (sigbus_data == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
memset(sigbus_data, 0, sizeof *sigbus_data);
|
|
|
|
|
|
|
|
|
|
pthread_setspecific(wl_shm_sigbus_data_key, sigbus_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(sigbus_data->current_pool == NULL ||
|
|
|
|
|
sigbus_data->current_pool == pool);
|
|
|
|
|
|
|
|
|
|
sigbus_data->current_pool = pool;
|
|
|
|
|
sigbus_data->access_count++;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-14 12:28:51 +00:00
|
|
|
/** Ends the access to a buffer started by wl_shm_buffer_begin_access
|
|
|
|
|
*
|
|
|
|
|
* \param buffer The SHM buffer
|
|
|
|
|
*
|
|
|
|
|
* This should be called after wl_shm_buffer_begin_access once the
|
|
|
|
|
* buffer is no longer being accessed. If a SIGBUS signal was
|
|
|
|
|
* generated in-between these two calls then the resource for the
|
|
|
|
|
* given buffer will be sent an error.
|
|
|
|
|
*
|
|
|
|
|
* \memberof wl_shm_buffer
|
|
|
|
|
*/
|
2013-11-13 15:32:05 +00:00
|
|
|
WL_EXPORT void
|
|
|
|
|
wl_shm_buffer_end_access(struct wl_shm_buffer *buffer)
|
|
|
|
|
{
|
|
|
|
|
struct wl_shm_sigbus_data *sigbus_data =
|
|
|
|
|
pthread_getspecific(wl_shm_sigbus_data_key);
|
|
|
|
|
|
2014-01-10 11:26:27 -08:00
|
|
|
assert(sigbus_data && sigbus_data->access_count >= 1);
|
2013-11-13 15:32:05 +00:00
|
|
|
|
|
|
|
|
if (--sigbus_data->access_count == 0) {
|
|
|
|
|
if (sigbus_data->fallback_mapping_used) {
|
|
|
|
|
wl_resource_post_error(buffer->resource,
|
|
|
|
|
WL_SHM_ERROR_INVALID_FD,
|
|
|
|
|
"error accessing SHM buffer");
|
|
|
|
|
sigbus_data->fallback_mapping_used = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sigbus_data->current_pool = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-05 13:57:46 -06:00
|
|
|
|
|
|
|
|
/** \cond */ /* Deprecated functions below. */
|
|
|
|
|
|
|
|
|
|
WL_EXPORT struct wl_shm_buffer *
|
|
|
|
|
wl_shm_buffer_create(struct wl_client *client,
|
|
|
|
|
uint32_t id, int32_t width, int32_t height,
|
|
|
|
|
int32_t stride, uint32_t format)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \endcond */
|
|
|
|
|
|
|
|
|
|
/* Functions at the end of this file are deprecated. Instead of adding new
|
|
|
|
|
* code here, add it before the comment above that states:
|
|
|
|
|
* Deprecated functions below.
|
|
|
|
|
*/
|