Blame lib/cloexec.c

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