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;
52 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
54 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv;
56 static int tbm_drm_master_fd = -1;
60 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
61 struct wl_resource *resource)
64 uint32_t capabilities;
65 char *device_name = NULL;
66 drm_magic_t magic = 0;
68 fd = open(tbm_drm_auth_srv->device_name, O_RDWR | O_CLOEXEC);
69 if (fd == -1 && errno == EINVAL) {
70 fd = open(tbm_drm_auth_srv->device_name, O_RDWR);
72 if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) == -1) {
73 TBM_ERR("failed to set fd\n");
80 TBM_ERR("failed to open drm : device_name, %s\n", tbm_drm_auth_srv->device_name);
82 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
83 "authenicate failed::open_drm");
87 if (drmGetMagic(fd, &magic) < 0) {
88 if (errno != EACCES) {
89 TBM_ERR("failed to get magic\n");
91 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
92 "authenicate failed::get_magic");
97 if (drmAuthMagic(tbm_drm_auth_srv->fd, magic) < 0) {
98 TBM_ERR("failed to authenticate magic\n");
100 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
101 "authenicate failed::auth_magic");
105 capabilities = tbm_drm_auth_srv->flags;
106 device_name = tbm_drm_auth_srv->device_name;
109 wl_tbm_drm_auth_send_authentication_info(resource, device_name, capabilities, fd);
115 if (device_name && device_name != tbm_drm_auth_srv->device_name)
121 _wayland_tbm_drm_auth_server_impl_get_authentication_info(struct wl_client *client,
122 struct wl_resource *resource)
124 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv = wl_resource_get_user_data(resource);
126 /* if display server is the client of the host display server, for embedded server */
127 _send_server_auth_info(tbm_drm_auth_srv, resource);
131 static const struct wl_tbm_drm_auth_interface _wayland_tbm_drm_auth_server_implementation = {
132 _wayland_tbm_drm_auth_server_impl_get_authentication_info,
136 _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
140 struct wl_resource *resource;
142 resource = wl_resource_create(client, &wl_tbm_drm_auth_interface, MIN(version, 1), id);
144 wl_client_post_no_memory(client);
148 wl_resource_set_implementation(resource,
149 &_wayland_tbm_drm_auth_server_implementation,
155 _tbm_getgrnam_r(const char *name)
157 struct group *grp = NULL;
158 struct group *grp_res = NULL;
164 buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
168 buf = calloc(1, buf_len * sizeof(char));
170 TBM_ERR("creating buffer failed\n");
174 grp = calloc(1, sizeof(struct group));
176 TBM_ERR("creating group failed\n");
180 ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
182 TBM_ERR("getgrnam_r failed errno:%d(%m)\n", ret);
186 if (grp_res == NULL) {
187 TBM_ERR("finding name:%s group failed\n", name);
207 _tbm_drm_auth_socket_init(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv)
209 const char *dir = NULL;
210 char socket_path[128];
215 dir = getenv("XDG_RUNTIME_DIR");
217 TBM_WRN("getting XDG_RUNTIME_DIR failed\n");
221 snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tbm-drm-auth");
223 ret = chmod(socket_path, 509);
225 TBM_WRN("changing modes of socket file failed:%s (%m)\n", socket_path);
229 ret = _tbm_getgrnam_r("root");
231 TBM_WRN("getting uid failed\n");
236 ret = _tbm_getgrnam_r("display");
238 TBM_WRN("getting gid failed\n");
243 ret = chown(socket_path, uid, gid);
245 TBM_WRN("changing owner of socket file failed:%s (%m)\n", socket_path);
251 tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *device_name, uint32_t flags)
253 if (!tbm_drm_auth_srv) {
254 TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0);
256 tbm_drm_auth_srv = calloc(1, sizeof(struct wayland_tbm_drm_auth_server));
257 TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0);
259 tbm_drm_auth_srv->display = (struct wl_display *)wl_display;
261 tbm_drm_auth_srv->device_name = drmGetDeviceNameFromFd(fd);
262 if (!tbm_drm_auth_srv->device_name) {
263 TBM_ERR("fail to get device name!\n");
267 tbm_drm_auth_srv->device_name = strdup(device_name);
269 tbm_drm_auth_srv->fd = fd;
270 tbm_drm_auth_srv->flags = flags;
272 if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
273 TBM_ERR("[TBM_DRM] fail to add socket\n");
275 if (tbm_drm_auth_srv->device_name)
276 free(tbm_drm_auth_srv->device_name);
278 free(tbm_drm_auth_srv);
279 tbm_drm_auth_srv = NULL;
284 _tbm_drm_auth_socket_init(tbm_drm_auth_srv);
286 /* init the client resource list */
287 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
288 tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
295 tbm_drm_helper_wl_auth_server_deinit(void)
297 if (tbm_drm_auth_srv) {
298 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
300 if (tbm_drm_auth_srv->device_name)
301 free(tbm_drm_auth_srv->device_name);
303 free(tbm_drm_auth_srv);
304 tbm_drm_auth_srv = NULL;
309 tbm_drm_helper_get_master_fd(void)
311 int new_fd, flags, fd = -1;
313 fd = tbm_drm_master_fd;
315 TBM_INFO("no presetted TBM DRM MASTER FD");
319 TBM_INFO("TBM DRM MASTER FD: %d\n", fd);
321 flags = fcntl(fd, F_GETFD);
323 TBM_ERR("fcntl failed: %m");
329 TBM_ERR("dup failed: %m");
333 if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
334 TBM_ERR("failed to set fd\n");
339 TBM_INFO("Return MASTER_FD: %d\n", new_fd);
345 tbm_drm_helper_set_tbm_master_fd(int fd)
347 int fd_max = tbm_bufmgr_get_fd_limit();
349 if (tbm_drm_master_fd == fd)
352 if (fd < 0 || fd > fd_max) {
353 TBM_ERR("%d out of fd range\n", fd);
357 if (tbm_drm_master_fd != -1)
358 TBM_WRN("already has TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);
360 tbm_drm_master_fd = fd;
362 TBM_INFO("TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);
366 tbm_drm_helper_unset_tbm_master_fd(void)
368 tbm_drm_master_fd = -1;
369 TBM_INFO("TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);