Blame m4/fcntl.m4

Packit 709fb3
# fcntl.m4 serial 9
Packit 709fb3
dnl Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
dnl This file is free software; the Free Software Foundation
Packit 709fb3
dnl gives unlimited permission to copy and/or distribute it,
Packit 709fb3
dnl with or without modifications, as long as this notice is preserved.
Packit 709fb3
Packit 709fb3
# For now, this module ensures that fcntl()
Packit 709fb3
# - supports F_DUPFD correctly
Packit 709fb3
# - supports or emulates F_DUPFD_CLOEXEC
Packit 709fb3
# - supports F_GETFD
Packit 709fb3
# Still to be ported to mingw:
Packit 709fb3
# - F_SETFD
Packit 709fb3
# - F_GETFL, F_SETFL
Packit 709fb3
# - F_GETOWN, F_SETOWN
Packit 709fb3
# - F_GETLK, F_SETLK, F_SETLKW
Packit 709fb3
AC_DEFUN([gl_FUNC_FCNTL],
Packit 709fb3
[
Packit 709fb3
  dnl Persuade glibc to expose F_DUPFD_CLOEXEC.
Packit 709fb3
  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
Packit 709fb3
  AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
Packit 709fb3
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit 709fb3
  AC_CHECK_FUNCS_ONCE([fcntl])
Packit 709fb3
  if test $ac_cv_func_fcntl = no; then
Packit 709fb3
    gl_REPLACE_FCNTL
Packit 709fb3
  else
Packit 709fb3
    dnl cygwin 1.5.x F_DUPFD has wrong errno, and allows negative target
Packit 709fb3
    dnl haiku alpha 2 F_DUPFD has wrong errno
Packit 709fb3
    AC_CACHE_CHECK([whether fcntl handles F_DUPFD correctly],
Packit 709fb3
      [gl_cv_func_fcntl_f_dupfd_works],
Packit 709fb3
      [AC_RUN_IFELSE(
Packit 709fb3
         [AC_LANG_PROGRAM(
Packit 709fb3
            [[#include <errno.h>
Packit 709fb3
              #include <fcntl.h>
Packit 709fb3
              #include <limits.h>
Packit 709fb3
              #include <sys/resource.h>
Packit 709fb3
              #include <unistd.h>
Packit 709fb3
              #ifndef RLIM_SAVED_CUR
Packit 709fb3
              # define RLIM_SAVED_CUR RLIM_INFINITY
Packit 709fb3
              #endif
Packit 709fb3
              #ifndef RLIM_SAVED_MAX
Packit 709fb3
              # define RLIM_SAVED_MAX RLIM_INFINITY
Packit 709fb3
              #endif
Packit 709fb3
            ]],
Packit 709fb3
            [[int result = 0;
Packit 709fb3
              int bad_fd = INT_MAX;
Packit 709fb3
              struct rlimit rlim;
Packit 709fb3
              if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
Packit 709fb3
                  && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
Packit 709fb3
                  && rlim.rlim_cur != RLIM_INFINITY
Packit 709fb3
                  && rlim.rlim_cur != RLIM_SAVED_MAX
Packit 709fb3
                  && rlim.rlim_cur != RLIM_SAVED_CUR)
Packit 709fb3
                bad_fd = rlim.rlim_cur;
Packit 709fb3
              if (fcntl (0, F_DUPFD, -1) != -1) result |= 1;
Packit 709fb3
              if (errno != EINVAL) result |= 2;
Packit 709fb3
              if (fcntl (0, F_DUPFD, bad_fd) != -1) result |= 4;
Packit 709fb3
              if (errno != EINVAL) result |= 8;
Packit 709fb3
              /* On OS/2 kLIBC, F_DUPFD does not work on a directory fd */
Packit 709fb3
              {
Packit 709fb3
                int fd;
Packit 709fb3
                fd = open (".", O_RDONLY);
Packit 709fb3
                if (fd == -1)
Packit 709fb3
                  result |= 16;
Packit 709fb3
                else if (fcntl (fd, F_DUPFD, STDERR_FILENO + 1) == -1)
Packit 709fb3
                  result |= 32;
Packit 709fb3
Packit 709fb3
                close (fd);
Packit 709fb3
              }
Packit 709fb3
              return result;]])],
Packit 709fb3
         [gl_cv_func_fcntl_f_dupfd_works=yes],
Packit 709fb3
         [gl_cv_func_fcntl_f_dupfd_works=no],
Packit 709fb3
         [case $host_os in
Packit 709fb3
            aix* | cygwin* | haiku*)
Packit 709fb3
               gl_cv_func_fcntl_f_dupfd_works="guessing no" ;;
Packit 709fb3
            *) gl_cv_func_fcntl_f_dupfd_works="guessing yes" ;;
Packit 709fb3
          esac])])
