Blame gnu/cloexec.c

Packit 1ef1a9
/* closexec.c - set or clear the close-on-exec descriptor flag
Packit 1ef1a9
Packit 1ef1a9
   Copyright (C) 1991, 2004-2006, 2009-2015 Free Software Foundation, Inc.
Packit 1ef1a9
Packit 1ef1a9
   This program is free software: you can redistribute it and/or modify
Packit 1ef1a9
   it under the terms of the GNU General Public License as published by
Packit 1ef1a9
   the Free Software Foundation; either version 3 of the License, or
Packit 1ef1a9
   (at your option) any later version.
Packit 1ef1a9
Packit 1ef1a9
   This program is distributed in the hope that it will be useful,
Packit 1ef1a9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1ef1a9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 1ef1a9
   GNU General Public License for more details.
Packit 1ef1a9
Packit 1ef1a9
   You should have received a copy of the GNU General Public License
Packit 1ef1a9
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 1ef1a9
Packit 1ef1a9
   The code is taken from glibc/manual/llio.texi  */
Packit 1ef1a9
Packit 1ef1a9
#include <config.h>
Packit 1ef1a9
Packit 1ef1a9
#include "cloexec.h"
Packit 1ef1a9
Packit 1ef1a9
#include <errno.h>
Packit 1ef1a9
#include <fcntl.h>
Packit 1ef1a9
#include <unistd.h>
Packit 1ef1a9
Packit 1ef1a9
/* Set the 'FD_CLOEXEC' flag of DESC if VALUE is true,
Packit 1ef1a9
   or clear the flag if VALUE is false.
Packit 1ef1a9
   Return 0 on success, or -1 on error with 'errno' set.
Packit 1ef1a9
Packit 1ef1a9
   Note that on MingW, this function does NOT protect DESC from being
Packit 1ef1a9
   inherited into spawned children.  Instead, either use dup_cloexec
Packit 1ef1a9
   followed by closing the original DESC, or use interfaces such as
Packit 1ef1a9
   open or pipe2 that accept flags like O_CLOEXEC to create DESC
Packit 1ef1a9
   non-inheritable in the first place.  */
Packit 1ef1a9
Packit 1ef1a9
int
Packit 1ef1a9
set_cloexec_flag (int desc, bool value)
Packit 1ef1a9
{
Packit 1ef1a9
#ifdef F_SETFD
Packit 1ef1a9
Packit 1ef1a9
  int flags = fcntl (desc, F_GETFD, 0);
Packit 1ef1a9
Packit 1ef1a9
  if (0 <= flags)
Packit 1ef1a9
    {
Packit 1ef1a9
      int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
Packit 1ef1a9
Packit 1ef1a9
      if (flags == newflags
Packit 1ef1a9
          || fcntl (desc, F_SETFD, newflags) != -1)
Packit 1ef1a9
        return 0;
Packit 1ef1a9
    }
Packit 1ef1a9
Packit 1ef1a9
  return -1;
Packit 1ef1a9
Packit 1ef1a9
#else /* !F_SETFD */
Packit 1ef1a9
Packit 1ef1a9
  /* Use dup2 to reject invalid file descriptors; the cloexec flag
Packit 1ef1a9
     will be unaffected.  */
Packit 1ef1a9
  if (desc < 0)
Packit 1ef1a9
    {
Packit 1ef1a9
      errno = EBADF;
Packit 1ef1a9
      return -1;
Packit 1ef1a9
    }
Packit 1ef1a9
  if (dup2 (desc, desc) < 0)
Packit 1ef1a9
    /* errno is EBADF here.  */
Packit 1ef1a9
    return -1;
Packit 1ef1a9
Packit 1ef1a9
  /* There is nothing we can do on this kind of platform.  Punt.  */
Packit 1ef1a9
  return 0;
Packit 1ef1a9
#endif /* !F_SETFD */
Packit 1ef1a9
}
Packit 1ef1a9
Packit 1ef1a9
Packit 1ef1a9
/* Duplicates a file handle FD, while marking the copy to be closed
Packit 1ef1a9
   prior to exec or spawn.  Returns -1 and sets errno if FD could not
Packit 1ef1a9
   be duplicated.  */
Packit 1ef1a9
Packit 1ef1a9
int
Packit 1ef1a9
dup_cloexec (int fd)
Packit 1ef1a9
{
Packit 1ef1a9
  return fcntl (fd, F_DUPFD_CLOEXEC, 0);
Packit 1ef1a9
}