[PATCH 3/3] os/inputthread: Fix setting of cloexec on file descriptors

Julien Cristau jcristau at debian.org
Mon Sep 12 11:41:22 UTC 2016


On Sun, Sep 11, 2016 at 20:01:51 -0700, Jeremy Huddleston Sequoia wrote:

> O_CLOEXEC is not a file bit.  It is not setable with F_SETFL.  One must use it
> when calling open(2).  To set it cloexec on an existing fd, F_SETFD and
> FD_CLOEXEC must be used.
> 
> This also fixes a build failure regression on configurations that don't have
> O_CLOEXEC defined.
> 
> cf: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
> Regressed-in: 30ac7567980a1eb79d084a63e0e74e1d9a3af673
> Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu at apple.com>
> ---
>  os/inputthread.c | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
> 
A possible further improvement would be to use pipe2(2) where available
to set O_CLOEXEC immediately, rather than as a separate step.

Anyway, this gets my
Reviewed-by: Julien Cristau <jcristau at debian.org>

Cheers,
Julien

> diff --git a/os/inputthread.c b/os/inputthread.c
> index 2ea39e7..6aa0a9c 100644
> --- a/os/inputthread.c
> +++ b/os/inputthread.c
> @@ -385,6 +385,7 @@ void
>  InputThreadPreInit(void)
>  {
>      int fds[2], hotplugPipe[2];
> +    int flags;
>  
>      if (!InputThreadEnable)
>          return;
> @@ -408,13 +409,23 @@ InputThreadPreInit(void)
>       * in parallel.
>       */
>      inputThreadInfo->readPipe = fds[0];
> -    fcntl(inputThreadInfo->readPipe, F_SETFL, O_NONBLOCK | O_CLOEXEC);
> +    fcntl(inputThreadInfo->readPipe, F_SETFL, O_NONBLOCK);
> +    flags = fcntl(inputThreadInfo->readPipe, F_GETFD);
> +    if (flags != -1) {
> +        flags |= FD_CLOEXEC;
> +        (void)fcntl(inputThreadInfo->readPipe, F_SETFD, &flags);
> +    }
>      SetNotifyFd(inputThreadInfo->readPipe, InputThreadNotifyPipe, X_NOTIFY_READ, NULL);
>  
>      inputThreadInfo->writePipe = fds[1];
>  
>      hotplugPipeRead = hotplugPipe[0];
> -    fcntl(hotplugPipeRead, F_SETFL, O_NONBLOCK | O_CLOEXEC);
> +    fcntl(hotplugPipeRead, F_SETFL, O_NONBLOCK);
> +    flags = fcntl(hotplugPipeRead, F_GETFD);
> +    if (flags != -1) {
> +        flags |= FD_CLOEXEC;
> +        (void)fcntl(hotplugPipeRead, F_SETFD, &flags);
> +    }
>      hotplugPipeWrite = hotplugPipe[1];
>  
>  #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
> -- 
> 2.10.0 (Apple Git-99)
> 
> _______________________________________________
> xorg-devel at lists.x.org: X.Org development
> Archives: http://lists.x.org/archives/xorg-devel
> Info: https://lists.x.org/mailman/listinfo/xorg-devel


More information about the xorg-devel mailing list