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;
58 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
59 struct wl_resource *resource)
62 uint32_t capabilities;
63 char *device_name = NULL;
64 drm_magic_t magic = 0;
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);
70 if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) == -1) {
71 TBM_LOG_E("failed to set fd\n");
78 TBM_LOG_E("failed to open drm : device_name, %s\n", tbm_drm_auth_srv->device_name);
80 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
81 "authenicate failed::open_drm");
85 if (drmGetMagic(fd, &magic) < 0) {
86 if (errno != EACCES) {
87 TBM_LOG_E("failed to get magic\n");
89 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
90 "authenicate failed::get_magic");
95 if (drmAuthMagic(tbm_drm_auth_srv->fd, magic) < 0) {
96 TBM_LOG_E("failed to authenticate magic\n");
98 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
99 "authenicate failed::auth_magic");
103 capabilities = tbm_drm_auth_srv->flags;
104 device_name = tbm_drm_auth_srv->device_name;
107 wl_tbm_drm_auth_send_authentication_info(resource, device_name, capabilities, fd);
113 if (device_name && device_name != tbm_drm_auth_srv->device_name)
119 _wayland_tbm_drm_auth_server_impl_get_authentication_info(struct wl_client *client,
120 struct wl_resource *resource)
122 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv = wl_resource_get_user_data(resource);
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);
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,
134 _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
138 struct wl_resource *resource;
140 resource = wl_resource_create(client, &wl_tbm_drm_auth_interface, MIN(version, 1), id);
142 wl_client_post_no_memory(client);
146 wl_resource_set_implementation(resource,
147 &_wayland_tbm_drm_auth_server_implementation,
153 _tbm_getgrnam_r(const char *name)
155 struct group *grp = NULL;
156 struct group *grp_res = NULL;
162 buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
166 buf = calloc(1, buf_len * sizeof(char));
168 TBM_LOG_E("creating buffer failed\n");
172 grp = calloc(1, sizeof(struct group));
174 TBM_LOG_E("creating group failed\n");
178 ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
180 TBM_LOG_E("getgrnam_r failed errno:%d(%m)\n", ret);
184 if (grp_res == NULL) {
185 TBM_LOG_E("finding name:%s group failed\n", name);
205 _tbm_drm_auth_socket_init(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv)
207 const char *dir = NULL;
208 char socket_path[128];
213 dir = getenv("XDG_RUNTIME_DIR");
215 TBM_LOG_W("getting XDG_RUNTIME_DIR failed\n");
219 snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tbm-drm-auth");
221 ret = chmod(socket_path, 509);
223 TBM_LOG_W("changing modes of socket file failed:%s (%m)\n", socket_path);
227 ret = _tbm_getgrnam_r("root");
229 TBM_LOG_W("getting uid failed\n");
234 ret = _tbm_getgrnam_r("display");
236 TBM_LOG_W("getting gid failed\n");
241 ret = chown(socket_path, uid, gid);
243 TBM_LOG_W("changing owner of socket file failed:%s (%m)\n", socket_path);
249 tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *device_name, uint32_t flags)
251 if (!tbm_drm_auth_srv) {
252 TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0);
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);
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;
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");
265 if (tbm_drm_auth_srv->device_name)
266 free(tbm_drm_auth_srv->device_name);
268 free(tbm_drm_auth_srv);
269 tbm_drm_auth_srv = NULL;
274 _tbm_drm_auth_socket_init(tbm_drm_auth_srv);
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);
285 tbm_drm_helper_wl_auth_server_deinit(void)
287 if (tbm_drm_auth_srv) {
288 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
290 if (tbm_drm_auth_srv->device_name)
291 free(tbm_drm_auth_srv->device_name);
293 free(tbm_drm_auth_srv);
294 tbm_drm_auth_srv = NULL;
299 tbm_drm_helper_get_master_fd(void)
307 value = (const char*)getenv("TDM_DRM_MASTER_FD");
311 const long sl = strtol(value, &end, 10);
313 TBM_LOG_E("%s: not a decimal number\n", value);
315 } else if (*end != '\0') {
316 TBM_LOG_E("%s: extra characters at end of input: %s\n", value, end);
318 } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
319 TBM_LOG_E("%s out of range of type long\n", value);
321 } else if (sl > INT_MAX) {
322 TBM_LOG_E("%ld greater than INT_MAX\n", sl);
324 } else if (sl < INT_MIN) {
325 TBM_LOG_E("%ld less than INT_MIN\n", sl);
330 TBM_LOG_E("%d out of fd range\n", fd);
335 TBM_LOG_I("TDM_DRM_MASTER_FD: %d\n", fd);
337 flags = fcntl(fd, F_GETFD);
339 TBM_LOG_E("fcntl failed: %m");
345 TBM_LOG_E("dup failed: %m");
349 if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
350 TBM_LOG_E("failed to set fd\n");
355 TBM_LOG_I("Return MASTER_FD: %d\n", new_fd);
361 tbm_drm_helper_set_tbm_master_fd(int fd)
366 snprintf(buf, sizeof(buf), "%d", fd);
368 ret = setenv("TBM_DRM_MASTER_FD", (const char*)buf, 1);
370 TBM_LOG_E("failed to set TIZEN_DRM_MASTER_FD to %d\n", fd);
374 TBM_LOG_I("TBM_DRM_MASTER_FD: %d\n", fd);
378 tbm_drm_helper_unset_tbm_master_fd(void)
382 ret = unsetenv("TBM_DRM_MASTER_FD");
384 TBM_LOG_E("failed to unset TBM_DRM_MASTER_FD\n");