Add excluding coverage comment
[platform/core/uifw/libtbm.git] / src / tbm_drm_helper_server.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #define WL_HIDE_DEPRECATED
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stddef.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41
42 #include <xf86drm.h>
43
44 #include "tbm_bufmgr_int.h"
45
46 #include "wayland-tbm-drm-auth-server-protocol.h"
47
48 struct wayland_tbm_drm_auth_server {
49         struct wl_display *display;
50         struct wl_global *wl_tbm_drm_auth_global;
51
52         char *device_name;
53         uint32_t fd;
54         uint32_t flags;
55 };
56
57 #define MIN(x,y) (((x)<(y))?(x):(y))
58
59 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv;
60
61 /* LCOV_EXCL_START */
62 static void
63 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
64                        struct wl_resource *resource)
65 {
66         int fd = -1;
67         uint32_t capabilities;
68         char *device_name = NULL;
69         drm_magic_t magic = 0;
70
71         fd = open(tbm_drm_auth_srv->device_name, O_RDWR | O_CLOEXEC);
72         if (fd == -1 && errno == EINVAL) {
73                 fd = open(tbm_drm_auth_srv->device_name, O_RDWR);
74                 if (fd != -1)
75                         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
76         }
77
78         if (fd < 0) {
79                 TBM_LOG_E("failed to open drm : device_name, %s\n", tbm_drm_auth_srv->device_name);
80
81                 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
82                                        "authenicate failed::open_drm");
83                 goto fini;
84         }
85
86         if (drmGetMagic(fd, &magic) < 0) {
87                 if (errno != EACCES) {
88                         TBM_LOG_E("failed to get magic\n");
89
90                         wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
91                                                "authenicate failed::get_magic");
92                         goto fini;
93                 }
94         }
95
96         if (drmAuthMagic(tbm_drm_auth_srv->fd, magic) < 0) {
97                 TBM_LOG_E("failed to authenticate magic\n");
98
99                 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
100                                        "authenicate failed::auth_magic");
101                 goto fini;
102         }
103
104         capabilities = tbm_drm_auth_srv->flags;
105         device_name = tbm_drm_auth_srv->device_name;
106
107         /* send */
108         wl_tbm_drm_auth_send_authentication_info(resource, device_name, capabilities, fd);
109
110 fini:
111         if (fd >= 0)
112                 close(fd);
113
114         if (device_name && device_name != tbm_drm_auth_srv->device_name)
115                 free(device_name);
116
117 }
118
119 static void
120 _wayland_tbm_drm_auth_server_impl_get_authentication_info(struct wl_client *client,
121                 struct wl_resource *resource)
122 {
123         struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv = wl_resource_get_user_data(resource);
124
125         /* if display server is the client of the host display server, for embedded server */
126         _send_server_auth_info(tbm_drm_auth_srv, resource);
127 }
128
129
130 static const struct wl_tbm_drm_auth_interface _wayland_tbm_drm_auth_server_implementation = {
131         _wayland_tbm_drm_auth_server_impl_get_authentication_info,
132 };
133
134 static void
135 _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
136                             uint32_t version,
137                             uint32_t id)
138 {
139         struct wl_resource *resource;
140
141         resource = wl_resource_create(client, &wl_tbm_drm_auth_interface, MIN(version, 1), id);
142         if (!resource) {
143                 wl_client_post_no_memory(client);
144                 return;
145         }
146
147         wl_resource_set_implementation(resource,
148                                        &_wayland_tbm_drm_auth_server_implementation,
149                                        data,
150                                        NULL);
151 }
152
153 int
154 tbm_drm_helper_wl_auth_server_init(void *wl_display,   int fd, const char *device_name, uint32_t flags)
155 {
156         if (!tbm_drm_auth_srv) {
157                 TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0);
158
159                 tbm_drm_auth_srv = calloc(1, sizeof(struct wayland_tbm_drm_auth_server));
160                 TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0);
161
162                 tbm_drm_auth_srv->display = (struct wl_display *)wl_display;
163                 tbm_drm_auth_srv->device_name = strdup(device_name);
164                 tbm_drm_auth_srv->fd = fd;
165                 tbm_drm_auth_srv->flags = flags;
166
167                 if(wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
168                         TBM_LOG_E("[TBM_DRM] fail to add socket\n");
169
170                         if (tbm_drm_auth_srv->device_name)
171                                 free(tbm_drm_auth_srv->device_name);
172
173                         free(tbm_drm_auth_srv);
174                         tbm_drm_auth_srv = NULL;
175
176                         return 0;
177                 }
178
179                 /* init the client resource list */
180                 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
181                                          tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
182         }
183
184         return 1;
185 }
186
187 void
188 tbm_drm_helper_wl_auth_server_deinit(void)
189 {
190         if (tbm_drm_auth_srv) {
191                 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
192
193                 if (tbm_drm_auth_srv->device_name)
194                         free(tbm_drm_auth_srv->device_name);
195
196                 free(tbm_drm_auth_srv);
197                 tbm_drm_auth_srv = NULL;
198         }
199 }
200
201 int
202 tbm_drm_helper_get_master_fd(void)
203 {
204     const char *value;
205     int ret, flags, fd = -1;
206     int new_fd = -1;
207
208     value = (const char*)getenv("TDM_DRM_MASTER_FD");
209     if (!value)
210         return -1;
211
212     ret = sscanf(value, "%d", &fd);
213     if (ret <= 0)
214         return -1;
215
216     TBM_LOG_I("TDM_DRM_MASTER_FD: %d\n", fd);
217
218     flags = fcntl(fd, F_GETFD);
219     if (flags == -1) {
220         TBM_LOG_E("fcntl failed: %m");
221         return -1;
222     }
223
224     new_fd = dup(fd);
225     if (new_fd < 0) {
226         TBM_LOG_E("dup failed: %m");
227         return -1;
228     }
229
230     fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC);
231
232     TBM_LOG_I("Return MASTER_FD: %d\n", new_fd);
233
234     return new_fd;
235 }
236
237 void
238 tbm_drm_helper_set_tbm_master_fd(int fd)
239 {
240     char buf[32];
241     int ret;
242
243     snprintf(buf, sizeof(buf), "%d", fd);
244
245     ret = setenv("TBM_DRM_MASTER_FD", (const char*)buf, 1);
246     if (ret)
247     {
248         TBM_LOG_E("failed to set TIZEN_DRM_MASTER_FD to %d", fd);
249         return;
250     }
251
252     TBM_LOG_I("TBM_DRM_MASTER_FD: %d\n", fd);
253 }
254
255 void
256 tbm_drm_helper_unset_tbm_master_fd(void)
257 {
258     int ret;
259
260     ret = unsetenv("TBM_DRM_MASTER_FD");
261     if (ret)
262     {
263         TBM_LOG_E("failed to unset TBM_DRM_MASTER_FD");
264         return;
265     }
266 }
267 /* LCOV_EXCL_STOP */