tbm_bufmgr_debug: change signature of tbm_bufmgr_debug_tbm_info_get
[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 #include <grp.h>
38
39 #include "tbm_bufmgr_int.h"
40
41 #include "wayland-tbm-drm-auth-server-protocol.h"
42
43 struct wayland_tbm_drm_auth_server {
44         struct wl_display *display;
45         struct wl_global *wl_tbm_drm_auth_global;
46
47         char *device_name;
48         uint32_t fd;
49         uint32_t flags;
50 };
51
52 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
53
54 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv;
55
56 /* LCOV_EXCL_START */
57 static void
58 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
59                        struct wl_resource *resource)
60 {
61         int fd = -1;
62         uint32_t capabilities;
63         char *device_name = NULL;
64         drm_magic_t magic = 0;
65
66         fd = open(tbm_drm_auth_srv->device_name, O_RDWR | O_CLOEXEC);
67         if (fd == -1 && errno == EINVAL) {
68                 fd = open(tbm_drm_auth_srv->device_name, O_RDWR);
69                 if (fd != -1) {
70                         if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) == -1) {
71                                 TBM_LOG_E("failed to set fd\n");
72                                 goto fini;
73                         }
74                 }
75         }
76
77         if (fd < 0) {
78                 TBM_LOG_E("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_E("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_E("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 static int
153 _tbm_getgrnam_r(const char *name)
154 {
155         struct group *grp = NULL;
156         struct group *grp_res = NULL;
157         char* buf = NULL;
158         size_t buf_len;
159         int ret;
160         int id;
161
162         buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
163         if (buf_len == -1)
164                 buf_len = 2048;
165
166         buf = calloc(1, buf_len * sizeof(char));
167         if (!buf) {
168                 TBM_LOG_E("creating buffer failed\n");
169                 goto failed;
170         }
171
172         grp = calloc(1, sizeof(struct group));
173         if (!grp) {
174                 TBM_LOG_E("creating group failed\n");
175                 goto failed;
176         }
177
178         ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
179         if (ret < 0) {
180                 TBM_LOG_E("getgrnam_r failed errno:%d(%m)\n", ret);
181                 goto failed;
182         }
183
184         if (grp_res == NULL) {
185                 TBM_LOG_E("finding name:%s group failed\n", name);
186                 goto failed;
187         }
188
189         id = grp->gr_gid;
190         free(buf);
191         free(grp);
192
193         return id;
194
195 failed:
196         if (buf)
197                 free(buf);
198         if (grp)
199                 free(grp);
200
201         return -1;
202 }
203
204 static void
205 _tbm_drm_auth_socket_init(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv)
206 {
207         const char *dir = NULL;
208         char socket_path[128];
209         int ret = -1;
210         uid_t uid;
211         gid_t gid;
212
213         dir = getenv("XDG_RUNTIME_DIR");
214         if (!dir) {
215                 TBM_LOG_W("getting XDG_RUNTIME_DIR failed\n");
216                 return;
217         }
218
219         snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tbm-drm-auth");
220
221         ret = chmod(socket_path, 509);
222         if (ret < 0) {
223                 TBM_LOG_W("changing modes of socket file failed:%s (%m)\n", socket_path);
224                 return;
225         }
226
227         ret = _tbm_getgrnam_r("root");
228         if (ret < 0) {
229                 TBM_LOG_W("getting uid failed\n");
230                 return;
231         }
232         uid = ret;
233
234         ret = _tbm_getgrnam_r("display");
235         if (ret < 0) {
236                 TBM_LOG_W("getting gid failed\n");
237                 return;
238         }
239         gid = ret;
240
241         ret = chown(socket_path, uid, gid);
242         if (ret < 0) {
243                 TBM_LOG_W("changing owner of socket file failed:%s (%m)\n", socket_path);
244                 return;
245         }
246 }
247
248 int
249 tbm_drm_helper_wl_auth_server_init(void *wl_display,   int fd, const char *device_name, uint32_t flags)
250 {
251         if (!tbm_drm_auth_srv) {
252                 TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0);
253
254                 tbm_drm_auth_srv = calloc(1, sizeof(struct wayland_tbm_drm_auth_server));
255                 TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0);
256
257                 tbm_drm_auth_srv->display = (struct wl_display *)wl_display;
258                 tbm_drm_auth_srv->device_name = strdup(device_name);
259                 tbm_drm_auth_srv->fd = fd;
260                 tbm_drm_auth_srv->flags = flags;
261
262                 if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
263                         TBM_LOG_E("[TBM_DRM] fail to add socket\n");
264
265                         if (tbm_drm_auth_srv->device_name)
266                                 free(tbm_drm_auth_srv->device_name);
267
268                         free(tbm_drm_auth_srv);
269                         tbm_drm_auth_srv = NULL;
270
271                         return 0;
272                 }
273
274                 _tbm_drm_auth_socket_init(tbm_drm_auth_srv);
275
276                 /* init the client resource list */
277                 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
278                                          tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
279         }
280
281         return 1;
282 }
283
284 void
285 tbm_drm_helper_wl_auth_server_deinit(void)
286 {
287         if (tbm_drm_auth_srv) {
288                 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
289
290                 if (tbm_drm_auth_srv->device_name)
291                         free(tbm_drm_auth_srv->device_name);
292
293                 free(tbm_drm_auth_srv);
294                 tbm_drm_auth_srv = NULL;
295         }
296 }
297
298 int
299 tbm_drm_helper_get_master_fd(void)
300 {
301         const char *value;
302         int flags, fd = -1;
303         int new_fd = -1;
304         char *end;
305         errno = 0;
306
307         value = (const char*)getenv("TDM_DRM_MASTER_FD");
308         if (!value)
309                 return -1;
310
311         const long int sl = strtol(value, &end, 10);
312         if (end == value) {
313                 TBM_LOG_E("%s: not a decimal number\n", value);
314                 return -1;
315         } else if (*end != '\0') {
316                 TBM_LOG_E("%s: extra characters at end of input: %s\n", value, end);
317                 return -1;
318         } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
319                 TBM_LOG_E("%s out of range of type long\n", value);
320                 return -1;
321         } else if (sl >= INT_MAX) {
322                 TBM_LOG_E("%ld greater than INT_MAX\n", sl);
323                 return -1;
324         } else if (sl <= INT_MIN) {
325                 TBM_LOG_E("%ld less than INT_MIN\n", sl);
326                 return -1;
327         } else {
328                 int fd_max = tbm_bufmgr_get_fd_limit();
329                 fd = (int)sl;
330                 if (fd < 0 || fd > fd_max) {
331                         TBM_LOG_E("%d out of fd range\n", fd);
332                         return -1;
333                 }
334         }
335
336         TBM_LOG_I("TDM_DRM_MASTER_FD: %d\n", fd);
337
338         flags = fcntl(fd, F_GETFD);
339         if (flags == -1) {
340                 TBM_LOG_E("fcntl failed: %m");
341                 return -1;
342         }
343
344         new_fd = dup(fd);
345         if (new_fd < 0) {
346                 TBM_LOG_E("dup failed: %m");
347                 return -1;
348         }
349
350         if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
351                 TBM_LOG_E("failed to set fd\n");
352                 close(new_fd);
353                 return -1;
354         }
355
356         TBM_LOG_I("Return MASTER_FD: %d\n", new_fd);
357
358         return new_fd;
359 }
360
361 void
362 tbm_drm_helper_set_tbm_master_fd(int fd)
363 {
364         char buf[32];
365         int ret;
366
367         snprintf(buf, sizeof(buf), "%d", fd);
368
369         ret = setenv("TBM_DRM_MASTER_FD", (const char*)buf, 1);
370         if (ret) {
371                 TBM_LOG_E("failed to set TIZEN_DRM_MASTER_FD to %d\n", fd);
372                 return;
373         }
374
375         TBM_LOG_I("TBM_DRM_MASTER_FD: %d\n", fd);
376 }
377
378 void
379 tbm_drm_helper_unset_tbm_master_fd(void)
380 {
381         int ret;
382
383         ret = unsetenv("TBM_DRM_MASTER_FD");
384         if (ret) {
385                 TBM_LOG_E("failed to unset TBM_DRM_MASTER_FD\n");
386                 return;
387         }
388 }
389 /* LCOV_EXCL_STOP */
390