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;
260 tbm_drm_auth_srv->device_name = strdup(device_name);
261 tbm_drm_auth_srv->fd = fd;
262 tbm_drm_auth_srv->flags = flags;
264 if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
265 TBM_ERR("[TBM_DRM] fail to add socket\n");
267 if (tbm_drm_auth_srv->device_name)
268 free(tbm_drm_auth_srv->device_name);
270 free(tbm_drm_auth_srv);
271 tbm_drm_auth_srv = NULL;
276 _tbm_drm_auth_socket_init(tbm_drm_auth_srv);
278 /* init the client resource list */
279 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
280 tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
287 tbm_drm_helper_wl_auth_server_deinit(void)
289 if (tbm_drm_auth_srv) {
290 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
292 if (tbm_drm_auth_srv->device_name)
293 free(tbm_drm_auth_srv->device_name);
295 free(tbm_drm_auth_srv);
296 tbm_drm_auth_srv = NULL;
301 tbm_drm_helper_get_master_fd(void)
303 int new_fd, flags, fd = -1;
305 fd = tbm_drm_master_fd;
307 TBM_INFO("no presetted TBM DRM MASTER FD");
311 TBM_INFO("TBM DRM MASTER FD: %d\n", fd);
313 flags = fcntl(fd, F_GETFD);
315 TBM_ERR("fcntl failed: %m");
321 TBM_ERR("dup failed: %m");
325 if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
326 TBM_ERR("failed to set fd\n");
331 TBM_INFO("Return MASTER_FD: %d\n", new_fd);
337 tbm_drm_helper_set_tbm_master_fd(int fd)
339 int fd_max = tbm_bufmgr_get_fd_limit();
341 if (tbm_drm_master_fd == fd)
344 if (fd < 0 || fd > fd_max) {
345 TBM_ERR("%d out of fd range\n", fd);
349 if (tbm_drm_master_fd != -1)
350 TBM_WRN("already has TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);
352 tbm_drm_master_fd = fd;
354 TBM_INFO("TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);
358 tbm_drm_helper_unset_tbm_master_fd(void)
360 tbm_drm_master_fd = -1;
361 TBM_INFO("TBM DRM MASTER FD: %d\n", tbm_drm_master_fd);