Back to index

nagios-plugins  1.4.16
Functions
cloexec.c File Reference
#include <config.h>
#include "cloexec.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

Go to the source code of this file.

Functions

int set_cloexec_flag (int desc, bool value)
int dup_cloexec (int fd)

Function Documentation

int dup_cloexec ( int  fd)

Definition at line 80 of file cloexec.c.

{
  return fcntl (fd, F_DUPFD_CLOEXEC, 0);
}
int set_cloexec_flag ( int  desc,
bool  value 
)

Definition at line 39 of file cloexec.c.

{
#ifdef F_SETFD

  int flags = fcntl (desc, F_GETFD, 0);

  if (0 <= flags)
    {
      int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);

      if (flags == newflags
          || fcntl (desc, F_SETFD, newflags) != -1)
        return 0;
    }

  return -1;

#else /* !F_SETFD */

  /* Use dup2 to reject invalid file descriptors; the cloexec flag
     will be unaffected.  */
  if (desc < 0)
    {
      errno = EBADF;
      return -1;
    }
  if (dup2 (desc, desc) < 0)
    /* errno is EBADF here.  */
    return -1;

  /* There is nothing we can do on this kind of platform.  Punt.  */
  return 0;
#endif /* !F_SETFD */
}

Here is the call graph for this function:

Here is the caller graph for this function: