tbm_drm_helper: set permission and group to tbm-drm-auth socket when it is added
[platform/core/uifw/libtbm.git] / src / tbm_drm_helper_server.c
index 65a9c45..ed92b96 100644 (file)
@@ -34,6 +34,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "config.h"
 
 #include <xf86drm.h>
+#include <grp.h>
 
 #include "tbm_bufmgr_int.h"
 
@@ -144,6 +145,109 @@ _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
                                       NULL);
 }
 
+static int
+_tbm_getgrnam_r(const char *name)
+{
+       struct group *grp = NULL;
+       struct group *grp_res = NULL;
+       char* buf = NULL;
+       size_t buf_len;
+       int ret;
+       int id;
+
+       buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if (buf_len == -1)
+               buf_len = 2048;
+
+       buf = calloc(1, buf_len * sizeof(char));
+       if (!buf) {
+               TBM_LOG_E("creating buffer failed\n");
+               goto failed;
+       }
+
+       grp = calloc(1, sizeof(struct group));
+       if (!grp) {
+               TBM_LOG_E("creating group failed\n");
+               goto failed;
+       }
+
+       ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
+       if (ret < 0) {
+               TBM_LOG_E("getgrnam_r failed errno:%d(%m)\n", ret);
+               goto failed;
+       }
+
+       if (grp_res == NULL) {
+               TBM_LOG_E("finding name:%s group failed\n", name);
+               goto failed;
+       }
+
+       id = grp->gr_gid;
+       free(buf);
+       free(grp);
+
+       return id;
+
+failed:
+       if (buf)
+               free(buf);
+       if (grp)
+               free(grp);
+
+       return -1;
+}
+
+static int
+_tbm_drm_auth_socket_init(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv)
+{
+       const char *dir = NULL;
+       char socket_path[128];
+       int ret = -1;
+       uid_t uid;
+       gid_t gid;
+
+       if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
+               TBM_LOG_E("createing a tdm-socket failed\n");
+               return 0;
+       }
+
+       dir = getenv("XDG_RUNTIME_DIR");
+       if (!dir) {
+               TBM_LOG_E("getting XDG_RUNTIME_DIR failed\n");
+               return 0;
+       }
+
+       snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tbm-drm-auth");
+
+       ret = chmod(socket_path, 509);
+       if (ret < 0) {
+               TBM_LOG_E("changing modes of socket file failed:%s (%m)\n", socket_path);
+               return 0;
+       }
+
+       ret = _tbm_getgrnam_r("root");
+       if (ret < 0) {
+               TBM_LOG_E("getting uid failed\n");
+               return 0;
+       }
+       uid = ret;
+
+       ret = _tbm_getgrnam_r("display");
+       if (ret < 0) {
+               TBM_LOG_E("getting gid failed\n");
+               return 0;
+       }
+       gid = ret;
+
+       ret = chown(socket_path, uid, gid);
+       if (ret < 0) {
+               TBM_LOG_E("changing owner of socket file failed:%s (%m)\n", socket_path);
+               return 0;
+       }
+
+       return 1;
+}
+
 int
 tbm_drm_helper_wl_auth_server_init(void *wl_display,   int fd, const char *device_name, uint32_t flags)
 {
@@ -158,8 +262,8 @@ tbm_drm_helper_wl_auth_server_init(void *wl_display,   int fd, const char *devic
                tbm_drm_auth_srv->fd = fd;
                tbm_drm_auth_srv->flags = flags;
 
-               if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
-                       TBM_LOG_E("[TBM_DRM] fail to add socket\n");
+               if (!_tbm_drm_auth_socket_init(tbm_drm_auth_srv)) {
+                       TBM_LOG_E("[TBM_DRM] fail to _tbm_drm_auth_socket_init\n");
 
                        if (tbm_drm_auth_srv->device_name)
                                free(tbm_drm_auth_srv->device_name);