Set close on exec flag FD_CLOEXEC
authorDavid Fries <David@Fries.net>
Sat, 10 Dec 2011 17:28:45 +0000 (11:28 -0600)
committerDave Airlie <airlied@redhat.com>
Sun, 11 Mar 2012 19:05:50 +0000 (19:05 +0000)
Set the close on exec flag when opening dri character devices, so they
will be closed and free any resouces allocated in exec.

Signed-off-by: David Fries <David@Fries.net>
Reviewed-by: Adam Jackson <ajax@redhat.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
src/egl/drivers/dri2/platform_wayland.c
src/egl/drivers/dri2/platform_x11.c
src/gallium/state_trackers/egl/drm/native_drm.c
src/gallium/state_trackers/egl/fbdev/native_fbdev.c
src/gallium/state_trackers/egl/wayland/native_drm.c
src/gallium/state_trackers/egl/x11/x11_screen.c
src/glx/dri2_glx.c

index ad649b141ddca8fe833fd6f4e26a02b4664a1ab2..0f439c7110764a2e5335b910f760651031f917d1 100644 (file)
@@ -748,7 +748,16 @@ drm_handle_device(void *data, struct wl_drm *drm, const char *device)
    if (!dri2_dpy->device_name)
       return;
 
-   dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
+#ifdef O_CLOEXEC
+   dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
+   if (dri2_dpy->fd == -1 && errno == EINVAL)
+#endif
+   {
+      dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
+      if (dri2_dpy->fd != -1)
+         fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
+            FD_CLOEXEC);
+   }
    if (dri2_dpy->fd == -1) {
       _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
              dri2_dpy->device_name, strerror(errno));
index a100f4d284244945e3a332d2c34c45dacd11060d..7486a91ec0c8887d653d740aacb0878c511e997b 100644 (file)
@@ -1083,7 +1083,16 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    if (!dri2_load_driver(disp))
       goto cleanup_conn;
 
+#ifdef O_CLOEXEC
    dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
+   if (dri2_dpy->fd == -1 && errno == EINVAL)
+#endif
+   {
+      dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
+      if (dri2_dpy->fd != -1)
+         fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
+            FD_CLOEXEC);
+   }
    if (dri2_dpy->fd == -1) {
       _eglLog(_EGL_WARNING,
              "DRI2: could not open %s (%s)", dri2_dpy->device_name,
index 6ac12100f765ac3ec5cdc7e9067703115a786787..ba7afdbe3b820d0003a5b3ee9923694e2ce1e806 100644 (file)
@@ -312,7 +312,16 @@ native_create_display(void *dpy, boolean use_sw)
    gbm = dpy;
 
    if (gbm == NULL) {
-      fd = open("/dev/dri/card0", O_RDWR);
+      const char *device_name="/dev/dri/card0";
+#ifdef O_CLOEXEC
+      fd = open(device_name, O_RDWR | O_CLOEXEC);
+      if (fd == -1 && errno == EINVAL)
+#endif
+      {
+         fd = open(device_name, O_RDWR);
+         if (fd != -1)
+            fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+      }
       /* FIXME: Use an internal constructor to create a gbm
        * device with gallium backend directly, without setenv */
       setenv("GBM_BACKEND", "gbm_gallium_drm.so", 1);
index e126888df902e060720a065dc2e8f3a7a632c4ce..b45ab5c4f2e86b5b3375616e870d9060a8ca250b 100644 (file)
@@ -515,7 +515,16 @@ native_create_display(void *dpy, boolean use_sw)
 
    /* well, this makes fd 0 being ignored */
    if (!dpy) {
-      fd = open("/dev/fb0", O_RDWR);
+      const char *device_name="/dev/fb0";
+#ifdef O_CLOEXEC
+      fd = open(device_name, O_RDWR | O_CLOEXEC);
+      if (fd == -1 && errno == EINVAL)
+#endif
+      {
+         fd = open(device_name, O_RDWR);
+         if (fd != -1)
+            fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+      }
    }
    else {
       fd = dup((int) pointer_to_intptr(dpy));
index 8daf7830dd464a3271944f8e7ff5756ca427a943..e3bd628675e062d20c215d0b90d3b26ed028994b 100644 (file)
@@ -133,7 +133,15 @@ drm_handle_device(void *data, struct wl_drm *drm, const char *device)
    if (!drmdpy->device_name)
       return;
 
-   drmdpy->fd = open(drmdpy->device_name, O_RDWR);
+#ifdef O_CLOEXEC
+   drmdpy->fd = open(drmdpy->device_name, O_RDWR | O_CLOEXEC);
+   if (drmdpy->fd == -1 && errno == EINVAL)
+#endif
+   {
+      drmdpy->fd = open(drmdpy->device_name, O_RDWR);
+      if (drmdpy->fd != -1)
+         fcntl(drmdpy->fd, F_SETFD, fcntl(drmdpy->fd, F_GETFD) | FD_CLOEXEC);
+   }
    if (drmdpy->fd == -1) {
       _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
               drmdpy->device_name, strerror(errno));
index 1e20f941c81cc54f556acfb9a156abb4a4759904..f8f9e2ae767bf12a9b0567e6df12ea59624aba54 100644 (file)
@@ -265,7 +265,15 @@ x11_screen_enable_dri2(struct x11_screen *xscr,
       if (!x11_screen_probe_dri2(xscr, NULL, NULL))
          return -1;
 
-      fd = open(xscr->dri_device, O_RDWR);
+#ifdef O_CLOEXEC
+      fd = open(xscr->dri_device, O_RDWR | O_CLOEXEC);
+      if (fd == -1 && errno == EINVAL)
+#endif
+      {
+         fd = open(xscr->dri_device, O_RDWR);
+         if (fd != -1)
+            fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+      }
       if (fd < 0) {
          _eglLog(_EGL_WARNING, "failed to open %s", xscr->dri_device);
          return -1;
index b09606addde1bc8999b3a3a3e23740ad14dee49f..b6988c3ca60e7b7ac52c5e25ccf20af6ba4f4b68 100644 (file)
@@ -1045,7 +1045,15 @@ dri2CreateScreen(int screen, struct glx_display * priv)
       goto handle_error;
    }
 
-   psc->fd = open(deviceName, O_RDWR);
+#ifdef O_CLOEXEC
+   psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
+   if (psc->fd == -1 && errno == EINVAL)
+#endif
+   {
+      psc->fd = open(deviceName, O_RDWR);
+      if (psc->fd != -1)
+         fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
+   }
    if (psc->fd < 0) {
       ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
       goto handle_error;