[Mesa-dev] [PATCH] egl/drm: Try to use CLOEXEC for drm fds

Derek Foreman derekf at osg.samsung.com
Fri May 1 10:34:43 PDT 2015


These fds can propagate to child processes if we don't set CLOEXEC,
so make a best effort to do that.

Signed-off-by: Derek Foreman <derekf at osg.samsung.com>
---
Noticed this when fixing up similar problems in weston - weston's
drm fd gets dup()ed here and loses CLOEXEC and ends up in every child
process the shell launches.

 src/egl/drivers/dri2/platform_drm.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/egl/drivers/dri2/platform_drm.c b/src/egl/drivers/dri2/platform_drm.c
index 486b003..c326d6c 100644
--- a/src/egl/drivers/dri2/platform_drm.c
+++ b/src/egl/drivers/dri2/platform_drm.c
@@ -596,7 +596,11 @@ dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
    struct dri2_egl_display *dri2_dpy;
    struct gbm_device *gbm;
    int fd = -1;
-   int i;
+   int i, flags = O_RDWR;
+
+#ifdef O_CLOEXEC
+   flags |= O_CLOEXEC;
+#endif
 
    loader_set_logger(_eglLog);
 
@@ -611,9 +615,9 @@ dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
       char buf[64];
       int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
       if (n != -1 && n < sizeof(buf))
-         fd = open(buf, O_RDWR);
+         fd = open(buf, flags);
       if (fd < 0)
-         fd = open("/dev/dri/card0", O_RDWR);
+         fd = open("/dev/dri/card0", flags);
       dri2_dpy->own_device = 1;
       gbm = gbm_create_device(fd);
       if (gbm == NULL)
@@ -639,6 +643,10 @@ dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
       }
    }
 
+   flags = fcntl(dri2_dpy->fd, F_GETFD);
+   if (flags >= 0 && !(flags & FD_CLOEXEC))
+      fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+
    dri2_dpy->fd = fd;
    dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
    dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->base.driver_name);
-- 
2.1.4



More information about the mesa-dev mailing list