Packit 709fb3
    case $gl_cv_func_fcntl_f_dupfd_works in
Packit 709fb3
      *yes) ;;
Packit 709fb3
      *) gl_REPLACE_FCNTL
Packit 709fb3
        AC_DEFINE([FCNTL_DUPFD_BUGGY], [1], [Define this to 1 if F_DUPFD
Packit 709fb3
          behavior does not match POSIX]) ;;
Packit 709fb3
    esac
Packit 709fb3
Packit 709fb3
    dnl Many systems lack F_DUPFD_CLOEXEC
Packit 709fb3
    AC_CACHE_CHECK([whether fcntl understands F_DUPFD_CLOEXEC],
Packit 709fb3
      [gl_cv_func_fcntl_f_dupfd_cloexec],
Packit 709fb3
      [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
Packit 709fb3
#include <fcntl.h>
Packit 709fb3
#ifndef F_DUPFD_CLOEXEC
Packit 709fb3
choke me
Packit 709fb3
#endif
Packit 709fb3
         ]])],
Packit 709fb3
         [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
Packit 709fb3
#ifdef __linux__
Packit 709fb3
/* The Linux kernel only added F_DUPFD_CLOEXEC in 2.6.24, so we always replace
Packit 709fb3
   it to support the semantics on older kernels that failed with EINVAL.  */
Packit 709fb3
choke me
Packit 709fb3
#endif
Packit 709fb3
           ]])],
Packit 709fb3
           [gl_cv_func_fcntl_f_dupfd_cloexec=yes],
Packit 709fb3
           [gl_cv_func_fcntl_f_dupfd_cloexec="needs runtime check"])],
Packit 709fb3
         [gl_cv_func_fcntl_f_dupfd_cloexec=no])])
Packit 709fb3
    if test "$gl_cv_func_fcntl_f_dupfd_cloexec" != yes; then
Packit 709fb3
      gl_REPLACE_FCNTL
Packit 709fb3
      dnl No witness macro needed for this bug.
Packit 709fb3
    fi
Packit 709fb3
  fi
Packit 709fb3
  dnl Replace fcntl() for supporting the gnulib-defined fchdir() function,
Packit 709fb3
  dnl to keep fchdir's bookkeeping up-to-date.
Packit 709fb3
  m4_ifdef([gl_FUNC_FCHDIR], [
Packit 709fb3
    gl_TEST_FCHDIR
Packit 709fb3
    if test $HAVE_FCHDIR = 0; then
Packit 709fb3
      gl_REPLACE_FCNTL
Packit 709fb3
    fi
Packit 709fb3
  ])
Packit 709fb3
])
Packit 709fb3
Packit 709fb3
AC_DEFUN([gl_REPLACE_FCNTL],
Packit 709fb3
[
Packit 709fb3
  AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
Packit 709fb3
  AC_CHECK_FUNCS_ONCE([fcntl])
Packit 709fb3
  if test $ac_cv_func_fcntl = no; then
Packit 709fb3
    HAVE_FCNTL=0
Packit 709fb3
  else
Packit 709fb3
    REPLACE_FCNTL=1
Packit 709fb3
  fi
Packit 709fb3
])