tbm_bufmgr: add checking of a map_count in the unmap function
[platform/core/uifw/libtbm.git] / src / tbm_drm_helper_client.c
index 0ca1575..ecd5653 100644 (file)
@@ -52,6 +52,8 @@ struct wayland_tbm_drm_auth_client {
        uint32_t capabilities;
 };
 
+static int tbm_drm_fd = -1;
+
 /* LCOV_EXCL_START */
 static void
 handle_tbm_drm_authentication_info(void *data, struct wl_tbm_drm_auth *wl_tbm_drm_auth, const char *device_name, uint32_t capabilities, int32_t auth_fd)
@@ -191,62 +193,60 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
 void
 tbm_drm_helper_set_fd(int fd)
 {
-       char buf[32];
-       int ret;
+       int fd_max = tbm_bufmgr_get_fd_limit();
 
-       snprintf(buf, sizeof(buf), "%d", fd);
+       if (tbm_drm_fd == fd)
+               return;
 
-       ret = setenv("TBM_DRM_FD", (const char*)buf, 1);
-       if (ret) {
-               TBM_LOG_E("failed to set TBM_DRM_FD to %d\n", fd);
+       if (fd < 0 || fd > fd_max) {
+               TBM_LOG_E("%d out of fd range\n", fd);
                return;
        }
 
-       TBM_LOG_I("TBM_DRM_FD: %d\n", fd);
+       if (tbm_drm_fd != -1)
+               TBM_LOG_W("already has TBM_DRM_FD: %d\n", tbm_drm_fd);
+
+       tbm_drm_fd = fd;
+
+       TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
 }
 
 void
 tbm_drm_helper_unset_fd(void)
 {
-       int ret;
-
-       ret = unsetenv("TBM_DRM_FD");
-       if (ret) {
-               TBM_LOG_E("failed to unset TBM_DRM_FD\n");
-               return;
-       }
+       tbm_drm_fd = -1;
+       TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
 }
 
 int
 tbm_drm_helper_get_fd(void)
 {
-       const char *value;
-       int ret, flags, fd = -1;
-       int new_fd = -1;
-
-       value = (const char*)getenv("TBM_DRM_FD");
-       if (!value)
-               return -1;
+       int new_fd, flags;
 
-       ret = sscanf(value, "%d", &fd);
-       if (ret <= 0)
+       if (tbm_drm_fd == -1) {
+               TBM_LOG_E("no drm fd");
                return -1;
+       }
 
-       TBM_LOG_I("TBM_DRM_FD: %d\n", fd);
+       TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
 
-       flags = fcntl(fd, F_GETFD);
+       flags = fcntl(tbm_drm_fd, F_GETFD);
        if (flags == -1) {
-               TBM_LOG_E("fcntl failed: %m\n");
+               TBM_LOG_E("fcntl failed: %m");
                return -1;
        }
 
-       new_fd = dup(fd);
+       new_fd = dup(tbm_drm_fd);
        if (new_fd < 0) {
-               TBM_LOG_E("dup failed: %m\n");
+               TBM_LOG_E("dup failed: %m");
                return -1;
        }
 
-       fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC);
+       if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
+               TBM_LOG_E("failed to set fd\n");
+               close(new_fd);
+               return -1;
+       }
 
        TBM_LOG_I("Return TBM_FD: %d\n", new_fd);