Blame m4/chown.m4

Packit 8f70b4
# serial 30
Packit 8f70b4
# Determine whether we need the chown wrapper.
Packit 8f70b4
Packit 8f70b4
dnl Copyright (C) 1997-2001, 2003-2005, 2007, 2009-2018 Free Software
Packit 8f70b4
dnl Foundation, Inc.
Packit 8f70b4
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
# chown should accept arguments of -1 for uid and gid, and it should
Packit 8f70b4
# dereference symlinks.  If it doesn't, arrange to use the replacement
Packit 8f70b4
# function.
Packit 8f70b4
Packit 8f70b4
# From Jim Meyering.
Packit 8f70b4
Packit 8f70b4
# This is taken from the following Autoconf patch:
Packit 8f70b4
# https://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commitdiff;h=7fbb553727ed7e0e689a17594b58559ecf3ea6e9
Packit 8f70b4
AC_DEFUN([AC_FUNC_CHOWN],
Packit 8f70b4
[
Packit 8f70b4
  AC_REQUIRE([AC_TYPE_UID_T])dnl
Packit 8f70b4
  AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
Packit 8f70b4
  AC_CHECK_HEADERS([unistd.h])
Packit 8f70b4
  AC_CACHE_CHECK([for working chown],
Packit 8f70b4
    [ac_cv_func_chown_works],
Packit 8f70b4
    [AC_RUN_IFELSE(
Packit 8f70b4
       [AC_LANG_PROGRAM(
Packit 8f70b4
          [AC_INCLUDES_DEFAULT
Packit 8f70b4
           [#include <fcntl.h>
Packit 8f70b4
          ]],
Packit 8f70b4
          [[
Packit 8f70b4
            char *f = "conftest.chown";
Packit 8f70b4
            struct stat before, after;
Packit 8f70b4
Packit 8f70b4
            if (creat (f, 0600) < 0)
Packit 8f70b4
              return 1;
Packit 8f70b4
            if (stat (f, &before) < 0)
Packit 8f70b4
              return 1;
Packit 8f70b4
            if (chown (f, (uid_t) -1, (gid_t) -1) == -1)
Packit 8f70b4
              return 1;
Packit 8f70b4
            if (stat (f, &after) < 0)
Packit 8f70b4
              return 1;
Packit 8f70b4
            return ! (before.st_uid == after.st_uid && before.st_gid == after.st_gid);
Packit 8f70b4
          ]])
Packit 8f70b4
       ],
Packit 8f70b4
       [ac_cv_func_chown_works=yes],
Packit 8f70b4
       [ac_cv_func_chown_works=no],
Packit 8f70b4
       [case "$host_os" in # ((
Packit 8f70b4
                           # Guess yes on Linux systems.
Packit 8f70b4
          linux-* | linux) ac_cv_func_chown_works="guessing yes" ;;
Packit 8f70b4
                           # Guess yes on glibc systems.
Packit 8f70b4
          *-gnu* | gnu*)   ac_cv_func_chown_works="guessing yes" ;;
Packit 8f70b4
                           # Guess no on native Windows.
Packit 8f70b4
          mingw*)          ac_cv_func_chown_works="guessing no" ;;
Packit 8f70b4
                           # If we don't know, assume the worst.
Packit 8f70b4
          *)               ac_cv_func_chown_works="guessing no" ;;
Packit 8f70b4
        esac
Packit 8f70b4
       ])
Packit 8f70b4
     rm -f conftest.chown
Packit 8f70b4
    ])
Packit 8f70b4
  case "$ac_cv_func_chown_works" in
Packit 8f70b4
    *yes)
