Blame m4/fcntl.m4

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