Debian Codegraph @

src/wayland-eglutils.c

raw
1 /*
2 * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25 #endif
26
27 #include "wayland-eglutils.h"
28 #include "wayland-thread.h"
29 #include "wayland-eglhandle.h"
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <assert.h>
37
38 EGLBoolean wlEglFindExtension(const char *extension, const char *extensions)
39 {
40 const char *start;
41 const char *where, *terminator;
42
43 start = extensions;
44 for (;;) {
45 where = strstr(start, extension);
46 if (!where) {
47 break;
48 }
49 terminator = where + strlen(extension);
50 if (where == start || *(where - 1) == ' ') {
51 if (*terminator == ' ' || *terminator == '\0') {
52 return EGL_TRUE;
53 }
54 }
55 start = terminator;
56 }
57
58 return EGL_FALSE;
59 }
60
61 EGLBoolean wlEglMemoryIsReadable(const void *p, size_t len)
62 {
63 int fds[2], result = -1;
64 if (pipe(fds) == -1) {
65 return EGL_FALSE;
66 }
67
68 if (fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1) {
69 goto done;
70 }
71
72 /* write will fail with EFAULT if the provided buffer is outside
73 * our accessible address space. */
74 result = write(fds[1], p, len);
75 assert(result != -1 || errno == EFAULT);
76
77 done:
78 close(fds[0]);
79 close(fds[1]);
80 return result != -1;
81 }
82
83 EGLBoolean wlEglCheckInterfaceType(struct wl_object *obj, const char *ifname)
84 {
85 /* The first member of a wl_object is a pointer to its wl_interface, */
86 struct wl_interface *interface = *(void **)obj;
87
88 /* Check if the memory for the wl_interface struct, and the
89 * interface name, are safe to read. */
90 int len = strlen(ifname);
91 if (!wlEglMemoryIsReadable(interface, sizeof (*interface)) ||
92 !wlEglMemoryIsReadable(interface->name, len + 1)) {
93 return EGL_FALSE;
94 }
95
96
97 return !strcmp(interface->name, ifname);
98 }
99
100 void wlEglSetErrorCallback(WlEglPlatformData *data,
101 EGLint error,
102 const char *file,
103 int line)
104 {
105 if (data && data->callbacks.setError) {
106 const char *defaultMsg = "Wayland external platform error";
107
108 if (file != NULL) {
109 char msg[256];
110 if (snprintf(msg, 256, "%s:%d: %s", file, line, defaultMsg) > 0) {
111 data->callbacks.setError(error, EGL_DEBUG_MSG_ERROR_KHR, msg);
112 return;
113 }
114 }
115 data->callbacks.setError(error, EGL_DEBUG_MSG_ERROR_KHR, defaultMsg);
116 }
117 }