Packit 8f70b4
      AC_DEFINE([HAVE_CHOWN], [1],
Packit 8f70b4
        [Define to 1 if your system has a working `chown' function.])
Packit 8f70b4
      ;;
Packit 8f70b4
  esac
Packit 8f70b4
])# AC_FUNC_CHOWN
Packit 8f70b4
Packit 8f70b4
AC_DEFUN_ONCE([gl_FUNC_CHOWN],
Packit 8f70b4
[
Packit 8f70b4
  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
Packit 8f70b4
  AC_REQUIRE([AC_TYPE_UID_T])
Packit 8f70b4
  AC_REQUIRE([AC_FUNC_CHOWN])
Packit 8f70b4
  AC_REQUIRE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK])
Packit 8f70b4
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit 8f70b4
  AC_CHECK_FUNCS_ONCE([chown fchown])
Packit 8f70b4
Packit 8f70b4
  dnl mingw lacks chown altogether.
Packit 8f70b4
  if test $ac_cv_func_chown = no; then
Packit 8f70b4
    HAVE_CHOWN=0
Packit 8f70b4
  else
Packit 8f70b4
    dnl Some old systems treated chown like lchown.
Packit 8f70b4
    if test $gl_cv_func_chown_follows_symlink = no; then
Packit 8f70b4
      REPLACE_CHOWN=1
Packit 8f70b4
    fi
Packit 8f70b4
Packit 8f70b4
    dnl Some old systems tried to use uid/gid -1 literally.
Packit 8f70b4
    case "$ac_cv_func_chown_works" in
Packit 8f70b4
      *no)
Packit 8f70b4
        AC_DEFINE([CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE], [1],
Packit 8f70b4
          [Define if chown is not POSIX compliant regarding IDs of -1.])
Packit 8f70b4
        REPLACE_CHOWN=1
Packit 8f70b4
        ;;
Packit 8f70b4
    esac
Packit 8f70b4
Packit 8f70b4
    dnl Solaris 9 ignores trailing slash.
Packit 8f70b4
    dnl FreeBSD 7.2 mishandles trailing slash on symlinks.
Packit 8f70b4
    dnl Likewise for AIX 7.1.
Packit 8f70b4
    AC_CACHE_CHECK([whether chown honors trailing slash],
Packit 8f70b4
      [gl_cv_func_chown_slash_works],
Packit 8f70b4
      [touch conftest.file && rm -f conftest.link
Packit 8f70b4
       AC_RUN_IFELSE([AC_LANG_PROGRAM([[
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
#include <stdlib.h>
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
]], [[    if (symlink ("conftest.file", "conftest.link")) return 1;
Packit 8f70b4
          if (chown ("conftest.link/", getuid (), getgid ()) == 0) return 2;
Packit 8f70b4
        ]])],
Packit 8f70b4
        [gl_cv_func_chown_slash_works=yes],
Packit 8f70b4
        [gl_cv_func_chown_slash_works=no],
Packit 8f70b4
        [case "$host_os" in
Packit 8f70b4
                   # Guess yes on glibc systems.
Packit 8f70b4
           *-gnu*) gl_cv_func_chown_slash_works="guessing yes" ;;
Packit 8f70b4
                   # If we don't know, assume the worst.
Packit 8f70b4
           *)      gl_cv_func_chown_slash_works="guessing no" ;;
Packit 8f70b4
         esac
Packit 8f70b4
        ])
Packit 8f70b4
      rm -f conftest.link conftest.file])
Packit 8f70b4
    case "$gl_cv_func_chown_slash_works" in
Packit 8f70b4
      *yes) ;;
Packit 8f70b4
      *)
Packit 8f70b4
        AC_DEFINE([CHOWN_TRAILING_SLASH_BUG], [1],
Packit 8f70b4
          [Define to 1 if chown mishandles trailing slash.])
Packit 8f70b4
        REPLACE_CHOWN=1
Packit 8f70b4
        ;;
Packit 8f70b4
    esac
Packit 8f70b4
Packit 8f70b4
    dnl OpenBSD fails to update ctime if ownership does not change.
Packit 8f70b4
    AC_CACHE_CHECK([whether chown always updates ctime],
Packit 8f70b4
      [gl_cv_func_chown_ctime_works],
Packit 8f70b4
      [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
#include <stdlib.h>
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
#include <fcntl.h>
Packit 8f70b4
#include <sys/stat.h>
Packit 8f70b4
]], [[    struct stat st1, st2;
Packit 8f70b4
          if (close (creat ("conftest.file", 0600))) return 1;
Packit 8f70b4
          if (stat ("conftest.file", &st1)) return 2;
Packit 8f70b4
          sleep (1);
Packit 8f70b4
          if (chown ("conftest.file", st1.st_uid, st1.st_gid)) return 3;
Packit 8f70b4
          if (stat ("conftest.file", &st2)) return 4;
Packit 8f70b4
          if (st2.st_ctime <= st1.st_ctime) return 5;
Packit 8f70b4
        ]])],
Packit 8f70b4
        [gl_cv_func_chown_ctime_works=yes],
Packit 8f70b4
        [gl_cv_func_chown_ctime_works=no],
Packit 8f70b4
        [case "$host_os" in
Packit 8f70b4
                   # Guess yes on glibc systems.
Packit 8f70b4
           *-gnu*) gl_cv_func_chown_ctime_works="guessing yes" ;;
Packit 8f70b4
                   # If we don't know, assume the worst.
Packit 8f70b4
           *)      gl_cv_func_chown_ctime_works="guessing no" ;;
Packit 8f70b4
         esac
Packit 8f70b4
        ])
Packit 8f70b4
      rm -f conftest.file])
Packit 8f70b4
    case "$gl_cv_func_chown_ctime_works" in
Packit 8f70b4
      *yes) ;;
Packit 8f70b4
      *)
Packit 8f70b4
        AC_DEFINE([CHOWN_CHANGE_TIME_BUG], [1], [Define to 1 if chown fails
Packit 8f70b4
          to change ctime when at least one argument was not -1.])
Packit 8f70b4
        REPLACE_CHOWN=1
Packit 8f70b4
        ;;
Packit 8f70b4
    esac
Packit 8f70b4
  fi
Packit 8f70b4
])
Packit 8f70b4
Packit 8f70b4
# Determine whether chown follows symlinks (it should).
Packit 8f70b4
AC_DEFUN_ONCE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK],
Packit 8f70b4
[
Packit 8f70b4
  AC_CACHE_CHECK(
Packit 8f70b4
    [whether chown dereferences symlinks],
Packit 8f70b4
    [gl_cv_func_chown_follows_symlink],
Packit 8f70b4
    [
Packit 8f70b4
      AC_RUN_IFELSE([AC_LANG_SOURCE([[
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
#include <stdlib.h>
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
Packit 8f70b4
        int
Packit 8f70b4
        main ()
Packit 8f70b4
        {
Packit 8f70b4
          int result = 0;
Packit 8f70b4
          char const *dangling_symlink = "conftest.dangle";
Packit 8f70b4
Packit 8f70b4
          unlink (dangling_symlink);
Packit 8f70b4
          if (symlink ("conftest.no-such", dangling_symlink))
Packit 8f70b4
            abort ();
Packit 8f70b4
Packit 8f70b4
          /* Exit successfully on a conforming system,
Packit 8f70b4
             i.e., where chown must fail with ENOENT.  */
Packit 8f70b4
          if (chown (dangling_symlink, getuid (), getgid ()) == 0)
Packit 8f70b4
            result |= 1;
Packit 8f70b4
          if (errno != ENOENT)
Packit 8f70b4
            result |= 2;
Packit 8f70b4
          return result;
Packit 8f70b4
        }
Packit 8f70b4
        ]])],
Packit 8f70b4
        [gl_cv_func_chown_follows_symlink=yes],
Packit 8f70b4
        [gl_cv_func_chown_follows_symlink=no],
Packit 8f70b4
        [gl_cv_func_chown_follows_symlink=yes]
Packit 8f70b4
      )
Packit 8f70b4
    ]
Packit 8f70b4
  )
Packit 8f70b4
Packit 8f70b4
  if test $gl_cv_func_chown_follows_symlink = no; then
Packit 8f70b4
    AC_DEFINE([CHOWN_MODIFIES_SYMLINK], [1],
Packit 8f70b4
      [Define if chown modifies symlinks.])
Packit 8f70b4
  fi
Packit 8f70b4
])