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