root/maint/gnulib/lib/fchmodat.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. orig_fchmodat
  2. fchmodat

   1 /* Change the protections of file relative to an open directory.
   2    Copyright (C) 2006, 2009-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* written by Jim Meyering and Paul Eggert */
  18 
  19 /* If the user's config.h happens to include <sys/stat.h>, let it include only
  20    the system's <sys/stat.h> here, so that orig_fchmodat doesn't recurse to
  21    rpl_fchmodat.  */
  22 #define __need_system_sys_stat_h
  23 #include <config.h>
  24 
  25 /* Specification.  */
  26 #include <sys/stat.h>
  27 #undef __need_system_sys_stat_h
  28 
  29 #if HAVE_FCHMODAT
  30 static int
  31 orig_fchmodat (int dir, char const *file, mode_t mode, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33   return fchmodat (dir, file, mode, flags);
  34 }
  35 #endif
  36 
  37 #include <errno.h>
  38 #include <fcntl.h>
  39 #include <stdio.h>
  40 #include <stdlib.h>
  41 #include <string.h>
  42 #include <unistd.h>
  43 
  44 #ifdef __osf__
  45 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
  46    eliminates this include because of the preliminary #include <sys/stat.h>
  47    above.  */
  48 # include "sys/stat.h"
  49 #else
  50 # include <sys/stat.h>
  51 #endif
  52 
  53 #include <intprops.h>
  54 
  55 /* Invoke chmod or lchmod on FILE, using mode MODE, in the directory
  56    open on descriptor FD.  If possible, do it without changing the
  57    working directory.  Otherwise, resort to using save_cwd/fchdir,
  58    then (chmod|lchmod)/restore_cwd.  If either the save_cwd or the
  59    restore_cwd fails, then give a diagnostic and exit nonzero.
  60    Note that an attempt to use a FLAG value of AT_SYMLINK_NOFOLLOW
  61    on a system without lchmod support causes this function to fail.  */
  62 
  63 #if HAVE_FCHMODAT
  64 int
  65 fchmodat (int dir, char const *file, mode_t mode, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67 # if HAVE_NEARLY_WORKING_FCHMODAT
  68   /* Correct the trailing slash handling.  */
  69   size_t len = strlen (file);
  70   if (len && file[len - 1] == '/')
  71     {
  72       struct stat st;
  73       if (fstatat (dir, file, &st, flags & AT_SYMLINK_NOFOLLOW) < 0)
  74         return -1;
  75       if (!S_ISDIR (st.st_mode))
  76         {
  77           errno = ENOTDIR;
  78           return -1;
  79         }
  80     }
  81 # endif
  82 
  83 # if NEED_FCHMODAT_NONSYMLINK_FIX
  84   if (flags == AT_SYMLINK_NOFOLLOW)
  85     {
  86       struct stat st;
  87 
  88 #  if defined O_PATH && defined AT_EMPTY_PATH
  89       /* Open a file descriptor with O_NOFOLLOW, to make sure we don't
  90          follow symbolic links, if /proc is mounted.  O_PATH is used to
  91          avoid a failure if the file is not readable.
  92          Cf. <https://sourceware.org/bugzilla/show_bug.cgi?id=14578>  */
  93       int fd = openat (dir, file, O_PATH | O_NOFOLLOW | O_CLOEXEC);
  94       if (fd < 0)
  95         return fd;
  96 
  97       /* Up to Linux 5.3 at least, when FILE refers to a symbolic link, the
  98          chmod call below will change the permissions of the symbolic link
  99          - which is undesired - and on many file systems (ext4, btrfs, jfs,
 100          xfs, ..., but not reiserfs) fail with error EOPNOTSUPP - which is
 101          misleading.  Therefore test for a symbolic link explicitly.
 102          Use fstatat because fstat does not work on O_PATH descriptors
 103          before Linux 3.6.  */
 104       if (fstatat (fd, "", &st, AT_EMPTY_PATH) != 0)
 105         {
 106           int stat_errno = errno;
 107           close (fd);
 108           errno = stat_errno;
 109           return -1;
 110         }
 111       if (S_ISLNK (st.st_mode))
 112         {
 113           close (fd);
 114           errno = EOPNOTSUPP;
 115           return -1;
 116         }
 117 
 118 #   if defined __linux__ || defined __ANDROID__ || defined __CYGWIN__
 119       static char const fmt[] = "/proc/self/fd/%d";
 120       char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)];
 121       sprintf (buf, fmt, fd);
 122       int chmod_result = chmod (buf, mode);
 123       int chmod_errno = errno;
 124       close (fd);
 125       if (chmod_result == 0)
 126         return chmod_result;
 127       if (chmod_errno != ENOENT)
 128         {
 129           errno = chmod_errno;
 130           return chmod_result;
 131         }
 132 #   endif
 133       /* /proc is not mounted or would not work as in GNU/Linux.  */
 134 
 135 #  else
 136       int fstatat_result = fstatat (dir, file, &st, AT_SYMLINK_NOFOLLOW);
 137       if (fstatat_result != 0)
 138         return fstatat_result;
 139       if (S_ISLNK (st.st_mode))
 140         {
 141           errno = EOPNOTSUPP;
 142           return -1;
 143         }
 144 #  endif
 145 
 146       /* Fall back on orig_fchmodat with no flags, despite a possible race.  */
 147       flags = 0;
 148     }
 149 # endif
 150 
 151   return orig_fchmodat (dir, file, mode, flags);
 152 }
 153 #else
 154 # define AT_FUNC_NAME fchmodat
 155 # define AT_FUNC_F1 lchmod
 156 # define AT_FUNC_F2 chmod
 157 # define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
 158 # define AT_FUNC_POST_FILE_PARAM_DECLS , mode_t mode, int flag
 159 # define AT_FUNC_POST_FILE_ARGS        , mode
 160 # include "at-func.c"
 161 #endif

/* [previous][next][first][last][top][bottom][index][help] */