Blame gl/cloexec.c

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