5ddf9917b2528656125f4e6e515be65bb8445cc1
[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 static void
62 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
63                        struct wl_resource *resource)
64 {
65         int fd = -1;
66         uint32_t capabilities;
67         char *device_name = NULL;
68         drm_magic_t magic = 0;
69
70         fd = open(tbm_drm_auth_srv->device_name, O_RDWR | O_CLOEXEC);
71         if (fd == -1 && errno == EINVAL) {
72                 fd = open(tbm_drm_auth_srv->device_name, O_RDWR);
73                 if (fd != -1)
74                         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
75         }
76
77         if (fd < 0) {
78                 TBM_LOG("failed to open drm : device_name, %s\n", tbm_drm_auth_srv->device_name);
79
80                 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
81                                        "authenicate failed::open_drm");
82                 goto fini;
83         }
84
85         if (drmGetMagic(fd, &magic) < 0) {
86                 if (errno != EACCES) {
87                         TBM_LOG("failed to get magic\n");
88
89                         wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
90                                                "authenicate failed::get_magic");
91                         goto fini;
92                 }
93         }
94
95         if (drmAuthMagic(tbm_drm_auth_srv->fd, magic) < 0) {
96                 TBM_LOG("failed to authenticate magic\n");
97
98                 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
99                                        "authenicate failed::auth_magic");
100                 goto fini;
101         }
102
103         capabilities = tbm_drm_auth_srv->flags;
104         device_name = tbm_drm_auth_srv->device_name;
105
106         /* send */
107         wl_tbm_drm_auth_send_authentication_info(resource, device_name, capabilities, fd);
108
109 fini:
110         if (fd >= 0)
111                 close(fd);
112
113         if (device_name && device_name != tbm_drm_auth_srv->device_name)
114                 free(device_name);
115
116 }
117
118 static void
119 _wayland_tbm_drm_auth_server_impl_get_authentication_info(struct wl_client *client,
120                 struct wl_resource *resource)
121 {
122         struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv = wl_resource_get_user_data(resource);
123
124         /* if display server is the client of the host display server, for embedded server */
125         _send_server_auth_info(tbm_drm_auth_srv, resource);
126 }
127
128
129 static const struct wl_tbm_drm_auth_interface _wayland_tbm_drm_auth_server_implementation = {
130         _wayland_tbm_drm_auth_server_impl_get_authentication_info,
131 };
132
133 static void
134 _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
135                             uint32_t version,
136                             uint32_t id)
137 {
138         struct wl_resource *resource;
139
140         resource = wl_resource_create(client, &wl_tbm_drm_auth_interface, MIN(version, 1), id);
141         if (!resource) {
142                 wl_client_post_no_memory(client);
143                 return;
144         }
145
146         wl_resource_set_implementation(resource,
147                                        &_wayland_tbm_drm_auth_server_implementation,
148                                        data,
149                                        NULL);
150 }
151
152 int
153 tbm_drm_helper_wl_auth_server_init(void *wl_display,   int fd, const char *device_name, uint32_t flags)
154 {
155         if (!tbm_drm_auth_srv) {
156                 TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0);
157
158                 tbm_drm_auth_srv = calloc(1, sizeof(struct wayland_tbm_drm_auth_server));
159                 TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0);
160
161                 tbm_drm_auth_srv->display = (struct wl_display *)wl_display;
162                 tbm_drm_auth_srv->device_name = strdup(device_name);
163                 tbm_drm_auth_srv->fd = fd;
164                 tbm_drm_auth_srv->flags = flags;
165
166                 if(wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
167                         TBM_LOG("[TBM_DRM] fail to add socket\n");
168
169                         if (tbm_drm_auth_srv->device_name)
170                                 free(tbm_drm_auth_srv->device_name);
171
172                         free(tbm_drm_auth_srv);
173                         tbm_drm_auth_srv = NULL;
174
175                         return 0;
176                 }
177
178                 /* init the client resource list */
179                 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
180                                          tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
181         }
182
183         return 1;
184 }
185
186 void
187 tbm_drm_helper_wl_auth_server_deinit(void)
188 {
189         if (tbm_drm_auth_srv) {
190                 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
191
192                 if (tbm_drm_auth_srv->device_name)
193                         free(tbm_drm_auth_srv->device_name);
194
195                 free(tbm_drm_auth_srv);
196                 tbm_drm_auth_srv = NULL;
197         }
198 }
199
200 int
201 tbm_drm_helper_get_master_fd(void)
202 {
203     const char *value;
204     int ret, flags, fd = -1;
205     int new_fd = -1;
206
207     value = (const char*)getenv("TDM_DRM_MASTER_FD");
208     if (!value)
209         return -1;
210
211     ret = sscanf(value, "%d", &fd);
212     if (ret <= 0)
213         return -1;
214
215     TBM_LOG("TDM_DRM_MASTER_FD: %d\n", fd);
216
217     flags = fcntl(fd, F_GETFD);
218     if (flags == -1) {
219         TBM_LOG("fcntl failed: %m");
220         return -1;
221     }
222
223     new_fd = dup(fd);
224     if (new_fd < 0) {
225         TBM_LOG("dup failed: %m");
226         return -1;
227     }
228
229     fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC);
230
231     TBM_LOG("Return MASTER_FD: %d\n", new_fd);
232
233     return new_fd;
234 }
235
236 void
237 tbm_drm_helper_set_tbm_master_fd(int fd)
238 {
239     char buf[32];
240     int ret;
241
242     snprintf(buf, sizeof(buf), "%d", fd);
243
244     ret = setenv("TBM_DRM_MASTER_FD", (const char*)buf, 1);
245     if (ret)
246     {
247         TBM_LOG("failed to set TIZEN_DRM_MASTER_FD to %d", fd);
248         return;
249     }
250
251     TBM_LOG("TBM_DRM_MASTER_FD: %d\n", fd);
252 }
253
254 void
255 tbm_drm_helper_unset_tbm_master_fd(void)
256 {
257     int ret;
258
259     ret = unsetenv("TBM_DRM_MASTER_FD");
260     if (ret)
261     {
262         TBM_LOG("failed to unset TBM_DRM_MASTER_FD");
263         return;
264     }
265 }
266