1 /**************************************************************************
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
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.
30 **************************************************************************/
32 #define WL_HIDE_DEPRECATED
39 #include "tbm_bufmgr_int.h"
41 #include "wayland-tbm-drm-auth-server-protocol.h"
43 struct wayland_tbm_drm_auth_server {
44 struct wl_display *display;
45 struct wl_global *wl_tbm_drm_auth_global;
53 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
55 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv;
57 static int tbm_drm_master_fd = -1;
61 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
62 struct wl_resource *resource)
65 uint32_t capabilities;
66 char *device_name = NULL;
67 drm_magic_t magic = 0;
69 fd = open(tbm_drm_auth_srv->device_name, O_RDWR | O_CLOEXEC);
70 if (fd == -1 && errno == EINVAL) {
71 fd = open(tbm_drm_auth_srv->device_name, O_RDWR);
73 if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) == -1) {
74 TBM_ERR("failed to set fd\n");
81 TBM_ERR("failed to open drm : device_name, %s (%m)\n", tbm_drm_auth_srv->device_name);
83 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
84 "authenicate failed::open_drm");
88 if (drmGetMagic(fd, &magic) < 0) {
89 if (errno != EACCES) {
90 TBM_ERR("failed to get magic\n");
92 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
93 "authenicate failed::get_magic");
98 if (drmAuthMagic(tbm_drm_auth_srv->fd, magic) < 0) {
99 TBM_ERR("failed to authenticate magic\n");
101 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
102 "authenicate failed::auth_magic");
106 capabilities = tbm_drm_auth_srv->flags;
107 device_name = tbm_drm_auth_srv->device_name;
110 wl_tbm_drm_auth_send_authentication_info(resource, device_name, capabilities, fd);
112 TBM_INFO("Send tbm drm auth info fd:%d device_name:%s capabilities:%x",
113 fd, device_name, capabilities);
119 if (device_name && device_name != tbm_drm_auth_srv->device_name)
125 _wayland_tbm_drm_auth_server_impl_get_authentication_info(struct wl_client *client,
126 struct wl_resource *resource)
128 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv = wl_resource_get_user_data(resource);
130 /* if display server is the client of the host display server, for embedded server */
131 _send_server_auth_info(tbm_drm_auth_srv, resource);
135 static const struct wl_tbm_drm_auth_interface _wayland_tbm_drm_auth_server_implementation = {
136 _wayland_tbm_drm_auth_server_impl_get_authentication_info,
140 _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
144 struct wl_resource *resource;
146 resource = wl_resource_create(client, &wl_tbm_drm_auth_interface, MIN(version, 1), id);
148 wl_client_post_no_memory(client);
152 wl_resource_set_implementation(resource,
153 &_wayland_tbm_drm_auth_server_implementation,
159 _tbm_getgrnam_r(const char *name)
161 struct group *grp = NULL;
162 struct group *grp_res = NULL;
168 buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
172 buf = calloc(1, buf_len * sizeof(char));
174 TBM_ERR("creating buffer failed\n");
178 grp = calloc(1, sizeof(struct group));
180 TBM_ERR("creating group failed\n");
184 ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
186 TBM_ERR("getgrnam_r failed errno:%d(%m)\n", ret);
190 if (grp_res == NULL) {
191 TBM_ERR("finding name:%s group failed\n", name);
211 _tbm_drm_auth_socket_init(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv)
213 const char *dir = NULL;
214 char socket_path[128];
219 dir = getenv("XDG_RUNTIME_DIR");
221 TBM_WRN("getting XDG_RUNTIME_DIR failed\n");
225 snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tbm-drm-auth");
227 ret = chmod(socket_path, 509);
229 TBM_WRN("changing modes of socket file failed:%s (%m)\n", socket_path);
233 ret = _tbm_getgrnam_r("root");
235 TBM_WRN("getting uid failed\n");
240 ret = _tbm_getgrnam_r("display");
242 TBM_WRN("getting gid failed\n");
247 ret = chown(socket_path, uid, gid);
249 TBM_WRN("changing owner of socket file failed:%s (%m)\n", socket_path);
255 _tbm_drm_auth_main(void *data)
257 wl_display_run(tbm_drm_auth_srv->display);
262 tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *device_name, uint32_t flags)
266 if (tbm_drm_auth_srv) return 1;
268 tbm_drm_auth_srv = calloc(1, sizeof(struct wayland_tbm_drm_auth_server));
269 TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0);
271 tbm_drm_auth_srv->display = wl_display_create();
272 if (!tbm_drm_auth_srv->display) {
273 TBM_ERR("fail to create wl_display");
278 tbm_drm_auth_srv->device_name = drmGetDeviceNameFromFd(fd);
279 if (!tbm_drm_auth_srv->device_name) {
280 TBM_ERR("fail to get device name");
284 tbm_drm_auth_srv->device_name = strdup(device_name);
287 tbm_drm_auth_srv->fd = fd;
288 tbm_drm_auth_srv->flags = flags;
290 if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
291 TBM_ERR("[TBM_DRM] fail to add socket");
295 _tbm_drm_auth_socket_init(tbm_drm_auth_srv);
297 /* init the client resource list */
298 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
299 tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
301 ret = pthread_create(&tbm_drm_auth_srv->thread, NULL, _tbm_drm_auth_main, NULL);
303 TBM_ERR("fail to create auth thread err:%d", ret);
307 pthread_setname_np(tbm_drm_auth_srv->thread, "tbm_drm_auth");
312 if (tbm_drm_auth_srv->display)
313 wl_display_destroy(tbm_drm_auth_srv->display);
315 if (tbm_drm_auth_srv->device_name)
316 free(tbm_drm_auth_srv->device_name);
318 free(tbm_drm_auth_srv);
319 tbm_drm_auth_srv = NULL;
325 tbm_drm_helper_wl_auth_server_deinit(void)
327 if (!tbm_drm_auth_srv) return;
329 wl_display_terminate(tbm_drm_auth_srv->display);
330 pthread_join(tbm_drm_auth_srv->thread, NULL);
332 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
334 wl_display_destroy(tbm_drm_auth_srv->display);
336 if (tbm_drm_auth_srv->device_name)
337 free(tbm_drm_auth_srv->device_name);
339 free(tbm_drm_auth_srv);
340 tbm_drm_auth_srv = NULL;
344 tbm_drm_helper_get_master_fd(void)
346 int new_fd, flags, fd = -1;
348 fd = tbm_drm_master_fd;
350 TBM_INFO("no presetted TBM DRM MASTER FD");
354 TBM_INFO("TBM DRM MASTER FD: %d\n", fd);
356 flags = fcntl(fd, F_GETFD);
358 TBM_ERR("fcntl failed: %m");
364 TBM_ERR("dup failed: %m");
368 if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
369 TBM_ERR("failed to set fd\n");
374 TBM_INFO("Return MASTER_FD: %d\n", new_fd);
380 tbm_drm_helper_set_tbm_master_fd(int fd)
382 int fd_max = tbm_bufmgr_get_fd_limit();
384 if (tbm_drm_master_fd == fd)
387 if (fd < 0 || fd > fd_max) {
388 TBM_ERR("%d out of fd range\n", fd);
392 if (tbm_drm_master_fd != -1)
393 TBM_WRN("already has TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);
395 tbm_drm_master_fd = fd;
397 TBM_INFO("TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);
401 tbm_drm_helper_unset_tbm_master_fd(void)
403 tbm_drm_master_fd = -1;
404 TBM_INFO("TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);
408 tbm_drm_helper_is_render_node(int fd)
410 if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)